vector3.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. type Vector3 struct {
  6. X float32
  7. Y float32
  8. Z float32
  9. }
  10. // Common Vector3 values
  11. var Vector3Zero = Vector3{0, 0, 0}
  12. var Vector3Up = Vector3{0, 1, 0}
  13. var Vector3Down = Vector3{0, -1, 0}
  14. var Vector3Right = Vector3{1, 0, 0}
  15. var Vector3Left = Vector3{-1, 0, 0}
  16. var Vector3Front = Vector3{0, 0, 1}
  17. var Vector3Back = Vector3{0, 0, -1}
  18. // NewVector creates and returns a pointer to a Vector3 type
  19. func NewVector3(x, y, z float32) *Vector3 {
  20. return &Vector3{X: x, Y: y, Z: z}
  21. }
  22. // Set sets the components of the vector
  23. func (v *Vector3) Set(x, y, z float32) *Vector3 {
  24. v.X = x
  25. v.Y = y
  26. v.Z = z
  27. return v
  28. }
  29. // SetX sets the value of the X component of this vector
  30. func (v *Vector3) SetX(x float32) *Vector3 {
  31. v.X = x
  32. return v
  33. }
  34. // SetY sets the value of the Y component of this vector
  35. func (v *Vector3) SetY(y float32) *Vector3 {
  36. v.Y = y
  37. return v
  38. }
  39. // SetZ sets the value of the Z component of this vector
  40. func (v *Vector3) SetZ(z float32) *Vector3 {
  41. v.Z = z
  42. return v
  43. }
  44. // SetComponent sets the value of this vector component
  45. // specified by its index: X=0, Y=1, Z=2
  46. func (v *Vector3) SetComponent(index int, value float32) {
  47. switch index {
  48. case 0:
  49. v.X = value
  50. case 1:
  51. v.Y = value
  52. case 2:
  53. v.Z = value
  54. default:
  55. panic("index is out of range: ")
  56. }
  57. }
  58. // SetByName sets the value of this vector component
  59. // specified by its name: "x|Z", "y|Y", or "z|Z".
  60. func (v *Vector3) SetByName(name string, value float32) {
  61. switch name {
  62. case "x", "X":
  63. v.X = value
  64. case "y", "Y":
  65. v.Y = value
  66. case "z", "Z":
  67. v.Z = value
  68. default:
  69. panic("Invalid Vector3 component name: " + name)
  70. }
  71. }
  72. // Copy copies the specified vector into this one.
  73. func (v *Vector3) Copy(src *Vector3) *Vector3 {
  74. *v = *src
  75. return v
  76. }
  77. // Add adds the specified vector to this.
  78. func (v *Vector3) Add(src *Vector3) *Vector3 {
  79. v.X += src.X
  80. v.Y += src.Y
  81. v.Z += src.Z
  82. return v
  83. }
  84. // AddScalar adds the specified value to each of the vector components
  85. func (v *Vector3) AddScalar(s float32) *Vector3 {
  86. v.X += s
  87. v.Y += s
  88. v.Z += s
  89. return v
  90. }
  91. // AddVectors adds the specified vectors and saves the result in
  92. // this vector: v = a * b
  93. func (v *Vector3) AddVectors(a, b *Vector3) *Vector3 {
  94. v.X = a.X + b.X
  95. v.Y = a.Y + b.Y
  96. v.Z = a.Z + b.Z
  97. return v
  98. }
  99. // Sub subtracts the specified vector from this one:
  100. // v = v - p
  101. func (v *Vector3) Sub(p *Vector3) *Vector3 {
  102. v.X -= p.X
  103. v.Y -= p.Y
  104. v.Z -= p.Z
  105. return v
  106. }
  107. // SubScalar subtracts the specified scalar from this vector:
  108. // v = v - s
  109. func (v *Vector3) SubScalar(s float32) *Vector3 {
  110. v.X -= s
  111. v.Y -= s
  112. v.Z -= s
  113. return v
  114. }
  115. // SubVectors subtracts the specified vectors and stores
  116. // the result in this vector: v = a - b
  117. func (v *Vector3) SubVectors(a, b *Vector3) *Vector3 {
  118. v.X = a.X - b.X
  119. v.Y = a.Y - b.Y
  120. v.Z = a.Z - b.Z
  121. return v
  122. }
  123. // Multiply multiplies this vector by the specified vector:
  124. // v = v * a
  125. func (v *Vector3) Multiply(a *Vector3) *Vector3 {
  126. v.X *= a.X
  127. v.Y *= a.Y
  128. v.Z *= a.Z
  129. return v
  130. }
  131. // MultiplyScalar multiples this vector by the specified scalar:
  132. // v = v * s
  133. func (v *Vector3) MultiplyScalar(s float32) *Vector3 {
  134. v.X *= s
  135. v.Y *= s
  136. v.Z *= s
  137. return v
  138. }
  139. // MultiplyVectors multiply the specified vectors storing the
  140. // result in this vector: v = a * b
  141. func (v *Vector3) MultiplyVectors(a, b *Vector3) *Vector3 {
  142. v.X = a.X * b.X
  143. v.Y = a.Y * b.Y
  144. v.Z = a.Z * b.Z
  145. return v
  146. }
  147. //// ApplyEuler rotates this vector from the specified euler angles
  148. //func (v *Vector3) ApplyEuler(euler *Euler) *Vector3 {
  149. //
  150. // var quaternion Quaternion
  151. // v.ApplyQuaternion(quaternion.SetFromEuler2(euler))
  152. // return v
  153. //}
  154. // ApplyAxisAngle rotates the vertex around the specified axis by
  155. // the specified angle
  156. func (v *Vector3) ApplyAxisAngle(axis *Vector3, angle float32) *Vector3 {
  157. var quaternion Quaternion
  158. v.ApplyQuaternion(quaternion.SetFromAxisAngle(axis, angle))
  159. return v
  160. }
  161. // ApplyMatrix3 multiplies the specified 3x3 matrix by this vector:
  162. // v = m * v
  163. func (v *Vector3) ApplyMatrix3(m *Matrix3) *Vector3 {
  164. x := v.X
  165. y := v.Y
  166. z := v.Z
  167. v.X = m[0]*x + m[3]*y + m[6]*z
  168. v.Y = m[1]*x + m[4]*y + m[7]*z
  169. v.Z = m[2]*x + m[5]*y + m[8]*z
  170. return v
  171. }
  172. // ApplyMatrix4 multiplies the specified 4x4 matrix by this vector:
  173. // v = m * v
  174. func (v *Vector3) ApplyMatrix4(m *Matrix4) *Vector3 {
  175. x := v.X
  176. y := v.Y
  177. z := v.Z
  178. v.X = m[0]*x + m[4]*y + m[8]*z + m[12]
  179. v.Y = m[1]*x + m[5]*y + m[9]*z + m[13]
  180. v.Z = m[2]*x + m[6]*y + m[10]*z + m[14]
  181. return v
  182. }
  183. func (v *Vector3) ApplyProjection(m *Matrix4) *Vector3 {
  184. x := v.X
  185. y := v.Y
  186. z := v.Z
  187. d := 1 / (m[3]*x + m[7]*y + m[11]*z + m[15]) // perspective divide
  188. v.X = (m[0]*x + m[4]*y + m[8]*z + m[12]) * d
  189. v.Y = (m[1]*x + m[5]*y + m[9]*z + m[13]) * d
  190. v.Z = (m[2]*x + m[6]*y + m[10]*z + m[14]) * d
  191. return v
  192. }
  193. // ApplyQuaternion transforms this vector by multiplying it by
  194. // the specified quaternion and then by the quaternion
  195. // inverse.
  196. // It basically applies the rotation encoded in the quaternion
  197. // to this vector.
  198. // v = q * v * inverse(q)
  199. func (v *Vector3) ApplyQuaternion(q *Quaternion) *Vector3 {
  200. x := v.X
  201. y := v.Y
  202. z := v.Z
  203. qx := q.x
  204. qy := q.y
  205. qz := q.z
  206. qw := q.w
  207. // calculate quat * vector
  208. ix := qw*x + qy*z - qz*y
  209. iy := qw*y + qz*x - qx*z
  210. iz := qw*z + qx*y - qy*x
  211. iw := -qx*x - qy*y - qz*z
  212. // calculate result * inverse quat
  213. v.X = ix*qw + iw*-qx + iy*-qz - iz*-qy
  214. v.Y = iy*qw + iw*-qy + iz*-qx - ix*-qz
  215. v.Z = iz*qw + iw*-qz + ix*-qy - iy*-qx
  216. return v
  217. }
  218. func (v *Vector3) TransformDirection(m *Matrix4) *Vector3 {
  219. x := v.X
  220. y := v.Y
  221. z := v.Z
  222. v.X = m[0]*x + m[4]*y + m[8]*z
  223. v.Y = m[1]*x + m[5]*y + m[9]*z
  224. v.Z = m[2]*x + m[6]*y + m[10]*z
  225. v.Normalize()
  226. return v
  227. }
  228. func (v *Vector3) Divide(other *Vector3) *Vector3 {
  229. v.X /= other.X
  230. v.Y /= other.Y
  231. v.Z /= other.Z
  232. return v
  233. }
  234. func (v *Vector3) DivideScalar(scalar float32) *Vector3 {
  235. if scalar != 0 {
  236. invScalar := 1 / scalar
  237. v.X *= invScalar
  238. v.Y *= invScalar
  239. v.Z *= invScalar
  240. } else {
  241. v.X = 0
  242. v.Y = 0
  243. v.Z = 0
  244. }
  245. return v
  246. }
  247. func (v *Vector3) Min(other *Vector3) *Vector3 {
  248. if v.X > other.X {
  249. v.X = other.X
  250. }
  251. if v.Y > other.Y {
  252. v.Y = other.Y
  253. }
  254. if v.Z > other.Z {
  255. v.Z = other.Z
  256. }
  257. return v
  258. }
  259. // Max sets this vector with maximum components from itself and the other vector
  260. func (v *Vector3) Max(other *Vector3) *Vector3 {
  261. if v.X < other.X {
  262. v.X = other.X
  263. }
  264. if v.Y < other.Y {
  265. v.Y = other.Y
  266. }
  267. if v.Z < other.Z {
  268. v.Z = other.Z
  269. }
  270. return v
  271. }
  272. func (this *Vector3) Clamp(min, max *Vector3) *Vector3 {
  273. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  274. if this.X < min.X {
  275. this.X = min.X
  276. } else if this.X > max.X {
  277. this.X = max.X
  278. }
  279. if this.Y < min.Y {
  280. this.Y = min.Y
  281. } else if this.Y > max.Y {
  282. this.Y = max.Y
  283. }
  284. if this.Z < min.Z {
  285. this.Z = min.Z
  286. } else if this.Z > max.Z {
  287. this.Z = max.Z
  288. }
  289. return this
  290. }
  291. func (this *Vector3) ClampScalar(minVal, maxVal float32) *Vector3 {
  292. min := NewVector3(minVal, minVal, minVal)
  293. max := NewVector3(maxVal, maxVal, maxVal)
  294. return this.Clamp(min, max)
  295. }
  296. func (this *Vector3) Floor() *Vector3 {
  297. this.X = Floor(this.X)
  298. this.Y = Floor(this.Y)
  299. this.Z = Floor(this.Z)
  300. return this
  301. }
  302. func (this *Vector3) Ceil() *Vector3 {
  303. this.X = Ceil(this.X)
  304. this.Y = Ceil(this.Y)
  305. this.Z = Ceil(this.Z)
  306. return this
  307. }
  308. func (this *Vector3) Round() *Vector3 {
  309. this.X = Floor(this.X + 0.5)
  310. this.Y = Floor(this.Y + 0.5)
  311. this.Z = Floor(this.Z + 0.5)
  312. return this
  313. }
  314. func (this *Vector3) RoundToZero() *Vector3 {
  315. if this.X < 0 {
  316. this.X = Ceil(this.X)
  317. } else {
  318. this.X = Floor(this.X)
  319. }
  320. if this.Y < 0 {
  321. this.Y = Ceil(this.Y)
  322. } else {
  323. this.Y = Floor(this.Y)
  324. }
  325. if this.Z < 0 {
  326. this.Z = Ceil(this.Z)
  327. } else {
  328. this.Z = Floor(this.Z)
  329. }
  330. return this
  331. }
  332. func (this *Vector3) Negate() *Vector3 {
  333. this.X = -this.X
  334. this.Y = -this.Y
  335. this.Z = -this.Z
  336. return this
  337. }
  338. func (this *Vector3) Dot(v *Vector3) float32 {
  339. return this.X*v.X + this.Y*v.Y + this.Z*v.Z
  340. }
  341. func (this *Vector3) LengthSq() float32 {
  342. return this.X*this.X + this.Y*this.Y + this.Z*this.Z
  343. }
  344. func (this *Vector3) Length() float32 {
  345. return Sqrt(this.X*this.X + this.Y*this.Y + this.Z*this.Z)
  346. }
  347. func (this *Vector3) LengthManhattan(v *Vector3) float32 {
  348. return Abs(this.X + Abs(this.Y+Abs(this.Z)))
  349. }
  350. func (this *Vector3) Normalize() *Vector3 {
  351. return this.DivideScalar(this.Length())
  352. }
  353. func (this *Vector3) SetLength(l float32) *Vector3 {
  354. oldLength := this.Length()
  355. if oldLength != 0 && l != oldLength {
  356. this.MultiplyScalar(l / oldLength)
  357. }
  358. return this
  359. }
  360. func (this *Vector3) Lerp(v *Vector3, alpha float32) *Vector3 {
  361. this.X += (v.X - this.X) * alpha
  362. this.Y += (v.Y - this.Y) * alpha
  363. this.Z += (v.Z - this.Z) * alpha
  364. return this
  365. }
  366. func (this *Vector3) LerpVectors(v1, v2 *Vector3, alpha float32) *Vector3 {
  367. this.SubVectors(v2, v2).MultiplyScalar(alpha).Add(v1)
  368. return this
  369. }
  370. func (this *Vector3) Cross(v *Vector3) *Vector3 {
  371. cx := this.Y*v.Z - this.Z*v.Y
  372. cy := this.Z*v.X - this.X*v.Z
  373. cz := this.X*v.Y - this.Y*v.X
  374. this.X = cx
  375. this.Y = cy
  376. this.Z = cz
  377. return this
  378. }
  379. func (this *Vector3) CrossVectors(a, b *Vector3) *Vector3 {
  380. cx := a.Y*b.Z - a.Z*b.Y
  381. cy := a.Z*b.X - a.X*b.Z
  382. cz := a.X*b.Y - a.Y*b.X
  383. this.X = cx
  384. this.Y = cy
  385. this.Z = cz
  386. return this
  387. }
  388. func (this *Vector3) ProjectOnVector(vector *Vector3) *Vector3 {
  389. var v1 Vector3
  390. v1.Copy(vector).Normalize()
  391. dot := this.Dot(&v1)
  392. return this.Copy(&v1).MultiplyScalar(dot)
  393. }
  394. func (this *Vector3) ProjectOnPlane(planeNormal *Vector3) *Vector3 {
  395. var v1 Vector3
  396. v1.Copy(this).ProjectOnVector(planeNormal)
  397. return this.Sub(&v1)
  398. }
  399. func (this *Vector3) Reflect(normal *Vector3) *Vector3 {
  400. // reflect incident vector off plane orthogonal to normal
  401. // normal is assumed to have unit length
  402. var v1 Vector3
  403. return this.Sub(v1.Copy(normal).MultiplyScalar(2 * this.Dot(normal)))
  404. }
  405. func (this *Vector3) AngleTo(v *Vector3) float32 {
  406. theta := this.Dot(v) / (this.Length() * v.Length())
  407. // clamp, to handle numerical problems
  408. return Acos(Clamp(theta, -1, 1))
  409. }
  410. func (this *Vector3) DistanceTo(v *Vector3) float32 {
  411. return Sqrt(this.DistanceToSquared(v))
  412. }
  413. func (this *Vector3) DistanceToSquared(v *Vector3) float32 {
  414. dx := this.X - v.X
  415. dy := this.Y - v.Y
  416. dz := this.Z - v.Z
  417. return dx*dx + dy*dy + dz*dz
  418. }
  419. // SetFromMatrixPosition set this vector from translation coordinates
  420. // in the specified transformation matrix.
  421. func (v *Vector3) SetFromMatrixPosition(m *Matrix4) *Vector3 {
  422. v.X = m[12]
  423. v.Y = m[13]
  424. v.Z = m[14]
  425. return v
  426. }
  427. func (this *Vector3) SetFromMatrixScale(m *Matrix4) *Vector3 {
  428. sx := this.Set(m[0], m[1], m[2]).Length()
  429. sy := this.Set(m[4], m[5], m[6]).Length()
  430. sz := this.Set(m[8], m[9], m[10]).Length()
  431. this.X = sx
  432. this.Y = sy
  433. this.Z = sz
  434. return this
  435. }
  436. // SetFromMatrixColumn set this vector with the column at index 'index'
  437. // of the 'm' matrix.
  438. func (v *Vector3) SetFromMatrixColumn(index int, m *Matrix4) *Vector3 {
  439. offset := index * 4
  440. v.X = m[offset]
  441. v.Y = m[offset+1]
  442. v.Z = m[offset+2]
  443. return v
  444. }
  445. func (this *Vector3) Equals(v *Vector3) bool {
  446. return (v.X == this.X) && (v.Y == this.Y) && (v.Z == this.Z)
  447. }
  448. func (v *Vector3) FromArray(array []float32, offset int) *Vector3 {
  449. v.X = array[offset]
  450. v.Y = array[offset+1]
  451. v.Z = array[offset+2]
  452. return v
  453. }
  454. func (v *Vector3) ToArray(array []float32, offset int) []float32 {
  455. array[offset] = v.X
  456. array[offset+1] = v.Y
  457. array[offset+2] = v.Z
  458. return array
  459. }
  460. func (this *Vector3) Clone() *Vector3 {
  461. return NewVector3(this.X, this.Y, this.Z)
  462. }
  463. // SetFromRotationMatrix sets this vector components to the Euler angles
  464. // from the specified pure rotation matrix.
  465. func (v *Vector3) SetFromRotationMatrix(m *Matrix4) *Vector3 {
  466. m11 := m[0]
  467. m12 := m[4]
  468. m13 := m[8]
  469. m22 := m[5]
  470. m23 := m[9]
  471. m32 := m[6]
  472. m33 := m[10]
  473. v.Y = Asin(Clamp(m13, -1, 1))
  474. if Abs(m13) < 0.99999 {
  475. v.X = Atan2(-m23, m33)
  476. v.Z = Atan2(-m12, m11)
  477. } else {
  478. v.X = Atan2(m32, m22)
  479. v.Z = 0
  480. }
  481. return v
  482. }
  483. // SetFromQuaternion sets this vector components to the Euler angles
  484. // from the specified quaternion
  485. func (v *Vector3) SetFromQuaternion(q *Quaternion) *Vector3 {
  486. matrix := NewMatrix4()
  487. matrix.MakeRotationFromQuaternion(q)
  488. v.SetFromRotationMatrix(matrix)
  489. return v
  490. }