...

Source file src/github.com/golang/geo/s2/matrix3x3.go

Documentation: github.com/golang/geo/s2

     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  		float64(m[0][0]*p.X) + float64(m[0][1]*p.Y) + float64(m[0][2]*p.Z),
    70  		float64(m[1][0]*p.X) + float64(m[1][1]*p.Y) + float64(m[1][2]*p.Z),
    71  		float64(m[2][0]*p.X) + float64(m[2][1]*p.Y) + float64(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 float64(m[0][0]*m[1][1]*m[2][2]) + float64(m[0][1]*m[1][2]*m[2][0]) +
    81  		float64(m[0][2]*m[1][0]*m[2][1]) - float64(m[0][2]*m[1][1]*m[2][0]) -
    82  		float64(m[0][1]*m[1][0]*m[2][2]) - float64(m[0][0]*m[1][2]*m[2][1])
    83  }
    84  
    85  // transpose reflects the matrix along its diagonal and returns the result.
    86  func (m *matrix3x3) transpose() *matrix3x3 {
    87  	m[0][1], m[1][0] = m[1][0], m[0][1]
    88  	m[0][2], m[2][0] = m[2][0], m[0][2]
    89  	m[1][2], m[2][1] = m[2][1], m[1][2]
    90  
    91  	return m
    92  }
    93  
    94  // String formats the matrix into an easier to read layout.
    95  func (m *matrix3x3) String() string {
    96  	return fmt.Sprintf("[ %0.4f %0.4f %0.4f ] [ %0.4f %0.4f %0.4f ] [ %0.4f %0.4f %0.4f ]",
    97  		m[0][0], m[0][1], m[0][2],
    98  		m[1][0], m[1][1], m[1][2],
    99  		m[2][0], m[2][1], m[2][2],
   100  	)
   101  }
   102  
   103  // getFrame returns the orthonormal frame for the given point on the unit sphere.
   104  func getFrame(p Point) matrix3x3 {
   105  	// Given the point p on the unit sphere, extend this into a right-handed
   106  	// coordinate frame of unit-length column vectors m = (x,y,z).  Note that
   107  	// the vectors (x,y) are an orthonormal frame for the tangent space at point p,
   108  	// while p itself is an orthonormal frame for the normal space at p.
   109  	m := matrix3x3{}
   110  	m.setCol(2, p)
   111  	m.setCol(1, Ortho(p))
   112  	m.setCol(0, Point{m.col(1).Cross(p.Vector)})
   113  	return m
   114  }
   115  
   116  // toFrame returns the coordinates of the given point with respect to its orthonormal basis m.
   117  // The resulting point q satisfies the identity (m * q == p).
   118  func toFrame(m matrix3x3, p Point) Point {
   119  	// The inverse of an orthonormal matrix is its transpose.
   120  	return m.transpose().mul(p)
   121  }
   122  
   123  // fromFrame returns the coordinates of the given point in standard axis-aligned basis
   124  // from its orthonormal basis m.
   125  // The resulting point p satisfies the identity (p == m * q).
   126  func fromFrame(m matrix3x3, q Point) Point {
   127  	return m.mul(q)
   128  }
   129  

View as plain text