2
0
mirror of synced 2025-02-23 06:48:15 +00:00
mobile/f32/mat3.go
Nigel Tao 75e726bb56 go.mobile/f32: add a Mat3 type.
LGTM=crawshaw
R=crawshaw
CC=golang-codereviews
https://golang.org/cl/150380043
2014-10-01 13:51:19 +10:00

53 lines
1.1 KiB
Go

// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package f32
import "fmt"
// A Mat3 is a 3x3 matrix of float32 values.
// Elements are indexed first by row then column, i.e. m[row][column].
type Mat3 [3]Vec3
func (m *Mat3) String() string {
return fmt.Sprintf(`Mat3[% 0.3f, % 0.3f, % 0.3f,
% 0.3f, % 0.3f, % 0.3f,
% 0.3f, % 0.3f, % 0.3f]`,
m[0][0], m[0][1], m[0][2],
m[1][0], m[1][1], m[1][2],
m[2][0], m[2][1], m[2][2])
}
func (m *Mat3) Identity() {
*m = Mat3{
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
}
}
func (m *Mat3) Eq(n *Mat3, epsilon float32) bool {
for i := range m {
for j := range m[i] {
diff := m[i][j] - n[i][j]
if diff < -epsilon || +epsilon < diff {
return false
}
}
}
return true
}
func (m *Mat3) Mul(m0, m1 *Mat3) {
// If you intend to make this faster, skip the usual loop unrolling
// games and go straight to halide/renderscript/etc.
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
m[i][j] = m1[i][0]*m0[0][j] +
m1[i][1]*m0[1][j] +
m1[i][2]*m0[2][j]
}
}
}