-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec4.go
162 lines (140 loc) · 4.17 KB
/
vec4.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
package gogm
import (
"fmt"
"math"
)
// Vec4 is a vector with 4 components, of type T.
type Vec4[T number] [4]T
// Vec4CopyVec4 copies the content of src to dst.
func Vec4CopyVec4[T1, T2 number](dst *Vec4[T1], src *Vec4[T2]) {
dst[0] = T1(src[0])
dst[1] = T1(src[1])
dst[2] = T1(src[2])
dst[3] = T1(src[3])
}
// Vec4CopyVec2 copies the content of src to dst.
func Vec4CopyVec2[T1, T2 number](dst *Vec4[T1], src *Vec2[T2]) {
dst[0] = T1(src[0])
dst[1] = T1(src[1])
}
// Vec4CopyVec3 copies the content of src to dst.
func Vec4CopyVec3[T1, T2 number](dst *Vec4[T1], src *Vec3[T2]) {
dst[0] = T1(src[0])
dst[1] = T1(src[1])
dst[2] = T1(src[2])
}
// String returns a string representation of the vector.
func (v1 *Vec4[T]) String() string {
return fmt.Sprintf("{%v, %v, %v, %v}", v1[0], v1[1], v1[2], v1[3])
}
// Len returns the length of the vector.
func (v1 *Vec4[T]) Len() float64 {
return math.Sqrt(math.Pow(float64(v1[0]), 2) + math.Pow(float64(v1[1]), 2) + math.Pow(float64(v1[2]), 2) + math.Pow(float64(v1[3]), 2))
}
// Normalize normalizes v2, and stores the result in v1.
func (v1 *Vec4[T]) Normalize(v2 *Vec4[T]) {
l := T(v2.Len())
v1[0] = v2[0] / l
v1[1] = v2[1] / l
v1[2] = v2[2] / l
v1[3] = v2[3] / l
}
// Inverse sets v1 to the inverse of v2.
// v1 = -v2
func (v1 *Vec4[T]) Inverse(v2 *Vec4[T]) {
v1[0] = -v2[0]
v1[1] = -v2[1]
v1[2] = -v2[2]
v1[3] = -v2[3]
}
// Add adds v2 with v3 component-wise, and stores the result in v1.
// v1 = v2 + v3
func (v1 *Vec4[T]) Add(v2 *Vec4[T], v3 *Vec4[T]) {
v1[0] = v2[0] + v3[0]
v1[1] = v2[1] + v3[1]
v1[2] = v2[2] + v3[2]
v1[3] = v2[3] + v3[3]
}
// Sub subtracts v2 from v3 component-wise, and stores the result in v1.
// v1 = v2 - v3
func (v1 *Vec4[T]) Sub(v2 *Vec4[T], v3 *Vec4[T]) {
v1[0] = v2[0] - v3[0]
v1[1] = v2[1] - v3[1]
v1[2] = v2[2] - v3[2]
v1[3] = v2[3] - v3[3]
}
// Mul multiplies v2 with v3 component-wise, and stores the result in v1.
// v1 = v2 * v3
func (v1 *Vec4[T]) Mul(v2 *Vec4[T], v3 *Vec4[T]) {
v1[0] = v2[0] * v3[0]
v1[1] = v2[1] * v3[1]
v1[2] = v2[2] * v3[2]
v1[3] = v2[3] * v3[3]
}
// Div divides v2 by v3 component-wise, and stores the result in v1.
// v1 = v2 / v3
func (v1 *Vec4[T]) Div(v2 *Vec4[T], v3 *Vec4[T]) {
v1[0] = v2[0] / v3[0]
v1[1] = v2[1] / v3[1]
v1[2] = v2[2] / v3[2]
v1[3] = v2[3] / v3[3]
}
// AddS adds each component of v2 with s, and stores the result in v1.
// v1 = v2 + s
func (v1 *Vec4[T]) AddS(v2 *Vec4[T], s T) {
v1[0] = v2[0] + s
v1[1] = v2[1] + s
v1[2] = v2[2] + s
v1[3] = v2[3] + s
}
// SubS subtracts each component of v2 by s, and stores the result in v1.
// v1 = v2 - s
func (v1 *Vec4[T]) SubS(v2 *Vec4[T], s T) {
v1[0] = v2[0] - s
v1[1] = v2[1] - s
v1[2] = v2[2] - s
v1[3] = v2[3] - s
}
// MulS multiplies each component of v2 with s, and stores the result in v1.
// v1 = v2 * s
func (v1 *Vec4[T]) MulS(v2 *Vec4[T], s T) {
v1[0] = v2[0] * s
v1[1] = v2[1] * s
v1[2] = v2[2] * s
v1[3] = v2[3] * s
}
// DivS divides each component of v2 by s, and stores the result in v1.
// v1 = v2 / s
func (v1 *Vec4[T]) DivS(v2 *Vec4[T], s T) {
v1[0] = v2[0] / s
v1[1] = v2[1] / s
v1[2] = v2[2] / s
v1[3] = v2[3] / s
}
// AddHomog adds v2 with v3 component-wise, and stores the result in v1.
// v1 = v2 + v3
func (v1 *Vec4[T]) AddHomog(v2 *Vec4[T], v3 *Vec4[T]) {
v1[0] = v2[0] + v3[0]
v1[1] = v2[1] + v3[1]
v1[2] = v2[2] + v3[2]
}
// CrossHomog takes the 3D cross product of v1 and v2, and stores the result in v1.
// v1 = v2 x v3
func (v1 *Vec4[T]) CrossHomog(v2 *Vec4[T], v3 *Vec4[T]) {
v1[0], v1[1], v1[2] = v2[1]*v3[2]-v3[1]*v2[2], v2[2]*v3[0]-v3[2]*v2[0], v2[0]*v3[1]-v3[0]*v2[1]
}
// CrossHomogFast takes the 3D cross product of v1 and v2, and stores the result in v1.
// v1 = v2 x v3
func (v1 *Vec4[T]) CrossHomogFast(v2 *Vec4[T], v3 *Vec4[T]) {
v1[0] = v2[1]*v3[2] - v3[1]*v2[2]
v1[1] = v2[2]*v3[0] - v3[2]*v2[0]
v1[2] = v2[0]*v3[1] - v3[0]*v2[1]
}
// Dot takes the dot product of v1 and v2, and returns the result.
func (v1 *Vec4[T]) Dot(v2 *Vec4[T]) T {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3]
}
// DotHomog takes the dot product of v1 and v2, and returns the result.
func (v1 *Vec4[T]) DotHomog(v2 *Vec4[T]) T {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]
}