matrix3x3.go (4127B)
1 // Copyright 2015 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package s2 16 17 import ( 18 "fmt" 19 20 "github.com/golang/geo/r3" 21 ) 22 23 // matrix3x3 represents a traditional 3x3 matrix of floating point values. 24 // This is not a full fledged matrix. It only contains the pieces needed 25 // to satisfy the computations done within the s2 package. 26 type matrix3x3 [3][3]float64 27 28 // col returns the given column as a Point. 29 func (m *matrix3x3) col(col int) Point { 30 return Point{r3.Vector{m[0][col], m[1][col], m[2][col]}} 31 } 32 33 // row returns the given row as a Point. 34 func (m *matrix3x3) row(row int) Point { 35 return Point{r3.Vector{m[row][0], m[row][1], m[row][2]}} 36 } 37 38 // setCol sets the specified column to the value in the given Point. 39 func (m *matrix3x3) setCol(col int, p Point) *matrix3x3 { 40 m[0][col] = p.X 41 m[1][col] = p.Y 42 m[2][col] = p.Z 43 44 return m 45 } 46 47 // setRow sets the specified row to the value in the given Point. 48 func (m *matrix3x3) setRow(row int, p Point) *matrix3x3 { 49 m[row][0] = p.X 50 m[row][1] = p.Y 51 m[row][2] = p.Z 52 53 return m 54 } 55 56 // scale multiplies the matrix by the given value. 57 func (m *matrix3x3) scale(f float64) *matrix3x3 { 58 return &matrix3x3{ 59 [3]float64{f * m[0][0], f * m[0][1], f * m[0][2]}, 60 [3]float64{f * m[1][0], f * m[1][1], f * m[1][2]}, 61 [3]float64{f * m[2][0], f * m[2][1], f * m[2][2]}, 62 } 63 } 64 65 // mul returns the multiplication of m by the Point p and converts the 66 // resulting 1x3 matrix into a Point. 67 func (m *matrix3x3) mul(p Point) Point { 68 return Point{r3.Vector{ 69 m[0][0]*p.X + m[0][1]*p.Y + m[0][2]*p.Z, 70 m[1][0]*p.X + m[1][1]*p.Y + m[1][2]*p.Z, 71 m[2][0]*p.X + m[2][1]*p.Y + m[2][2]*p.Z, 72 }} 73 } 74 75 // det returns the determinant of this matrix. 76 func (m *matrix3x3) det() float64 { 77 // | a b c | 78 // det | d e f | = aei + bfg + cdh - ceg - bdi - afh 79 // | g h i | 80 return m[0][0]*m[1][1]*m[2][2] + m[0][1]*m[1][2]*m[2][0] + m[0][2]*m[1][0]*m[2][1] - 81 m[0][2]*m[1][1]*m[2][0] - m[0][1]*m[1][0]*m[2][2] - m[0][0]*m[1][2]*m[2][1] 82 } 83 84 // transpose reflects the matrix along its diagonal and returns the result. 85 func (m *matrix3x3) transpose() *matrix3x3 { 86 m[0][1], m[1][0] = m[1][0], m[0][1] 87 m[0][2], m[2][0] = m[2][0], m[0][2] 88 m[1][2], m[2][1] = m[2][1], m[1][2] 89 90 return m 91 } 92 93 // String formats the matrix into an easier to read layout. 94 func (m *matrix3x3) String() string { 95 return fmt.Sprintf("[ %0.4f %0.4f %0.4f ] [ %0.4f %0.4f %0.4f ] [ %0.4f %0.4f %0.4f ]", 96 m[0][0], m[0][1], m[0][2], 97 m[1][0], m[1][1], m[1][2], 98 m[2][0], m[2][1], m[2][2], 99 ) 100 } 101 102 // getFrame returns the orthonormal frame for the given point on the unit sphere. 103 func getFrame(p Point) matrix3x3 { 104 // Given the point p on the unit sphere, extend this into a right-handed 105 // coordinate frame of unit-length column vectors m = (x,y,z). Note that 106 // the vectors (x,y) are an orthonormal frame for the tangent space at point p, 107 // while p itself is an orthonormal frame for the normal space at p. 108 m := matrix3x3{} 109 m.setCol(2, p) 110 m.setCol(1, Point{p.Ortho()}) 111 m.setCol(0, Point{m.col(1).Cross(p.Vector)}) 112 return m 113 } 114 115 // toFrame returns the coordinates of the given point with respect to its orthonormal basis m. 116 // The resulting point q satisfies the identity (m * q == p). 117 func toFrame(m matrix3x3, p Point) Point { 118 // The inverse of an orthonormal matrix is its transpose. 119 return m.transpose().mul(p) 120 } 121 122 // fromFrame returns the coordinates of the given point in standard axis-aligned basis 123 // from its orthonormal basis m. 124 // The resulting point p satisfies the identity (p == m * q). 125 func fromFrame(m matrix3x3, q Point) Point { 126 return m.mul(q) 127 }