From c75e06d274d5746bdd48ae02a21e815767c5571e Mon Sep 17 00:00:00 2001 From: Vanic Date: Fri, 16 Jan 2026 18:52:40 +0300 Subject: [PATCH] Determination of triangulation. Close #16 --- mods/CORE/MapGen/src/MapGen/Triangle.lua | 12 +- mods/CORE/MapGen/src/MapGen/Triangulation.lua | 115 ++++++++++++------ 2 files changed, 80 insertions(+), 47 deletions(-) diff --git a/mods/CORE/MapGen/src/MapGen/Triangle.lua b/mods/CORE/MapGen/src/MapGen/Triangle.lua index 02ceda8..5386a14 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangle.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangle.lua @@ -150,7 +150,7 @@ function Triangle:getCenter() return (pos1 + pos2 + pos3) / 3 end ----Returns the area of ​​a triangle. +---Returns the area of a triangle. ---@return number function Triangle:getArea() local a, b, c = self:getSidesLength() @@ -287,7 +287,7 @@ end ---Note: This function does not take into account the Y-coordinate, ---that is, the calculation is performed on the projection of the triangle XZ. ---@param p MapGen.Peak ----@return MapGen.Triangle.CircumCircleEnum +---@return boolean function Triangle:inCircumCircle2D(p) local pos = p:getPeakPos() @@ -296,13 +296,7 @@ function Triangle:inCircumCircle2D(p) local distance = (c - pos):length() - if distance - r < 1e-10 then - return 'ON' - elseif distance < r then - return 'IN' - else - return 'BEHIND' - end + return distance <= r end return Triangle diff --git a/mods/CORE/MapGen/src/MapGen/Triangulation.lua b/mods/CORE/MapGen/src/MapGen/Triangulation.lua index 7c05483..d6321aa 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangulation.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangulation.lua @@ -8,9 +8,10 @@ -- * splitting code into different files (classes); -- * Adapting the code to the mapgen concept; -- * Adding tetrahedralization; +-- * Fixed non-determinism by sorting vertices before processing; -local assert, mathMax, tableRemove - = assert, math.max, table.remove +local assert, mathMax, tableRemove, tableSort + = assert, math.max, table.remove, table.sort local modInfo = Mod.getInfo('smc__core__map_gen') local require = modInfo.require @@ -44,6 +45,51 @@ local function findTrianglesNeighbours(triangles) end end +---Filter vertices by group and return a new table +---@param vertices table +---@param groupKey string +---@return table +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 +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. --- ---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. ---@param vertices table -function Triangulation.triangulate( vertices ) - vertices = table.copy_with_metatables(vertices) +function Triangulation.triangulate(vertices) + -- Filter vertices with is2d group (creates a new table) + vertices = filterVerticesByGroup(vertices, "is2d") - -- Only peaks with a `is2d` group can be triangulated. - for i, vertex in ipairs(vertices) do - if vertex:getGroups().is2d == nil then - table.remove(vertices, i) - end - end + -- Sort deterministically to ensure consistent results across sessions + sortVerticesDeterministically(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 return {Triangle:new(vertices[1], vertices[2], vertices[3])} @@ -95,21 +138,21 @@ function Triangulation.triangulate( vertices ) end end - ---Finding the vertices of a supertriangle + -- Finding the vertices of a supertriangle local convex_mult = Triangulation.convexMultiplier local dx, dy = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult local deltaMax = mathMax(dx, dy) local midx, midy = (minX + maxX) * 0.5, (minY + maxY) * 0.5 - local p1 = FakePeak:new(vector.new( midx - 2 * deltaMax,0, midy - 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 p1 = FakePeak:new(vector.new(midx - 2 * deltaMax, 0, midy - deltaMax)) + local p2 = FakePeak:new(vector.new(midx, 0, midy + 2 * 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 vertices[p1.id], vertices[p2.id], vertices[p3.id] = p1, p2, p3 ---@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 ---@type MapGen.Triangulation.Edge[] @@ -119,11 +162,11 @@ function Triangulation.triangulate( vertices ) for j = #triangles, 1, -1 do 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.e2 edges[#edges + 1] = curTriangle.e3 - tableRemove( triangles, j ) + tableRemove(triangles, j) end end @@ -131,8 +174,8 @@ function Triangulation.triangulate( vertices ) for j = #edges - 1, 1, -1 do for k = #edges, j + 1, -1 do if edges[j] and edges[k] and edges[j]:same(edges[k]) then - tableRemove( edges, j ) - tableRemove( edges, k-1 ) + tableRemove(edges, j) + tableRemove(edges, k - 1) end end end @@ -143,17 +186,17 @@ function Triangulation.triangulate( vertices ) 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 local triangle = triangles[i] if triangle.p1.id > nvertices or triangle.p2.id > nvertices or triangle.p3.id > nvertices then - tableRemove( triangles, i ) + tableRemove(triangles, i) end end - -- Removing the vertices of a super supertriangle. - for _ = 1,3 do - tableRemove( vertices ) + -- Removing the vertices of a supertriangle. + for _ = 1, 3 do + tableRemove(vertices) end findTrianglesNeighbours(triangles) @@ -166,14 +209,11 @@ end ---Vertices that do not have a `is3d` group will not participate in the triangulation. ---@param vertices table 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. - for i, vertex in ipairs(vertices) do - if vertex:getGroups().is3d == nil then - table.remove(vertices, i) - end - end + -- Sort deterministically to ensure consistent results across sessions + sortVerticesDeterministically(vertices) local nvertices = #vertices @@ -255,8 +295,8 @@ function Triangulation.tetrahedralize(vertices) for j = #faces - 1, 1, -1 do for k = #faces, j + 1, -1 do if faces[j] and faces[k] and faces[j]:same(faces[k]) then - tableRemove( faces, j ) - tableRemove( faces, k-1 ) + tableRemove(faces, j) + tableRemove(faces, k - 1) end end end @@ -265,20 +305,19 @@ function Triangulation.tetrahedralize(vertices) for j = 1, #faces do tetrahedrons[#tetrahedrons + 1] = Tetrahedron:new(faces[j].p1, faces[j].p2, faces[j].p3, vertices[i]) end - end -- Remove tetrahedra associated with the vertices of a supertetrahedron. for i = #tetrahedrons, 1, -1 do 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 - tableRemove( tetrahedrons, i ) + tableRemove(tetrahedrons, i) end end -- Removing the vertices of a supertetrahedron. - for i = 1, 4 do - tableRemove( vertices ) + for _ = 1, 4 do + tableRemove(vertices) end return tetrahedrons