forked from go-gl/mathgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vecn.go
358 lines (301 loc) · 8.68 KB
/
vecn.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// A vector of N elements backed by a slice
//
// As with MatMxN, this is not for hardcore linear algebra with large dimensions. Use github.com/gonum/matrix
// or something like BLAS/LAPACK for that. This is for corner cases in 3D math where you require
// something a little bigger that 4D, but still relatively small.
//
// This VecN uses several sync.Pool objects as a memory pool. The rule is that for any sized vector, the backing slice
// has CAPACITY (not length) of 2^p where p is Ceil(log_2(N)) -- or in other words, rounding up the base-2
// log of the size of the vector. E.G. a VecN of size 17 will have a backing slice of Cap 32.
type VecN struct {
vec []float32
}
// Creates a new vector with a backing slice filled with the contents
// of initial. It is NOT backed by initial, but rather a slice with cap
// 2^p where p is Ceil(log_2(len(initial))), with the data from initial copied into
// it.
func NewVecNFromData(initial []float32) *VecN {
if initial == nil {
return &VecN{}
}
var internal []float32
if shouldPool {
internal = grabFromPool(len(initial))
} else {
internal = make([]float32, len(initial))
}
copy(internal, initial)
return &VecN{vec: internal}
}
// Creates a new vector with a backing slice of
// 2^p where p = Ceil(log_2(n))
func NewVecN(n int) *VecN {
if shouldPool {
return &VecN{vec: grabFromPool(n)}
} else {
return &VecN{vec: make([]float32, n)}
}
}
// Returns the raw slice backing the VecN
//
// This may be sent back to the memory pool at any time
// and you aren't advised to rely on this value
func (vn VecN) Raw() []float32 {
return vn.vec
}
// Gets the element at index i from the vector.
// This does not bounds check, and will panic if i is
// out of range.
func (vn VecN) Get(i int) float32 {
return vn.vec[i]
}
func (vn *VecN) Set(i int, val float32) {
vn.vec[i] = val
}
// Sends the allocated memory through the callback if it exists
func (vn *VecN) destroy() {
if vn == nil || vn.vec == nil {
return
}
if shouldPool {
returnToPool(vn.vec)
}
vn.vec = nil
}
// Resizes the underlying slice to the desired amount, reallocating or retrieving from the pool
// if necessary. The values after a Resize cannot be expected to be related to the values before a Resize.
//
// If the caller is a nil pointer, this returns a value as if NewVecN(n) had been called,
// otherwise it simply returns the caller.
func (vn *VecN) Resize(n int) *VecN {
if vn == nil {
return NewVecN(n)
}
if n <= cap(vn.vec) {
if vn.vec != nil {
vn.vec = vn.vec[:n]
} else {
vn.vec = []float32{}
}
return vn
}
if shouldPool && vn.vec != nil {
returnToPool(vn.vec)
}
*vn = (*NewVecN(n))
return vn
}
// Sets the vector's backing slice to the given
// new one.
func (vn *VecN) SetBackingSlice(newSlice []float32) {
vn.vec = newSlice
}
// Return the len of the vector's underlying slice.
// This is not titled Len because it conflicts the package's
// convention of calling the Norm the Len.
func (vn *VecN) Size() int {
return len(vn.vec)
}
// Returns the cap of the vector's underlying slice.
func (vn *VecN) Cap() int {
return cap(vn.vec)
}
// Sets the vector's size to n and zeroes out the vector.
// If n is bigger than the vector's size, it will realloc.
func (vn *VecN) Zero(n int) {
vn.Resize(n)
for i := range vn.vec {
vn.vec[i] = 0
}
}
// Adds vn and addend, storing the result in dst.
// If dst does not have sufficient size it will be resized
// Dst may be one of the other arguments. If dst is nil, it will be allocated.
// The value returned is dst, for easier method chaining
//
// If vn and addend are not the same size, this function will add min(vn.Size(), addend.Size())
// elements.
func (vn *VecN) Add(dst *VecN, subtrahend *VecN) *VecN {
if vn == nil || subtrahend == nil {
return nil
}
size := intMin(len(vn.vec), len(subtrahend.vec))
dst = dst.Resize(size)
for i := 0; i < size; i++ {
dst.vec[i] = vn.vec[i] + subtrahend.vec[i]
}
return dst
}
// Subtracts addend from vn, storing the result in dst.
// If dst does not have sufficient size it will be resized
// Dst may be one of the other arguments. If dst is nil, it will be allocated.
// The value returned is dst, for easier method chaining
//
// If vn and addend are not the same size, this function will add min(vn.Size(), addend.Size())
// elements.
func (vn *VecN) Sub(dst *VecN, addend *VecN) *VecN {
if vn == nil || addend == nil {
return nil
}
size := intMin(len(vn.vec), len(addend.vec))
dst = dst.Resize(size)
for i := 0; i < size; i++ {
dst.vec[i] = vn.vec[i] - addend.vec[i]
}
return dst
}
// Takes the binary cross product of vn and other, and stores it in dst.
// If either vn or other are not of size 3 this function will panic
//
// If dst is not of sufficient size, or is nil, a new slice is allocated.
// Dst is permitted to be one of the other arguments
func (vn *VecN) Cross(dst *VecN, other *VecN) *VecN {
if vn == nil || other == nil {
return nil
}
if len(vn.vec) != 3 || len(other.vec) != 3 {
panic("Cannot take binary cross product of non-3D elements (7D cross product not implemented)")
}
dst = dst.Resize(3)
dst.vec[0], dst.vec[1], dst.vec[2] = vn.vec[1]*other.vec[2]-vn.vec[2]*other.vec[1], vn.vec[2]*other.vec[0]-vn.vec[0]*other.vec[2], vn.vec[0]*other.vec[1]-vn.vec[1]*other.vec[0]
return dst
}
func intMin(a, b int) int {
if a < b {
return a
}
return b
}
func intAbs(a int) int {
if a < 0 {
return -a
}
return a
}
// Computes the dot product of two VecNs, if
// the two vectors are not of the same length -- this
// will return NaN.
func (vn *VecN) Dot(other *VecN) float32 {
if vn == nil || other == nil || len(vn.vec) != len(other.vec) {
return float32(math.NaN())
}
var result float32 = 0.0
for i, el := range vn.vec {
result += el * other.vec[i]
}
return result
}
// Computes the vector length (also called the Norm) of the
// vector. Equivalent to math.Sqrt(vn.Dot(vn)) with the appropriate
// type conversions.
//
// If vn is nil, this returns NaN
func (vn *VecN) Len() float32 {
if vn == nil {
return float32(math.NaN())
}
if len(vn.vec) == 0 {
return 0
}
return float32(math.Sqrt(float64(vn.Dot(vn))))
}
// Normalizes the vector and stores the result in dst, which
// will be returned. Dst will be appropraitely resized to the
// size of vn.
//
// The destination can be vn itself and nothing will go wrong.
//
// This is equivalent to vn.Mul(dst, 1/vn.Len())
func (vn *VecN) Normalize(dst *VecN) *VecN {
if vn == nil {
return nil
}
return vn.Mul(dst, 1/vn.Len())
}
// Multiplied the vector by some scalar value and stores the result in dst, which
// will be returned. Dst will be appropraitely resized to the
// size of vn.
//
// The destination can be vn itself and nothing will go wrong.
func (vn *VecN) Mul(dst *VecN, c float32) *VecN {
if vn == nil {
return nil
}
dst = dst.Resize(len(vn.vec))
for i, el := range vn.vec {
dst.vec[i] = el * c
}
return dst
}
// Performs the vector outer product between vn and v2.
// The outer product is like a "reverse" dot product. Where the dot product
// aligns both vectors with the "sized" part facing "inward" (Vec3*Vec3=Mat1x3*Mat3x1=Mat1x1=Scalar).
// The outer product multiplied them with it facing "outward"
// (Vec3*Vec3=Mat3x1*Mat1x3=Mat3x3).
//
// The matrix dst will be Reshaped to the correct size, if vn or v2 are nil,
// this returns nil.
func (vn *VecN) OuterProd(dst *MatMxN, v2 *VecN) *MatMxN {
if vn == nil || v2 == nil {
return nil
}
dst = dst.Reshape(len(vn.vec), len(v2.vec))
for c, el1 := range v2.vec {
for r, el2 := range vn.vec {
dst.Set(r, c, el1*el2)
}
}
return dst
}
func (vn *VecN) ApproxEqual(vn2 *VecN) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !FloatEqual(el, vn2.vec[i]) {
return false
}
}
return true
}
func (vn *VecN) ApproxEqualThreshold(vn2 *VecN, epsilon float32) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !FloatEqualThreshold(el, vn2.vec[i], epsilon) {
return false
}
}
return true
}
func (vn *VecN) ApproxEqualFunc(vn2 *VecN, comp func(float32, float32) bool) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !comp(el, vn2.vec[i]) {
return false
}
}
return true
}
func (vn *VecN) Vec2() Vec2 {
raw := vn.Raw()
return Vec2{raw[0], raw[1]}
}
func (vn *VecN) Vec3() Vec3 {
raw := vn.Raw()
return Vec3{raw[0], raw[1], raw[2]}
}
func (vn *VecN) Vec4() Vec4 {
raw := vn.Raw()
return Vec4{raw[0], raw[1], raw[2], raw[3]}
}