-
Notifications
You must be signed in to change notification settings - Fork 10
/
math.go
254 lines (218 loc) · 6.25 KB
/
math.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// © Copyright 2021 HP Development Company, L.P.
// SPDX-License Identifier: BSD-2-Clause
package go3mf
import (
"fmt"
"math"
)
type pairEntry struct {
a, b uint32
}
// pairMatch implements a n-log-n tree class which is able to identify
// duplicate pairs of numbers in a given data set.
type pairMatch map[pairEntry]uint32
// AddMatch adds a match to the set.
// If the match exists it is overridden.
func (t pairMatch) AddMatch(data1, data2, param uint32) {
t[newPairEntry(data1, data2)] = param
}
// CheckMatch check if a match is in the set.
func (t pairMatch) CheckMatch(data1, data2 uint32) (val uint32, ok bool) {
val, ok = t[newPairEntry(data1, data2)]
return
}
func newPairEntry(data1, data2 uint32) pairEntry {
if data1 < data2 {
return pairEntry{data1, data2}
}
return pairEntry{data2, data1}
}
// vec3I represents a 3D vector typed as int32
type vec3I struct {
X int32 // X coordinate
Y int32 // Y coordinate
Z int32 // Z coordinate
}
const micronsAccuracy = 1e-6
func newvec3IFromVec3(vec Point3D) vec3I {
a := vec3I{
X: int32(math.Floor(float64(vec.X() / micronsAccuracy))),
Y: int32(math.Floor(float64(vec.Y() / micronsAccuracy))),
Z: int32(math.Floor(float64(vec.Z() / micronsAccuracy))),
}
return a
}
// vectorTree implements a n*log(n) lookup tree class to identify vectors by their position
type vectorTree map[vec3I]uint32
// AddVector adds a vector to the dictionary.
func (t vectorTree) AddVector(vec Point3D, value uint32) {
t[newvec3IFromVec3(vec)] = value
}
// FindVector returns the identifier of the vector.
func (t vectorTree) FindVector(vec Point3D) (val uint32, ok bool) {
val, ok = t[newvec3IFromVec3(vec)]
return
}
// RemoveVector removes the vector from the dictionary.
func (t vectorTree) RemoveVector(vec Point3D) {
delete(t, newvec3IFromVec3(vec))
}
// Point2D defines a node of a slice as an array of 2 coordinates: x and y.
type Point2D [2]float32
// X returns the x coordinate.
func (n Point2D) X() float32 {
return n[0]
}
// Y returns the y coordinate.
func (n Point2D) Y() float32 {
return n[1]
}
// Point3D defines a node of a mesh as an array of 3 coordinates: x, y and z.
type Point3D [3]float32
// X returns the x coordinate.
func (v1 Point3D) X() float32 {
return v1[0]
}
// Y returns the y coordinate.
func (v1 Point3D) Y() float32 {
return v1[1]
}
// Z returns the z coordinate.
func (v1 Point3D) Z() float32 {
return v1[2]
}
// Matrix is a 4x4 matrix in row major order.
//
// m[4*r + c] is the element in the r'th row and c'th column.
type Matrix [16]float32
// String returns the string representation of a Matrix.
func (m1 Matrix) String() string {
return fmt.Sprintf("%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f",
m1[0], m1[1], m1[2], m1[4], m1[5], m1[6], m1[8], m1[9], m1[10], m1[12], m1[13], m1[14])
}
// Identity returns the 4x4 identity matrix.
// The identity matrix is a square matrix with the value 1 on its
// diagonals. The characteristic property of the identity matrix is that
// any matrix multiplied by it is itself. (MI = M; IN = N)
func Identity() Matrix {
return Matrix{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
}
// Translate returns a matrix with a relative translation applied.
func (m1 Matrix) Translate(x, y, z float32) Matrix {
m1[12] += x
m1[13] += y
m1[14] += z
return m1
}
// Mul performs a "matrix product" between this matrix
// and another matrix.
func (m1 Matrix) Mul(m2 Matrix) Matrix {
return Matrix{
m1[0]*m2[0] + m1[4]*m2[1] + m1[8]*m2[2] + m1[12]*m2[3],
m1[1]*m2[0] + m1[5]*m2[1] + m1[9]*m2[2] + m1[13]*m2[3],
m1[2]*m2[0] + m1[6]*m2[1] + m1[10]*m2[2] + m1[14]*m2[3],
m1[3]*m2[0] + m1[7]*m2[1] + m1[11]*m2[2] + m1[15]*m2[3],
m1[0]*m2[4] + m1[4]*m2[5] + m1[8]*m2[6] + m1[12]*m2[7],
m1[1]*m2[4] + m1[5]*m2[5] + m1[9]*m2[6] + m1[13]*m2[7],
m1[2]*m2[4] + m1[6]*m2[5] + m1[10]*m2[6] + m1[14]*m2[7],
m1[3]*m2[4] + m1[7]*m2[5] + m1[11]*m2[6] + m1[15]*m2[7],
m1[0]*m2[8] + m1[4]*m2[9] + m1[8]*m2[10] + m1[12]*m2[11],
m1[1]*m2[8] + m1[5]*m2[9] + m1[9]*m2[10] + m1[13]*m2[11],
m1[2]*m2[8] + m1[6]*m2[9] + m1[10]*m2[10] + m1[14]*m2[11],
m1[3]*m2[8] + m1[7]*m2[9] + m1[11]*m2[10] + m1[15]*m2[11],
m1[0]*m2[12] + m1[4]*m2[13] + m1[8]*m2[14] + m1[12]*m2[15],
m1[1]*m2[12] + m1[5]*m2[13] + m1[9]*m2[14] + m1[13]*m2[15],
m1[2]*m2[12] + m1[6]*m2[13] + m1[10]*m2[14] + m1[14]*m2[15],
m1[3]*m2[12] + m1[7]*m2[13] + m1[11]*m2[14] + m1[15]*m2[15],
}
}
// Mul3D performs a "matrix product" between this matrix
// and another 3D point.
func (m1 Matrix) Mul3D(v Point3D) Point3D {
return Point3D{
m1[0]*v[0] + m1[4]*v[1] + m1[8]*v[2] + m1[12],
m1[1]*v[0] + m1[5]*v[1] + m1[9]*v[2] + m1[13],
m1[2]*v[0] + m1[6]*v[1] + m1[10]*v[2] + m1[14],
}
}
// Mul2D performs a "matrix product" between this matrix
// and another 2D point.
func (m1 Matrix) Mul2D(v Point2D) Point2D {
return Point2D{
m1[0]*v[0] + m1[4]*v[1] + m1[12],
m1[1]*v[0] + m1[5]*v[1] + m1[13],
}
}
// MulBox performs a "matrix product" between this matrix
// and a box
func (m1 Matrix) MulBox(b Box) Box {
if m1[15] == 0 {
return b
}
box := Box{
Min: m1.Mul3D(b.Min),
Max: m1.Mul3D(b.Max),
}
if box.Min.X() > box.Max.X() {
box.Min[0], box.Max[0] = box.Max[0], box.Min[0]
}
if box.Min.Y() > box.Max.Y() {
box.Min[1], box.Max[1] = box.Max[1], box.Min[1]
}
if box.Min.Z() > box.Max.Z() {
box.Min[2], box.Max[2] = box.Max[2], box.Min[2]
}
return box
}
// Box defines a box in the 3D space.
type Box struct {
Min Point3D
Max Point3D
}
var emptyBox = Box{}
func newLimitBox() Box {
return Box{
Min: Point3D{math.MaxFloat32, math.MaxFloat32, math.MaxFloat32},
Max: Point3D{-math.MaxFloat32, -math.MaxFloat32, -math.MaxFloat32},
}
}
func (b Box) extend(v Box) Box {
return Box{
Min: Point3D{
min(b.Min.X(), v.Min.X()),
min(b.Min.Y(), v.Min.Y()),
min(b.Min.Z(), v.Min.Z()),
},
Max: Point3D{
max(b.Max.X(), v.Max.X()),
max(b.Max.Y(), v.Max.Y()),
max(b.Max.Z(), v.Max.Z()),
},
}
}
func (b Box) extendPoint(v Point3D) Box {
return Box{
Min: Point3D{
min(b.Min.X(), v.X()),
min(b.Min.Y(), v.Y()),
min(b.Min.Z(), v.Z()),
},
Max: Point3D{
max(b.Max.X(), v.X()),
max(b.Max.Y(), v.Y()),
max(b.Max.Z(), v.Z()),
},
}
}
func min(x, y float32) float32 {
if x < y {
return x
}
return y
}
func max(x, y float32) float32 {
if x > y {
return x
}
return y
}