-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwarp.go
271 lines (232 loc) · 6.79 KB
/
warp.go
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package texture
import (
"image/color"
"math"
)
const (
twoPi = math.Pi * 2
)
type WarpFunc interface {
Eval(x, y float64) (float64, float64)
}
// Warp applies a deformation to the values passed into the Eval2 function.
type Warp struct {
Name string
Src Field
Func WarpFunc
}
func NewWarp(src Field, wf WarpFunc) *Warp {
return &Warp{"Warp", src, wf}
}
// Eval2 implements the Field interface.
func (w *Warp) Eval2(x, y float64) float64 {
x, y = w.Func.Eval(x, y)
return w.Src.Eval2(x, y)
}
// WarpVF applies a deformation to the values passed into the Eval2 function.
type WarpVF struct {
Name string
Src VectorField
Func WarpFunc
}
func NewWarpVF(src VectorField, wf WarpFunc) *WarpVF {
return &WarpVF{"WarpVF", src, wf}
}
// Eval2 implements the VectorField interface.
func (w *WarpVF) Eval2(x, y float64) []float64 {
x, y = w.Func.Eval(x, y)
return w.Src.Eval2(x, y)
}
// WarpCF applies a deformation to the values passed into the Eval2 function.
type WarpCF struct {
Name string
Src ColorField
Func WarpFunc
}
func NewWarpCF(src ColorField, wf WarpFunc) *WarpCF {
return &WarpCF{"WarpCF", src, wf}
}
// Eval2 implements the ColorField interface.
func (w *WarpCF) Eval2(x, y float64) color.Color {
x, y = w.Func.Eval(x, y)
return w.Src.Eval2(x, y)
}
// RadialWF performs a scaled warp around Center for use in the above warp types.
type RadialWF struct {
Name string
Center []float64 // Center of warp
RScale float64 // Radial scale
CScale float64 // Circumference scale
}
func NewRadialWF(c []float64, rs, cs float64) *RadialWF {
return &RadialWF{"RadialWF", c, rs, cs}
}
// Eval implements the WarpFunc interface
func (wf *RadialWF) Eval(x, y float64) (float64, float64) {
lx, ly := x-wf.Center[0], y-wf.Center[1]
rr := math.Hypot(lx, ly) * wf.RScale
rx := rr*math.Atan2(ly, lx)*wf.CScale + wf.Center[0]
ry := rr + wf.Center[1]
return rx, ry
}
// SwirlWF performs a swirl warp around Center in x for use in the above warp types.
// The cordinates are converted to polar (r, th) and th advanced by scale * r.
type SwirlWF struct {
Name string
Center []float64 // Center of warp
Scale float64 // Scale factor
}
func NewSwirlWF(c []float64, s float64) *SwirlWF {
return &SwirlWF{"SwirlWF", c, s / twoPi}
}
// Eval implements the WarpFunc interface
func (wf *SwirlWF) Eval(x, y float64) (float64, float64) {
dx, dy := x-wf.Center[0], y-wf.Center[1]
r, th := toPolar(dx, dy)
th += r * wf.Scale
dx, dy = toEuclidean(r, th)
return wf.Center[0] + dx, wf.Center[1] + dy
}
// DrainWF performs a drain warp around Center in x for use in the above warp types.
// The x value is scaled about the central x value by |dy|^alpha*scale
type DrainWF struct {
Name string
Center []float64 // Center of warp
Scale float64 // Scale factor
Effct float64 // Effect radius
}
func NewDrainWF(c []float64, s, e float64) *DrainWF {
return &DrainWF{"DrainWF", c, s, e}
}
// Eval implements the WarpFunc interface
func (wf *DrainWF) Eval(x, y float64) (float64, float64) {
dx, dy := x-wf.Center[0], y-wf.Center[1]
r, th := toPolar(dx, dy)
if r > wf.Effct {
return x, y
}
th += (1 - r/wf.Effct) * wf.Scale
dx, dy = toEuclidean(r, th)
return wf.Center[0] + dx, wf.Center[1] + dy
}
// RadialNLWF performs a radius warp around Center based on an NL
type RadialNLWF struct {
Name string
Center []float64 // Center of warp
NL *NonLinear // [0,1] => [0,1]
Effct float64 // Effect radius
}
func NewRadialNLWF(c []float64, nl *NonLinear, e float64) *RadialNLWF {
return &RadialNLWF{"RadialNLWF", c, nl, e}
}
// Eval implements the WarpFunc interface
func (wf *RadialNLWF) Eval(x, y float64) (float64, float64) {
dx, dy := x-wf.Center[0], y-wf.Center[1]
r, th := toPolar(dx, dy)
if r > wf.Effct {
return x, y
}
t := r / wf.Effct
tp := (wf.NL.Eval(t) + 1) / 2 // NL returns results in [-1,1]
r = tp * wf.Effct
dx, dy = toEuclidean(r, th)
return wf.Center[0] + dx, wf.Center[1] + dy
}
// PinchXWF performs a pinched warp around Center in x for use in the above warp types.
// The x value is scaled about the central x value by |dy|^alpha*scale
type PinchXWF struct {
Name string
Center []float64 // Center of warp
Init float64 // Initial scale
Scale float64 // Scale factor
Alpha float64 // Power factor
}
func NewPinchXWF(c []float64, i, s, a float64) *PinchXWF {
return &PinchXWF{"PinchXWF", c, i, s, a}
}
// Eval implements the WarpFunc interface
func (wf *PinchXWF) Eval(x, y float64) (float64, float64) {
dx, dy := x-wf.Center[0], y-wf.Center[1]
dy *= wf.Scale
if dy < 0 {
dy = -dy
}
if !within(wf.Alpha, 1, 0.00001) {
dy = math.Pow(dy, wf.Alpha)
}
dx *= 1 / (dy + wf.Init)
return wf.Center[0] + dx, y
}
// RippleXWF performs a sin warp using the y value, lambda and offset and applies it to the x value, scaled
// by the amplitude.
type RippleXWF struct {
Name string
Lambda float64
Amplit float64
Offset float64
}
func NewRippleXWF(l, a, o float64) *RippleXWF {
return &RippleXWF{"RippleXWF", l, a, o}
}
// Eval implements the WarpFunc interface
func (wf *RippleXWF) Eval(x, y float64) (float64, float64) {
_, l := MapValueToLambda(y+wf.Offset, wf.Lambda)
l = l / wf.Lambda * twoPi
dx := math.Sin(l) * wf.Amplit
return x + dx, y
}
// RadialRippleWF performs a sin warp using the r value, lambda and offset and applies it to the r value, scaled
// by the amplitude.
type RadialRippleWF struct {
Name string
Center []float64
Lambda float64
Amplit float64
Offset float64
}
func NewRadialRippleWF(c []float64, l, a, o float64) *RadialRippleWF {
return &RadialRippleWF{"RadialRippleWF", c, l, a, o}
}
// Eval implements the WarpFunc interface
func (wf *RadialRippleWF) Eval(x, y float64) (float64, float64) {
dx, dy := x-wf.Center[0], y-wf.Center[1]
r, th := toPolar(dx, dy)
_, l := MapValueToLambda(r+wf.Offset, wf.Lambda)
l = l / wf.Lambda * twoPi
dr := math.Sin(l) * wf.Amplit
return toEuclidean(r+dr, th)
}
// RadialWiggleWF performs a sin warp using the r value, lambda and offset and applies it to the th value, scaled
// by the amplitude.
type RadialWiggleWF struct {
Name string
Center []float64
Lambda float64
Amplit float64
Offset float64
}
func NewRadialWiggleWF(c []float64, l, a, o float64) *RadialWiggleWF {
return &RadialWiggleWF{"RadialWiggleWF", c, l, a, o}
}
// Eval implements the WarpFunc interface
func (wf *RadialWiggleWF) Eval(x, y float64) (float64, float64) {
dx, dy := x-wf.Center[0], y-wf.Center[1]
r, th := toPolar(dx, dy)
_, l := MapValueToLambda(r+wf.Offset, wf.Lambda)
l = l / wf.Lambda * twoPi
dth := math.Sin(l) * wf.Amplit
return toEuclidean(r, th+dth)
}
func toPolar(dx, dy float64) (float64, float64) {
return math.Hypot(dx, dy), math.Atan2(dy, dx)
}
func toEuclidean(r, th float64) (float64, float64) {
return r * math.Cos(th), r * math.Sin(th)
}
func within(a, b, eps float64) bool {
d := a - b
if d < 0 {
d = -d
}
return d < eps
}