mirror of
https://github.com/HybridDog/vector_extras.git
synced 2024-11-23 07:53:47 +01:00
Remove unused vector.triangle code and try to make it a bit faster
This commit is contained in:
parent
f7dbb1a884
commit
275ec4af3b
66
init.lua
66
init.lua
@ -1023,63 +1023,18 @@ function funcs.triangle(pos1, pos2, pos3)
|
|||||||
z = {"y", "x"},
|
z = {"y", "x"},
|
||||||
}
|
}
|
||||||
local other_dirs = all_other_dirs[dir]
|
local other_dirs = all_other_dirs[dir]
|
||||||
-- Sort the positions along the other directions
|
|
||||||
--[[
|
|
||||||
local sorteds = {}
|
|
||||||
for i = 1,2 do
|
|
||||||
local odir = other_dirs[i]
|
|
||||||
local ps = {}
|
|
||||||
if pos1[odir] < pos2[odir] then
|
|
||||||
if pos1[odir] < pos3[odir] then
|
|
||||||
ps[1] = pos1
|
|
||||||
if pos2[odir] < pos3[odir] then
|
|
||||||
ps[2] = pos2
|
|
||||||
ps[3] = pos3
|
|
||||||
else
|
|
||||||
ps[3] = pos2
|
|
||||||
ps[2] = pos3
|
|
||||||
end
|
|
||||||
else
|
|
||||||
ps[1] = pos3
|
|
||||||
ps[2] = pos1
|
|
||||||
ps[3] = pos2
|
|
||||||
end
|
|
||||||
elseif pos1[odir] < pos3[odir] then
|
|
||||||
ps[1] = pos2
|
|
||||||
ps[2] = pos1
|
|
||||||
ps[3] = pos3
|
|
||||||
else
|
|
||||||
ps[3] = pos1
|
|
||||||
if pos2[odir] < pos3[odir] then
|
|
||||||
ps[1] = pos2
|
|
||||||
ps[2] = pos3
|
|
||||||
else
|
|
||||||
ps[1] = pos3
|
|
||||||
ps[2] = pos2
|
|
||||||
end
|
|
||||||
end
|
|
||||||
sorteds[i] = ps
|
|
||||||
end
|
|
||||||
--~ p1[i] = sorteds[odir][1][odir]
|
|
||||||
--~ p2[i] = sorteds[odir][3][odir]
|
|
||||||
--]]
|
|
||||||
-- The boundaries of the 2D AABB along other_dirs
|
|
||||||
local odir1, odir2 = other_dirs[1], other_dirs[2]
|
local odir1, odir2 = other_dirs[1], other_dirs[2]
|
||||||
|
|
||||||
local pos1_2d = {pos1[odir1], pos1[odir2]}
|
local pos1_2d = {pos1[odir1], pos1[odir2]}
|
||||||
local pos2_2d = {pos2[odir1], pos2[odir2]}
|
local pos2_2d = {pos2[odir1], pos2[odir2]}
|
||||||
local pos3_2d = {pos3[odir1], pos3[odir2]}
|
local pos3_2d = {pos3[odir1], pos3[odir2]}
|
||||||
|
-- The boundaries of the 2D AABB along other_dirs
|
||||||
local p1 = {}
|
local p1 = {}
|
||||||
local p2 = {}
|
local p2 = {}
|
||||||
for i = 1,2 do
|
for i = 1,2 do
|
||||||
p1[i] = math.floor(math.min(pos1_2d[i], pos2_2d[i], pos3_2d[i]))
|
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]))
|
p2[i] = math.ceil(math.max(pos1_2d[i], pos2_2d[i], pos3_2d[i]))
|
||||||
end
|
end
|
||||||
-- The lines along the triangle boundaries
|
|
||||||
--~ local lines = {}
|
|
||||||
--~ local ps2 = sorteds[2]
|
|
||||||
--~ local s = {ps2[1][other_dirs[1]], ps2[1][other_dirs[2]]}
|
|
||||||
--~ local e = {ps2[2][other_dirs[1]], ps2[2][other_dirs[2]]}
|
|
||||||
--~ local line_left = {s[1], s[2], e[1] - s[1], e[2] - s[2]}
|
|
||||||
|
|
||||||
-- https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/rasterization-stage
|
-- https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/rasterization-stage
|
||||||
local function edgefunc(p1, p2, pos)
|
local function edgefunc(p1, p2, pos)
|
||||||
@ -1087,25 +1042,30 @@ function funcs.triangle(pos1, pos2, pos3)
|
|||||||
- (pos[2] - p1[2]) * (p2[1] - p1[1])
|
- (pos[2] - p1[2]) * (p2[1] - p1[1])
|
||||||
end
|
end
|
||||||
local a_all_inv = 1.0 / edgefunc(pos1_2d, pos2_2d, pos3_2d)
|
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
|
-- Calculate the triangle points
|
||||||
local points = {}
|
local points = {}
|
||||||
local barycentric_coords = {}
|
local barycentric_coords = {}
|
||||||
local n = 0
|
local n = 0
|
||||||
|
-- It is possible to further optimize this
|
||||||
for v1 = p1[1], p2[1] do
|
for v1 = p1[1], p2[1] do
|
||||||
for v2 = p1[2], p2[2] do
|
local p = {v1, p1[2]}
|
||||||
-- Not optimized
|
local k3 = edgefunc(pos1_2d, pos2_2d, p) * a_all_inv
|
||||||
local p = {v1, v2}
|
local k1 = edgefunc(pos2_2d, pos3_2d, p) * a_all_inv
|
||||||
local k3 = edgefunc(pos1_2d, pos2_2d, p) * a_all_inv
|
for _ = p1[2], p2[2] do
|
||||||
local k1 = edgefunc(pos2_2d, pos3_2d, p) * a_all_inv
|
|
||||||
local k2 = 1 - k1 - k3
|
local k2 = 1 - k1 - k3
|
||||||
if k1 >= 0 and k2 >= 0 and k3 >= 0 then
|
if k1 >= 0 and k2 >= 0 and k3 >= 0 then
|
||||||
-- On triangle
|
-- On triangle
|
||||||
local h = math.floor(k1 * pos1[dir] + k2 * pos2[dir] +
|
local h = math.floor(k1 * pos1[dir] + k2 * pos2[dir] +
|
||||||
k3 * pos3[dir] + 0.5)
|
k3 * pos3[dir] + 0.5)
|
||||||
n = n+1
|
n = n+1
|
||||||
points[n] = {[odir1] = v1, [odir2] = v2, [dir] = h}
|
points[n] = {[odir1] = v1, [odir2] = p[2], [dir] = h}
|
||||||
barycentric_coords[n] = {k1, k2, k3}
|
barycentric_coords[n] = {k1, k2, k3}
|
||||||
end
|
end
|
||||||
|
p[2] = p[2]+1
|
||||||
|
k3 = k3 + step_k3
|
||||||
|
k1 = k1 + step_k1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return points, n, barycentric_coords
|
return points, n, barycentric_coords
|
||||||
|
Loading…
Reference in New Issue
Block a user