Determination of triangulation. Close #16
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<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.
|
||||
---
|
||||
---Doesn't take into account the Y coordinate of the vertices.
|
||||
@@ -52,14 +98,11 @@ end
|
||||
---Vertices that do not have a `is2d` group will not participate in the triangulation.
|
||||
---@param vertices table<integer, MapGen.Peak>
|
||||
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.
|
||||
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
|
||||
|
||||
@@ -95,7 +138,7 @@ 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)
|
||||
@@ -119,7 +162,7 @@ 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
|
||||
@@ -143,7 +186,7 @@ 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
|
||||
@@ -151,7 +194,7 @@ function Triangulation.triangulate( vertices )
|
||||
end
|
||||
end
|
||||
|
||||
-- Removing the vertices of a super supertriangle.
|
||||
-- Removing the vertices of a supertriangle.
|
||||
for _ = 1, 3 do
|
||||
tableRemove(vertices)
|
||||
end
|
||||
@@ -166,14 +209,11 @@ end
|
||||
---Vertices that do not have a `is3d` group will not participate in the triangulation.
|
||||
---@param vertices table<integer, MapGen.Peak>
|
||||
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
|
||||
|
||||
@@ -265,7 +305,6 @@ 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.
|
||||
@@ -277,7 +316,7 @@ function Triangulation.tetrahedralize(vertices)
|
||||
end
|
||||
|
||||
-- Removing the vertices of a supertetrahedron.
|
||||
for i = 1, 4 do
|
||||
for _ = 1, 4 do
|
||||
tableRemove(vertices)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user