-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Interpolator.scala
255 lines (216 loc) · 8.09 KB
/
Interpolator.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package doodle
package interact
package animation
import cats.Invariant
import cats.syntax.invariant._
import doodle.core.Angle
import doodle.interact.easing.Easing
/** An interpolator constructs a transducer from a starting value, a stopping
* value, and the number of elements or steps to produce between these values.
*/
trait Interpolator[A] {
/** Enumerate a half-open interval, starting with start and ending with stop.
* The uneased case allows exact computation of the interval while the easing
* will probably introduce numeric error.
*/
def halfOpen(start: A, stop: A, steps: Long): Transducer[A]
/** Enumerate a half-open interval, starting with start and ending with stop,
* and passed through the given easing.
*/
def halfOpen(start: A, stop: A, steps: Long, easing: Easing): Transducer[A]
/** Interpolate a closed interval, starting with start and ending with the
* first value after stop. The uneased case allows exact computation of the
* interval while the easing will probably introduce numeric error.
*/
def closed(start: A, stop: A, steps: Long): Transducer[A]
/** Interpolate a closed interval, starting with start and ending with the
* first value after stop, and passed through the given easing.
*/
def closed(start: A, stop: A, steps: Long, easing: Easing): Transducer[A]
}
object Interpolator {
/** Invariant functor instance for Interpolator
*/
implicit object interpolatorInvariant extends Invariant[Interpolator] {
def imap[A, B](fa: Interpolator[A])(f: A => B)(g: B => A): Interpolator[B] =
new Interpolator[B] {
def halfOpen(start: B, stop: B, steps: Long): Transducer[B] =
fa.halfOpen(g(start), g(stop), steps).map(f)
def halfOpen(
start: B,
stop: B,
steps: Long,
easing: Easing
): Transducer[B] =
fa.halfOpen(g(start), g(stop), steps, easing).map(f)
def closed(start: B, stop: B, steps: Long): Transducer[B] =
fa.closed(g(start), g(stop), steps).map(f)
def closed(
start: B,
stop: B,
steps: Long,
easing: Easing
): Transducer[B] =
fa.closed(g(start), g(stop), steps, easing).map(f)
}
}
/** Perform Kahan summation given the total so far, the value to add to the
* total, and the error term (which starts at 0.0). Returns the updated total
* and the new error term.
*
* Kahan's algorithm is a way to sum floating point numbers that reduces
* error compared to straightforward addition.
*/
def kahanSum(total: Double, x: Double, error: Double): (Double, Double) = {
val y = x - error
val nextTotal = total + y
val nextError = (nextTotal - total) - y
(nextTotal, nextError)
}
/** Interpolator instance for Double
*/
implicit val doubleInterpolator: Interpolator[Double] =
new Interpolator[Double] {
def halfOpen(
start: Double,
stop: Double,
steps: Long
): Transducer[Double] =
if (start == stop) Transducer.empty
else
new Transducer[Double] {
// State is the current value and the number of steps
type State = (Double, Long)
val increment = (stop - start) / steps
val initial: State = (start, 0)
def next(current: State): State = {
val (x, s) = current
(x + increment, s + 1)
}
def output(state: State): Double = {
val (x, _) = state
x
}
def stopped(state: State): Boolean = {
val (x, s) = state
if (s >= steps) true
else if (stop >= start) (x >= stop)
else (x <= stop)
}
}
def halfOpen(
start: Double,
stop: Double,
steps: Long,
easing: Easing
): Transducer[Double] =
if (start == stop) Transducer.empty
else
new Transducer[Double] {
// The state consists of a number between [0, 1) that we project to
// [start, stop) and the number of steps taken. We count steps so we
// can stop exactly at the right time, which otherwise due to numeric
// error may not happen.
type State = (Double, Long)
val increment = 1.0 / steps
val initial: State = (0.0, 0)
// Convert [0, 1) to [start, stop)
def project(x: Double): Double =
start + (easing(x) * (stop - start))
def next(current: State): State = {
val (x, s) = current
(x + increment, s + 1)
}
def output(state: State): Double = {
val (x, _) = state
project(x)
}
def stopped(state: State): Boolean = {
val (x, s) = state
if (s >= steps) true
else (x >= 1.0)
}
}
def closed(
start: Double,
stop: Double,
steps: Long
): Transducer[Double] =
if (start == stop) Transducer.pure(stop)
else
new Transducer[Double] {
// State = (Current value, Steps, Error)
// Error is for Kahan summation
type State = (Double, Long, Double)
val increment = (stop - start) / (steps - 1)
val initial: State = (start, 0, 0.0)
def next(current: State): State = {
val (total, steps, error) = current
val (nextTotal, nextError) = kahanSum(total, increment, error)
(nextTotal, steps + 1, nextError)
}
def output(state: State): Double = {
val (total, s, _) = state
if (s + 1 >= steps) stop
else total
}
def stopped(state: State): Boolean = {
val (_, s, _) = state
(s >= steps)
}
}
def closed(
start: Double,
stop: Double,
steps: Long,
easing: Easing
): Transducer[Double] =
if (start == stop) Transducer.pure(stop)
else
new Transducer[Double] {
// The state consists of a number between [0, 1] that we project to
// [start, stop], the number of steps taken, and the error for Kahan
// summation. We count steps so we can stop exactly at the right
// time, which otherwise due to numeric error may not happen.
type State = (Double, Long, Double)
val increment = 1.0 / (steps - 1)
val initial: State = (0.0, 0, 0.0)
// Convert [0, 1] to [start, stop]
def project(x: Double): Double =
start + (easing(x) * (stop - start))
def next(current: State): State = {
val (total, steps, error) = current
val (nextTotal, nextError) = kahanSum(total, increment, error)
(nextTotal, steps + 1, nextError)
}
def output(state: State): Double = {
val (total, s, _) = state
if (s + 1 >= steps) stop
else project(total)
}
def stopped(state: State): Boolean = {
val (_, s, _) = state
(s >= steps)
}
}
}
/** Interpolator instance for Angle
*/
implicit val angleInterpolator: Interpolator[Angle] =
doubleInterpolator.imap(turns => Angle.turns(turns))(angle => angle.toTurns)
}