2018-07-18 18:05:06 +00:00
|
|
|
package geom
|
|
|
|
|
|
|
|
// Matrix3x3F32 is an matrix with 3 vectors, i, j and k (floating point, 32 bits).
|
|
|
|
type Matrix3x3F32 struct {
|
|
|
|
I Vector3F32
|
|
|
|
J Vector3F32
|
|
|
|
K Vector3F32
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mx3x3F32 is a shorthand function to create a matrix.
|
|
|
|
func Mx3x3F32(i, j, k Vector3F32) Matrix3x3F32 {
|
|
|
|
return Matrix3x3F32{i, j, k}
|
|
|
|
}
|
|
|
|
|
2019-03-06 18:06:08 +00:00
|
|
|
// Adj calculates the adjugate matrix of matrix m.
|
|
|
|
func (m Matrix3x3F32) Adj() Matrix3x3F32 {
|
|
|
|
return Mx3x3F32(
|
|
|
|
Vec3F32(m.J.Y*m.K.Z-m.K.Y*m.J.Z, m.K.Y*m.I.Z-m.I.Y*m.K.Z, m.I.Y*m.J.Z-m.J.Y*m.I.Z),
|
|
|
|
Vec3F32(m.K.X*m.J.Z-m.J.X*m.K.Z, m.I.X*m.K.Z-m.K.X*m.I.Z, m.J.X*m.I.Z-m.I.X*m.J.Z),
|
|
|
|
Vec3F32(m.J.X*m.K.Y-m.K.X*m.J.Y, m.K.X*m.I.Y-m.I.X*m.K.Y, m.I.X*m.J.Y-m.J.X*m.I.Y),
|
|
|
|
)
|
2018-07-18 18:05:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 18:06:08 +00:00
|
|
|
// Det calculates the determinant of the matrix m.
|
|
|
|
func (m Matrix3x3F32) Det() float32 {
|
|
|
|
return m.I.X*m.J.Y*m.K.Z +
|
|
|
|
m.J.X*m.K.Y*m.I.Z +
|
|
|
|
m.K.X*m.I.Y*m.J.Z -
|
|
|
|
m.K.X*m.J.Y*m.I.Z -
|
|
|
|
m.J.X*m.I.Y*m.K.Z -
|
|
|
|
m.I.X*m.K.Y*m.J.Z
|
2018-07-18 18:05:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 18:06:08 +00:00
|
|
|
// MulMx multiplies the matrix m with matrix n.
|
|
|
|
func (m Matrix3x3F32) MulMx(n Matrix3x3F32) Matrix3x3F32 {
|
2018-07-18 18:05:06 +00:00
|
|
|
return Matrix3x3F32{
|
|
|
|
Vec3F32(
|
|
|
|
n.I.X*m.I.X+n.I.Y*m.J.X+n.I.Z*m.K.X,
|
|
|
|
n.I.X*m.I.Y+n.I.Y*m.J.Y+n.I.Z*m.K.Y,
|
|
|
|
n.I.X*m.I.Z+n.I.Y*m.J.Z+n.I.Z*m.K.Z),
|
|
|
|
Vec3F32(
|
|
|
|
n.J.X*m.I.X+n.J.Y*m.J.X+n.J.Z*m.K.X,
|
|
|
|
n.J.X*m.I.Y+n.J.Y*m.J.Y+n.J.Z*m.K.Y,
|
|
|
|
n.J.X*m.I.Z+n.J.Y*m.J.Z+n.J.Z*m.K.Z),
|
|
|
|
Vec3F32(
|
|
|
|
n.K.X*m.I.X+n.K.Y*m.J.X+n.K.Z*m.K.X,
|
|
|
|
n.K.X*m.I.Y+n.K.Y*m.J.Y+n.K.Z*m.K.Y,
|
|
|
|
n.K.X*m.I.Z+n.K.Y*m.J.Z+n.K.Z*m.K.Z),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 18:06:08 +00:00
|
|
|
// MulVec multiplies the matrix m with vector v.
|
|
|
|
func (m Matrix3x3F32) MulVec(v Vector3F32) Vector3F32 {
|
|
|
|
return Vec3F32(
|
|
|
|
v.X*m.I.X+v.Y*m.J.X+v.Z*m.K.X,
|
|
|
|
v.X*m.I.Y+v.Y*m.J.Y+v.Z*m.K.Y,
|
|
|
|
v.X*m.I.Z+v.Y*m.J.Z+v.Z*m.K.Z)
|
2018-07-18 18:05:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 18:06:08 +00:00
|
|
|
// ScaleVec scales the matrix m by vector v.
|
|
|
|
func (m Matrix3x3F32) ScaleVec(v Vector3F32) Matrix3x3F32 {
|
|
|
|
return Matrix3x3F32{
|
|
|
|
Vec3F32(v.X*m.I.X, v.X*m.I.Y, v.X*m.I.Z),
|
|
|
|
Vec3F32(v.Y*m.I.X, v.Y*m.I.Y, v.Y*m.I.Z),
|
|
|
|
Vec3F32(v.Z*m.I.X, v.Z*m.I.Y, v.Z*m.I.Z),
|
|
|
|
}
|
2018-07-18 18:05:06 +00:00
|
|
|
}
|