2020-06-28 22:42:24 +02:00
|
|
|
local mt_vector = vector
|
|
|
|
local vector = getfenv(1)
|
|
|
|
|
|
|
|
function new(v)
|
|
|
|
return setmetatable(v, vector)
|
|
|
|
end
|
|
|
|
|
|
|
|
function from_xyzw(v)
|
|
|
|
return new{v.x, v.y, v.z, v.w}
|
|
|
|
end
|
|
|
|
|
2020-12-19 15:28:53 +01:00
|
|
|
function from_minetest(v)
|
|
|
|
return new{v.x, v.y, v.z}
|
|
|
|
end
|
|
|
|
|
2020-06-28 22:42:24 +02:00
|
|
|
function to_xyzw(v)
|
|
|
|
return {x = v[1], y = v[2], z = v[3], w = v[4]}
|
|
|
|
end
|
|
|
|
|
|
|
|
function to_minetest(v)
|
|
|
|
return mt_vector.new(unpack(v))
|
|
|
|
end
|
|
|
|
|
|
|
|
function combine(v1, v2, f)
|
2020-09-22 19:14:21 +02:00
|
|
|
local new_vector = {}
|
|
|
|
for key, value in pairs(v1) do
|
|
|
|
new_vector[key] = f(value, v2[key])
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
2020-09-22 19:14:21 +02:00
|
|
|
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, ...)
|
2020-09-22 19:14:21 +02:00
|
|
|
local new_vector = {}
|
|
|
|
for key, value in pairs(v) do
|
2020-12-19 15:28:53 +01:00
|
|
|
new_vector[key] = f(value, ...)
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
2020-09-22 19:14:21 +02:00
|
|
|
return new(new_vector)
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function combinator(f)
|
|
|
|
return function(v1, v2)
|
|
|
|
return combine(v1, v2, f)
|
2020-12-19 15:28:53 +01:00
|
|
|
end, function(v, ...)
|
|
|
|
return apply(v, f, ...)
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add, add_scalar = combinator(function(a, b) return a + b end)
|
|
|
|
subtract, subtract_scalar = combinator(function(a, b) return a - b end)
|
|
|
|
multiply, multiply_scalar = combinator(function(a, b) return a * b end)
|
|
|
|
divide, divide_scalar = combinator(function(a, b) return a / b end)
|
|
|
|
|
2020-09-22 19:14:21 +02:00
|
|
|
function norm(v)
|
2020-06-28 22:42:24 +02:00
|
|
|
local sum = 0
|
|
|
|
for _, c in pairs(v) do
|
|
|
|
sum = sum + c*c
|
|
|
|
end
|
2020-09-22 19:14:21 +02:00
|
|
|
return sum
|
|
|
|
end
|
|
|
|
|
|
|
|
function length(v)
|
|
|
|
return math.sqrt(norm(v))
|
|
|
|
end
|
|
|
|
|
|
|
|
function normalize(v)
|
|
|
|
return divide_scalar(v, length(v))
|
2020-12-19 15:28:53 +01:00
|
|
|
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)
|
2020-06-28 22:42:24 +02:00
|
|
|
end
|