curves.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math32
  5. // Curve constructs an array of Vector3
  6. type Curve struct {
  7. points []Vector3
  8. length float32
  9. }
  10. func (c *Curve) GetPoints() []Vector3 {
  11. return c.points
  12. }
  13. func (c *Curve) GetLength() float32 {
  14. return c.length
  15. }
  16. func (c *Curve) SetLength() {
  17. points := c.points
  18. l := float32(0.0)
  19. for i := 1; i < len(points); i++ {
  20. p0 := points[i].Clone()
  21. p1 := points[i-1].Clone()
  22. l += (p0.Sub(p1)).Length()
  23. }
  24. c.length = l
  25. }
  26. // Continue combines two curves
  27. // creates and returns a pointer to a new curve
  28. // combined curves are unaffected
  29. func (c *Curve) Continue(other *Curve) *Curve {
  30. last := c.points[len(c.points)-1].Clone()
  31. first := other.points[0].Clone()
  32. var continued, otherpoints []Vector3
  33. for i := 0; i < len(c.points); i++ {
  34. continued = append(continued, *c.points[i].Clone())
  35. }
  36. for i := 1; i < len(other.points); i++ {
  37. otherpoints = append(otherpoints, *other.points[i].Clone())
  38. }
  39. for i := 0; i < len(otherpoints); i++ {
  40. continued = append(continued, *otherpoints[i].Sub(first).Add(last))
  41. }
  42. newC := new(Curve)
  43. newC.points = continued
  44. newC.SetLength()
  45. return newC
  46. }
  47. // NewBezierQuadratic creates and returns a pointer to a new curve
  48. // Uses Vector3 pointers origin, control, and destination to calculate with
  49. // int npoints as the desired number of points along the curve
  50. func NewBezierQuadratic(origin, control, destination *Vector3, npoints int) *Curve {
  51. c := new(Curve)
  52. if npoints <= 2 {
  53. npoints = 3
  54. }
  55. var equation = func(t, v0, v1, v2 float32) float32 {
  56. a0 := 1.0 - t
  57. result := a0*a0*v0 + 2.0*t*a0*v1 + t*t*v2
  58. return result
  59. }
  60. var bezier []Vector3
  61. for i := 0; i <= npoints; i++ {
  62. t := float32(i) / float32(npoints)
  63. x := equation(t, origin.X, control.X, destination.X)
  64. y := equation(t, origin.Y, control.Y, destination.Y)
  65. z := equation(t, origin.Z, control.Z, destination.Z)
  66. vect := NewVector3(x, y, z)
  67. bezier = append(bezier, *vect)
  68. }
  69. c.points = bezier
  70. c.SetLength()
  71. return c
  72. }
  73. // NewBezierCubic creates and returns a pointer to a new curve
  74. // Uses Vector3 pointers origin, control1, control2, and destination to calculate with
  75. // int npoints as the desired number of points along the curve
  76. func NewBezierCubic(origin, control1, control2, destination *Vector3, npoints int) *Curve {
  77. c := new(Curve)
  78. if npoints <= 3 {
  79. npoints = 4
  80. }
  81. var equation = func(t, v0, v1, v2, v3 float32) float32 {
  82. a0 := 1.0 - t
  83. result := a0*a0*a0*v0 + 3.0*t*a0*a0*v1 + 3.0*t*t*a0*v2 + t*t*t*v3
  84. return result
  85. }
  86. var bezier []Vector3
  87. for i := 0; i <= npoints; i++ {
  88. t := float32(i) / float32(npoints)
  89. x := equation(t, origin.X, control1.X, control2.X, destination.X)
  90. y := equation(t, origin.Y, control1.Y, control2.Y, destination.Y)
  91. z := equation(t, origin.Z, control1.Z, control2.Z, destination.Z)
  92. vect := NewVector3(x, y, z)
  93. bezier = append(bezier, *vect)
  94. }
  95. c.points = bezier
  96. c.SetLength()
  97. return c
  98. }
  99. // NewHermiteSpline creates and returns a pointer to a new curve
  100. // Uses Vector3 pointers origin, tangent1, destination, and tangent2 to calculate with
  101. // int npoints as the desired number of points along the curve
  102. func NewHermiteSpline(origin, tangent1, destination, tangent2 *Vector3, npoints int) *Curve {
  103. c := new(Curve)
  104. var equation = func(t float32, v0, tan0, v1, tan1 *Vector3) *Vector3 {
  105. t2 := t * t
  106. t3 := t * t2
  107. p0 := (2.0 * t3) - (3.0 * t2) + 1.0
  108. p1 := (-2.0 * t3) + (3.0 * t2)
  109. p2 := t3 - (2.0 * t2) + t
  110. p3 := t3 - t2
  111. x := (v0.X * p0) + (v1.X * p1) + (tan0.X * p2) + (tan1.X * p3)
  112. y := (v0.Y * p0) + (v1.Y * p1) + (tan0.Y * p2) + (tan1.Y * p3)
  113. z := (v0.Z * p0) + (v1.Z * p1) + (tan0.Z * p2) + (tan1.Z * p3)
  114. return NewVector3(x, y, z)
  115. }
  116. step := float32(1.0) / float32(npoints)
  117. var hermite []Vector3
  118. for i := 0; i <= npoints; i++ {
  119. vect := equation(float32(i)*step, origin, tangent1, destination, tangent2)
  120. hermite = append(hermite, *vect)
  121. }
  122. c.points = hermite
  123. c.SetLength()
  124. return c
  125. }
  126. // NewCatmullRomSpline creates and returns a pointer to a new curve
  127. // Uses array of Vector3 pointers with int npoints as the desired number of points between supplied points
  128. // Use Boolean closed with true to close the start and end points
  129. func NewCatmullRomSpline(points []*Vector3, npoints int, closed bool) *Curve {
  130. c := new(Curve)
  131. var equation = func(t float32, v0, v1, v2, v3 *Vector3) *Vector3 {
  132. t2 := t * t
  133. t3 := t * t2
  134. x := 0.5 * ((((2.0 * v1.X) + ((-v0.X + v2.X) * t)) +
  135. (((((2.0 * v0.X) - (5.0 * v1.X)) + (4.0 * v2.X)) - v3.X) * t2)) +
  136. ((((-v0.X + (3.0 * v1.X)) - (3.0 * v2.X)) + v3.X) * t3))
  137. y := 0.5 * ((((2.0 * v1.Y) + ((-v0.Y + v2.Y) * t)) +
  138. (((((2.0 * v0.Y) - (5.0 * v1.Y)) + (4.0 * v2.Y)) - v3.Y) * t2)) +
  139. ((((-v0.Y + (3.0 * v1.Y)) - (3.0 * v2.Y)) + v3.Y) * t3))
  140. z := 0.5 * ((((2.0 * v1.Z) + ((-v0.Z + v2.Z) * t)) +
  141. (((((2.0 * v0.Z) - (5.0 * v1.Z)) + (4.0 * v2.Z)) - v3.Z) * t2)) +
  142. ((((-v0.Z + (3.0 * v1.Z)) - (3.0 * v2.Z)) + v3.Z) * t3))
  143. return NewVector3(x, y, z)
  144. }
  145. step := float32(1.0) / float32(npoints)
  146. var catmull []Vector3
  147. var t float32
  148. if closed {
  149. count := len(points)
  150. for i := 0; i < count; i++ {
  151. t = 0.0
  152. for n := 0; n < npoints; n++ {
  153. vect := equation(t, points[i%count], points[(i+1)%count], points[(i+2)%count], points[(i+3)%count])
  154. catmull = append(catmull, *vect)
  155. t += step
  156. }
  157. }
  158. catmull = append(catmull, catmull[0])
  159. } else {
  160. total := []*Vector3{points[0].Clone()}
  161. total = append(total, points...)
  162. total = append(total, points[len(points)-1].Clone())
  163. var i int
  164. for i = 0; i < len(total)-3; i++ {
  165. t = 0
  166. for n := 0; n < npoints; n++ {
  167. vect := equation(t, total[i], total[i+1], total[i+2], total[i+3])
  168. catmull = append(catmull, *vect)
  169. t += step
  170. }
  171. }
  172. i--
  173. vect := equation(t, total[i], total[i+1], total[i+2], total[i+3])
  174. catmull = append(catmull, *vect)
  175. }
  176. c.points = catmull
  177. c.SetLength()
  178. return c
  179. }