github.com/acrespo/mobile@v0.0.0-20190107162257-dc0771356504/exp/f32/mat3.go (about)

     1  // Copyright 2014 The Go 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  
     5  package f32
     6  
     7  import "fmt"
     8  
     9  // A Mat3 is a 3x3 matrix of float32 values.
    10  // Elements are indexed first by row then column, i.e. m[row][column].
    11  type Mat3 [3]Vec3
    12  
    13  func (m Mat3) String() string {
    14  	return fmt.Sprintf(`Mat3[% 0.3f, % 0.3f, % 0.3f,
    15       % 0.3f, % 0.3f, % 0.3f,
    16       % 0.3f, % 0.3f, % 0.3f]`,
    17  		m[0][0], m[0][1], m[0][2],
    18  		m[1][0], m[1][1], m[1][2],
    19  		m[2][0], m[2][1], m[2][2])
    20  }
    21  
    22  func (m *Mat3) Identity() {
    23  	*m = Mat3{
    24  		{1, 0, 0},
    25  		{0, 1, 0},
    26  		{0, 0, 1},
    27  	}
    28  }
    29  
    30  func (m *Mat3) Eq(n *Mat3, epsilon float32) bool {
    31  	for i := range m {
    32  		for j := range m[i] {
    33  			diff := m[i][j] - n[i][j]
    34  			if diff < -epsilon || +epsilon < diff {
    35  				return false
    36  			}
    37  		}
    38  	}
    39  	return true
    40  }
    41  
    42  // Mul stores a × b in m.
    43  func (m *Mat3) Mul(a, b *Mat3) {
    44  	// Store the result in local variables, in case m == a || m == b.
    45  	m00 := a[0][0]*b[0][0] + a[0][1]*b[1][0] + a[0][2]*b[2][0]
    46  	m01 := a[0][0]*b[0][1] + a[0][1]*b[1][1] + a[0][2]*b[2][1]
    47  	m02 := a[0][0]*b[0][2] + a[0][1]*b[1][2] + a[0][2]*b[2][2]
    48  	m10 := a[1][0]*b[0][0] + a[1][1]*b[1][0] + a[1][2]*b[2][0]
    49  	m11 := a[1][0]*b[0][1] + a[1][1]*b[1][1] + a[1][2]*b[2][1]
    50  	m12 := a[1][0]*b[0][2] + a[1][1]*b[1][2] + a[1][2]*b[2][2]
    51  	m20 := a[2][0]*b[0][0] + a[2][1]*b[1][0] + a[2][2]*b[2][0]
    52  	m21 := a[2][0]*b[0][1] + a[2][1]*b[1][1] + a[2][2]*b[2][1]
    53  	m22 := a[2][0]*b[0][2] + a[2][1]*b[1][2] + a[2][2]*b[2][2]
    54  	m[0][0] = m00
    55  	m[0][1] = m01
    56  	m[0][2] = m02
    57  	m[1][0] = m10
    58  	m[1][1] = m11
    59  	m[1][2] = m12
    60  	m[2][0] = m20
    61  	m[2][1] = m21
    62  	m[2][2] = m22
    63  }