2021-12-31 03:42:32 +01:00
|
|
|
local wea = worldeditadditions
|
2022-09-19 02:16:22 +02:00
|
|
|
local wea_c = worldeditadditions_core
|
|
|
|
local Vector3 = wea_c.Vector3
|
2021-12-28 19:31:46 +01:00
|
|
|
|
2021-12-31 03:42:32 +01:00
|
|
|
wea.conv = {}
|
2020-06-09 02:21:32 +02:00
|
|
|
|
2021-12-31 03:42:32 +01:00
|
|
|
dofile(wea.modpath.."/lib/conv/kernels.lua")
|
|
|
|
dofile(wea.modpath.."/lib/conv/kernel_gaussian.lua")
|
2020-06-09 02:21:32 +02:00
|
|
|
|
2021-12-31 03:42:32 +01:00
|
|
|
dofile(wea.modpath.."/lib/conv/convolve.lua")
|
2020-06-09 21:43:29 +02:00
|
|
|
|
|
|
|
--- Creates a new kernel.
|
|
|
|
-- Note that the gaussian kernel only allows for creating a square kernel.
|
|
|
|
-- arg is only used by the gaussian kernel as the sigma value (default: 2)
|
|
|
|
-- @param name string The name of the kernel to create (possible values: box, pascal, gaussian).
|
|
|
|
-- @param width number The width of the kernel to create (must be an odd integer).
|
|
|
|
-- @param height number The height of the kernel to create (must be an odd integer).
|
|
|
|
-- @param arg number The argument to pass when creating the kernel. Currently only used by gaussian kernel as the sigma value.
|
2021-12-31 03:42:32 +01:00
|
|
|
function wea.get_conv_kernel(name, width, height, arg)
|
2020-06-09 21:43:29 +02:00
|
|
|
if width % 2 ~= 1 then
|
|
|
|
return false, "Error: The width must be an odd integer.";
|
|
|
|
end
|
|
|
|
if height % 2 ~= 1 then
|
|
|
|
return false, "Error: The height must be an odd integer";
|
|
|
|
end
|
|
|
|
|
|
|
|
if name == "box" then
|
2021-12-31 03:42:32 +01:00
|
|
|
return true, wea.conv.kernel_box(width, height)
|
2020-06-09 21:43:29 +02:00
|
|
|
elseif name == "pascal" then
|
2021-12-31 03:42:32 +01:00
|
|
|
return true, wea.conv.kernel_pascal(width, height, true)
|
2020-06-09 21:43:29 +02:00
|
|
|
elseif name == "gaussian" then
|
|
|
|
if width ~= height then
|
|
|
|
return false, "Error: When using a gaussian kernel the width and height must be identical."
|
|
|
|
end
|
|
|
|
-- Default to sigma = 2
|
|
|
|
if arg == nil then arg = 2 end
|
2021-12-31 03:42:32 +01:00
|
|
|
local success, result = wea.conv.kernel_gaussian(width, arg)
|
2020-06-09 21:43:29 +02:00
|
|
|
return success, result
|
|
|
|
end
|
2020-06-09 22:11:34 +02:00
|
|
|
|
|
|
|
return false, "Error: Unknown kernel. Valid values: box, pascal, gaussian"
|
2020-06-09 21:43:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-12-31 03:42:32 +01:00
|
|
|
function wea.convolve(pos1, pos2, kernel, kernel_size)
|
2020-06-09 21:43:29 +02:00
|
|
|
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
|
|
|
|
|
2021-12-28 19:31:46 +01:00
|
|
|
local border_size = Vector3.new(
|
|
|
|
(kernel_size.x-1) / 2, -- x = height
|
|
|
|
0,
|
|
|
|
(kernel_size.z-1) / 2 -- z = width
|
|
|
|
)
|
2020-06-10 01:48:39 +02:00
|
|
|
|
2021-12-28 19:31:46 +01:00
|
|
|
pos1.z = pos1.z - border_size.x
|
|
|
|
pos2.z = pos2.z + border_size.x
|
|
|
|
pos1.x = pos1.x - border_size.z
|
|
|
|
pos2.x = pos2.x + border_size.z
|
2020-06-10 01:48:39 +02:00
|
|
|
|
2020-06-09 21:43:29 +02:00
|
|
|
local manip, area = worldedit.manip_helpers.init(pos1, pos2)
|
|
|
|
local data = manip:get_data()
|
|
|
|
|
|
|
|
local stats = { added = 0, removed = 0 }
|
|
|
|
|
|
|
|
local node_id_air = minetest.get_content_id("air")
|
|
|
|
|
2022-09-19 02:16:22 +02:00
|
|
|
local heightmap, heightmap_size = wea_c.terrain.make_heightmap(pos1, pos2, manip, area, data)
|
|
|
|
local heightmap_conv = wea_c.table.shallowcopy(heightmap)
|
2020-06-09 21:43:29 +02:00
|
|
|
|
2021-12-31 03:42:32 +01:00
|
|
|
wea.conv.convolve(
|
2020-06-09 21:43:29 +02:00
|
|
|
heightmap_conv,
|
|
|
|
heightmap_size,
|
|
|
|
kernel,
|
|
|
|
kernel_size
|
|
|
|
)
|
2020-06-10 01:48:39 +02:00
|
|
|
|
|
|
|
-- print("original")
|
2022-09-19 18:34:53 +02:00
|
|
|
-- wea_c.format.array_2d(heightmap, (pos2.z - pos1.z) + 1)
|
2020-06-10 01:48:39 +02:00
|
|
|
-- print("transformed")
|
2022-09-19 18:34:53 +02:00
|
|
|
-- wea_c.format.array_2d(heightmap_conv, (pos2.z - pos1.z) + 1)
|
2020-06-10 01:48:39 +02:00
|
|
|
|
2022-09-19 02:16:22 +02:00
|
|
|
wea_c.terrain.apply_heightmap_changes(
|
2020-08-21 14:27:40 +02:00
|
|
|
pos1, pos2, area, data,
|
|
|
|
heightmap, heightmap_conv, heightmap_size
|
|
|
|
)
|
2020-06-09 21:43:29 +02:00
|
|
|
|
|
|
|
worldedit.manip_helpers.finish(manip, data)
|
|
|
|
|
|
|
|
return true, stats
|
|
|
|
end
|