...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package s2
16
17 import (
18 "fmt"
19
20 "github.com/golang/geo/r3"
21 )
22
23
24
25
26 type matrix3x3 [3][3]float64
27
28
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
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
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
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
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
66
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
76 func (m *matrix3x3) det() float64 {
77
78
79
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
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
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
104 func getFrame(p Point) matrix3x3 {
105
106
107
108
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
117
118 func toFrame(m matrix3x3, p Point) Point {
119
120 return m.transpose().mul(p)
121 }
122
123
124
125
126 func fromFrame(m matrix3x3, q Point) Point {
127 return m.mul(q)
128 }
129
View as plain text