-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.go
572 lines (469 loc) · 15.8 KB
/
vector.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package noodle
import (
"math"
"unsafe"
)
//Vector a interface for any vector
type Vector interface {
Decompose() []float32
}
//Vector2 a 2 dimensional vector
type Vector2 struct {
X float32
Y float32
}
//NewVector2 creates a new vector with defined components
func NewVector2(x, y float32) Vector2 { return Vector2{X: x, Y: y} }
//NewVector2d creates a new vector with double precesion float (float64)
func NewVector2d(x, y float64) Vector2 { return Vector2{X: float32(x), Y: float32(y)} }
//NewVector2i creates a new vector
func NewVector2i(x, y int) Vector2 { return Vector2{X: float32(x), Y: float32(y)} }
//NewVector2Zero creates a vector with all components equaling 0
func NewVector2Zero() Vector2 { return Vector2{X: 0, Y: 0} }
//NewVector2Up creates a normalized vector pointing up
func NewVector2Up() Vector2 { return Vector2{X: 0, Y: 1} }
//NewVector2Right creates a normalized vector pointing right
func NewVector2Right() Vector2 { return Vector2{X: 1, Y: 0} }
//NewVector2One creates a vector that is completely 1
func NewVector2One() Vector2 { return Vector2{X: 1, Y: 1} }
//DecomposePointer the vector into a slice of floats
func (v Vector2) DecomposePointer() *[2]float32 { return (*[2]float32)(unsafe.Pointer(&v)) }
//Decompose the Vector into a new slice of floats.
func (v Vector2) Decompose() []float32 { return []float32{v.X, v.Y} }
//Add two vectors (v1 + v2)
func (v Vector2) Add(v2 Vector2) Vector2 {
return Vector2{X: v.X + v2.X, Y: v.Y + v2.Y}
}
//Subtract two vectors (v1 - v2)
func (v Vector2) Subtract(v2 Vector2) Vector2 {
return Vector2{X: v.X - v2.X, Y: v.Y - v2.Y}
}
//Length of the vector
func (v Vector2) Length() float32 {
return float32(math.Sqrt(float64(v.X*v.X) + float64(v.Y*v.Y)))
}
//SqrLength is the squared length of the vector
func (v Vector2) SqrLength() float32 {
return float32(float64(v.X*v.X) + float64(v.Y*v.Y))
}
//DotProduct of the vector
func (v Vector2) DotProduct(v2 Vector2) float32 {
return v.X*v2.X + v.Y*v2.Y
}
//Perpendicular to this vector
func (v Vector2) Perpendicular() Vector2 {
return Vector2{X: v.Y, Y: v.X}
}
//RotateByRadians rotates the vector in radians. Use Deg2Rad to convert degress into radians.
func (v Vector2) RotateByRadians(radians float32) Vector2 {
sin := float32(math.Sin(float64(radians)))
cos := float32(math.Cos(float64(radians)))
return Vector2{
X: (cos * v.X) - (sin * v.Y),
Y: (sin * v.X) + (cos * v.Y),
}
}
//Angle the vector creates with another vector
func (v Vector2) Angle(v2 Vector2) float32 {
result := float32(math.Atan2(float64(v2.Y-v.Y), float64(v2.X-v.X))) * Rad2Deg
if result < 0 {
result += 360
}
return result
}
//Scale the vector (v * scale)
func (v Vector2) Scale(scale float32) Vector2 {
return Vector2{X: v.X * scale, Y: v.Y * scale}
}
//Multiply a vector by another vector
func (v Vector2) Multiply(v2 Vector2) Vector2 {
return Vector2{X: v.X * v2.X, Y: v.Y * v2.Y}
}
//Negate or Inverts a vector
func (v Vector2) Negate() Vector2 {
return Vector2{X: -v.X, Y: -v.Y}
}
//Divide a vector by a value ( v1.x / d, v1.y / d )
func (v Vector2) Divide(d float32) Vector2 {
return Vector2{X: v.X / d, Y: v.Y / d}
}
//DivideV a vector by another vector: ( v1.x / v2.x, v1.y / v2.y )
func (v Vector2) DivideV(v2 Vector2) Vector2 {
return Vector2{X: v.X / v2.X, Y: v.Y / v2.Y}
}
//Normalize a vector
func (v Vector2) Normalize() Vector2 {
len := v.Length()
if len == 0 {
return v
}
return v.Divide(len)
}
//Lerp a vector towards another vector
func (v Vector2) Lerp(target Vector2, amount float32) Vector2 {
return Vector2{
X: v.X + amount*(target.X-v.X),
Y: v.Y + amount*(target.Y-v.Y),
}
}
//Distance between two vectors
func (v Vector2) Distance(v2 Vector2) float32 {
d := v2.Subtract(v)
return d.Length()
}
//Reflect a vector. The mirror normal can be invisioned as a mirror perpendicular to the surface that is hit.
func (v Vector2) Reflect(mirrorNormal Vector2) Vector2 {
return v.Add(v.Scale(-2 * v.DotProduct(mirrorNormal)))
}
//Min value for each pair of components
func (v Vector2) Min(v2 Vector2) Vector2 {
return Vector2{
X: float32(math.Min(float64(v.X), float64(v2.X))),
Y: float32(math.Min(float64(v.Y), float64(v2.Y))),
}
}
//Max value for each pair of components
func (v Vector2) Max(v2 Vector2) Vector2 {
return Vector2{
X: float32(math.Max(float64(v.X), float64(v2.X))),
Y: float32(math.Max(float64(v.Y), float64(v2.Y))),
}
}
//ToVector3 converts this vector2 into a vector3
func (v Vector2) ToVector3() Vector3 { return NewVector3(v.X, v.Y, 0) }
//Vector3 a 3 dimensional vector
type Vector3 struct {
X float32
Y float32
Z float32
}
//NewVector3 creates a new vector with defined components
func NewVector3(x, y, z float32) Vector3 { return Vector3{X: x, Y: y, Z: z} }
//NewVector3d creates a new vector with double precesion float (float64)
func NewVector3d(x, y, z float64) Vector3 { return Vector3{X: float32(x), Y: float32(y), Z: float32(z)} }
//NewVector3i creates a new vector
func NewVector3i(x, y, z int) Vector3 { return Vector3{X: float32(x), Y: float32(y), Z: float32(z)} }
//NewVector3Zero creates a vector with all components equaling 0
func NewVector3Zero() Vector3 { return Vector3{X: 0, Y: 0, Z: 0} }
//NewVector3Up creates a normalized vector pointing up
func NewVector3Up() Vector3 { return Vector3{X: 0, Y: 1, Z: 0} }
//NewVector3Right creates a normalized vector pointing right
func NewVector3Right() Vector3 { return Vector3{X: 1, Y: 0, Z: 0} }
//NewVector3Forward creates a normalized vector pointing forwards
func NewVector3Forward() Vector3 { return Vector3{X: 0, Y: 0, Z: 1} }
//NewVector3One creats a factor that is entirely 1
func NewVector3One() Vector3 { return Vector3{X: 1, Y: 1, Z: 1} }
//DecomposePointer the vector into a slice of floats
func (v Vector3) DecomposePointer() *[3]float32 { return (*[3]float32)(unsafe.Pointer(&v)) }
//Decompose the Vector into a new slice of floats.
func (v Vector3) Decompose() []float32 { return []float32{v.X, v.Y, v.Z} }
//Add two vectors (v1 + v2)
func (v Vector3) Add(v2 Vector3) Vector3 {
return Vector3{X: v.X + v2.X, Y: v.Y + v2.Y, Z: v.Z + v2.Z}
}
//Subtract two vectors (v1 - v2)
func (v Vector3) Subtract(v2 Vector3) Vector3 {
return Vector3{X: v.X - v2.X, Y: v.Y - v2.Y, Z: v.Z - v2.Z}
}
//Length of the vector
func (v Vector3) Length() float32 {
return float32(math.Sqrt(float64(v.X*v.X) + float64(v.Y*v.Y) + float64(v.Z*v.Z)))
}
//SqrLength is the squared length of the vector
func (v Vector3) SqrLength() float32 {
return float32(float64(v.X*v.X) + float64(v.Y*v.Y) + float64(v.Z*v.Z))
}
//DotProduct of the vector
func (v Vector3) DotProduct(v2 Vector3) float32 {
return v.X*v2.X + v.Y*v2.Y + v.Z*v2.Z
}
//CrossProduct of the vector
func (v Vector3) CrossProduct(other Vector3) Vector3 {
v1 := v.DecomposePointer()
v2 := other.DecomposePointer()
return Vector3{v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}
/*
return Vector3{
X: v.Y*v2.Z - v.Z*v2.Y,
Y: v.Z*v2.X - v.X*v2.Z,
Z: v.X*v2.Y - v.Y*v2.X,
}
*/
}
//Perpendicular to this vector
func (v Vector3) Perpendicular() Vector3 {
cardinalAxis := &Vector3{X: 1, Y: 0, Z: 0}
min := math.Abs(float64(v.X))
if math.Abs(float64(v.Y)) < min {
min = math.Abs(float64(v.Y))
cardinalAxis.X = 0
cardinalAxis.Y = 1
cardinalAxis.Z = 0
}
if math.Abs(float64(v.Z)) < min {
cardinalAxis.X = 0
cardinalAxis.Y = 0
cardinalAxis.Z = 1
}
return v.CrossProduct(*cardinalAxis)
}
//Distance between two vectors
func (v Vector3) Distance(v2 Vector3) float32 {
d := v2.Subtract(v)
return d.Length()
}
//Angle the vector creates with another vector
func (v Vector3) Angle(v2 Vector3) float32 {
result := float32(math.Acos(Clamp(float64(v.DotProduct(v2)), -1, 1))) * Rad2Deg
if result < 0 {
result += 360
}
return result
}
//Scale the vector (v * scale)
func (v Vector3) Scale(scale float32) Vector3 {
return Vector3{X: v.X * scale, Y: v.Y * scale, Z: v.Z * scale}
}
//Multiply a vector by another vector
func (v Vector3) Multiply(v2 Vector3) Vector3 {
return Vector3{X: v.X * v2.X, Y: v.Y * v2.Y, Z: v.Z * v2.Z}
}
//Negate or Inverts a vector
func (v Vector3) Negate() Vector3 {
return Vector3{X: -v.X, Y: -v.Y, Z: -v.Z}
}
//Divide a vector by a value (v / d)
func (v Vector3) Divide(d float32) Vector3 {
return Vector3{X: v.X / d, Y: v.Y / d, Z: v.Z / d}
}
//DivideV a vector by another vecotr (v / v2)
func (v Vector3) DivideV(v2 Vector3) Vector3 {
return Vector3{X: v.X / v2.X, Y: v.Y / v2.Y, Z: v.Z / v2.Z}
}
//Normalize a vector
func (v Vector3) Normalize() Vector3 {
length := v.Length()
if length == 0 {
length = 1
}
ilength := 1 / length
return v.Scale(ilength)
}
//Lerp a vector towards another vector
func (v Vector3) Lerp(target Vector3, amount float32) Vector3 {
return Vector3{
X: v.X + amount*(target.X-v.X),
Y: v.Y + amount*(target.Y-v.Y),
Z: v.Z + amount*(target.Z-v.Z),
}
}
//Reflect a vector. The mirror normal can be invisioned as a mirror perpendicular to the surface that is hit.
func (v Vector3) Reflect(mirrorNormal Vector3) Vector3 {
return v.Subtract(mirrorNormal.Scale(2)).Scale(v.DotProduct(mirrorNormal))
}
//Rotate rotates the vector
func (v Vector3) Rotate(q Quaternion) Vector3 {
return Vector3{
X: v.X*(q.X*q.X+q.W*q.W-q.Y*q.Y-q.Z*q.Z) + v.Y*(2*q.X*q.Y-2*q.W*q.Z) + v.Z*(2*q.X*q.Z+2*q.W*q.Y),
Y: v.X*(2*q.W*q.Z+2*q.X*q.Y) + v.Y*(q.W*q.W-q.X*q.X+q.Y*q.Y-q.Z*q.Z) + v.Z*(-2*q.W*q.X+2*q.Y*q.Z),
Z: v.X*(-2*q.W*q.Y+2*q.X*q.Z) + v.Y*(2*q.W*q.X+2*q.Y*q.Z) + v.Z*(q.W*q.W-q.X*q.X-q.Y*q.Y+q.Z*q.Z),
}
}
//OrthoNormalize makes two vectors normalized and orthogonal to each other
func (v *Vector3) OrthoNormalize(v2 *Vector3) {
var tmp Vector3
tmp = v.Normalize()
v.X = tmp.X
v.Y = tmp.Y
v.Z = tmp.Z
tmp = v.CrossProduct(*v2).Normalize().CrossProduct(*v)
v2.X = tmp.X
v2.Y = tmp.Y
v2.Z = tmp.Z
}
//Transform a vector by a given matrix
func (v Vector3) Transform(m Matrix) Vector3 {
return Vector3{
X: m.M0*v.X + m.M4*v.Y + m.M8*v.Z + m.M12,
Y: m.M1*v.X + m.M5*v.Y + m.M9*v.Z + m.M13,
Z: m.M2*v.X + m.M6*v.Y + m.M10*v.Z + m.M14,
}
}
//Min value for each pair of components
func (v Vector3) Min(v2 Vector3) Vector3 {
return Vector3{
X: float32(math.Min(float64(v.X), float64(v2.X))),
Y: float32(math.Min(float64(v.Y), float64(v2.Y))),
Z: float32(math.Min(float64(v.Z), float64(v2.Z))),
}
}
//Max value for each pair of components
func (v Vector3) Max(v2 Vector3) Vector3 {
return Vector3{
X: float32(math.Max(float64(v.X), float64(v2.X))),
Y: float32(math.Max(float64(v.Y), float64(v2.Y))),
Z: float32(math.Max(float64(v.Z), float64(v2.Z))),
}
}
//Barycenter computers the coordinates (u, v, w) for the vector with respect to triangle (a, b, c). Assumes vector is on plane with triangle
func (v Vector3) Barycenter(a, b, c Vector3) Vector3 {
v0 := b.Subtract(a)
v1 := c.Subtract(a)
v2 := v.Subtract(a)
d00 := v0.DotProduct(v0)
d01 := v0.DotProduct(v1)
d11 := v1.DotProduct(v1)
d20 := v2.DotProduct(v0)
d21 := v2.DotProduct(v1)
denom := d00*d11 - d01*d01
y := (d11*d20 - d01*d21) / denom
z := (d00*d21 - d01*d20) / denom
x := 1 - (z + y)
return Vector3{X: x, Y: y, Z: z}
}
//ToVector4 coverts the vector3 into a vector4
func (v Vector3) ToVector4() Vector4 { return NewVector4(v.X, v.Y, v.Z, 0) }
//Vector4 A 4 dimensional vector
type Vector4 struct {
X float32
Y float32
Z float32
W float32
}
//NewVector4 creates a new vector with defined components
func NewVector4(x, y, z, w float32) Vector4 { return Vector4{X: x, Y: y, Z: z, W: w} }
//NewVector4d creates a new vector with double precesion float (float64)
func NewVector4d(x, y, z, w float64) Vector4 {
return Vector4{X: float32(x), Y: float32(y), Z: float32(z), W: float32(w)}
}
//NewVector4i creates a new vector
func NewVector4i(x, y, z, w int) Vector4 {
return Vector4{X: float32(x), Y: float32(y), Z: float32(z), W: float32(w)}
}
//NewVector4Zero creates a vector with all components equaling 0
func NewVector4Zero() Vector4 { return Vector4{X: 0, Y: 0, Z: 0, W: 0} }
//DecomposePointer the vector into a slice of floats
func (v Vector4) DecomposePointer() *[4]float32 { return (*[4]float32)(unsafe.Pointer(&v)) }
//Decompose the Vector into a new slice of floats.
func (v Vector4) Decompose() []float32 { return []float32{v.X, v.Y, v.Z, v.W} }
//Add two vectors (v1 + v2)
func (v Vector4) Add(v2 Vector4) Vector4 {
return Vector4{X: v.X + v2.X, Y: v.Y + v2.Y, Z: v.Z + v2.Z, W: v.W + v2.W}
}
//Subtract two vectors (v1 - v2)
func (v Vector4) Subtract(v2 Vector4) Vector4 {
return Vector4{X: v.X - v2.X, Y: v.Y - v2.Y, Z: v.Z - v2.Z, W: v.W - v2.W}
}
//Scale the vector (v * scale)
func (v Vector4) Scale(scale float32) Vector4 {
return Vector4{X: v.X * scale, Y: v.Y * scale, Z: v.Z * scale, W: v.W * scale}
}
//Multiply a vector by another vector
func (v Vector4) Multiply(v2 Vector4) Vector4 {
return Vector4{X: v.X * v2.X, Y: v.Y * v2.Y, Z: v.Z * v2.Z, W: v.W * v2.W}
}
//Divide a vector by a value (v / d)
func (v Vector4) Divide(d float32) Vector4 {
return Vector4{X: v.X / d, Y: v.Y / d, Z: v.Z / d, W: v.W / d}
}
//DivideV a vector by another vecotr (v / v2)
func (v Vector4) DivideV(v2 Vector4) Vector4 {
return Vector4{X: v.X / v2.X, Y: v.Y / v2.Y, Z: v.Z / v2.Z, W: v.W / v2.W}
}
//Negate or Inverts a vector
func (v Vector4) Negate() Vector4 {
return Vector4{X: -v.X, Y: -v.Y, Z: -v.Z, W: -v.W}
}
//Length of the vector
func (v Vector4) Length() float32 {
return float32(math.Sqrt(float64(v.X*v.X) + float64(v.Y*v.Y) + float64(v.Z*v.Z) + float64(v.W*v.W)))
}
//SqrLength is the squared length of the vector
func (v Vector4) SqrLength() float32 {
return float32(float64(v.X*v.X) + float64(v.Y*v.Y) + float64(v.Z*v.Z) + float64(v.W*v.W))
}
//DotProduct of the vector
func (v Vector4) DotProduct(v2 Vector4) float32 {
return v.X*v2.X + v.Y*v2.Y + v.Z*v2.Z + v.W*v2.W
}
//Normalize a vector
func (v Vector4) Normalize() Vector4 {
length := v.Length()
if length == 0 {
length = 1
}
ilength := 1 / length
return v.Scale(ilength)
}
//Distance between two vectors
func (v Vector4) Distance(v2 Vector4) float32 {
d := v2.Subtract(v)
return d.Length()
}
//Lerp a vector towards another vector
func (v Vector4) Lerp(target Vector4, amount float32) Vector4 {
return Vector4{
X: v.X + amount*(target.X-v.X),
Y: v.Y + amount*(target.Y-v.Y),
Z: v.Z + amount*(target.Z-v.Z),
W: v.W + amount*(target.W-v.W),
}
}
//Min value for each pair of components
func (v Vector4) Min(v2 Vector4) Vector4 {
return Vector4{
X: float32(math.Min(float64(v.X), float64(v2.X))),
Y: float32(math.Min(float64(v.Y), float64(v2.Y))),
Z: float32(math.Min(float64(v.Z), float64(v2.Z))),
W: float32(math.Min(float64(v.W), float64(v2.W))),
}
}
//Max value for each pair of components
func (v Vector4) Max(v2 Vector4) Vector4 {
return Vector4{
X: float32(math.Max(float64(v.X), float64(v2.X))),
Y: float32(math.Max(float64(v.Y), float64(v2.Y))),
Z: float32(math.Max(float64(v.Z), float64(v2.Z))),
W: float32(math.Max(float64(v.W), float64(v2.W))),
}
}
//Transform is a structure of tranlsation, rotation and scale properties
type Transform struct {
Position Vector3
Rotation Quaternion
Scale Vector3
}
//NewTransformIdentity creates a new blank transform
func NewTransformIdentity() Transform {
return Transform{
Position: Vector3{0, 0, 0},
Rotation: NewQuaternionIdentity(),
Scale: Vector3{1, 1, 1},
}
}
//NewTransform creates a new transform
func NewTransform(translation Vector3, rotation Quaternion, scale Vector3) Transform {
return Transform{
Position: translation,
Rotation: rotation,
Scale: scale,
}
}
//ToMatrix turns this tranform into a matrix that performs it.
func (t Transform) ToMatrix() Matrix {
return NewMatrixTransform(t)
}
//Transform2D is a structure of tranlsation, rotation and scale properties
type Transform2D struct {
Position Vector2
Rotation float32
Scale Vector2
}
//NewTransform2D creates a new transform
func NewTransform2D(translation Vector2, rotation float32, scale Vector2) Transform2D {
return Transform2D{
Position: translation,
Rotation: rotation,
Scale: scale,
}
}