2020-06-09 02:21:32 +02:00
|
|
|
|
|
|
|
--[[
|
|
|
|
Convolves over a given 2D heightmap with a given matrix.
|
|
|
|
Note that this *mutates* the given heightmap.
|
|
|
|
Note also that the dimensions of the matrix must *only* be odd.
|
|
|
|
@param {number[]} heightmap The 2D heightmap to convolve over.
|
|
|
|
@param {[number,number]} heightmap_size The size of the heightmap as [ height, width ]
|
|
|
|
@param {number[]} matrix The matrix to convolve with.
|
|
|
|
@param {[number, number]} matrix_size The size of the convolution matrix as [ height, width ]
|
|
|
|
]]--
|
2020-06-09 23:00:56 +02:00
|
|
|
function worldeditadditions.conv.convolve(heightmap, heightmap_size, matrix, matrix_size)
|
2020-06-09 02:21:32 +02:00
|
|
|
if matrix_size[0] % 2 ~= 1 or matrix_size[1] % 2 ~= 1 then
|
|
|
|
return false, "Error: The matrix size must contain only odd numbers (even number detected)"
|
|
|
|
end
|
|
|
|
|
2020-06-09 23:00:56 +02:00
|
|
|
local border_size = {}
|
|
|
|
border_size[0] = (matrix_size[0]-1) / 2 -- height
|
|
|
|
border_size[1] = (matrix_size[1]-1) / 2 -- width
|
2020-08-21 21:59:50 +02:00
|
|
|
-- print("[convolve] matrix_size", matrix_size[0], matrix_size[1])
|
|
|
|
-- print("[convolve] border_size", border_size[0], border_size[1])
|
2021-02-26 03:20:53 +01:00
|
|
|
-- print("[convolve] heightmap_size: ", heightmap_size.z, heightmap_size.x)
|
2020-08-21 21:59:50 +02:00
|
|
|
--
|
2021-02-26 03:20:53 +01:00
|
|
|
-- print("[convolve] z: from", (heightmap_size.z-border_size[0]) - 1, "to", border_size[0], "step", -1)
|
|
|
|
-- print("[convolve] x: from", (heightmap_size.x-border_size[1]) - 1, "to", border_size[1], "step", -1)
|
2020-06-09 02:21:32 +02:00
|
|
|
|
|
|
|
-- Convolve over only the bit that allows us to use the full convolution matrix
|
2021-02-26 03:20:53 +01:00
|
|
|
for z = (heightmap_size.z-border_size[0]) - 1, border_size[0], -1 do
|
|
|
|
for x = (heightmap_size.x-border_size[1]) - 1, border_size[1], -1 do
|
2020-06-09 02:21:32 +02:00
|
|
|
local total = 0
|
|
|
|
|
2020-06-09 23:00:56 +02:00
|
|
|
|
2021-02-26 03:20:53 +01:00
|
|
|
local hi = (z * heightmap_size.x) + x
|
2020-06-10 01:48:39 +02:00
|
|
|
-- print("[convolve/internal] z", z, "x", x, "hi", hi)
|
|
|
|
|
|
|
|
-- No continue statement in Lua :-/
|
|
|
|
if heightmap[hi] ~= -1 then
|
|
|
|
for mz = matrix_size[0]-1, 0, -1 do
|
|
|
|
for mx = matrix_size[1]-1, 0, -1 do
|
|
|
|
local mi = (mz * matrix_size[1]) + mx
|
|
|
|
local cz = z + (mz - border_size[0])
|
|
|
|
local cx = x + (mx - border_size[1])
|
|
|
|
|
2021-02-26 03:20:53 +01:00
|
|
|
local i = (cz * heightmap_size.x) + cx
|
2020-06-10 01:48:39 +02:00
|
|
|
|
|
|
|
-- A value of -1 = nothing in this column (so we should ignore it)
|
|
|
|
if heightmap[i] ~= -1 then
|
|
|
|
total = total + (matrix[mi] * heightmap[i])
|
|
|
|
end
|
2020-06-09 23:00:56 +02:00
|
|
|
end
|
2020-06-09 02:21:32 +02:00
|
|
|
end
|
2020-06-10 01:48:39 +02:00
|
|
|
-- Rounding hack - ref https://stackoverflow.com/a/18313481/1460422
|
|
|
|
-- heightmap[hi] = math.floor(total + 0.5)
|
|
|
|
heightmap[hi] = math.ceil(total)
|
2020-06-09 02:21:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true, heightmap
|
|
|
|
end
|