| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 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 |
x |
| 51 |
func Eye() *Mat { |
| 52 |
return &Mat{&array{ |
| 53 |
1, 0, 0, |
| 54 |
0, 1, 0, |
| 55 |
0, 0, 1, |
| 56 |
}} |
| 57 |
} |
| 58 |
|
| 59 |
. |
| 60 |
|
| 61 |
y⎤ |
| 62 |
x⎥ |
| 63 |
0⎦ |
| 64 |
|
| 65 |
|
| 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 |
|
| 75 |
|
| 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 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 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 |
t |
| 138 |
|
| 139 |
t |
| 140 |
|
| 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 |
} |