diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 0ea1935..b92c577 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -308,22 +308,30 @@ local function generateNode(mapGenerator, hight, temp, humidity, data, index, x, end end +---@param px number +---@param py number +---@param x1 number +---@param y1 number +---@param x2 number +---@param y2 number +---@param x3 number +---@param y3 number local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3) - -- Функция для вычисления векторного произведения + -- Function for calculating the vector product local function crossProduct(ax, ay, bx, by) return ax * by - ay * bx end - -- Векторы от вершин к точке и между вершинами + -- Vectors from vertices to a point and between vertices local d1 = crossProduct(x2 - x1, y2 - y1, px - x1, py - y1) local d2 = crossProduct(x3 - x2, y3 - y2, px - x2, py - y2) local d3 = crossProduct(x1 - x3, y1 - y3, px - x3, py - y3) - -- Проверка знаков векторных произведений + -- Checking the signs of vector products local has_positive = (d1 > 0) or (d2 > 0) or (d3 > 0) local has_negative = (d1 < 0) or (d2 < 0) or (d3 < 0) - -- Точка внутри, если все произведения одного знака + -- Dot inside if all products have the same sign return not (has_positive and has_negative) end @@ -350,33 +358,62 @@ local function calcWeight(height, x, y, z) return 1 - vector.distance(pos2, posP) / height:length() end ----@param triangle MapGen.Triangle +---Calculates the height of the landscape based +---on a pre-triangulated plane stored in a layer. +--- +---Returns `nil` if the point is not included in any triangle. +---@param layer MapGen.Layer ---@param x number ---@param y number ---@param z number ----@return number -local function calcNoiseHeightValue(triangle, x, y, z) - local peak1 = triangle.p1 - local peak2 = triangle.p2 - local peak3 = triangle.p3 +---@return number? +function MapGen:getNoiseHeightValue(layer, x, y, z) + ---@param triangle MapGen.Triangle + for _, triangle in ipairs(layer.trianglesList) do + local peak1 = triangle.p1 + local peak2 = triangle.p2 + local peak3 = triangle.p3 - local noiseHeightValue = 0 + local pos1 = peak1:getPeakPos() + local pos2 = peak2:getPeakPos() + local pos3 = peak3:getPeakPos() - local totalWeight = 0.0 + -- If the point is in the triangle defined by the `MapGen.Peak`... + if pointInTriangle( + x, z, + pos1.x, pos1.z, + pos2.x, pos2.z, + pos3.x, pos3.z + ) + then + local noiseHeightValue = 0.0 - local weight = calcWeight(triangle.h1, x, y, z) - noiseHeightValue = noiseHeightValue + peak1:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight - totalWeight = totalWeight + weight + local totalWeight = 0.0 + -- ... we calculate the height for this point using smoothing. + -- We calculate the height value using the smoothing formula: + -- ( peak1 * weight1 + peak2 * weight2 + peak3 * weight3) / totalWeight, when + -- totalWeight = weight1 + weight2 + weight3 + local weight = calcWeight(triangle.h1, x, y, z) + noiseHeightValue = noiseHeightValue + peak1:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight + totalWeight = totalWeight + weight - weight = calcWeight(triangle.h2, x, y, z) - noiseHeightValue = noiseHeightValue + peak2:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight - totalWeight = totalWeight + weight + weight = calcWeight(triangle.h2, x, y, z) + noiseHeightValue = noiseHeightValue + peak2:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight + totalWeight = totalWeight + weight - weight = calcWeight(triangle.h3, x, y, z) - noiseHeightValue = noiseHeightValue + peak3:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight - totalWeight = totalWeight + weight + weight = calcWeight(triangle.h3, x, y, z) + noiseHeightValue = noiseHeightValue + peak3:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight + totalWeight = totalWeight + weight - return mathRound(noiseHeightValue / totalWeight) + -- The height must be an integer value + -- because the world is made up of whole blocks. + return mathRound(noiseHeightValue / totalWeight) + end + end + + -- If the point does not fall within the triangle, 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 triangle, calculating the height of the landscape is impossible.', tostring(x), tostring(y), tostring(z))) end ---@param mapGenerator MapGen @@ -399,64 +436,17 @@ local function generatorHandler(mapGenerator, data, index, x, y, z) oceanNoise = core.get_value_noise(oceanNoiseParams) end - -- Default noise's values. - local noiseHeightValue = 0 - local noiseTempValue = 0 - local noiseHumidityValue = 0 + -- Calculating landscape height + local noiseHeightValue = mapGenerator:getNoiseHeightValue(layer, x, y, z) - ---@param triangle MapGen.Triangle - for _, triangle in ipairs(layer.trianglesList) do - local peak1 = triangle.p1 - local peak2 = triangle.p2 - local peak3 = triangle.p3 - - local pos1 = peak1:getPeakPos() - local pos2 = peak2:getPeakPos() - local pos3 = peak3:getPeakPos() - - if pointInTriangle( - x, z, - pos1.x, pos1.z, - pos2.x, pos2.z, - pos3.x, pos3.z - ) - then - noiseHeightValue = calcNoiseHeightValue(triangle, x, y, z) - break - end + if noiseHeightValue == nil then + noiseHeightValue = 0.0 end + local noiseTempValue = 0.0 + local noiseHumidityValue = 0.0 + generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z) - - --[[ We get the two nearest peaks. - ---@type {peak: MapGen.Peak, weight: number}[]|table, number - local peaks, totalWeight = layer:getPeaksByPos(x, y, z, MAX_DISTANCE) - - ---@diagnostic disable-next-line: param-type-not-match - if table.is_empty(peaks) then - --[[minetest.log('warning', - 'No `MapGen.Peak` were found for the following coordinates: '.. - tostring(x)..' '..tostring(y)..' '..tostring(z) - ) --] ] - generateNode(mapGenerator, oceanNoise:get_2d({x = x, y = z}), noiseTempValue, noiseHumidityValue, data, index, x, y, z) - end - - if #peaks == 1 then - -- TODO: добавить температуру и влажностьf - generateNode(mapGenerator, peaks[1].peak:getMultinoise().landscapeNoise:get_2d({x = x, y = z}), noiseTempValue, noiseHumidityValue, data, index, x, y, z) - end - - ---@param v {peak: MapGen.Peak, weight: number} - for _, v in ipairs(peaks) do - --TODO: дописать группу ландашфта и проверку на неё - noiseHeightValue = noiseHeightValue + v.peak:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * v.weight - --noiseTempValue = noiseTempValue + v.peak:getMultinoise().tempNoise:get_2d({x = x, y = z}) * v.weight - --noiseHumidityValue = noiseHumidityValue + v.peak:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * v.weight - end - - noiseHeightValue = mathRound(noiseHeightValue / totalWeight) - --noiseTempValue = mathRound(noiseTempValue / totalWeight) - --noiseHumidityValue = mathRound(noiseHumidityValue / totalWeight) --]] end ---@param minPos table diff --git a/mods/CORE/MapGen/src/MapGen/Peak.lua b/mods/CORE/MapGen/src/MapGen/Peak.lua index 3dd6960..8cc85aa 100644 --- a/mods/CORE/MapGen/src/MapGen/Peak.lua +++ b/mods/CORE/MapGen/src/MapGen/Peak.lua @@ -77,7 +77,7 @@ function Peak:__eq( other ) return pos1.x == pos2.x and pos1.z == pos2.z end -function Peak:__tostring() +function Peak:toString() return ('Peak (%s) x: %.2f y: %.2f'):format( self.id, self:getPeakPos().x, self:getPeakPos().y ) end diff --git a/mods/CORE/MapGen/src/MapGen/Peak/Multinoise.lua b/mods/CORE/MapGen/src/MapGen/Peak/Multinoise.lua index eb6e2fb..11c4139 100644 --- a/mods/CORE/MapGen/src/MapGen/Peak/Multinoise.lua +++ b/mods/CORE/MapGen/src/MapGen/Peak/Multinoise.lua @@ -1,9 +1,9 @@ ---For EmmyLua only ---@class MapGen.Peak.Multinoise ----@field landscapeNoise ValueNoise? ----@field tempNoise ValueNoise? ----@field humidityNoise ValueNoise? +---@field landscapeNoise ValueNoise +---@field tempNoise ValueNoise +---@field humidityNoise ValueNoise local Multinoise = {} return Multinoise diff --git a/mods/CORE/MapGen/src/MapGen/Peak/MultinoiseParams.lua b/mods/CORE/MapGen/src/MapGen/Peak/MultinoiseParams.lua index a45fca6..911e4f2 100644 --- a/mods/CORE/MapGen/src/MapGen/Peak/MultinoiseParams.lua +++ b/mods/CORE/MapGen/src/MapGen/Peak/MultinoiseParams.lua @@ -1,9 +1,9 @@ ---For EmmyLua only ---@class MapGen.Peak.MultinoiseParams ----@field landscapeNoise NoiseParams? ----@field tempNoise NoiseParams? ----@field humidityNoise NoiseParams? +---@field landscapeNoise NoiseParams +---@field tempNoise NoiseParams +---@field humidityNoise NoiseParams local MultinoiseParams = {} return MultinoiseParams diff --git a/mods/CORE/MapGen/src/MapGen/Triangle.lua b/mods/CORE/MapGen/src/MapGen/Triangle.lua new file mode 100644 index 0000000..fa5ff06 --- /dev/null +++ b/mods/CORE/MapGen/src/MapGen/Triangle.lua @@ -0,0 +1,182 @@ +local sqrt = math.sqrt + +local modInfo = Mod.getInfo() +local require = modInfo.require + +---@type MapGen.Triangulation.Edge +local Edge = require('MapGen.Triangulation.Edge') +---@type MapGen.Triangulation.FakePeak +local FakePeak = require('MapGen.Triangulation.FakePeak') + +-- Helpers functions -- + +---Triangle semi-perimeter by Heron's formula +---@param a number +---@param b number +---@param c number +---@return number +local function quatCross(a, b, c) + local p = (a + b + c) * (a + b - c) * (a - b + c) * (-a + b + c) + return sqrt( p ) +end + +---Cross product (p1-p2, p2-p3) +---@param p1 MapGen.Peak +---@param p2 MapGen.Peak +---@param p3 MapGen.Peak +local function crossProduct(p1, p2, p3) + local pos1 = p1:getPeakPos() + local pos2 = p2:getPeakPos() + local pos3 = p3:getPeakPos() + + local x1, x2 = pos2.x - pos1.x, pos3.x - pos2.x + local y1, y2 = pos2.z - pos1.z, pos3.z - pos2.z + return x1 * y2 - y1 * x2 +end + +---Checks if angle (p1-p2-p3) is flat +---@param p1 MapGen.Peak +---@param p2 MapGen.Peak +---@param p3 MapGen.Peak +local function isFlatAngle(p1, p2, p3) + return (crossProduct(p1, p2, p3) == 0) +end + +-- Class registration -- + +---@class MapGen.Triangle +---@field p1 MapGen.Peak +---@field p2 MapGen.Peak +---@field p3 MapGen.Peak +---@field e1 MapGen.Triangulation.Edge +---@field e2 MapGen.Triangulation.Edge +---@field e3 MapGen.Triangulation.Edge +---@field h1 MapGen.Triangulation.Edge +---@field h2 MapGen.Triangulation.Edge +---@field h3 MapGen.Triangulation.Edge +local Triangle = {} + +---@param p1 MapGen.Peak +---@param p2 MapGen.Peak +---@param p3 MapGen.Peak +---@return MapGen.Triangle +function Triangle:new( p1, p2, p3 ) + assert(not isFlatAngle(p1, p2, p3), ("angle (p1, p2, p3) is flat:\n %s\n %s\n %s") + :format(tostring(p1), tostring(p2), tostring(p3))) + + local instance = setmetatable({ + p1 = p1, + p2 = p2, + p3 = p3, + h1 = Triangle:getHeight(p1, p2, p3), + h2 = Triangle:getHeight(p2, p3, p1), + h3 = Triangle:getHeight(p3, p1, p2), + e1 = Edge:new(p1, p2), + e2 = Edge:new(p2, p3), + e3 = Edge:new(p3, p1) + }, {__index = self}) + + return instance +end + +---Returns the length of the perpendicular +---from Peak1 to the line Peak2-Peak3 +---@param p1 MapGen.Peak +---@param p2 MapGen.Peak +---@param p3 MapGen.Peak +---@return MapGen.Triangulation.Edge +function Triangle:getHeight(p1, p2, p3) + local pos1 = p1:getPeakPos() + local pos2 = p2:getPeakPos() + local pos3 = p3:getPeakPos() + + local v = pos3 - pos2 + ---@type vector + local w = pos1 - pos2 + + -- Formula: (w * v) / (v * v) + -- When * the dot product. + local t = w:dot(v) / v:dot(v) + + local posP = pos2 + v * t + + return Edge:new(p1, FakePeak:new(posP)) +end + +function Triangle:toString() + return (('Triangle: \n %s\n %s\n %s') + :format(tostring(self.p1.getPeakPos()), tostring(self.p2.getPeakPos()), tostring(self.p3.getPeakPos()))) +end + +---Checks if the triangle is defined clockwise (sequence p1-p2-p3) +function Triangle:isCW() + return (crossProduct(self.p1, self.p2, self.p3) < 0) +end + +---Checks if the triangle is defined counter-clockwise (sequence p1-p2-p3) +function Triangle:isCCW() + return (crossProduct(self.p1, self.p2, self.p3) > 0) +end + +---Returns the length of the edges +function Triangle:getSidesLength() + return self.e1:length(), self.e2:length(), self.e3:length() +end + +---Returns the coordinates of the center +function Triangle:getCenter() + local pos1 = self.p1:getPeakPos() + local pos2 = self.p2:getPeakPos() + local pos3 = self.p3:getPeakPos() + + return (pos1 + pos2 + pos3) / 3 +end + +---Returns the coordinates of the circumcircle center +function Triangle:getCircumCenter() + local pos1 = self.p1:getPeakPos() + local pos2 = self.p2:getPeakPos() + local pos3 = self.p3:getPeakPos() + + local D = ( pos1.x * (pos2.z - pos3.z) + + pos2.x * (pos3.z - pos1.z) + + pos3.x * (pos1.z - pos2.z)) * 2 + local x = (( pos1.x * pos1.x + pos1.z * pos1.z) * (pos2.z - pos3.z) + + ( pos2.x * pos2.x + pos2.z * pos2.z) * (pos3.z - pos1.z) + + ( pos3.x * pos3.x + pos3.z * pos3.z) * (pos1.z - pos2.z)) + local z = (( pos1.x * pos1.x + pos1.z * pos1.z) * (pos3.x - pos2.x) + + ( pos2.x * pos2.x + pos2.z * pos2.z) * (pos1.x - pos3.x) + + ( pos3.x * pos3.x + pos3.z * pos3.z) * (pos2.x - pos1.x)) + + return (x / D), (z / D) +end + +--- Returns the radius of the circumcircle +function Triangle:getCircumRadius() + local a, b, c = self:getSidesLength() + + return ((a * b * c) / quatCross(a, b, c)) +end + +---Returns the coordinates of the circumcircle center and its radius +function Triangle:getCircumCircle() + local x, z = self:getCircumCenter() + local r = self:getCircumRadius() + + return x, z, r +end + +---Returns the area +function Triangle:getArea() + local a, b, c = self:getSidesLength() + + return (quatCross(a, b, c) / 4) +end + +---Checks if a given point lies into the triangle circumcircle +---@param p MapGen.Peak +function Triangle:inCircumCircle(p) + return p:isInCircle(self:getCircumCircle()) +end + +return Triangle \ No newline at end of file diff --git a/mods/CORE/MapGen/src/MapGen/Triangulation.lua b/mods/CORE/MapGen/src/MapGen/Triangulation.lua index a1a7902..959300f 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangulation.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangulation.lua @@ -3,242 +3,18 @@ -- Date of copying: 07.11.2025 -- Commit: e8d26241767cd6af377b5eda0c1e27122b6afe8a -local setmetatable, tostring, assert = setmetatable, tostring, assert -local max, sqrt = math.max, math.sqrt +local tostring, assert = tostring, assert +local max = math.max local remove = table.remove local modInfo = Mod.getInfo() local require = modInfo.require ----@type MapGen.Peak -local Peak = require("MapGen.Peak") +---@type MapGen.Triangle +local Triangle = require('MapGen.Triangle') +---@type MapGen.Triangulation.FakePeak +local FakePeak = require('MapGen.Triangulation.FakePeak') ---local isffi = false, ffi = pcall( require, 'ffi' ) - ---local ffiPoint, ffiEdge, ffiTriangle - ----Triangle semi-perimeter by Heron's formula ----@param a number ----@param b number ----@param c number ----@return number -local function quatCross(a, b, c) - local p = (a + b + c) * (a + b - c) * (a - b + c) * (-a + b + c) - return sqrt( p ) -end - ----Cross product (p1-p2, p2-p3) ----@param p1 MapGen.Peak ----@param p2 MapGen.Peak ----@param p3 MapGen.Peak -local function crossProduct(p1, p2, p3) - local pos1 = p1:getPeakPos() - local pos2 = p2:getPeakPos() - local pos3 = p3:getPeakPos() - - local x1, x2 = pos2.x - pos1.x, pos3.x - pos2.x - local y1, y2 = pos2.z - pos1.z, pos3.z - pos2.z - return x1 * y2 - y1 * x2 -end - ----Checks if angle (p1-p2-p3) is flat ----@param p1 MapGen.Peak ----@param p2 MapGen.Peak ----@param p3 MapGen.Peak -local function isFlatAngle(p1, p2, p3) - return (crossProduct(p1, p2, p3) == 0) -end - ----@class MapGen.Triangulation.FakePeak : MapGen.Peak ----@field id number ----@field getPeakPos fun():vector -local FakePeak = Mod:getClassExtended(Peak, {}) - ----@param peakPos vector ----@return MapGen.Peak -function FakePeak:new(peakPos) - local instance = Peak:new(peakPos, {}, 1) - - return instance -end - ----@class MapGen.Triangulation.Edge ----@field p1 MapGen.Peak ----@field p2 MapGen.Peak -local Edge = {} - ----@param p1 MapGen.Peak ----@param p2 MapGen.Peak ----@return MapGen.Triangulation.Edge -function Edge:new( p1, p2 ) - local instance = setmetatable({ - p1 = p1, - p2 = p2 - }, {__index = self}) - - return instance -end - ----@param other MapGen.Triangulation.Edge -function Edge:__eq( other ) - return self.p1 == other.p1 and self.p2 == other.p2 -end - -function Edge:__tostring() - return (('Edge :\n %s\n %s'):format(tostring(self.p1), tostring(self.p2))) -end - -function Edge:same(otherEdge) - return ((self.p1 == otherEdge.p1) and (self.p2 == otherEdge.p2)) - or ((self.p1 == otherEdge.p2) and (self.p2 == otherEdge.p1)) -end - -function Edge:length() - return self.p1:dist(self.p2) -end - ----@return vector -function Edge:getMidPoint() - local pos1 = self.p1:getPeakPos() - local pos2 = self.p2:getPeakPos() - - return (pos1 + pos2 )/2 -end - ----@class MapGen.Triangle ----@field p1 MapGen.Peak ----@field p2 MapGen.Peak ----@field p3 MapGen.Peak ----@field e1 MapGen.Triangulation.Edge ----@field e2 MapGen.Triangulation.Edge ----@field e3 MapGen.Triangulation.Edge ----@field h1 MapGen.Triangulation.Edge ----@field h2 MapGen.Triangulation.Edge ----@field h3 MapGen.Triangulation.Edge -local Triangle = {} - ----@param p1 MapGen.Peak ----@param p2 MapGen.Peak ----@param p3 MapGen.Peak ----@return MapGen.Triangle -function Triangle:new( p1, p2, p3 ) - assert(not isFlatAngle(p1, p2, p3), ("angle (p1, p2, p3) is flat:\n %s\n %s\n %s") - :format(tostring(p1), tostring(p2), tostring(p3))) - - local instance = setmetatable({ - p1 = p1, - p2 = p2, - p3 = p3, - h1 = Triangle:getHeight(p1, p2, p3), - h2 = Triangle:getHeight(p2, p3, p1), - h3 = Triangle:getHeight(p3, p1, p2), - e1 = Edge:new(p1, p2), - e2 = Edge:new(p2, p3), - e3 = Edge:new(p3, p1) - }, {__index = self}) - - return instance -end - ----Returns the length of the perpendicular ----from Peak1 to the line Peak2-Peak3 ----@param p1 MapGen.Peak ----@param p2 MapGen.Peak ----@param p3 MapGen.Peak ----@return MapGen.Triangulation.Edge -function Triangle:getHeight(p1, p2, p3) - local pos1 = p1:getPeakPos() - local pos2 = p2:getPeakPos() - local pos3 = p3:getPeakPos() - - local v = pos3 - pos2 - ---@type vector - local w = pos1 - pos2 - - -- Formula: (w * v) / (v * v) - -- When * the dot product. - local t = w:dot(v) / v:dot(v) - - local posP = pos2 + v * t - - return Edge:new(p1, FakePeak:new(posP)) -end - -function Triangle:__tostring() - return (('Triangle: \n %s\n %s\n %s') - :format(tostring(self.p1.getPeakPos()), tostring(self.p2.getPeakPos()), tostring(self.p3.getPeakPos()))) -end - ----Checks if the triangle is defined clockwise (sequence p1-p2-p3) -function Triangle:isCW() - return (crossProduct(self.p1, self.p2, self.p3) < 0) -end - ----Checks if the triangle is defined counter-clockwise (sequence p1-p2-p3) -function Triangle:isCCW() - return (crossProduct(self.p1, self.p2, self.p3) > 0) -end - ----Returns the length of the edges -function Triangle:getSidesLength() - return self.e1:length(), self.e2:length(), self.e3:length() -end - ----Returns the coordinates of the center -function Triangle:getCenter() - local pos1 = self.p1:getPeakPos() - local pos2 = self.p2:getPeakPos() - local pos3 = self.p3:getPeakPos() - - return (pos1 + pos2 + pos3) / 3 -end - ----Returns the coordinates of the circumcircle center -function Triangle:getCircumCenter() - local pos1 = self.p1:getPeakPos() - local pos2 = self.p2:getPeakPos() - local pos3 = self.p3:getPeakPos() - - local D = ( pos1.x * (pos2.z - pos3.z) + - pos2.x * (pos3.z - pos1.z) + - pos3.x * (pos1.z - pos2.z)) * 2 - local x = (( pos1.x * pos1.x + pos1.z * pos1.z) * (pos2.z - pos3.z) + - ( pos2.x * pos2.x + pos2.z * pos2.z) * (pos3.z - pos1.z) + - ( pos3.x * pos3.x + pos3.z * pos3.z) * (pos1.z - pos2.z)) - local z = (( pos1.x * pos1.x + pos1.z * pos1.z) * (pos3.x - pos2.x) + - ( pos2.x * pos2.x + pos2.z * pos2.z) * (pos1.x - pos3.x) + - ( pos3.x * pos3.x + pos3.z * pos3.z) * (pos2.x - pos1.x)) - - return (x / D), (z / D) -end - ---- Returns the radius of the circumcircle -function Triangle:getCircumRadius() - local a, b, c = self:getSidesLength() - - return ((a * b * c) / quatCross(a, b, c)) -end - ----Returns the coordinates of the circumcircle center and its radius -function Triangle:getCircumCircle() - local x, z = self:getCircumCenter() - local r = self:getCircumRadius() - - return x, z, r -end - ----Returns the area -function Triangle:getArea() - local a, b, c = self:getSidesLength() - - return (quatCross(a, b, c) / 4) -end - ----Checks if a given point lies into the triangle circumcircle ----@param p MapGen.Peak -function Triangle:inCircumCircle(p) - return p:isInCircle(self:getCircumCircle()) -end ---@class MapGen.Triangulation local Triangulation = { @@ -328,14 +104,7 @@ function Triangulation.triangulate( vertices ) triangles[n + 1] = Triangle:new(edges[j].p1, edges[j].p2, vertices[i]) end end - for _, tri in ipairs(triangles) do - print('tria:', tri:__tostring()) - end - print('---') - for _, v in ipairs(vertices) do - print('v: ', tostring(v:getPeakPos())) - end - print('---') + 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 @@ -350,23 +119,4 @@ function Triangulation.triangulate( vertices ) return triangles end ---[[ -function Triangulation.setffi( set ) - if ffi then - isffi = set - end -end - -if isffi then - ffi.cdef(([[ - typedef struct { %s x, z; uint32_t id; } Point; - typedef struct { Point p1, p2; } Edge; - typedef struct { Point p1, p2, p3; Edge e1, e2, e3; } Triangle; - ] ]):format( _G.DELAUNAY_FFI_TYPE or 'double' )) - - ffiPoint = ffi.metatype( "Point", Point ) - ffiEdge = ffi.metatype( "Edge", Edge ) - ffiTriangle = ffi.metatype( "Triangle", Triangle ) -end --]] - return Triangulation \ No newline at end of file diff --git a/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua b/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua new file mode 100644 index 0000000..7e77c99 --- /dev/null +++ b/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua @@ -0,0 +1,44 @@ +---@class MapGen.Triangulation.Edge +---@field p1 MapGen.Peak +---@field p2 MapGen.Peak +local Edge = {} + +---@param p1 MapGen.Peak +---@param p2 MapGen.Peak +---@return MapGen.Triangulation.Edge +function Edge:new( p1, p2 ) + local instance = setmetatable({ + p1 = p1, + p2 = p2 + }, {__index = self}) + + return instance +end + +---@param other MapGen.Triangulation.Edge +function Edge:__eq( other ) + return self.p1 == other.p1 and self.p2 == other.p2 +end + +function Edge:__tostring() + return (('Edge :\n %s\n %s'):format(tostring(self.p1), tostring(self.p2))) +end + +function Edge:same(otherEdge) + return ((self.p1 == otherEdge.p1) and (self.p2 == otherEdge.p2)) + or ((self.p1 == otherEdge.p2) and (self.p2 == otherEdge.p1)) +end + +function Edge:length() + return self.p1:dist(self.p2) +end + +---@return vector +function Edge:getMidPoint() + local pos1 = self.p1:getPeakPos() + local pos2 = self.p2:getPeakPos() + + return (pos1 + pos2 )/2 +end + +return Edge \ No newline at end of file diff --git a/mods/CORE/MapGen/src/MapGen/Triangulation/FakePeak.lua b/mods/CORE/MapGen/src/MapGen/Triangulation/FakePeak.lua new file mode 100644 index 0000000..51de3ce --- /dev/null +++ b/mods/CORE/MapGen/src/MapGen/Triangulation/FakePeak.lua @@ -0,0 +1,21 @@ +local modInfo = Mod.getInfo() +local require = modInfo.require + +---@type MapGen.Peak +local Peak = require('MapGen.Peak') + +---@class MapGen.Triangulation.FakePeak : MapGen.Peak +---@field id number +---@field getPeakPos fun():vector +local FakePeak = Mod:getClassExtended(Peak, {}) + +---@param peakPos vector +---@return MapGen.Peak +function FakePeak:new(peakPos) + ---@diagnostic disable-next-line: missing-fields, param-type-not-match + local instance = Peak:new(peakPos, {}, 1) + + return instance +end + +return FakePeak \ No newline at end of file