vector4.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. // Vector4 is a vector/point in homogeneous coordinates with X, Y, Z and W components.
  6. type Vector4 struct {
  7. X float32
  8. Y float32
  9. Z float32
  10. W float32
  11. }
  12. // NewVector4 creates and returns a pointer to a new Vector4
  13. func NewVector4(x, y, z, w float32) *Vector4 {
  14. return &Vector4{X: x, Y: y, Z: z, W: w}
  15. }
  16. // Set sets this vector X, Y, Z and W components.
  17. // Returns the pointer to this updated vector.
  18. func (v *Vector4) Set(x, y, z, w float32) *Vector4 {
  19. v.X = x
  20. v.Y = y
  21. v.Z = z
  22. v.W = w
  23. return v
  24. }
  25. // SetVector3 sets this vector from another Vector3 and W
  26. func (v *Vector4) SetVector3(other *Vector3, w float32) *Vector4 {
  27. v.X = other.X
  28. v.Y = other.Y
  29. v.Z = other.Z
  30. v.W = w
  31. return v
  32. }
  33. // SetX sets this vector X component.
  34. // Returns the pointer to this updated Vector.
  35. func (v *Vector4) SetX(x float32) *Vector4 {
  36. v.X = x
  37. return v
  38. }
  39. // SetY sets this vector Y component.
  40. // Returns the pointer to this updated vector.
  41. func (v *Vector4) SetY(y float32) *Vector4 {
  42. v.Y = y
  43. return v
  44. }
  45. // SetZ sets this vector Z component.
  46. // Returns the pointer to this updated vector.
  47. func (v *Vector4) SetZ(z float32) *Vector4 {
  48. v.Z = z
  49. return v
  50. }
  51. // SetW sets this vector W component.
  52. // Returns the pointer to this updated vector.
  53. func (v *Vector4) SetW(w float32) *Vector4 {
  54. v.W = w
  55. return v
  56. }
  57. // SetComponent sets this vector component value by its index: 0 for X, 1 for Y, 2 for Z, 3 for W.
  58. // Returns the pointer to this updated vector
  59. func (v *Vector4) SetComponent(index int, value float32) *Vector4 {
  60. switch index {
  61. case 0:
  62. v.X = value
  63. case 1:
  64. v.Y = value
  65. case 2:
  66. v.Z = value
  67. case 3:
  68. v.Z = value
  69. default:
  70. panic("index is out of range")
  71. }
  72. return v
  73. }
  74. // Component returns this vector component by its index: 0 for X, 1 for Y, 2 for Z, 3 for W.
  75. func (v *Vector4) Component(index int) float32 {
  76. switch index {
  77. case 0:
  78. return v.X
  79. case 1:
  80. return v.Y
  81. case 2:
  82. return v.Z
  83. case 3:
  84. return v.W
  85. default:
  86. panic("index is out of range")
  87. }
  88. }
  89. // SetByName sets this vector component value by its case insensitive name: "x", "y", "z" or "w".
  90. func (v *Vector4) SetByName(name string, value float32) {
  91. switch name {
  92. case "x", "X":
  93. v.X = value
  94. case "y", "Y":
  95. v.Y = value
  96. case "z", "Z":
  97. v.Z = value
  98. case "w", "W":
  99. v.W = value
  100. default:
  101. panic("Invalid Vector4 component name: " + name)
  102. }
  103. }
  104. // Copy copies other vector to this one.
  105. // Returns the pointer to this updated vector.
  106. func (v *Vector4) Copy(other *Vector4) *Vector4 {
  107. *v = *other
  108. return v
  109. }
  110. // Add adds other vector to this one.
  111. // Returns the pointer to this updated vector.
  112. func (v *Vector4) Add(other *Vector4) *Vector4 {
  113. v.X += other.X
  114. v.Y += other.Y
  115. v.Z += other.Z
  116. v.W += other.W
  117. return v
  118. }
  119. // AddScalar adds scalar s to each component of this vector.
  120. // Returns the pointer to this updated vector.
  121. func (v *Vector4) AddScalar(s float32) *Vector4 {
  122. v.X += s
  123. v.Y += s
  124. v.Z += s
  125. v.W += s
  126. return v
  127. }
  128. // AddVectors adds vectors a and b to this one.
  129. // Returns the pointer to this updated vector.
  130. func (v *Vector4) AddVectors(a, b *Vector4) *Vector4 {
  131. v.X = a.X + b.X
  132. v.Y = a.Y + b.Y
  133. v.Z = a.Z + b.Z
  134. v.W = a.W + b.W
  135. return v
  136. }
  137. // Sub subtracts other vector from this one.
  138. // Returns the pointer to this updated vector.
  139. func (v *Vector4) Sub(other *Vector4) *Vector4 {
  140. v.X -= other.X
  141. v.Y -= other.Y
  142. v.Z -= other.Z
  143. v.W -= other.W
  144. return v
  145. }
  146. // SubScalar subtracts scalar s from each component of this vector.
  147. // Returns the pointer to this updated vector.
  148. func (v *Vector4) SubScalar(s float32) *Vector4 {
  149. v.X -= s
  150. v.Y -= s
  151. v.Z -= s
  152. v.W -= s
  153. return v
  154. }
  155. // SubVectors sets this vector to a - b.
  156. // Returns the pointer to this updated vector.
  157. func (v *Vector4) SubVectors(a, b *Vector4) *Vector4 {
  158. v.X = a.X - b.X
  159. v.Y = a.Y - b.Y
  160. v.Z = a.Y - b.Z
  161. v.W = a.Y - b.W
  162. return v
  163. }
  164. // Multiply multiplies each component of this vector by the corresponding one from other vector.
  165. // Returns the pointer to this updated vector.
  166. func (v *Vector4) Multiply(other *Vector4) *Vector4 {
  167. v.X *= other.X
  168. v.Y *= other.Y
  169. v.Z *= other.Z
  170. v.W *= other.W
  171. return v
  172. }
  173. // MultiplyScalar multiplies each component of this vector by the scalar s.
  174. // Returns the pointer to this updated vector.
  175. func (v *Vector4) MultiplyScalar(scalar float32) *Vector4 {
  176. v.X *= scalar
  177. v.Y *= scalar
  178. v.Z *= scalar
  179. v.W *= scalar
  180. return v
  181. }
  182. // Divide divides each component of this vector by the corresponding one from other vector.
  183. // Returns the pointer to this updated vector
  184. func (v *Vector4) Divide(other *Vector4) *Vector4 {
  185. v.X /= other.X
  186. v.Y /= other.Y
  187. v.Z /= other.Z
  188. v.W /= other.W
  189. return v
  190. }
  191. // DivideScalar divides each component of this vector by the scalar s.
  192. // If scalar is zero, sets this vector to zero.
  193. // Returns the pointer to this updated vector.
  194. func (v *Vector4) DivideScalar(scalar float32) *Vector4 {
  195. if scalar != 0 {
  196. invScalar := 1 / scalar
  197. v.X *= invScalar
  198. v.Y *= invScalar
  199. v.Z *= invScalar
  200. v.W *= invScalar
  201. } else {
  202. v.X = 0
  203. v.Y = 0
  204. v.Z = 0
  205. v.W = 0
  206. }
  207. return v
  208. }
  209. // Min sets this vector components to the minimum values of itself and other vector.
  210. // Returns the pointer to this updated vector.
  211. func (v *Vector4) Min(other *Vector4) *Vector4 {
  212. if v.X > other.X {
  213. v.X = other.X
  214. }
  215. if v.Y > other.Y {
  216. v.Y = other.Y
  217. }
  218. if v.Z > other.Z {
  219. v.Z = other.Z
  220. }
  221. if v.W > other.W {
  222. v.W = other.W
  223. }
  224. return v
  225. }
  226. // Max sets this vector components to the maximum value of itself and other vector.
  227. // Returns the pointer to this updated vector.
  228. func (v *Vector4) Max(other *Vector4) *Vector4 {
  229. if v.X < other.X {
  230. v.X = other.X
  231. }
  232. if v.Y < other.Y {
  233. v.Y = other.Y
  234. }
  235. if v.Z < other.Z {
  236. v.Z = other.Z
  237. }
  238. if v.W < other.W {
  239. v.W = other.W
  240. }
  241. return v
  242. }
  243. // Clamp sets this vector components to be no less than the corresponding components of min
  244. // and not greater than the corresponding component of max.
  245. // Assumes min < max, if this assumption isn't true it will not operate correctly.
  246. // Returns the pointer to this updated vector.
  247. func (v *Vector4) Clamp(min, max *Vector4) *Vector4 {
  248. if v.X < min.X {
  249. v.X = min.X
  250. } else if v.X > max.X {
  251. v.X = max.X
  252. }
  253. if v.Y < min.Y {
  254. v.Y = min.Y
  255. } else if v.Y > max.Y {
  256. v.Y = max.Y
  257. }
  258. if v.Z < min.Z {
  259. v.Z = min.Z
  260. } else if v.Z > max.Z {
  261. v.Z = max.Z
  262. }
  263. if v.W < min.W {
  264. v.W = min.W
  265. } else if v.W > max.W {
  266. v.W = max.W
  267. }
  268. return v
  269. }
  270. // ClampScalar sets this vector components to be no less than minVal and not greater than maxVal.
  271. // Returns the pointer to this updated vector.
  272. func (v *Vector4) ClampScalar(minVal, maxVal float32) *Vector4 {
  273. min := NewVector4(minVal, minVal, minVal, minVal)
  274. max := NewVector4(maxVal, maxVal, maxVal, maxVal)
  275. return v.Clamp(min, max)
  276. }
  277. // Floor applies math32.Floor() to each of this vector's components.
  278. // Returns the pointer to this updated vector.
  279. func (v *Vector4) Floor() *Vector4 {
  280. v.X = Floor(v.X)
  281. v.Y = Floor(v.Y)
  282. v.Z = Floor(v.Z)
  283. v.W = Floor(v.W)
  284. return v
  285. }
  286. // Ceil applies math32.Ceil() to each of this vector's components.
  287. // Returns the pointer to this updated vector.
  288. func (v *Vector4) Ceil() *Vector4 {
  289. v.X = Ceil(v.X)
  290. v.Y = Ceil(v.Y)
  291. v.Z = Ceil(v.Z)
  292. v.W = Ceil(v.W)
  293. return v
  294. }
  295. // Round rounds each of this vector's components.
  296. // Returns the pointer to this updated vector.
  297. func (v *Vector4) Round() *Vector4 {
  298. v.X = Floor(v.X + 0.5)
  299. v.Y = Floor(v.Y + 0.5)
  300. v.Z = Floor(v.Z + 0.5)
  301. v.W = Floor(v.W + 0.5)
  302. return v
  303. }
  304. // Negate negates each of this vector's components.
  305. // Returns the pointer to this updated vector.
  306. func (v *Vector4) Negate() *Vector4 {
  307. v.X = -v.X
  308. v.Y = -v.Y
  309. v.Z = -v.Z
  310. v.W = -v.W
  311. return v
  312. }
  313. // Dot returns the dot product of this vector with other.
  314. // None of the vectors are changed.
  315. func (v *Vector4) Dot(other *Vector4) float32 {
  316. return v.X*other.X + v.Y*other.Y + v.Z*other.Z + v.W*other.W
  317. }
  318. // LengthSq returns the length squared of this vector.
  319. // LengthSq can be used to compare vectors' lengths without the need to perform a square root.
  320. func (v *Vector4) LengthSq() float32 {
  321. return v.X*v.X + v.Y*v.Y + v.Z*v.Z + v.W*v.W
  322. }
  323. // Length returns the length of this vector.
  324. func (v *Vector4) Length() float32 {
  325. return Sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z + v.W*v.W)
  326. }
  327. // Normalize normalizes this vector so its length will be 1.
  328. // Returns the pointer to this updated vector.
  329. func (v *Vector4) Normalize() *Vector4 {
  330. return v.DivideScalar(v.Length())
  331. }
  332. // SetLength sets this vector to have the specified length.
  333. // If the current length is zero, does nothing.
  334. // Returns the pointer to this updated vector.
  335. func (v *Vector4) SetLength(l float32) *Vector4 {
  336. oldLength := v.Length()
  337. if oldLength != 0 && l != oldLength {
  338. v.MultiplyScalar(l / oldLength)
  339. }
  340. return v
  341. }
  342. // Lerp sets each of this vector's components to the linear interpolated value of
  343. // alpha between ifself and the corresponding other component.
  344. // Returns the pointer to this updated vector.
  345. func (v *Vector4) Lerp(other *Vector4, alpha float32) *Vector4 {
  346. v.X += (other.X - v.X) * alpha
  347. v.Y += (other.Y - v.Y) * alpha
  348. v.Z += (other.Z - v.Z) * alpha
  349. v.W += (other.W - v.W) * alpha
  350. return v
  351. }
  352. // Equals returns if this vector is equal to other.
  353. func (v *Vector4) Equals(other *Vector4) bool {
  354. return (other.X == v.X) && (other.Y == v.Y) && (other.Z == v.Z) && (other.W == v.W)
  355. }
  356. // FromArray sets this vector's components from the specified array and offset
  357. // Returns the pointer to this updated vector.
  358. func (v *Vector4) FromArray(array []float32, offset int) *Vector4 {
  359. v.X = array[offset]
  360. v.Y = array[offset+1]
  361. v.Z = array[offset+2]
  362. v.W = array[offset+3]
  363. return v
  364. }
  365. // ToArray copies this vector's components to array starting at offset.
  366. // Returns the array.
  367. func (v *Vector4) ToArray(array []float32, offset int) []float32 {
  368. array[offset] = v.X
  369. array[offset+1] = v.Y
  370. array[offset+2] = v.Z
  371. array[offset+3] = v.W
  372. return array
  373. }
  374. // ApplyMatrix4 multiplies the specified 4x4 matrix by this vector.
  375. // Returns the pointer to this updated vector.
  376. func (v *Vector4) ApplyMatrix4(m *Matrix4) *Vector4 {
  377. x := v.X
  378. y := v.Y
  379. z := v.Z
  380. w := v.W
  381. v.X = m[0]*x + m[4]*y + m[8]*z + m[12]*w
  382. v.Y = m[1]*x + m[5]*y + m[9]*z + m[13]*w
  383. v.Z = m[2]*x + m[6]*y + m[10]*z + m[14]*w
  384. v.W = m[3]*x + m[7]*y + m[11]*z + m[15]*w
  385. return v
  386. }
  387. // SetAxisAngleFromQuaternion set this vector to be the axis (x, y, z) and angle (w) of a rotation specified the quaternion q.
  388. // Assumes q is normalized.
  389. func (v *Vector4) SetAxisAngleFromQuaternion(q *Quaternion) *Vector4 {
  390. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  391. v.W = 2 * Acos(q.W())
  392. s := Sqrt(1 - q.W()*q.W())
  393. if s < 0.0001 {
  394. v.X = 1
  395. v.Y = 0
  396. v.Z = 0
  397. } else {
  398. v.X = q.X() / s
  399. v.Y = q.Y() / s
  400. v.Z = q.Z() / s
  401. }
  402. return v
  403. }
  404. // SetAxisFromRotationMatrix this vector to be the axis (x, y, z) and angle (w) of a rotation specified the matrix m.
  405. // Assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled).
  406. func (v *Vector4) SetAxisFromRotationMatrix(m *Matrix4) *Vector4 {
  407. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  408. var angle, x, y, z float32 // variables for result
  409. var epsilon float32 = 0.01 // margin to allow for rounding errors
  410. var epsilon2 float32 = 0.1 // margin to distinguish between 0 and 180 degrees
  411. m11 := m[0]
  412. m12 := m[4]
  413. m13 := m[8]
  414. m21 := m[1]
  415. m22 := m[5]
  416. m23 := m[9]
  417. m31 := m[2]
  418. m32 := m[6]
  419. m33 := m[10]
  420. if (Abs(m12-m21) < epsilon) && (Abs(m13-m31) < epsilon) && (Abs(m23-m32) < epsilon) {
  421. // singularity found
  422. // first check for identity matrix which must have +1 for all terms
  423. // in leading diagonal and zero in other terms
  424. if (Abs(m12+m21) < epsilon2) && (Abs(m13+m31) < epsilon2) && (Abs(m23+m32) < epsilon2) && (Abs(m11+m22+m33-3) < epsilon2) {
  425. // v singularity is identity matrix so angle = 0
  426. v.Set(1, 0, 0, 0)
  427. return v // zero angle, arbitrary axis
  428. }
  429. // otherwise this singularity is angle = 180
  430. angle = Pi
  431. var xx = (m11 + 1) / 2
  432. var yy = (m22 + 1) / 2
  433. var zz = (m33 + 1) / 2
  434. var xy = (m12 + m21) / 4
  435. var xz = (m13 + m31) / 4
  436. var yz = (m23 + m32) / 4
  437. if (xx > yy) && (xx > zz) { // m11 is the largest diagonal term
  438. if xx < epsilon {
  439. x = 0
  440. y = 0.707106781
  441. z = 0.707106781
  442. } else {
  443. x = Sqrt(xx)
  444. y = xy / x
  445. z = xz / x
  446. }
  447. } else if yy > zz { // m22 is the largest diagonal term
  448. if yy < epsilon {
  449. x = 0.707106781
  450. y = 0
  451. z = 0.707106781
  452. } else {
  453. y = Sqrt(yy)
  454. x = xy / y
  455. z = yz / y
  456. }
  457. } else { // m33 is the largest diagonal term so base result on this
  458. if zz < epsilon {
  459. x = 0.707106781
  460. y = 0.707106781
  461. z = 0
  462. } else {
  463. z = Sqrt(zz)
  464. x = xz / z
  465. y = yz / z
  466. }
  467. }
  468. v.Set(x, y, z, angle)
  469. return v // return 180 deg rotation
  470. }
  471. // as we have reached here there are no singularities so we can handle normally
  472. s := Sqrt((m32-m23)*(m32-m23) + (m13-m31)*(m13-m31) + (m21-m12)*(m21-m12)) // used to normalize
  473. if Abs(s) < 0.001 {
  474. s = 1
  475. }
  476. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  477. // caught by singularity test above, but I've left it in just in case
  478. v.X = (m32 - m23) / s
  479. v.Y = (m13 - m31) / s
  480. v.Z = (m21 - m12) / s
  481. v.W = Acos((m11 + m22 + m33 - 1) / 2)
  482. return v
  483. }
  484. // Clone returns a copy of this vector
  485. func (v *Vector4) Clone() *Vector4 {
  486. return NewVector4(v.X, v.Y, v.Z, v.W)
  487. }