matrix3.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // Copy copies src matrix into this one.
  41. // Returns the pointer to this updated matrix.
  42. func (m *Matrix3) Copy(src *Matrix3) *Matrix3 {
  43. *m = *src
  44. return m
  45. }
  46. // ApplyToVector3Array multiplies length vectors in the array starting at offset by this matrix.
  47. // Returns pointer to the updated array.
  48. // This matrix is unchanged.
  49. func (m *Matrix3) ApplyToVector3Array(array []float32, offset int, length int) []float32 {
  50. var v1 Vector3
  51. j := offset
  52. for i := 0; i < length; i += 3 {
  53. v1.X = array[j]
  54. v1.Y = array[j+1]
  55. v1.Z = array[j+2]
  56. v1.ApplyMatrix3(m)
  57. array[j] = v1.X
  58. array[j+1] = v1.Y
  59. array[j+2] = v1.Z
  60. }
  61. return array
  62. }
  63. // MultiplyScalar multiplies each of this matrix's components by the specified scalar.
  64. // Returns pointer to this updated matrix.
  65. func (m *Matrix3) MultiplyScalar(s float32) *Matrix3 {
  66. m[0] *= s
  67. m[3] *= s
  68. m[6] *= s
  69. m[1] *= s
  70. m[4] *= s
  71. m[7] *= s
  72. m[2] *= s
  73. m[5] *= s
  74. m[8] *= s
  75. return m
  76. }
  77. // Determinant calculates and returns the determinant of this matrix.
  78. func (m *Matrix3) Determinant() float32 {
  79. return m[0]*m[4]*m[8] -
  80. m[0]*m[5]*m[7] -
  81. m[1]*m[3]*m[8] +
  82. m[1]*m[5]*m[6] +
  83. m[2]*m[3]*m[7] -
  84. m[2]*m[4]*m[6]
  85. }
  86. // GetInverse sets this matrix to the inverse of the src matrix.
  87. // Returns the pointer to this updated Matrix and an error.
  88. // An error is returned if the src matrix cannot be inverted.
  89. // In this case sets this matrix to the identity matrix.
  90. func (m *Matrix3) GetInverse(src *Matrix4) (*Matrix3, error) {
  91. m[0] = src[10]*src[5] - src[6]*src[9]
  92. m[1] = -src[10]*src[1] + src[2]*src[9]
  93. m[2] = src[6]*src[1] - src[2]*src[5]
  94. m[3] = -src[10]*src[4] + src[6]*src[8]
  95. m[4] = src[10]*src[0] - src[2]*src[8]
  96. m[5] = -src[6]*src[0] + src[2]*src[4]
  97. m[6] = src[9]*src[4] - src[5]*src[8]
  98. m[7] = -src[9]*src[0] + src[1]*src[8]
  99. m[8] = src[5]*src[0] - src[1]*src[4]
  100. det := src[0]*m[0] + src[1]*m[3] + src[2]*m[6]
  101. // no inverse
  102. if det == 0 {
  103. m.Identity()
  104. return m, errors.New("Matrix3.GetInverse(): can't invert matrix, determinant is 0")
  105. }
  106. m.MultiplyScalar(1.0 / det)
  107. return m, nil
  108. }
  109. // Transpose transposes this matrix.
  110. // Returns pointer to this updated matrix.
  111. func (m *Matrix3) Transpose() *Matrix3 {
  112. var tmp float32
  113. tmp = m[1]
  114. m[1] = m[3]
  115. m[3] = tmp
  116. tmp = m[2]
  117. m[2] = m[6]
  118. m[6] = tmp
  119. tmp = m[5]
  120. m[5] = m[7]
  121. m[7] = tmp
  122. return m
  123. }
  124. // GetNormalMatrix set this matrix to the matrix to transform the normal vectors
  125. // from the src matrix to transform the vertices.
  126. // If the src matrix cannot be inverted, returns nil and an error,
  127. // otherwise returns pointer to this updated matrix and no error.
  128. func (m *Matrix3) GetNormalMatrix(src *Matrix4) (*Matrix3, error) {
  129. inv, err := m.GetInverse(src)
  130. if err != nil {
  131. return nil, err
  132. }
  133. *m = *inv.Transpose()
  134. return m, nil
  135. }
  136. // FromArray set this matrix array starting at offset.
  137. // Returns pointer to this updated matrix.
  138. func (m *Matrix3) FromArray(array []float32, offset int) *Matrix3 {
  139. copy(m[:], array[offset:offset+9])
  140. return m
  141. }
  142. // ToArray copies this matrix to array starting at offset.
  143. // Returns pointer to the updated array.
  144. func (m *Matrix3) ToArray(array []float32, offset int) []float32 {
  145. copy(array[offset:], m[:])
  146. return array
  147. }
  148. // Clone creates and returns a pointer to a copy of this matrix.
  149. func (m *Matrix3) Clone() *Matrix3 {
  150. var cloned Matrix3
  151. cloned = *m
  152. return &cloned
  153. }