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
|
/**********************************************************************
*
* Simply vector/matrix utility
*
* Copyright (c) 2021 Dario Deledda. All rights reserved.
* Use of this source code is governed by an MIT license
* that can be found in the LICENSE file.
*
* TODO:
**********************************************************************/
module m4
import math
pub struct Vec4 {
pub mut:
e [4]f32
}
/*********************************************************************
*
* Utility
*
*********************************************************************/
pub fn (x Vec4) str() string {
return '|${x.e[0]:-6.3},${x.e[1]:-6.3},${x.e[2]:-6.3},${x.e[3]:-6.3}|'
}
// create a Vec4 function passing x,y,z as parameteres. w is set to 1
pub fn vec3(x f32, y f32, z f32) Vec4 {
return Vec4{
e: [x, y, z, 1]!
}
}
// Remove all the raw zeros
[direct_array_access]
pub fn (a Vec4) clean() Vec4 {
mut x := Vec4{}
for c, value in a.e {
if f32_abs(value) < precision {
x.e[c] = 0
} else {
x.e[c] = value
}
}
return x
}
// Set all elements to value
pub fn (mut x Vec4) copy(value f32) {
x.e = [value, value, value, value]!
}
// Scale the vector using a scalar
pub fn (x Vec4) mul_scalar(value f32) Vec4 {
return Vec4{
e: [x.e[0] * value, x.e[1] * value, x.e[2] * value, x.e[3] * value]!
}
}
// Reciprocal of the vector
pub fn (x Vec4) inv() Vec4 {
return Vec4{
e: [
if x.e[0] != 0 { 1.0 / x.e[0] } else { f32(0) },
if x.e[1] != 0 { 1.0 / x.e[1] } else { f32(0) },
if x.e[2] != 0 { 1.0 / x.e[2] } else { f32(0) },
if x.e[3] != 0 { 1.0 / x.e[3] } else { f32(0) },
]!
}
}
// Normalize the vector
pub fn (x Vec4) normalize() Vec4 {
m := x.mod()
if m == 0 {
return zero_v4()
}
return Vec4{
e: [
x.e[0] * (1 / m),
x.e[1] * (1 / m),
x.e[2] * (1 / m),
x.e[3] * (1 / m),
]!
}
}
// Normalize only xyz, w set to 0
pub fn (x Vec4) normalize3() Vec4 {
m := x.mod3()
if m == 0 {
return zero_v4()
}
return Vec4{
e: [
x.e[0] * (1 / m),
x.e[1] * (1 / m),
x.e[2] * (1 / m),
0,
]!
}
}
// Module of the vector xyzw
pub fn (x Vec4) mod() f32 {
return f32(math.sqrt(x.e[0] * x.e[0] + x.e[1] * x.e[1] + x.e[2] * x.e[2] + x.e[3] * x.e[3]))
}
// Module for 3d vector xyz, w ignored
pub fn (x Vec4) mod3() f32 {
return f32(math.sqrt(x.e[0] * x.e[0] + x.e[1] * x.e[1] + x.e[2] * x.e[2]))
}
/*********************************************************************
*
* Math
*
*********************************************************************/
// Return a zero vector
pub fn zero_v4() Vec4 {
return Vec4{
e: [
f32(0),
0,
0,
0,
]!
}
}
// Return all one vector
pub fn one_v4() Vec4 {
return Vec4{
e: [
f32(1),
1,
1,
1,
]!
}
}
// Return a blank vector
pub fn blank_v4() Vec4 {
return Vec4{
e: [
f32(0),
0,
0,
1,
]!
}
}
// Set all elements to value
pub fn set_v4(value f32) Vec4 {
return Vec4{
e: [
value,
value,
value,
value,
]!
}
}
// Sum of all the elements
pub fn (x Vec4) sum() f32 {
return x.e[0] + x.e[1] + x.e[2] + x.e[3]
}
/*********************************************************************
*
* Operators
*
*********************************************************************/
// Addition
pub fn (a Vec4) + (b Vec4) Vec4 {
return Vec4{
e: [
a.e[0] + b.e[0],
a.e[1] + b.e[1],
a.e[2] + b.e[2],
a.e[3] + b.e[3],
]!
}
}
// Subtraction
pub fn (a Vec4) - (b Vec4) Vec4 {
return Vec4{
e: [
a.e[0] - b.e[0],
a.e[1] - b.e[1],
a.e[2] - b.e[2],
a.e[3] - b.e[3],
]!
}
}
// Dot product
pub fn (a Vec4) * (b Vec4) f32 {
return a.e[0] * b.e[0] + a.e[1] * b.e[1] + a.e[2] * b.e[2] + a.e[3] * b.e[3]
}
// Cross product
pub fn (a Vec4) % (b Vec4) Vec4 {
return Vec4{
e: [
(a.e[1] * b.e[2]) - (a.e[2] * b.e[1]),
(a.e[2] * b.e[0]) - (a.e[0] * b.e[2]),
(a.e[0] * b.e[1]) - (a.e[1] * b.e[0]),
0,
]!
}
}
// Components multiplication
pub fn (x Vec4) mul_vec4(y Vec4) Vec4 {
return Vec4{
e: [
x.e[0] * y.e[0],
x.e[1] * y.e[1],
x.e[2] * y.e[2],
x.e[3] * y.e[3],
]!
}
}
|