-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvector.lua
274 lines (236 loc) · 6.14 KB
/
vector.lua
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
-- Localize globals
local assert, math, pairs, rawget, rawset, setmetatable, unpack, vector = assert, math, pairs, rawget, rawset, setmetatable, unpack, vector
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
local mt_vector = vector
index_aliases = {
x = 1,
y = 2,
z = 3,
w = 4;
"x", "y", "z", "w";
}
metatable = {
__index = function(table, key)
local index = index_aliases[key]
if index ~= nil then
return rawget(table, index)
end
return _ENV[key]
end,
__newindex = function(table, key, value)
-- TODO
local index = index_aliases[key]
if index ~= nil then
return rawset(table, index, value)
end
return rawset(table, key, value)
end
}
function new(v)
return setmetatable(v, metatable)
end
function zeros(n)
local v = {}
for i = 1, n do
v[i] = 0
end
return new(v)
end
function from_xyzw(v)
return new{v.x, v.y, v.z, v.w}
end
function from_minetest(v)
return new{v.x, v.y, v.z}
end
function to_xyzw(v)
return {x = v[1], y = v[2], z = v[3], w = v[4]}
end
--+ not necessarily required, as Minetest respects the metatable
function to_minetest(v)
return mt_vector.new(unpack(v))
end
function equals(v, w)
for k, v in pairs(v) do
if v ~= w[k] then return false end
end
return true
end
metatable.__eq = equals
function combine(v, w, f)
local new_vector = {}
for key, value in pairs(v) do
new_vector[key] = f(value, w[key])
end
return new(new_vector)
end
function apply(v, f, ...)
local new_vector = {}
for key, value in pairs(v) do
new_vector[key] = f(value, ...)
end
return new(new_vector)
end
function combinator(f)
return function(v, w)
return combine(v, w, f)
end, function(v, ...)
return apply(v, f, ...)
end
end
function invert(v)
local res = {}
for key, value in pairs(v) do
res[key] = -value
end
return new(res)
end
add, add_scalar = combinator(function(v, w) return v + w end)
subtract, subtract_scalar = combinator(function(v, w) return v - w end)
multiply, multiply_scalar = combinator(function(v, w) return v * w end)
divide, divide_scalar = combinator(function(v, w) return v / w end)
pow, pow_scalar = combinator(function(v, w) return v ^ w end)
metatable.__add = add
metatable.__unm = invert
metatable.__sub = subtract
metatable.__mul = multiply
metatable.__div = divide
--+ linear interpolation
--: ratio number from 0 (all the first vector) to 1 (all the second vector)
function interpolate(v, w, ratio)
return add(multiply_scalar(v, 1 - ratio), multiply_scalar(w, ratio))
end
function norm(v)
local sum = 0
for _, value in pairs(v) do
sum = sum + value ^ 2
end
return sum
end
function length(v)
return math.sqrt(norm(v))
end
-- Minor code duplication for the sake of performance
function distance(v, w)
local sum = 0
for key, value in pairs(v) do
sum = sum + (value - w[key]) ^ 2
end
return math.sqrt(sum)
end
function normalize(v)
return divide_scalar(v, length(v))
end
function normalize_zero(v)
local len = length(v)
if len == 0 then
-- Return a zeroed vector with the same keys
local zeroed = {}
for k in pairs(v) do
zeroed[k] = 0
end
return new(zeroed)
end
return divide_scalar(v, len)
end
function floor(v)
return apply(v, math.floor)
end
function ceil(v)
return apply(v, math.ceil)
end
function clamp(v, min, max)
return apply(apply(v, math.max, min), math.min, max)
end
function cross3(v, w)
assert(#v == 3 and #w == 3)
return new{
v[2] * w[3] - v[3] * w[2],
v[3] * w[1] - v[1] * w[3],
v[1] * w[2] - v[2] * w[1]
}
end
function dot(v, w)
local sum = 0
for i, c in pairs(v) do
sum = sum + c * w[i]
end
return sum
end
function reflect(v, normal --[[**normalized** plane normal vector]])
return subtract(v, multiply_scalar(normal, 2 * dot(v, normal))) -- reflection of v at the plane
end
--+ Angle between two vectors
--> Signed angle in radians
function angle(v, w)
-- Based on dot(v, w) = |v| * |w| * cos(x)
return math.acos(dot(v, w) / length(v) / length(w))
end
-- See https://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToAngle/
function axis_angle3(euler_rotation)
assert(#euler_rotation == 3)
euler_rotation = divide_scalar(euler_rotation, 2)
local cos = apply(euler_rotation, math.cos)
local sin = apply(euler_rotation, math.sin)
return normalize_zero{
sin[1] * sin[2] * cos[3] + cos[1] * cos[2] * sin[3],
sin[1] * cos[2] * cos[3] + cos[1] * sin[2] * sin[3],
cos[1] * sin[2] * cos[3] - sin[1] * cos[2] * sin[3],
}, 2 * math.acos(cos[1] * cos[2] * cos[3] - sin[1] * sin[2] * sin[3])
end
-- Uses Rodrigues' rotation formula
-- axis must be normalized
function rotate3(v, axis, angle)
assert(#v == 3 and #axis == 3)
local cos = math.cos(angle)
return multiply_scalar(v, cos)
-- Minetest's coordinate system is *left-handed*, so `v` and `axis` must be swapped here
+ multiply_scalar(cross3(v, axis), math.sin(angle))
+ multiply_scalar(axis, dot(axis, v) * (1 - cos))
end
function box_box_collision(diff, box, other_box)
for index, diff in pairs(diff) do
if box[index] + diff > other_box[index + 3] or other_box[index] > box[index + 3] + diff then
return false
end
end
return true
end
local function moeller_trumbore(origin, direction, triangle, is_tri)
local point_1, point_2, point_3 = unpack(triangle)
local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1)
local h = cross3(direction, edge_2)
local a = dot(edge_1, h)
if math.abs(a) < 1e-9 then
return
end
local f = 1 / a
local diff = subtract(origin, point_1)
local u = f * dot(diff, h)
if u < 0 or u > 1 then
return
end
local q = cross3(diff, edge_1)
local v = f * dot(direction, q)
if v < 0 or (is_tri and u or 0) + v > 1 then
return
end
local pos_on_line = f * dot(edge_2, q)
if pos_on_line >= 0 then
return pos_on_line, u, v
end
end
function ray_triangle_intersection(origin, direction, triangle)
return moeller_trumbore(origin, direction, triangle, true)
end
function ray_parallelogram_intersection(origin, direction, parallelogram)
return moeller_trumbore(origin, direction, parallelogram)
end
function triangle_normal(triangle)
local point_1, point_2, point_3 = unpack(triangle)
local edge_1, edge_2 = subtract(point_2, point_1), subtract(point_3, point_1)
return normalize(cross3(edge_1, edge_2))
end
-- Export environment
return _ENV