vector3.go 15 KB

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