matrix3.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. import "errors"
  6. // Matrix3 is 3x3 matrix organized internally as column matrix
  7. type Matrix3 [9]float32
  8. // NewMatrix3 creates and returns a pointer to a new Matrix3
  9. // initialized as the identity matrix.
  10. func NewMatrix3() *Matrix3 {
  11. var m Matrix3
  12. m.Identity()
  13. return &m
  14. }
  15. // Set sets all the elements of the matrix row by row starting at row1, column1,
  16. // row1, column2, row1, column3 and so forth.
  17. // Returns the pointer to this updated Matrix.
  18. func (m *Matrix3) Set(n11, n12, n13, n21, n22, n23, n31, n32, n33 float32) *Matrix3 {
  19. m[0] = n11
  20. m[3] = n12
  21. m[6] = n13
  22. m[1] = n21
  23. m[4] = n22
  24. m[7] = n23
  25. m[2] = n31
  26. m[5] = n32
  27. m[8] = n33
  28. return m
  29. }
  30. // Identity sets this matrix as the identity matrix.
  31. // Returns the pointer to this updated matrix.
  32. func (m *Matrix3) Identity() *Matrix3 {
  33. m.Set(
  34. 1, 0, 0,
  35. 0, 1, 0,
  36. 0, 0, 1,
  37. )
  38. return m
  39. }
  40. // Zero sets this matrix as the zero matrix.
  41. // Returns the pointer to this updated matrix.
  42. func (m *Matrix3) Zero() *Matrix3 {
  43. m.Set(
  44. 0, 0, 0,
  45. 0, 0, 0,
  46. 0, 0, 0,
  47. )
  48. return m
  49. }
  50. // Copy copies src matrix into this one.
  51. // Returns the pointer to this updated matrix.
  52. func (m *Matrix3) Copy(src *Matrix3) *Matrix3 {
  53. *m = *src
  54. return m
  55. }
  56. // MakeRotationFromQuaternion sets this matrix as a rotation matrix from the specified quaternion.
  57. // Returns pointer to this updated matrix.
  58. func (m *Matrix3) MakeRotationFromQuaternion(q *Quaternion) *Matrix3 {
  59. x := q.X
  60. y := q.Y
  61. z := q.Z
  62. w := q.W
  63. x2 := x + x
  64. y2 := y + y
  65. z2 := z + z
  66. xx := x * x2
  67. xy := x * y2
  68. xz := x * z2
  69. yy := y * y2
  70. yz := y * z2
  71. zz := z * z2
  72. wx := w * x2
  73. wy := w * y2
  74. wz := w * z2
  75. m[0] = 1 - (yy + zz)
  76. m[3] = xy - wz
  77. m[6] = xz + wy
  78. m[1] = xy + wz
  79. m[4] = 1 - (xx + zz)
  80. m[7] = yz - wx
  81. m[2] = xz - wy
  82. m[5] = yz + wx
  83. m[8] = 1 - (xx + yy)
  84. return m
  85. }
  86. // ApplyToVector3Array multiplies length vectors in the array starting at offset by this matrix.
  87. // Returns pointer to the updated array.
  88. // This matrix is unchanged.
  89. func (m *Matrix3) ApplyToVector3Array(array []float32, offset int, length int) []float32 {
  90. var v1 Vector3
  91. j := offset
  92. for i := 0; i < length; i += 3 {
  93. v1.X = array[j]
  94. v1.Y = array[j+1]
  95. v1.Z = array[j+2]
  96. v1.ApplyMatrix3(m)
  97. array[j] = v1.X
  98. array[j+1] = v1.Y
  99. array[j+2] = v1.Z
  100. }
  101. return array
  102. }
  103. // Multiply multiply this matrix by the other matrix
  104. // Returns pointer to this updated matrix.
  105. func (m *Matrix3) Multiply(other *Matrix3) *Matrix3 {
  106. return m.MultiplyMatrices(m, other)
  107. }
  108. // MultiplyMatrices multiply matrix a by b storing the result in this matrix.
  109. // Returns pointer to this updated matrix.
  110. func (m *Matrix3) MultiplyMatrices(a, b *Matrix3) *Matrix3 {
  111. a11 := a[0]
  112. a12 := a[3]
  113. a13 := a[6]
  114. a21 := a[1]
  115. a22 := a[4]
  116. a23 := a[7]
  117. a31 := a[2]
  118. a32 := a[5]
  119. a33 := a[8]
  120. b11 := b[0]
  121. b12 := b[3]
  122. b13 := b[6]
  123. b21 := b[1]
  124. b22 := b[4]
  125. b23 := b[7]
  126. b31 := b[2]
  127. b32 := b[5]
  128. b33 := b[8]
  129. m[0] = a11*b11 + a12*b21 + a13*b31
  130. m[3] = a11*b12 + a12*b22 + a13*b32
  131. m[6] = a11*b13 + a12*b23 + a13*b33
  132. m[1] = a21*b11 + a22*b21 + a23*b31
  133. m[4] = a21*b12 + a22*b22 + a23*b32
  134. m[7] = a21*b13 + a22*b23 + a23*b33
  135. m[2] = a31*b11 + a32*b21 + a33*b31
  136. m[5] = a31*b12 + a32*b22 + a33*b32
  137. m[8] = a31*b13 + a32*b23 + a33*b33
  138. return m
  139. }
  140. // MultiplyScalar multiplies each of this matrix's components by the specified scalar.
  141. // Returns pointer to this updated matrix.
  142. func (m *Matrix3) MultiplyScalar(s float32) *Matrix3 {
  143. m[0] *= s
  144. m[3] *= s
  145. m[6] *= s
  146. m[1] *= s
  147. m[4] *= s
  148. m[7] *= s
  149. m[2] *= s
  150. m[5] *= s
  151. m[8] *= s
  152. return m
  153. }
  154. // ScaleColumns multiplies the matrix columns by the vector components.
  155. // This can be used when multiplying this matrix by a diagonal matrix if we store the diagonal components as a vector.
  156. // Returns pointer to this updated matrix.
  157. func (m *Matrix3) ScaleColumns(v *Vector3) *Matrix3 {
  158. m[0] *= v.X
  159. m[1] *= v.X
  160. m[2] *= v.X
  161. m[3] *= v.Y
  162. m[4] *= v.Y
  163. m[5] *= v.Y
  164. m[6] *= v.Z
  165. m[7] *= v.Z
  166. m[8] *= v.Z
  167. }
  168. // Determinant calculates and returns the determinant of this matrix.
  169. func (m *Matrix3) Determinant() float32 {
  170. return m[0]*m[4]*m[8] -
  171. m[0]*m[5]*m[7] -
  172. m[1]*m[3]*m[8] +
  173. m[1]*m[5]*m[6] +
  174. m[2]*m[3]*m[7] -
  175. m[2]*m[4]*m[6]
  176. }
  177. // GetInverse sets this matrix to the inverse of the src matrix.
  178. // If the src matrix cannot be inverted returns error and
  179. // sets this matrix to the identity matrix.
  180. func (m *Matrix3) GetInverse(src *Matrix4) error {
  181. m[0] = src[10]*src[5] - src[6]*src[9]
  182. m[1] = -src[10]*src[1] + src[2]*src[9]
  183. m[2] = src[6]*src[1] - src[2]*src[5]
  184. m[3] = -src[10]*src[4] + src[6]*src[8]
  185. m[4] = src[10]*src[0] - src[2]*src[8]
  186. m[5] = -src[6]*src[0] + src[2]*src[4]
  187. m[6] = src[9]*src[4] - src[5]*src[8]
  188. m[7] = -src[9]*src[0] + src[1]*src[8]
  189. m[8] = src[5]*src[0] - src[1]*src[4]
  190. det := src[0]*m[0] + src[1]*m[3] + src[2]*m[6]
  191. // no inverse
  192. if det == 0 {
  193. m.Identity()
  194. return errors.New("Cannot inverse matrix")
  195. }
  196. m.MultiplyScalar(1.0 / det)
  197. return nil
  198. }
  199. // Transpose transposes this matrix.
  200. // Returns pointer to this updated matrix.
  201. func (m *Matrix3) Transpose() *Matrix3 {
  202. var tmp float32
  203. tmp = m[1]
  204. m[1] = m[3]
  205. m[3] = tmp
  206. tmp = m[2]
  207. m[2] = m[6]
  208. m[6] = tmp
  209. tmp = m[5]
  210. m[5] = m[7]
  211. m[7] = tmp
  212. return m
  213. }
  214. // GetNormalMatrix set this matrix to the matrix to transform the normal vectors
  215. // from the src matrix to transform the vertices.
  216. // If the src matrix cannot be inverted returns error.
  217. func (m *Matrix3) GetNormalMatrix(src *Matrix4) error {
  218. err := m.GetInverse(src)
  219. m.Transpose()
  220. return err
  221. }
  222. // FromArray set this matrix array starting at offset.
  223. // Returns pointer to this updated matrix.
  224. func (m *Matrix3) FromArray(array []float32, offset int) *Matrix3 {
  225. copy(m[:], array[offset:offset+9])
  226. return m
  227. }
  228. // ToArray copies this matrix to array starting at offset.
  229. // Returns pointer to the updated array.
  230. func (m *Matrix3) ToArray(array []float32, offset int) []float32 {
  231. copy(array[offset:], m[:])
  232. return array
  233. }
  234. // Clone creates and returns a pointer to a copy of this matrix.
  235. func (m *Matrix3) Clone() *Matrix3 {
  236. var cloned Matrix3
  237. cloned = *m
  238. return &cloned
  239. }