gonum.org/v1/gonum@v0.14.0/spatial/r3/mat_safe.go (about) 1 // Copyright ©2021 The Gonum 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 //go:build safe 6 // +build safe 7 8 // TODO(kortschak): Get rid of this rigmarole if https://golang.org/issue/50118 9 // is accepted. 10 11 package r3 12 13 import ( 14 "gonum.org/v1/gonum/blas/blas64" 15 "gonum.org/v1/gonum/mat" 16 ) 17 18 type array [9]float64 19 20 // At returns the value of a matrix element at row i, column j. 21 // At expects indices in the range [0,2]. 22 // It will panic if i or j are out of bounds for the matrix. 23 func (m *Mat) At(i, j int) float64 { 24 if uint(i) > 2 { 25 panic(mat.ErrRowAccess) 26 } 27 if uint(j) > 2 { 28 panic(mat.ErrColAccess) 29 } 30 if m.data == nil { 31 m.data = new(array) 32 } 33 return m.data[i*3+j] 34 } 35 36 // Set sets the element at row i, column j to the value v. 37 func (m *Mat) Set(i, j int, v float64) { 38 if uint(i) > 2 { 39 panic(mat.ErrRowAccess) 40 } 41 if uint(j) > 2 { 42 panic(mat.ErrColAccess) 43 } 44 if m.data == nil { 45 m.data = new(array) 46 } 47 m.data[i*3+j] = v 48 } 49 50 // Eye returns the 3×3 Identity matrix 51 func Eye() *Mat { 52 return &Mat{&array{ 53 1, 0, 0, 54 0, 1, 0, 55 0, 0, 1, 56 }} 57 } 58 59 // Skew returns the 3×3 skew symmetric matrix (right hand system) of v. 60 // 61 // ⎡ 0 -z y⎤ 62 // Skew({x,y,z}) = ⎢ z 0 -x⎥ 63 // ⎣-y x 0⎦ 64 // 65 // Deprecated: use Mat.Skew() 66 func Skew(v Vec) (M *Mat) { 67 return &Mat{&array{ 68 0, -v.Z, v.Y, 69 v.Z, 0, -v.X, 70 -v.Y, v.X, 0, 71 }} 72 } 73 74 // Mul takes the matrix product of a and b, placing the result in the receiver. 75 // If the number of columns in a does not equal 3, Mul will panic. 76 func (m *Mat) Mul(a, b mat.Matrix) { 77 ra, ca := a.Dims() 78 rb, cb := b.Dims() 79 switch { 80 case ra != 3: 81 panic(mat.ErrShape) 82 case cb != 3: 83 panic(mat.ErrShape) 84 case ca != rb: 85 panic(mat.ErrShape) 86 } 87 if m.data == nil { 88 m.data = new(array) 89 } 90 if ca != 3 { 91 // General matrix multiplication for the case where the inner dimension is not 3. 92 var t mat.Dense 93 t.Mul(a, b) 94 copy(m.data[:], t.RawMatrix().Data) 95 return 96 } 97 98 a00 := a.At(0, 0) 99 b00 := b.At(0, 0) 100 a01 := a.At(0, 1) 101 b01 := b.At(0, 1) 102 a02 := a.At(0, 2) 103 b02 := b.At(0, 2) 104 a10 := a.At(1, 0) 105 b10 := b.At(1, 0) 106 a11 := a.At(1, 1) 107 b11 := b.At(1, 1) 108 a12 := a.At(1, 2) 109 b12 := b.At(1, 2) 110 a20 := a.At(2, 0) 111 b20 := b.At(2, 0) 112 a21 := a.At(2, 1) 113 b21 := b.At(2, 1) 114 a22 := a.At(2, 2) 115 b22 := b.At(2, 2) 116 *(m.data) = array{ 117 a00*b00 + a01*b10 + a02*b20, a00*b01 + a01*b11 + a02*b21, a00*b02 + a01*b12 + a02*b22, 118 a10*b00 + a11*b10 + a12*b20, a10*b01 + a11*b11 + a12*b21, a10*b02 + a11*b12 + a12*b22, 119 a20*b00 + a21*b10 + a22*b20, a20*b01 + a21*b11 + a22*b21, a20*b02 + a21*b12 + a22*b22, 120 } 121 } 122 123 // RawMatrix returns the blas representation of the matrix with the backing 124 // data of this matrix. Changes to the returned matrix will be reflected in 125 // the receiver. 126 func (m *Mat) RawMatrix() blas64.General { 127 if m.data == nil { 128 m.data = new(array) 129 } 130 return blas64.General{Rows: 3, Cols: 3, Data: m.data[:], Stride: 3} 131 } 132 133 func arrayFrom(vals []float64) *array { 134 return (*array)(vals) 135 } 136 137 // Mat returns a 3×3 rotation matrix corresponding to the receiver. It 138 // may be used to perform rotations on a 3-vector or to apply the rotation 139 // to a 3×n matrix of column vectors. If the receiver is not a unit 140 // quaternion, the returned matrix will not be a pure rotation. 141 func (r Rotation) Mat() *Mat { 142 w, i, j, k := r.Real, r.Imag, r.Jmag, r.Kmag 143 ii := 2 * i * i 144 jj := 2 * j * j 145 kk := 2 * k * k 146 wi := 2 * w * i 147 wj := 2 * w * j 148 wk := 2 * w * k 149 ij := 2 * i * j 150 jk := 2 * j * k 151 ki := 2 * k * i 152 return &Mat{&array{ 153 1 - (jj + kk), ij - wk, ki + wj, 154 ij + wk, 1 - (ii + kk), jk - wi, 155 ki - wj, jk + wi, 1 - (ii + jj), 156 }} 157 }