Determination of triangulation. Close #16

This commit is contained in:
Van
2026-01-16 18:52:40 +03:00
parent 6d3e9af2f8
commit c75e06d274
2 changed files with 80 additions and 47 deletions

View File

@@ -150,7 +150,7 @@ function Triangle:getCenter()
return (pos1 + pos2 + pos3) / 3 return (pos1 + pos2 + pos3) / 3
end end
---Returns the area of ​​a triangle. ---Returns the area of a triangle.
---@return number ---@return number
function Triangle:getArea() function Triangle:getArea()
local a, b, c = self:getSidesLength() local a, b, c = self:getSidesLength()
@@ -287,7 +287,7 @@ end
---Note: This function does not take into account the Y-coordinate, ---Note: This function does not take into account the Y-coordinate,
---that is, the calculation is performed on the projection of the triangle XZ. ---that is, the calculation is performed on the projection of the triangle XZ.
---@param p MapGen.Peak ---@param p MapGen.Peak
---@return MapGen.Triangle.CircumCircleEnum ---@return boolean
function Triangle:inCircumCircle2D(p) function Triangle:inCircumCircle2D(p)
local pos = p:getPeakPos() local pos = p:getPeakPos()
@@ -296,13 +296,7 @@ function Triangle:inCircumCircle2D(p)
local distance = (c - pos):length() local distance = (c - pos):length()
if distance - r < 1e-10 then return distance <= r
return 'ON'
elseif distance < r then
return 'IN'
else
return 'BEHIND'
end
end end
return Triangle return Triangle

View File

