-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisplace.go
166 lines (147 loc) · 5.61 KB
/
displace.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
package texture
import (
"github.com/jphsd/graphics2d"
"image/color"
)
// Displace allows a source field to be evaluated at locations determined by an offset and scaling of
// the input x, y coordinates taken from other sources. If Indep is true, then the mapped x, y
// is independent of the original x, y location.
type Displace struct {
Name string
Src, SrcX, SrcY Field
Xfm *graphics2d.Aff3
Indep bool
}
// NewDisplace creates a new Displace instance using the same transform for both x and y displacements.
func NewDisplace(in, dx, dy Field, scale float64) *Displace {
xfm := graphics2d.NewAff3()
xfm.Scale(scale, scale)
return &Displace{"Displace", in, dx, dy, xfm, false}
}
// Eval2 implements the Field interface.
func (d *Displace) Eval2(x, y float64) float64 {
vp := []float64{d.SrcX.Eval2(x, y), d.SrcY.Eval2(x, y)}
p := d.Xfm.Apply(vp)[0]
if !d.Indep {
return d.Src.Eval2(x+p[0], y+p[1])
}
return d.Src.Eval2(p[0], p[1])
}
// Displace2 allows a source field to be evaluated at locations determined by axis independent transforms of
// the input x, y coordinates taken from other sources. If Indep is true, then the mapped x, y
// is independent of the original x, y location.
type Displace2 struct {
Name string
Src, SrcX, SrcY Field
XfmX, XfmY *graphics2d.Aff3
Indep bool
}
// NewDisplace2 creates a new Displace2 instance using the same source for both x and y displacements.
func NewDisplace2(in, dx, dy Field, xfmx, xfmy *graphics2d.Aff3) *Displace2 {
return &Displace2{"Displace2", in, dx, dy, xfmx, xfmy, false}
}
// Eval2 implements the Field interface.
func (d *Displace2) Eval2(x, y float64) float64 {
px := d.XfmX.Apply([]float64{d.SrcX.Eval2(x, y), 0})[0]
py := d.XfmY.Apply([]float64{0, d.SrcY.Eval2(x, y), 0})[0]
if !d.Indep {
return d.Src.Eval2(x+px[0], y+py[1])
}
return d.Src.Eval2(px[0], py[1])
}
// DisplaceVF allows a source field to be evaluated at locations determined by an offset and scaling of
// the input x, y coordinates taken from other sources. If Indep is true, then the mapped x, y
// is independent of the original x, y location.
type DisplaceVF struct {
Name string
Src VectorField
SrcX, SrcY Field
Xfm *graphics2d.Aff3
Indep bool
}
// NewDisplaceVF creates a new Displace instance using the same transform for both x and y displacements.
func NewDisplaceVF(in VectorField, dx, dy Field, scale float64) *DisplaceVF {
xfm := graphics2d.NewAff3()
xfm.Scale(scale, scale)
return &DisplaceVF{"DisplaceVF", in, dx, dy, xfm, false}
}
// Eval2 implements the Field interface.
func (d *DisplaceVF) Eval2(x, y float64) []float64 {
vp := []float64{d.SrcX.Eval2(x, y), d.SrcY.Eval2(x, y)}
p := d.Xfm.Apply(vp)[0]
if !d.Indep {
return d.Src.Eval2(x+p[0], y+p[1])
}
return d.Src.Eval2(p[0], p[1])
}
// Displace2VF allows a source field to be evaluated at locations determined by axis independent transforms of
// the input x, y coordinates taken from other sources. If Indep is true, then the mapped x, y
// is independent of the original x, y location.
type Displace2VF struct {
Name string
Src VectorField
SrcX, SrcY Field
XfmX, XfmY *graphics2d.Aff3
Indep bool
}
// NewDisplace2VF creates a new Displace2 instance using the same source for both x and y displacements.
func NewDisplace2VF(in VectorField, dx, dy Field, xfmx, xfmy *graphics2d.Aff3) *Displace2VF {
return &Displace2VF{"Displace2VF", in, dx, dy, xfmx, xfmy, false}
}
// Eval2 implements the Field interface.
func (d *Displace2VF) Eval2(x, y float64) []float64 {
px := d.XfmX.Apply([]float64{d.SrcX.Eval2(x, y), 0})[0]
py := d.XfmY.Apply([]float64{0, d.SrcY.Eval2(x, y), 0})[0]
if !d.Indep {
return d.Src.Eval2(x+px[0], y+py[1])
}
return d.Src.Eval2(px[0], py[1])
}
// DisplaceCF allows a source field to be evaluated at locations determined by an offset and scaling of
// the input x, y coordinates taken from other sources. If Indep is true, then the mapped x, y
// is independent of the original x, y location.
type DisplaceCF struct {
Name string
Src ColorField
SrcX, SrcY Field
Xfm *graphics2d.Aff3
Indep bool
}
// NewDisplaceCF creates a new Displace instance using the same transform for both x and y displacements.
func NewDisplaceCF(in ColorField, dx, dy Field, scale float64) *DisplaceCF {
xfm := graphics2d.NewAff3()
xfm.Scale(scale, scale)
return &DisplaceCF{"DisplaceCF", in, dx, dy, xfm, false}
}
// Eval2 implements the Field interface.
func (d *DisplaceCF) Eval2(x, y float64) color.Color {
vp := []float64{d.SrcX.Eval2(x, y), d.SrcY.Eval2(x, y)}
p := d.Xfm.Apply(vp)[0]
if !d.Indep {
return d.Src.Eval2(x+p[0], y+p[1])
}
return d.Src.Eval2(p[0], p[1])
}
// Displace2CF allows a source field to be evaluated at locations determined by axis independent transforms of
// the input x, y coordinates taken from other sources. If Indep is true, then the mapped x, y
// is independent of the original x, y location.
type Displace2CF struct {
Name string
Src ColorField
SrcX, SrcY Field
XfmX, XfmY *graphics2d.Aff3
Indep bool
}
// NewDisplace2CF creates a new Displace2 instance using the same source for both x and y displacements.
func NewDisplace2CF(in ColorField, dx, dy Field, xfmx, xfmy *graphics2d.Aff3) *Displace2CF {
return &Displace2CF{"Displace2CF", in, dx, dy, xfmx, xfmy, false}
}
// Eval2 implements the Field interface.
func (d *Displace2CF) Eval2(x, y float64) color.Color {
px := d.XfmX.Apply([]float64{d.SrcX.Eval2(x, y), 0})[0]
py := d.XfmY.Apply([]float64{0, d.SrcY.Eval2(x, y), 0})[0]
if !d.Indep {
return d.Src.Eval2(x+px[0], y+py[1])
}
return d.Src.Eval2(px[0], py[1])
}