2020-06-28 22:42:24 +02:00
|
|
|
local mt_vector = vector
|
|
|
|
local vector = getfenv(1)
|
|
|
|
|
2020-12-22 09:41:59 +01:00
|
|
|
index_aliases = {
|
2021-03-27 20:10:49 +01:00
|
|
|
x = 1,
|
|
|
|
y = 2,
|
|
|
|
z = 3,
|
|
|
|
w = 4
|
2020-12-22 09:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
modlib.table.add_all(index_aliases, modlib.table.flip(index_aliases))
|
|
|
|
|
|
|
|
metatable = {
|
2021-03-27 20:10:49 +01:00
|
|
|
__index = function(table, key)
|
|
|
|
local index = index_aliases[key]
|
|
|
|
if index ~= nil then
|
|
|
|
return table[index]
|
|
|
|
end
|
|
|
|
return vector[key]
|
|
|
|
end,
|
|
|
|
__newindex = function(table, key, value)
|
|
|
|
local index = letters[key]
|
|
|
|
if index ~= nil then
|
|
|
|
return rawset(table, index, value)
|
|
|
|
end
|
|
|
|
end
|
2020-12-22 09:41:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-28 22:42:24 +02:00
|
|
|
function new(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return setmetatable(v, metatable)
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function from_xyzw(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return new{v.x, v.y, v.z, v.w}
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
|
2020-12-19 15:28:53 +01:00
|
|
|
function from_minetest(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return new{v.x, v.y, v.z}
|
2020-12-19 15:28:53 +01:00
|
|
|
end
|
|
|
|
|
2020-06-28 22:42:24 +02:00
|
|
|
function to_xyzw(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return {x = v[1], y = v[2], z = v[3], w = v[4]}
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
|
2021-02-05 08:51:42 +01:00
|
|
|
--+ not necessarily required, as Minetest respects the metatable
|
2020-06-28 22:42:24 +02:00
|
|
|
function to_minetest(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return mt_vector.new(unpack(v))
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
|
2021-03-24 10:38:10 +01:00
|
|
|
function equals(v, w)
|
2021-03-27 20:10:49 +01:00
|
|
|
for k, v in pairs(v) do
|
|
|
|
if v ~= w[k] then return false end
|
|
|
|
end
|
|
|
|
return true
|
2020-12-22 09:41:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
metatable.__eq = equals
|
|
|
|
|
2021-03-24 10:38:10 +01:00
|
|
|
function less_than(v, w)
|
2021-03-27 20:10:49 +01:00
|
|
|
for k, v in pairs(v) do
|
|
|
|
if v >= w[k] then return false end
|
|
|
|
end
|
|
|
|
return true
|
2020-12-22 09:41:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
metatable.__lt = less_than
|
|
|
|
|
2021-03-24 10:38:10 +01:00
|
|
|
function less_or_equal(v, w)
|
2021-03-27 20:10:49 +01:00
|
|
|
for k, v in pairs(v) do
|
|
|
|
if v > w[k] then return false end
|
|
|
|
end
|
|
|
|
return true
|
2020-12-22 09:41:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
metatable.__le = less_or_equal
|
|
|
|
|
2021-03-24 10:38:10 +01:00
|
|
|
function combine(v, w, f)
|
2021-03-27 20:10:49 +01:00
|
|
|
local new_vector = {}
|
|
|
|
for key, value in pairs(v) do
|
|
|
|
new_vector[key] = f(value, w[key])
|
|
|
|
end
|
|
|
|
return new(new_vector)
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
|
2020-12-19 15:28:53 +01:00
|
|
|
function apply(v, f, ...)
|
2021-03-27 20:10:49 +01:00
|
|
|
local new_vector = {}
|
|
|
|
for key, value in pairs(v) do
|
|
|
|
new_vector[key] = f(value, ...)
|
|
|
|
end
|
|
|
|
return new(new_vector)
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function combinator(f)
|
2021-03-27 20:10:49 +01:00
|
|
|
return function(v, w)
|
|
|
|
return combine(v, w, f)
|
|
|
|
end, function(v, ...)
|
|
|
|
return apply(v, f, ...)
|
|
|
|
end
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
|
2020-12-27 23:22:28 +01:00
|
|
|
function invert(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
for key, value in pairs(v) do
|
|
|
|
v[key] = -value
|
|
|
|
end
|
2020-12-27 23:22:28 +01:00
|
|
|
end
|
|
|
|
|
2021-03-24 10:38:10 +01:00
|
|
|
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)
|
2020-06-28 22:42:24 +02:00
|
|
|
|
2020-12-22 09:41:59 +01:00
|
|
|
metatable.__add = add
|
2020-12-27 23:22:28 +01:00
|
|
|
metatable.__unm = invert
|
2020-12-22 09:41:59 +01:00
|
|
|
metatable.__sub = subtract
|
|
|
|
metatable.__mul = multiply
|
|
|
|
metatable.__div = divide
|
|
|
|
|
2021-02-02 15:43:55 +01:00
|
|
|
--+ linear interpolation
|
|
|
|
--: ratio number from 0 (all the first vector) to 1 (all the second vector)
|
2021-03-24 10:38:10 +01:00
|
|
|
function interpolate(v, w, ratio)
|
2021-03-27 20:10:49 +01:00
|
|
|
return add(multiply(v, 1 - ratio), multiply(w, ratio))
|
2021-02-02 15:43:55 +01:00
|
|
|
end
|
|
|
|
|
2020-09-22 19:14:21 +02:00
|
|
|
function norm(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
local sum = 0
|
|
|
|
for _, value in pairs(v) do
|
|
|
|
sum = sum + value ^ 2
|
|
|
|
end
|
|
|
|
return sum
|
2020-09-22 19:14:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function length(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return math.sqrt(norm(v))
|
2020-09-22 19:14:21 +02:00
|
|
|
end
|
|
|
|
|
2021-02-07 14:01:40 +01:00
|
|
|
-- Minor code duplication for the sake of performance
|
2021-03-24 10:38:10 +01:00
|
|
|
function distance(v, w)
|
2021-03-27 20:10:49 +01:00
|
|
|
local sum = 0
|
|
|
|
for key, value in pairs(v) do
|
|
|
|
sum = sum + (value - w[key]) ^ 2
|
|
|
|
end
|
|
|
|
return math.sqrt(sum)
|
2021-02-07 14:01:40 +01:00
|
|
|
end
|
|
|
|
|
2020-09-22 19:14:21 +02:00
|
|
|
function normalize(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return divide_scalar(v, length(v))
|
2020-12-19 15:28:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function floor(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return apply(v, math.floor)
|
2020-12-19 15:28:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function ceil(v)
|
2021-03-27 20:10:49 +01:00
|
|
|
return apply(v, math.ceil)
|
2020-12-19 15:28:53 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function clamp(v, min, max)
|
2021-03-27 20:10:49 +01:00
|
|
|
return apply(apply(v, math.max, min), math.min, max)
|
2020-12-21 19:48:37 +01:00
|
|
|
end
|
|
|
|
|
2021-03-24 10:38:10 +01:00
|
|
|
function cross3(v, w)
|
2021-03-27 20:10:49 +01:00
|
|
|
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]
|
|
|
|
}
|
2020-12-22 09:41:59 +01:00
|
|
|
end
|
|
|
|
|
2021-03-24 10:38:10 +01:00
|
|
|
function dot(v, w)
|
2021-03-27 20:10:49 +01:00
|
|
|
local sum = 0
|
|
|
|
for i, c in pairs(v) do
|
|
|
|
sum = sum + c * w[i]
|
|
|
|
end
|
|
|
|
return sum
|
2020-12-22 09:41:59 +01:00
|
|
|
end
|
|
|
|
|
2021-03-24 10:36:28 +01:00
|
|
|
--+ Angle between two vectors
|
|
|
|
--> Signed angle in radians
|
2021-03-24 10:38:10 +01:00
|
|
|
function angle(v, w)
|
2021-03-27 20:10:49 +01:00
|
|
|
-- Based on dot(v, w) = |v| * |w| * cos(x)
|
|
|
|
return math.acos(dot(v, w) / length(v) / length(w))
|
2021-03-24 10:36:28 +01:00
|
|
|
end
|
|
|
|
|
2020-12-21 19:48:37 +01:00
|
|
|
function box_box_collision(diff, box, other_box)
|
2021-03-27 20:10:49 +01:00
|
|
|
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
|
2020-12-21 19:59:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--+ Möller-Trumbore
|
|
|
|
function ray_triangle_intersection(origin, direction, triangle)
|
2021-03-27 20:10:49 +01:00
|
|
|
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 u + v > 1 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local pos_on_line = f * dot(edge_2, q)
|
|
|
|
if pos_on_line >= 0 then
|
|
|
|
return pos_on_line
|
|
|
|
end
|
2020-12-21 19:59:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function triangle_normal(triangle)
|
2021-03-27 20:10:49 +01:00
|
|
|
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))
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|