mirror of
https://github.com/HybridDog/vector_extras.git
synced 2025-06-30 15:20:22 +02:00
Merge remote-tracking branch 'upstream/master' into nalc-1.2-dev
This commit is contained in:
83
init.lua
83
init.lua
@ -6,7 +6,8 @@ function funcs.pos_to_string(pos)
|
||||
return "("..pos.x.."|"..pos.y.."|"..pos.z..")"
|
||||
end
|
||||
|
||||
local r_corr = 0.25 --remove a bit more nodes (if shooting diagonal) to let it look like a hole (sth like antialiasing)
|
||||
local r_corr = 0.25 --remove a bit more nodes (if shooting diagonal) to let it
|
||||
-- look like a hole (sth like antialiasing)
|
||||
|
||||
-- this doesn't need to be calculated every time
|
||||
local f_1 = 0.5-r_corr
|
||||
@ -255,18 +256,6 @@ function funcs.sort_positions(ps, preferred_coords)
|
||||
table.sort(ps, ps_sorting)
|
||||
end
|
||||
|
||||
function funcs.scalar(v1, v2)
|
||||
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z
|
||||
end
|
||||
|
||||
function funcs.cross(v1, v2)
|
||||
return {
|
||||
x = v1.y*v2.z - v1.z*v2.y,
|
||||
y = v1.z*v2.x - v1.x*v2.z,
|
||||
z = v1.x*v2.y - v1.y*v2.x
|
||||
}
|
||||
end
|
||||
|
||||
-- Tschebyschew norm
|
||||
function funcs.maxnorm(v)
|
||||
return math.max(math.max(math.abs(v.x), math.abs(v.y)), math.abs(v.z))
|
||||
@ -1018,6 +1007,74 @@ function funcs.serialize(vec)
|
||||
return "{x=" .. vec.x .. ",y=" .. vec.y .. ",z=" .. vec.z .. "}"
|
||||
end
|
||||
|
||||
function funcs.triangle(pos1, pos2, pos3)
|
||||
local normal = vector.cross(vector.subtract(pos2, pos1),
|
||||
vector.subtract(pos3, pos1))
|
||||
-- Find the biggest absolute component of the normal vector
|
||||
local dir = vector.get_max_coord({
|
||||
x = math.abs(normal.x),
|
||||
y = math.abs(normal.y),
|
||||
z = math.abs(normal.z),
|
||||
})
|
||||
-- Find the other directions for the for loops
|
||||
local all_other_dirs = {
|
||||
x = {"z", "y"},
|
||||
y = {"z", "x"},
|
||||
z = {"y", "x"},
|
||||
}
|
||||
local other_dirs = all_other_dirs[dir]
|
||||
local odir1, odir2 = other_dirs[1], other_dirs[2]
|
||||
|
||||
local pos1_2d = {pos1[odir1], pos1[odir2]}
|
||||
local pos2_2d = {pos2[odir1], pos2[odir2]}
|
||||
local pos3_2d = {pos3[odir1], pos3[odir2]}
|
||||
-- The boundaries of the 2D AABB along other_dirs
|
||||
local p1 = {}
|
||||
local p2 = {}
|
||||
for i = 1,2 do
|
||||
p1[i] = math.floor(math.min(pos1_2d[i], pos2_2d[i], pos3_2d[i]))
|
||||
p2[i] = math.ceil(math.max(pos1_2d[i], pos2_2d[i], pos3_2d[i]))
|
||||
end
|
||||
|
||||
-- https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/rasterization-stage
|
||||
local function edgefunc(p1, p2, pos)
|
||||
return (pos[1] - p1[1]) * (p2[2] - p1[2])
|
||||
- (pos[2] - p1[2]) * (p2[1] - p1[1])
|
||||
end
|
||||
-- eps is used to prevend holes in neighbouring triangles
|
||||
-- It should be smaller than the smallest possible barycentric value
|
||||
-- FIXME: I'm not sure if it really does what it should.
|
||||
local eps = 0.5 / math.max(p2[1] - p1[1], p2[2] - p1[2])
|
||||
local a_all_inv = 1.0 / edgefunc(pos1_2d, pos2_2d, pos3_2d)
|
||||
local step_k3 = - (pos2_2d[1] - pos1_2d[1]) * a_all_inv
|
||||
local step_k1 = - (pos3_2d[1] - pos2_2d[1]) * a_all_inv
|
||||
-- Calculate the triangle points
|
||||
local points = {}
|
||||
local barycentric_coords = {}
|
||||
local n = 0
|
||||
-- It is possible to further optimize this
|
||||
for v1 = p1[1], p2[1] do
|
||||
local p = {v1, p1[2]}
|
||||
local k3 = edgefunc(pos1_2d, pos2_2d, p) * a_all_inv
|
||||
local k1 = edgefunc(pos2_2d, pos3_2d, p) * a_all_inv
|
||||
for _ = p1[2], p2[2] do
|
||||
local k2 = 1 - k1 - k3
|
||||
if k1 >= -eps and k2 >= -eps and k3 >= -eps then
|
||||
-- On triangle
|
||||
local h = math.floor(k1 * pos1[dir] + k2 * pos2[dir] +
|
||||
k3 * pos3[dir] + 0.5)
|
||||
n = n+1
|
||||
points[n] = {[odir1] = v1, [odir2] = p[2], [dir] = h}
|
||||
barycentric_coords[n] = {k1, k2, k3}
|
||||
end
|
||||
p[2] = p[2]+1
|
||||
k3 = k3 + step_k3
|
||||
k1 = k1 + step_k1
|
||||
end
|
||||
end
|
||||
return points, n, barycentric_coords
|
||||
end
|
||||
|
||||
|
||||
vector_extras_functions = funcs
|
||||
|
||||
|
Reference in New Issue
Block a user