-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec3.go
158 lines (134 loc) · 2.72 KB
/
vec3.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
/* FILE WAS AUTO-GENERATED FROM f64/vecN */
package f64
// Mul scales v by s
func (v Vec3) Mul(s float64) (res Vec3) {
initVec3(&res, len(v))
for i := range v {
res[i] = v[i] * s
}
return
}
// Abs returns the absolute value of v
func (v Vec3) Abs() (res Vec3) {
initVec3(&res, len(v))
for i := range v {
res[i] = absfloat64(v[i])
}
return
}
// Sub o from v
func (v Vec3) Sub(o Vec3) Vec3 {
return v.Add(o.Neg())
}
// Div scales v by the multiplicative inverse of s
func (v Vec3) Div(s float64) Vec3 {
return v.Mul(1.0 / s)
}
// Neg returns the negative vector of v (-v)
func (v Vec3) Neg() Vec3 {
return v.Mul(-1.0)
}
// Inv returns the multiplicative inverse of v
func (v Vec3) Inv() (res Vec3) {
initVec3(&res, len(v))
for i := range v {
res[i] = 1.0 / v[i]
}
return
}
// Add o to v
func (v Vec3) Add(o Vec3) (res Vec3) {
initVec3(&res, len(v))
//TODO: this doesnt technically work for different size vecNs, or vecS
for i := range v {
res[i] = v[i] + o.At(i)
}
return
}
// MulV multiplies v*s per dim
func (v Vec3) MulV(o Vec3) (res Vec3) {
initVec3(&res, len(v))
for i := range v {
res[i] = v[i] * o.At(i)
}
return
}
// Dot returns the dot product of v and o (v⋅o)
func (v Vec3) Dot(o Vec3) (d float64) {
for i := range v {
d += v[i] * o.At(i)
}
return
}
// Within returns true if v is within the bounds of o
// considering both values as their absolute.
func (v Vec3) Within(o Vec3) bool {
v = v.Abs()
o = o.Abs()
for i := range v {
if v[i] > o.At(i) {
return false
}
}
return true
}
// Eq returns true if v and o are equal
func (v Vec3) Eq(o Vec3) bool {
for i := range v {
if v[i] != o[i] {
return false
}
}
return true
}
// Mag returns the L2 norm of v
func (v Vec3) Mag() float64 {
return sqrtfloat64(v.Dot(v))
}
// Unit returns the (l2) normalized vector of v
// or the zero vector if v is zero.
func (v Vec3) Unit() Vec3 {
d := v.Mag()
switch d {
case 1, 0:
return v
}
return v.Div(d)
}
/* VECC_START */
var _ VecC = (*Vec3)(nil)
// NewVec3 creates a new Vec3 from the provided values.
func NewVec3(vs ...float64) Vec3 {
out := Vec3{}
out.Set(vs...)
return out
}
// At returns the value at dimension dim.
func (v *Vec3) At(dim int) float64 {
if dim >= len(*v) {
return 0
}
return (*v)[dim]
}
// Get a slice of the underlying values
func (v *Vec3) Get() []float64 {
return (*v)[:]
}
// Set vector values
func (v *Vec3) Set(vs ...float64) {
initVec3(v, len(vs))
copy((*v)[:], vs)
}
// Vec2 version of this vector.
func (v *Vec3) Vec2() Vec2 {
return NewVec2((*v)[:]...)
}
// Vec3 version of this vector.
func (v *Vec3) Vec3() Vec3 {
return NewVec3((*v)[:]...)
}
// Vec4 version of this vector.
func (v *Vec3) Vec4() Vec4 {
return NewVec4((*v)[:]...)
}
/* VECC_END */