MapGen: removing unnecessary code. Completion of work on a new triangulation method. Close #25
This commit is contained in:
@@ -3,6 +3,17 @@ local mathAbs, mathRound, mathMin, mathMax, id
|
|||||||
|
|
||||||
local getInstance = Class.getInstance
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
local LAST_PROFILE = 0.0
|
||||||
|
local PROFILE = true
|
||||||
|
local function profile(text)
|
||||||
|
if not PROFILE then return end
|
||||||
|
|
||||||
|
local delta = LAST_PROFILE - core.get_us_time()
|
||||||
|
LAST_PROFILE = core.get_us_time()
|
||||||
|
|
||||||
|
core.log(text..': '..tostring(delta))
|
||||||
|
end
|
||||||
|
|
||||||
local modInfo = Mod.getInfo('smc__core__map_gen')
|
local modInfo = Mod.getInfo('smc__core__map_gen')
|
||||||
local require = modInfo.require
|
local require = modInfo.require
|
||||||
|
|
||||||
@@ -26,9 +37,6 @@ local StaticLayer = require('MapGen.StaticLayer')
|
|||||||
---@type MapGen.Biome
|
---@type MapGen.Biome
|
||||||
local Biome = require('MapGen.Biome')
|
local Biome = require('MapGen.Biome')
|
||||||
|
|
||||||
---@type MapGen.Triangulation
|
|
||||||
local Triangulation = require('MapGen.Triangulation')
|
|
||||||
|
|
||||||
---@type MapGen.Cavern
|
---@type MapGen.Cavern
|
||||||
local Cavern = require('MapGen.Cavern')
|
local Cavern = require('MapGen.Cavern')
|
||||||
|
|
||||||
@@ -415,22 +423,6 @@ function MapGen:registerStaticLayer(name, def)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Adds groups to the already registered groups in the peak by coordinate.
|
|
||||||
---If the peak does not exist, an error will be raised.
|
|
||||||
---@param layerName string The name of the layer in which the new peak will be included.
|
|
||||||
---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
|
|
||||||
---@param pos MapPosition The position of the peak to which the group should be added.
|
|
||||||
---@param groups table<string, number> Groups to be added.
|
|
||||||
function MapGen:addGroupsToPeakByPos(layerName, category, pos, groups)
|
|
||||||
local layer = self:getLayerByName(layerName)
|
|
||||||
|
|
||||||
local peak = self:getPeakByPos(layer, category, pos)
|
|
||||||
assert( peak ~= nil, ('In the `%s` layer in the `%s` category there is no peak with coordinates: %s, %s, %s.'):format(
|
|
||||||
layerName, category, pos.x, pos.y, pos.z))
|
|
||||||
|
|
||||||
peak:addGroups(groups)
|
|
||||||
end
|
|
||||||
|
|
||||||
---Mark a point on the biomes diagram and constrain the world area by height to mark it as `MapGen.Biome`.
|
---Mark a point on the biomes diagram and constrain the world area by height to mark it as `MapGen.Biome`.
|
||||||
---@param layerName string The name of the layer in which the new biome will be included.
|
---@param layerName string The name of the layer in which the new biome will be included.
|
||||||
---@param biomeName string The name to be assigned to the biome. The name must be unique for each biome.
|
---@param biomeName string The name to be assigned to the biome. The name must be unique for each biome.
|
||||||
@@ -817,96 +809,6 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
|
|||||||
--core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z)))
|
--core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z)))
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[
|
|
||||||
3D noise smoothing is too slow, so it was decided to stop using it.
|
|
||||||
|
|
||||||
---@param P vector
|
|
||||||
---@param A vector
|
|
||||||
---@param B vector
|
|
||||||
---@param C vector
|
|
||||||
local function pointInTetrahedron(P, A, B, C, D)
|
|
||||||
local function det3(a, b, c)
|
|
||||||
return a.x * (b.y * c.z - b.z * c.y)
|
|
||||||
- a.y * (b.x * c.z - b.z * c.x)
|
|
||||||
+ a.z * (b.x * c.y - b.y * c.x)
|
|
||||||
end
|
|
||||||
|
|
||||||
local v0 = B - A
|
|
||||||
local v1 = C - A
|
|
||||||
local v2 = D - A
|
|
||||||
local v3 = P - A
|
|
||||||
|
|
||||||
local d = det3(v0, v1, v2)
|
|
||||||
|
|
||||||
local u = det3(v3, v1, v2) / d
|
|
||||||
local v = det3(v0, v3, v2) / d
|
|
||||||
local w = det3(v0, v1, v3) / d
|
|
||||||
|
|
||||||
return (u >= 0 and v >= 0 and w >= 0 and (u + v + w) <= 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
---Calculates a smoothed 3D noise value based on a preliminary space partitioning.
|
|
||||||
---
|
|
||||||
---Returns `nil` if the point is not included in any tetrahedron.
|
|
||||||
---
|
|
||||||
---@param layer MapGen.Layer
|
|
||||||
---@param x number
|
|
||||||
---@param y number
|
|
||||||
---@param z number
|
|
||||||
---@return number?
|
|
||||||
function MapGen:getNoises3dValues(layer, x, y, z)
|
|
||||||
---@param triangle MapGen.Tetrahedron
|
|
||||||
for _, tetrahedron in ipairs(layer.tetrahedronsList) do
|
|
||||||
local peak1 = tetrahedron.p1
|
|
||||||
local peak2 = tetrahedron.p2
|
|
||||||
local peak3 = tetrahedron.p3
|
|
||||||
local peak4 = tetrahedron.p4
|
|
||||||
|
|
||||||
local pos1 = peak1:getPeakPos()
|
|
||||||
local pos2 = peak2:getPeakPos()
|
|
||||||
local pos3 = peak3:getPeakPos()
|
|
||||||
local pos4 = peak4:getPeakPos()
|
|
||||||
|
|
||||||
-- If the point is in the tetrahedron defined by the `MapGen.Peak`...
|
|
||||||
if pointInTetrahedron(
|
|
||||||
vector.new(x, y, z),
|
|
||||||
pos1,
|
|
||||||
pos2,
|
|
||||||
pos3,
|
|
||||||
pos4
|
|
||||||
)
|
|
||||||
then
|
|
||||||
local noiseValue = 0.0
|
|
||||||
|
|
||||||
local totalWeight = 0.0
|
|
||||||
|
|
||||||
-- ... we calculate the height for this point using smoothing.
|
|
||||||
local weight = calcWeight(tetrahedron.h1, x, y, z)
|
|
||||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_3d({x = x, y = y, z = z}) * weight
|
|
||||||
totalWeight = totalWeight + weight
|
|
||||||
|
|
||||||
weight = calcWeight(tetrahedron.h2, x, y, z)
|
|
||||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_3d({x = x, y = y, z = z}) * weight
|
|
||||||
totalWeight = totalWeight + weight
|
|
||||||
|
|
||||||
weight = calcWeight(tetrahedron.h3, x, y, z)
|
|
||||||
noiseValue = noiseValue + peak2:getMultinoise().someNoise:get_3d({x = x, y = y, z = z}) * weight
|
|
||||||
totalWeight = totalWeight + weight
|
|
||||||
|
|
||||||
weight = calcWeight(tetrahedron.h4, x, y, z)
|
|
||||||
noiseValue = noiseValue + peak3:getMultinoise().someNoise:get_2d({x = x, y = y, z = z}) * weight
|
|
||||||
totalWeight = totalWeight + weight
|
|
||||||
|
|
||||||
return mathRound(noiseValue / totalWeight)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- If the point does not fall within the tetrahedron, it is impossible to calculate the height.
|
|
||||||
-- For debugging purposes only, may slow down generation:
|
|
||||||
-- core.log('warning', string.format('The point %s %s %s does not fall within any tetrahedron, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z)))
|
|
||||||
end
|
|
||||||
--]]
|
|
||||||
|
|
||||||
---Generator handler for specific coordinates.
|
---Generator handler for specific coordinates.
|
||||||
---@param mapGenerator MapGen
|
---@param mapGenerator MapGen
|
||||||
---@param data number[]
|
---@param data number[]
|
||||||
@@ -915,6 +817,7 @@ end
|
|||||||
---@param y integer
|
---@param y integer
|
||||||
---@param z integer
|
---@param z integer
|
||||||
local function generatorHandler(mapGenerator, data, index, x, y, z)
|
local function generatorHandler(mapGenerator, data, index, x, y, z)
|
||||||
|
LAST_PROFILE = core.get_us_time()
|
||||||
-- If the node belongs to a static layer, then a static node is generated.
|
-- If the node belongs to a static layer, then a static node is generated.
|
||||||
local static = mapGenerator:getStaticByHeight(y)
|
local static = mapGenerator:getStaticByHeight(y)
|
||||||
if static ~= nil then
|
if static ~= nil then
|
||||||
@@ -950,6 +853,7 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
|
|||||||
data[index] = core.CONTENT_IGNORE
|
data[index] = core.CONTENT_IGNORE
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
profile('getNoisesValue')
|
||||||
|
|
||||||
-- We recalculate the climate based on altitude.
|
-- We recalculate the climate based on altitude.
|
||||||
noiseTempValue = layer:calcTemp(noiseTempValue, y)
|
noiseTempValue = layer:calcTemp(noiseTempValue, y)
|
||||||
@@ -963,6 +867,8 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
|
|||||||
}
|
}
|
||||||
|
|
||||||
generateNode(mapGenerator, layer, factors, data, index, x, y, z)
|
generateNode(mapGenerator, layer, factors, data, index, x, y, z)
|
||||||
|
profile('generateNode')
|
||||||
|
--PROFILE = false
|
||||||
end
|
end
|
||||||
|
|
||||||
---Chunk generation handler.
|
---Chunk generation handler.
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ local Triangle = require('MapGen.Triangle')
|
|||||||
---@field cavernsList MapGen.Cavern[]
|
---@field cavernsList MapGen.Cavern[]
|
||||||
---@field regionsByName table<string, MapGen.Region>
|
---@field regionsByName table<string, MapGen.Region>
|
||||||
---@field trianglesList table<MapGen.Layer.Category, MapGen.Triangle[]>
|
---@field trianglesList table<MapGen.Layer.Category, MapGen.Triangle[]>
|
||||||
---@field tetrahedronsList MapGen.Tetrahedron[] **Not supported**
|
|
||||||
local Layer = {
|
local Layer = {
|
||||||
peaksList = {
|
peaksList = {
|
||||||
landscape = {},
|
landscape = {},
|
||||||
|
|||||||
@@ -1,155 +0,0 @@
|
|||||||
---@diagnostic disable: param-type-not-match
|
|
||||||
local mathAbs = math.abs
|
|
||||||
|
|
||||||
local getInstance = Class.getInstance
|
|
||||||
|
|
||||||
local modInfo = Mod.getInfo('smc__core__map_gen')
|
|
||||||
local require = modInfo.require
|
|
||||||
|
|
||||||
---@type MapGen.Triangle
|
|
||||||
local Triangle = require('MapGen.Triangle')
|
|
||||||
---@type MapGen.Triangulation.Edge
|
|
||||||
local Edge = require('MapGen.Triangulation.Edge')
|
|
||||||
---@type MapGen.Triangulation.FakePeak
|
|
||||||
local FakePeak = require('MapGen.Triangulation.FakePeak')
|
|
||||||
|
|
||||||
---@class MapGen.Tetrahedron
|
|
||||||
---@field p1 MapGen.Peak
|
|
||||||
---@field p2 MapGen.Peak
|
|
||||||
---@field p3 MapGen.Peak
|
|
||||||
---@field p4 MapGen.Peak
|
|
||||||
---@field h1 MapGen.Triangulation.Edge
|
|
||||||
---@field h2 MapGen.Triangulation.Edge
|
|
||||||
---@field h3 MapGen.Triangulation.Edge
|
|
||||||
---@field h4 MapGen.Triangulation.Edge
|
|
||||||
---@field f1 MapGen.Triangle
|
|
||||||
---@field f2 MapGen.Triangle
|
|
||||||
---@field f3 MapGen.Triangle
|
|
||||||
---@field f4 MapGen.Triangle
|
|
||||||
local Tetrahedron = {}
|
|
||||||
|
|
||||||
---@param p1 MapGen.Peak
|
|
||||||
---@param p2 MapGen.Peak
|
|
||||||
---@param p3 MapGen.Peak
|
|
||||||
---@param p4 MapGen.Peak
|
|
||||||
---@return MapGen.Tetrahedron
|
|
||||||
function Tetrahedron:new(p1, p2, p3, p4)
|
|
||||||
local instance = getInstance(self, {
|
|
||||||
p1 = p1,
|
|
||||||
p2 = p2,
|
|
||||||
p3 = p3,
|
|
||||||
p4 = p4,
|
|
||||||
h1 = self:getHeight(p1, p2, p3, p4),
|
|
||||||
h2 = self:getHeight(p2, p3, p4, p1),
|
|
||||||
h3 = self:getHeight(p3, p4, p1, p2),
|
|
||||||
h4 = self:getHeight(p4, p1, p2, p3),
|
|
||||||
f1 = Triangle:new(p1, p2, p3),
|
|
||||||
f2 = Triangle:new(p1, p2, p4),
|
|
||||||
f3 = Triangle:new(p1, p3, p4),
|
|
||||||
f4 = Triangle:new(p2, p3, p4),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
__tostring = self.toString
|
|
||||||
})
|
|
||||||
|
|
||||||
return instance
|
|
||||||
end
|
|
||||||
|
|
||||||
---Returns a string describing the object in a readable form.
|
|
||||||
---@return string
|
|
||||||
function Tetrahedron:toString()
|
|
||||||
return ('Tetrahedron: \n %s\n %s\n %s\n %s'):format(
|
|
||||||
tostring(self.p1.getPeakPos()),
|
|
||||||
tostring(self.p2.getPeakPos()),
|
|
||||||
tostring(self.p3.getPeakPos()),
|
|
||||||
tostring(self.p4.getPeakPos())
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param p1 MapGen.Peak
|
|
||||||
---@param p2 MapGen.Peak
|
|
||||||
---@param p3 MapGen.Peak
|
|
||||||
---@param p4 MapGen.Peak
|
|
||||||
function Tetrahedron:getHeight(p1, p2, p3, p4)
|
|
||||||
local pos1 = p1:getPeakPos()
|
|
||||||
local pos2 = p2:getPeakPos()
|
|
||||||
local pos3 = p3:getPeakPos()
|
|
||||||
local pos4 = p4:getPeakPos()
|
|
||||||
|
|
||||||
-- Векторы, лежащие в плоскости
|
|
||||||
local AB = pos3 - pos2
|
|
||||||
local AC = pos4 - pos2
|
|
||||||
|
|
||||||
-- Нормаль к плоскости
|
|
||||||
local normal = AB:cross(AC)
|
|
||||||
local normal_length = normal:length()
|
|
||||||
|
|
||||||
-- Если плоскость вырождена (точки на одной прямой)
|
|
||||||
if normal_length < 1e-10 then
|
|
||||||
return nil -- или другая обработка ошибки
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Вектор от точки на плоскости к нашей точке
|
|
||||||
local AP = pos1 - pos2
|
|
||||||
|
|
||||||
-- Расстояние от точки до плоскости
|
|
||||||
local distance = AP:dot(normal) / normal_length
|
|
||||||
|
|
||||||
-- Проекция точки на плоскость (вектор!)
|
|
||||||
local projection = pos1 - (normal * (distance / normal_length))
|
|
||||||
|
|
||||||
return Edge:new(p1, FakePeak:new(projection))
|
|
||||||
end
|
|
||||||
|
|
||||||
---Returns the center of the circumscribed sphere.
|
|
||||||
---@return vector
|
|
||||||
function Tetrahedron:getCircumSphereCenter()
|
|
||||||
local pos1 = self.p1.getPeakPos()
|
|
||||||
local pos2 = self.p2.getPeakPos()
|
|
||||||
local pos3 = self.p3.getPeakPos()
|
|
||||||
local pos4 = self.p4.getPeakPos()
|
|
||||||
|
|
||||||
local a = pos2 - pos1
|
|
||||||
local b = pos3 - pos1
|
|
||||||
local c = pos4 - pos1
|
|
||||||
|
|
||||||
local a2 = a:dot(a)
|
|
||||||
local b2 = b:dot(b)
|
|
||||||
local c2 = c:dot(c)
|
|
||||||
|
|
||||||
local cross_bc = b:cross(c)
|
|
||||||
local cross_ca = c:cross(a)
|
|
||||||
local cross_ab = a:cross(b)
|
|
||||||
|
|
||||||
local det = a:dot(b:cross(c)) * 2
|
|
||||||
|
|
||||||
if mathAbs(det) < 0 then
|
|
||||||
return (pos1 + pos2 + pos3 + pos4) / 4
|
|
||||||
end
|
|
||||||
|
|
||||||
local center = pos1 + (cross_bc * a2 + cross_ca * b2 + cross_ab * c2) / det
|
|
||||||
|
|
||||||
return center
|
|
||||||
end
|
|
||||||
|
|
||||||
---Returns the radius of the circumscribed sphere.
|
|
||||||
---@return number
|
|
||||||
function Tetrahedron:getCircumSphereRadius()
|
|
||||||
local center = self:getCircumSphereCenter()
|
|
||||||
local pos1 = self.p1.getPeakPos()
|
|
||||||
return (pos1 - center):length()
|
|
||||||
end
|
|
||||||
|
|
||||||
---Checks whether a given point lies within the circumscribed sphere of a tetrahedron.
|
|
||||||
---@param p MapGen.Peak
|
|
||||||
function Tetrahedron:inCircumSphere(p)
|
|
||||||
local pos = p.getPeakPos()
|
|
||||||
local c = self:getCircumSphereCenter()
|
|
||||||
local r = self:getCircumSphereRadius()
|
|
||||||
|
|
||||||
local distance = (pos - c):length()
|
|
||||||
|
|
||||||
return distance <= r
|
|
||||||
end
|
|
||||||
|
|
||||||
return Tetrahedron
|
|
||||||
@@ -1,327 +0,0 @@
|
|||||||
---@diagnostic disable: param-type-not-match
|
|
||||||
-- Tringulation lib
|
|
||||||
-- Original code: https://github.com/iskolbin/Triangulation/blob/master/Triangulation.lua
|
|
||||||
-- Date of copying: 07.11.2025
|
|
||||||
-- Commit: e8d26241767cd6af377b5eda0c1e27122b6afe8a
|
|
||||||
|
|
||||||
-- Main changes:
|
|
||||||
-- * refactoring code to our standard;
|
|
||||||
-- * 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, tableSort
|
|
||||||
= assert, math.max, table.remove, table.sort
|
|
||||||
|
|
||||||
local modInfo = Mod.getInfo('smc__core__map_gen')
|
|
||||||
local require = modInfo.require
|
|
||||||
|
|
||||||
---@type MapGen.Triangle
|
|
||||||
local Triangle = require('MapGen.Triangle')
|
|
||||||
---@type MapGen.Tetrahedron
|
|
||||||
local Tetrahedron = require('MapGen.Tetrahedron')
|
|
||||||
---@type MapGen.Triangulation.FakePeak
|
|
||||||
local FakePeak = require('MapGen.Triangulation.FakePeak')
|
|
||||||
|
|
||||||
---@class MapGen.Triangulation
|
|
||||||
---@field convexMultiplier number
|
|
||||||
local Triangulation = {
|
|
||||||
convexMultiplier = 1e3,
|
|
||||||
}
|
|
||||||
|
|
||||||
---Finds and writes adjacent triangles into triangles.
|
|
||||||
---@param triangles MapGen.Triangle[]
|
|
||||||
local function findTrianglesNeighbours(triangles)
|
|
||||||
for _, triangle in ipairs(triangles) do
|
|
||||||
for _, t in ipairs(triangles) do
|
|
||||||
if triangle.e1 == t.e1 and triangle.p3 ~= t.p3 then
|
|
||||||
triangle.neighbours.t1 = t
|
|
||||||
elseif triangle.e2 == t.e2 and triangle.p3 ~= t.p1 then
|
|
||||||
triangle.neighbours.t2 = t
|
|
||||||
elseif triangle.e3 == t.e3 and triangle.p3 ~= t.p2 then
|
|
||||||
triangle.neighbours.t3 = t
|
|
||||||
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.
|
|
||||||
---
|
|
||||||
---Doesn't take into account the Y coordinate of the vertices.
|
|
||||||
---That is, all triangles that don't lie in the XZ plane will be projected onto it.
|
|
||||||
---
|
|
||||||
---Vertices that do not have a `is2d` group will not participate in the triangulation.
|
|
||||||
---@param vertices table<integer, MapGen.Peak>
|
|
||||||
function Triangulation.triangulate(vertices)
|
|
||||||
-- Filter vertices with is2d group (creates a new table)
|
|
||||||
vertices = filterVerticesByGroup(vertices, "is2d")
|
|
||||||
|
|
||||||
-- Sort deterministically to ensure consistent results across sessions
|
|
||||||
sortVerticesDeterministically(vertices)
|
|
||||||
|
|
||||||
local nvertices = #vertices
|
|
||||||
|
|
||||||
assert(nvertices > 2, "Cannot triangulate, needs more than 3 vertices")
|
|
||||||
|
|
||||||
if nvertices == 3 then
|
|
||||||
return {Triangle:new(vertices[1], vertices[2], vertices[3])}
|
|
||||||
end
|
|
||||||
|
|
||||||
local minPos = vertices[1]:getPeakPos()
|
|
||||||
local minX, minY = minPos.x, minPos.z
|
|
||||||
local maxX, maxY = minX, minY
|
|
||||||
|
|
||||||
for i = 1, nvertices do
|
|
||||||
local vertex = vertices[i]
|
|
||||||
local vertexPos = vertex:getPeakPos()
|
|
||||||
vertex.id = i
|
|
||||||
|
|
||||||
if vertexPos.x < minX then
|
|
||||||
minX = vertexPos.x
|
|
||||||
end
|
|
||||||
|
|
||||||
if vertexPos.z < minY then
|
|
||||||
minY = vertexPos.z
|
|
||||||
end
|
|
||||||
|
|
||||||
if vertexPos.x > maxX then
|
|
||||||
maxX = vertexPos.x
|
|
||||||
end
|
|
||||||
|
|
||||||
if vertexPos.z > maxY then
|
|
||||||
maxY = vertexPos.z
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 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))
|
|
||||||
|
|
||||||
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])}
|
|
||||||
|
|
||||||
for i = 1, nvertices do
|
|
||||||
---@type MapGen.Triangulation.Edge[]
|
|
||||||
local edges = {}
|
|
||||||
|
|
||||||
-- Finding bad triangles.
|
|
||||||
for j = #triangles, 1, -1 do
|
|
||||||
local curTriangle = triangles[j]
|
|
||||||
|
|
||||||
if curTriangle:inCircumCircle2D(vertices[i]) then
|
|
||||||
edges[#edges + 1] = curTriangle.e1
|
|
||||||
edges[#edges + 1] = curTriangle.e2
|
|
||||||
edges[#edges + 1] = curTriangle.e3
|
|
||||||
tableRemove(triangles, j)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Removing duplicate edges.
|
|
||||||
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)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Create new triangles.
|
|
||||||
for j = 1, #edges do
|
|
||||||
triangles[#triangles + 1] = Triangle:new(edges[j].p1, edges[j].p2, vertices[i])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- 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)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Removing the vertices of a supertriangle.
|
|
||||||
for _ = 1, 3 do
|
|
||||||
tableRemove(vertices)
|
|
||||||
end
|
|
||||||
|
|
||||||
findTrianglesNeighbours(triangles)
|
|
||||||
|
|
||||||
return triangles
|
|
||||||
end
|
|
||||||
|
|
||||||
---The Delaunay method divides space into tetrahedra based on a list of their vertices.
|
|
||||||
---
|
|
||||||
---Vertices that do not have a `is3d` group will not participate in the triangulation.
|
|
||||||
---@param vertices table<integer, MapGen.Peak>
|
|
||||||
function Triangulation.tetrahedralize(vertices)
|
|
||||||
-- Filter vertices with is3d group (creates a new table)
|
|
||||||
vertices = filterVerticesByGroup(vertices, "is3d")
|
|
||||||
|
|
||||||
-- Sort deterministically to ensure consistent results across sessions
|
|
||||||
sortVerticesDeterministically(vertices)
|
|
||||||
|
|
||||||
local nvertices = #vertices
|
|
||||||
|
|
||||||
assert(nvertices > 3, "Cannot tetrahedralize, needs more than 4 vertices")
|
|
||||||
|
|
||||||
if nvertices == 4 then
|
|
||||||
return {Tetrahedron:new(vertices[1], vertices[2], vertices[3], vertices[4])}
|
|
||||||
end
|
|
||||||
|
|
||||||
local minPos = vertices[1]:getPeakPos()
|
|
||||||
local minX, minY, minZ = minPos.x, minPos.y, minPos.z
|
|
||||||
local maxX, maxY, maxZ = minX, minY, minZ
|
|
||||||
|
|
||||||
for i = 1, nvertices do
|
|
||||||
local vertex = vertices[i]
|
|
||||||
local vertexPos = vertex:getPeakPos()
|
|
||||||
vertex.id = i
|
|
||||||
|
|
||||||
if vertexPos.x < minX then
|
|
||||||
minX = vertexPos.x
|
|
||||||
end
|
|
||||||
|
|
||||||
if vertexPos.y < minY then
|
|
||||||
minY = vertexPos.y
|
|
||||||
end
|
|
||||||
|
|
||||||
if vertexPos.z < minZ then
|
|
||||||
minZ = vertexPos.z
|
|
||||||
end
|
|
||||||
|
|
||||||
if vertexPos.x > maxX then
|
|
||||||
maxX = vertexPos.x
|
|
||||||
end
|
|
||||||
|
|
||||||
if vertexPos.y > maxY then
|
|
||||||
maxY = vertexPos.y
|
|
||||||
end
|
|
||||||
|
|
||||||
if vertexPos.z > maxZ then
|
|
||||||
maxZ = vertexPos.z
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Finding the vertices of a supertetrahedron
|
|
||||||
local convex_mult = Triangulation.convexMultiplier
|
|
||||||
local dx, dy, dz = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult, (maxZ - minZ) * convex_mult
|
|
||||||
local deltaMax = mathMax(dx, dy, dz)
|
|
||||||
local midx, midy, midz = (minX + maxX) * 0.5, (minY + maxY) * 0.5, (minZ + maxZ) * 0.5
|
|
||||||
|
|
||||||
local p1 = FakePeak:new(vector.new(midx - 2 * deltaMax, midy - deltaMax, midz - deltaMax))
|
|
||||||
local p2 = FakePeak:new(vector.new(midx + 2 * deltaMax, midy - deltaMax, midz - deltaMax))
|
|
||||||
local p3 = FakePeak:new(vector.new(midx, midy + 2 * deltaMax, midz - deltaMax))
|
|
||||||
local p4 = FakePeak:new(vector.new(midx, midy, midz + 2 * deltaMax))
|
|
||||||
|
|
||||||
p1.id, p2.id, p3.id, p4.id = nvertices + 1, nvertices + 2, nvertices + 3, nvertices + 4
|
|
||||||
vertices[p1.id], vertices[p2.id], vertices[p3.id], vertices[p4.id] = p1, p2, p3, p4
|
|
||||||
|
|
||||||
---@type MapGen.Tetrahedron[]
|
|
||||||
local tetrahedrons = {Tetrahedron:new(p1, p2, p3, p4)}
|
|
||||||
|
|
||||||
for i = 1, nvertices do
|
|
||||||
---@type MapGen.Triangle[]
|
|
||||||
local faces = {}
|
|
||||||
|
|
||||||
-- Finding bad tetrahedrons.
|
|
||||||
for j = #tetrahedrons, 1, -1 do
|
|
||||||
local curTetrahedron = tetrahedrons[j]
|
|
||||||
|
|
||||||
if curTetrahedron:inCircumSphere(vertices[i]) then
|
|
||||||
faces[#faces + 1] = curTetrahedron.f1
|
|
||||||
faces[#faces + 1] = curTetrahedron.f2
|
|
||||||
faces[#faces + 1] = curTetrahedron.f3
|
|
||||||
faces[#faces + 1] = curTetrahedron.f4
|
|
||||||
tableRemove(tetrahedrons, j)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Removing duplicate faces.
|
|
||||||
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)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Create new tetrahedrons.
|
|
||||||
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)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Removing the vertices of a supertetrahedron.
|
|
||||||
for _ = 1, 4 do
|
|
||||||
tableRemove(vertices)
|
|
||||||
end
|
|
||||||
|
|
||||||
return tetrahedrons
|
|
||||||
end
|
|
||||||
|
|
||||||
return Triangulation
|
|
||||||
Reference in New Issue
Block a user