forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurves.go
202 lines (179 loc) · 5.88 KB
/
curves.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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package math32
// Curve constructs an array of Vector3
type Curve struct {
points []Vector3
length float32
}
func (c *Curve) GetPoints() []Vector3 {
return c.points
}
func (c *Curve) GetLength() float32 {
return c.length
}
func (c *Curve) SetLength() {
points := c.points
l := float32(0.0)
for i := 1; i < len(points); i++ {
p0 := points[i].Clone()
p1 := points[i-1].Clone()
l += (p0.Sub(p1)).Length()
}
c.length = l
}
// Continue combines two curves
// creates and returns a pointer to a new curve
// combined curves are unaffected
func (c *Curve) Continue(other *Curve) *Curve {
last := c.points[len(c.points)-1].Clone()
first := other.points[0].Clone()
var continued, otherpoints []Vector3
for i := 0; i < len(c.points); i++ {
continued = append(continued, *c.points[i].Clone())
}
for i := 1; i < len(other.points); i++ {
otherpoints = append(otherpoints, *other.points[i].Clone())
}
for i := 0; i < len(otherpoints); i++ {
continued = append(continued, *otherpoints[i].Sub(first).Add(last))
}
newC := new(Curve)
newC.points = continued
newC.SetLength()
return newC
}
// NewBezierQuadratic creates and returns a pointer to a new curve
// Uses Vector3 pointers origin, control, and destination to calculate with
// int npoints as the desired number of points along the curve
func NewBezierQuadratic(origin, control, destination *Vector3, npoints int) *Curve {
c := new(Curve)
if npoints <= 2 {
npoints = 3
}
var equation = func(t, v0, v1, v2 float32) float32 {
a0 := 1.0 - t
result := a0*a0*v0 + 2.0*t*a0*v1 + t*t*v2
return result
}
var bezier []Vector3
for i := 0; i <= npoints; i++ {
t := float32(i) / float32(npoints)
x := equation(t, origin.X, control.X, destination.X)
y := equation(t, origin.Y, control.Y, destination.Y)
z := equation(t, origin.Z, control.Z, destination.Z)
vect := NewVector3(x, y, z)
bezier = append(bezier, *vect)
}
c.points = bezier
c.SetLength()
return c
}
// NewBezierCubic creates and returns a pointer to a new curve
// Uses Vector3 pointers origin, control1, control2, and destination to calculate with
// int npoints as the desired number of points along the curve
func NewBezierCubic(origin, control1, control2, destination *Vector3, npoints int) *Curve {
c := new(Curve)
if npoints <= 3 {
npoints = 4
}
var equation = func(t, v0, v1, v2, v3 float32) float32 {
a0 := 1.0 - t
result := a0*a0*a0*v0 + 3.0*t*a0*a0*v1 + 3.0*t*t*a0*v2 + t*t*t*v3
return result
}
var bezier []Vector3
for i := 0; i <= npoints; i++ {
t := float32(i) / float32(npoints)
x := equation(t, origin.X, control1.X, control2.X, destination.X)
y := equation(t, origin.Y, control1.Y, control2.Y, destination.Y)
z := equation(t, origin.Z, control1.Z, control2.Z, destination.Z)
vect := NewVector3(x, y, z)
bezier = append(bezier, *vect)
}
c.points = bezier
c.SetLength()
return c
}
// NewHermiteSpline creates and returns a pointer to a new curve
// Uses Vector3 pointers origin, tangent1, destination, and tangent2 to calculate with
// int npoints as the desired number of points along the curve
func NewHermiteSpline(origin, tangent1, destination, tangent2 *Vector3, npoints int) *Curve {
c := new(Curve)
var equation = func(t float32, v0, tan0, v1, tan1 *Vector3) *Vector3 {
t2 := t * t
t3 := t * t2
p0 := (2.0 * t3) - (3.0 * t2) + 1.0
p1 := (-2.0 * t3) + (3.0 * t2)
p2 := t3 - (2.0 * t2) + t
p3 := t3 - t2
x := (v0.X * p0) + (v1.X * p1) + (tan0.X * p2) + (tan1.X * p3)
y := (v0.Y * p0) + (v1.Y * p1) + (tan0.Y * p2) + (tan1.Y * p3)
z := (v0.Z * p0) + (v1.Z * p1) + (tan0.Z * p2) + (tan1.Z * p3)
return NewVector3(x, y, z)
}
step := float32(1.0) / float32(npoints)
var hermite []Vector3
for i := 0; i <= npoints; i++ {
vect := equation(float32(i)*step, origin, tangent1, destination, tangent2)
hermite = append(hermite, *vect)
}
c.points = hermite
c.SetLength()
return c
}
// NewCatmullRomSpline creates and returns a pointer to a new curve
// Uses array of Vector3 pointers with int npoints as the desired number of points between supplied points
// Use Boolean closed with true to close the start and end points
func NewCatmullRomSpline(points []*Vector3, npoints int, closed bool) *Curve {
c := new(Curve)
var equation = func(t float32, v0, v1, v2, v3 *Vector3) *Vector3 {
t2 := t * t
t3 := t * t2
x := 0.5 * ((((2.0 * v1.X) + ((-v0.X + v2.X) * t)) +
(((((2.0 * v0.X) - (5.0 * v1.X)) + (4.0 * v2.X)) - v3.X) * t2)) +
((((-v0.X + (3.0 * v1.X)) - (3.0 * v2.X)) + v3.X) * t3))
y := 0.5 * ((((2.0 * v1.Y) + ((-v0.Y + v2.Y) * t)) +
(((((2.0 * v0.Y) - (5.0 * v1.Y)) + (4.0 * v2.Y)) - v3.Y) * t2)) +
((((-v0.Y + (3.0 * v1.Y)) - (3.0 * v2.Y)) + v3.Y) * t3))
z := 0.5 * ((((2.0 * v1.Z) + ((-v0.Z + v2.Z) * t)) +
(((((2.0 * v0.Z) - (5.0 * v1.Z)) + (4.0 * v2.Z)) - v3.Z) * t2)) +
((((-v0.Z + (3.0 * v1.Z)) - (3.0 * v2.Z)) + v3.Z) * t3))
return NewVector3(x, y, z)
}
step := float32(1.0) / float32(npoints)
var catmull []Vector3
var t float32
if closed {
count := len(points)
for i := 0; i < count; i++ {
t = 0.0
for n := 0; n < npoints; n++ {
vect := equation(t, points[i%count], points[(i+1)%count], points[(i+2)%count], points[(i+3)%count])
catmull = append(catmull, *vect)
t += step
}
}
catmull = append(catmull, catmull[0])
} else {
total := []*Vector3{points[0].Clone()}
total = append(total, points...)
total = append(total, points[len(points)-1].Clone())
var i int
for i = 0; i < len(total)-3; i++ {
t = 0
for n := 0; n < npoints; n++ {
vect := equation(t, total[i], total[i+1], total[i+2], total[i+3])
catmull = append(catmull, *vect)
t += step
}
}
i--
vect := equation(t, total[i], total[i+1], total[i+2], total[i+3])
catmull = append(catmull, *vect)
}
c.points = catmull
c.SetLength()
return c
}