@@ -8,9 +8,10 @@
-- * splitting code into different files (classes); -- * splitting code into different files (classes);
-- * Adapting the code to the mapgen concept; -- * Adapting the code to the mapgen concept;
-- * Adding tetrahedralization; -- * Adding tetrahedralization;
-- * Fixed non-determinism by sorting vertices before processing;
local assert, mathMax, tableRemove local assert, mathMax, tableRemove, tableSort
= assert, math.max, table.remove = assert, math.max, table.remove, table.sort
local modInfo = Mod.getInfo('smc__core__map_gen') local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require local require = modInfo.require
@@ -44,6 +45,51 @@ local function findTrianglesNeighbours(triangles)
end end
end end
---Filter vertices by group and return a new table
---@param vertices table<integer, MapGen.Peak>
---@param groupKey string
---@return table<integer, MapGen.Peak>
local function filterVerticesByGroup(vertices, groupKey)
local result = {}
for i = 1, #vertices do
local vertex = vertices[i]
if vertex:getGroups()[groupKey] ~= nil then
result[#result + 1] = vertex
end
end
return result
end
---Sort vertices deterministically by position to ensure consistent triangulation
---@param vertices table<integer, MapGen.Peak>
local function sortVerticesDeterministically(vertices)
tableSort(vertices, function(a, b)
local posA = a.getPeakPos()
local posB = b.getPeakPos()
-- Sort by X first
if posA.x ~= posB.x then
return posA.x < posB.x
end
-- Then by Z
if posA.z ~= posB.z then
return posA.z < posB.z
end
-- Then by Y
if posA.y ~= posB.y then
return posA.y < posB.y
end
-- Fallback to unique identifier if positions are identical
-- This handles potential duplicate positions
local nameA = a.getName and a:getName() or ""
local nameB = b.getName and b:getName() or ""
return nameA < nameB
end)
end
---The Delaunay method divides a plane into triangles based on a list of their vertices. ---The Delaunay method divides a plane into triangles based on a list of their vertices.
--- ---
---Doesn't take into account the Y coordinate of the vertices. ---Doesn't take into account the Y coordinate of the vertices.
@@ -51,19 +97,16 @@ end
--- ---
---Vertices that do not have a `is2d` group will not participate in the triangulation. ---Vertices that do not have a `is2d` group will not participate in the triangulation.
---@param vertices table<integer, MapGen.Peak> ---@param vertices table<integer, MapGen.Peak>
function Triangulation.triangulate( vertices ) function Triangulation.triangulate(vertices)
vertices = table.copy_with_metatables(vertices) -- Filter vertices with is2d group (creates a new table)
vertices = filterVerticesByGroup(vertices, "is2d")
-- Only peaks with a `is2d` group can be triangulated. -- Sort deterministically to ensure consistent results across sessions
for i, vertex in ipairs(vertices) do sortVerticesDeterministically(vertices)
if vertex:getGroups().is2d == nil then
table.remove(vertices, i)
end
end
local nvertices = #vertices local nvertices = #vertices
assert( nvertices > 2, "Cannot triangulate, needs more than 3 vertices" ) assert(nvertices > 2, "Cannot triangulate, needs more than 3 vertices")
if nvertices == 3 then if nvertices == 3 then
return {Triangle:new(vertices[1], vertices[2], vertices[3])} return {Triangle:new(vertices[1], vertices[2], vertices[3])}
@@ -95,21 +138,21 @@ function Triangulation.triangulate( vertices )
end end
end end
---Finding the vertices of a supertriangle -- Finding the vertices of a supertriangle
local convex_mult = Triangulation.convexMultiplier local convex_mult = Triangulation.convexMultiplier
local dx, dy = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult local dx, dy = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult
local deltaMax = mathMax(dx, dy) local deltaMax = mathMax(dx, dy)
local midx, midy = (minX + maxX) * 0.5, (minY + maxY) * 0.5 local midx, midy = (minX + maxX) * 0.5, (minY + maxY) * 0.5
local p1 = FakePeak:new(vector.new( midx - 2 * deltaMax,0, midy - deltaMax )) local p1 = FakePeak:new(vector.new(midx - 2 * deltaMax, 0, midy - deltaMax))
local p2 = FakePeak:new(vector.new(midx,0, midy + 2 * deltaMax )) local p2 = FakePeak:new(vector.new(midx, 0, midy + 2 * deltaMax))
local p3 = FakePeak:new( vector.new(midx + 2 * deltaMax,0, midy - deltaMax )) local p3 = FakePeak:new(vector.new(midx + 2 * deltaMax, 0, midy - deltaMax))
p1.id, p2.id, p3.id = nvertices + 1, nvertices + 2, nvertices + 3 p1.id, p2.id, p3.id = nvertices + 1, nvertices + 2, nvertices + 3
vertices[p1.id], vertices[p2.id], vertices[p3.id] = p1, p2, p3 vertices[p1.id], vertices[p2.id], vertices[p3.id] = p1, p2, p3
---@type MapGen.Triangle[] ---@type MapGen.Triangle[]
local triangles = {Triangle:new( vertices[nvertices + 1], vertices[nvertices + 2], vertices[nvertices + 3] )} local triangles = {Triangle:new(vertices[nvertices + 1], vertices[nvertices + 2], vertices[nvertices + 3])}
for i = 1, nvertices do for i = 1, nvertices do
---@type MapGen.Triangulation.Edge[] ---@type MapGen.Triangulation.Edge[]
@@ -119,11 +162,11 @@ function Triangulation.triangulate( vertices )
for j = #triangles, 1, -1 do for j = #triangles, 1, -1 do
local curTriangle = triangles[j] local curTriangle = triangles[j]
if curTriangle:inCircumCircle2D(vertices[i]) == 'IN' then if curTriangle:inCircumCircle2D(vertices[i]) then
edges[#edges + 1] = curTriangle.e1 edges[#edges + 1] = curTriangle.e1
edges[#edges + 1] = curTriangle.e2 edges[#edges + 1] = curTriangle.e2
edges[#edges + 1] = curTriangle.e3 edges[#edges + 1] = curTriangle.e3
tableRemove( triangles, j ) tableRemove(triangles, j)
end end
end end
@@ -131,8 +174,8 @@ function Triangulation.triangulate( vertices )
for j = #edges - 1, 1, -1 do for j = #edges - 1, 1, -1 do
for k = #edges, j + 1, -1 do for k = #edges, j + 1, -1 do
if edges[j] and edges[k] and edges[j]:same(edges[k]) then if edges[j] and edges[k] and edges[j]:same(edges[k]) then
tableRemove( edges, j ) tableRemove(edges, j)
tableRemove( edges, k-1 ) tableRemove(edges, k - 1)
end end
end end
end end
@@ -143,17 +186,17 @@ function Triangulation.triangulate( vertices )
end end
end end
-- Remove tetrahedra associated with the vertices of a supertriangle. -- Remove triangles associated with the vertices of a supertriangle.
for i = #triangles, 1, -1 do for i = #triangles, 1, -1 do
local triangle = triangles[i] local triangle = triangles[i]
if triangle.p1.id > nvertices or triangle.p2.id > nvertices or triangle.p3.id > nvertices then if triangle.p1.id > nvertices or triangle.p2.id > nvertices or triangle.p3.id > nvertices then
tableRemove( triangles, i ) tableRemove(triangles, i)
end end
end end
-- Removing the vertices of a super supertriangle. -- Removing the vertices of a supertriangle.
for _ = 1,3 do for _ = 1, 3 do
tableRemove( vertices ) tableRemove(vertices)
end end
findTrianglesNeighbours(triangles) findTrianglesNeighbours(triangles)
@@ -166,14 +209,11 @@ end
---Vertices that do not have a `is3d` group will not participate in the triangulation. ---Vertices that do not have a `is3d` group will not participate in the triangulation.
---@param vertices table<integer, MapGen.Peak> ---@param vertices table<integer, MapGen.Peak>
function Triangulation.tetrahedralize(vertices) function Triangulation.tetrahedralize(vertices)
vertices = table.copy_with_metatables(vertices) -- Filter vertices with is3d group (creates a new table)
vertices = filterVerticesByGroup(vertices, "is3d")
-- Only peaks with a `is3d` group can be triangulated. -- Sort deterministically to ensure consistent results across sessions
for i, vertex in ipairs(vertices) do sortVerticesDeterministically(vertices)
if vertex:getGroups().is3d == nil then
table.remove(vertices, i)
end
end
local nvertices = #vertices local nvertices = #vertices
@@ -255,8 +295,8 @@ function Triangulation.tetrahedralize(vertices)
for j = #faces - 1, 1, -1 do for j = #faces - 1, 1, -1 do
for k = #faces, j + 1, -1 do for k = #faces, j + 1, -1 do
if faces[j] and faces[k] and faces[j]:same(faces[k]) then if faces[j] and faces[k] and faces[j]:same(faces[k]) then
tableRemove( faces, j ) tableRemove(faces, j)
tableRemove( faces, k-1 ) tableRemove(faces, k - 1)
end end
end end
end end
@@ -265,20 +305,19 @@ function Triangulation.tetrahedralize(vertices)
for j = 1, #faces do for j = 1, #faces do
tetrahedrons[#tetrahedrons + 1] = Tetrahedron:new(faces[j].p1, faces[j].p2, faces[j].p3, vertices[i]) tetrahedrons[#tetrahedrons + 1] = Tetrahedron:new(faces[j].p1, faces[j].p2, faces[j].p3, vertices[i])
end end
end end
-- Remove tetrahedra associated with the vertices of a supertetrahedron. -- Remove tetrahedra associated with the vertices of a supertetrahedron.
for i = #tetrahedrons, 1, -1 do for i = #tetrahedrons, 1, -1 do
local tetrahedron = tetrahedrons[i] local tetrahedron = tetrahedrons[i]
if tetrahedron.p1.id > nvertices or tetrahedron.p2.id > nvertices or tetrahedron.p3.id > nvertices or tetrahedron.p4.id > nvertices then if tetrahedron.p1.id > nvertices or tetrahedron.p2.id > nvertices or tetrahedron.p3.id > nvertices or tetrahedron.p4.id > nvertices then
tableRemove( tetrahedrons, i ) tableRemove(tetrahedrons, i)
end end
end end
-- Removing the vertices of a supertetrahedron. -- Removing the vertices of a supertetrahedron.
for i = 1, 4 do for _ = 1, 4 do
tableRemove( vertices ) tableRemove(vertices)
end end
return tetrahedrons return tetrahedrons