matrix3.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // If the src matrix cannot be inverted returns error and
  88. // sets this matrix to the identity matrix.
  89. func (m *Matrix3) GetInverse(src *Matrix4) error {
  90. m[0] = src[10]*src[5] - src[6]*src[9]
  91. m[1] = -src[10]*src[1] + src[2]*src[9]
  92. m[2] = src[6]*src[1] - src[2]*src[5]
  93. m[3] = -src[10]*src[4] + src[6]*src[8]
  94. m[4] = src[10]*src[0] - src[2]*src[8]
  95. m[5] = -src[6]*src[0] + src[2]*src[4]
  96. m[6] = src[9]*src[4] - src[5]*src[8]
  97. m[7] = -src[9]*src[0] + src[1]*src[8]
  98. m[8] = src[5]*src[0] - src[1]*src[4]
  99. det := src[0]*m[0] + src[1]*m[3] + src[2]*m[6]
  100. // no inverse
  101. if det == 0 {
  102. m.Identity()
  103. return errors.New("Cannot inverse matrix")
  104. }
  105. m.MultiplyScalar(1.0 / det)
  106. return nil
  107. }
  108. // Transpose transposes this matrix.
  109. // Returns pointer to this updated matrix.
  110. func (m *Matrix3) Transpose() *Matrix3 {
  111. var tmp float32
  112. tmp = m[1]
  113. m[1] = m[3]
  114. m[3] = tmp
  115. tmp = m[2]
  116. m[2] = m[6]
  117. m[6] = tmp
  118. tmp = m[5]
  119. m[5] = m[7]
  120. m[7] = tmp
  121. return m
  122. }
  123. // GetNormalMatrix set this matrix to the matrix to transform the normal vectors
  124. // from the src matrix to transform the vertices.
  125. // If the src matrix cannot be inverted returns error.
  126. func (m *Matrix3) GetNormalMatrix(src *Matrix4) error {
  127. err := m.GetInverse(src)
  128. m.Transpose()
  129. return err
  130. }
  131. // FromArray set this matrix array starting at offset.
  132. // Returns pointer to this updated matrix.
  133. func (m *Matrix3) FromArray(array []float32, offset int) *Matrix3 {
  134. copy(m[:], array[offset:offset+9])
  135. return m
  136. }
  137. // ToArray copies this matrix to array starting at offset.
  138. // Returns pointer to the updated array.
  139. func (m *Matrix3) ToArray(array []float32, offset int) []float32 {
  140. copy(array[offset:], m[:])
  141. return array
  142. }
  143. // Clone creates and returns a pointer to a copy of this matrix.
  144. func (m *Matrix3) Clone() *Matrix3 {
  145. var cloned Matrix3
  146. cloned = *m
  147. return &cloned
  148. }