Minetest-WorldEditAdditions/worldeditadditions/lib/torus.lua

101 lines
3.6 KiB
Lua
Raw Normal View History

2022-09-18 23:58:33 +02:00
local wea_c = worldeditadditions_core
local Vector3 = wea_c.Vector3
--- Generates a torus shape at the given position with the given parameters.
-- @param position Vector The position at which to generate the torus.
-- @param major_radius number The major radius of the torus - i.e. the distance from the middle to the ring.
2021-05-15 03:18:41 +02:00
-- @param minor_radius number The minor radius of the torus - i.e. the radius fo the ring itself.
-- @param target_node string The name of the target node to generate the torus with.
-- @param axes=xz string|nil The axes upon which the torus should lay flat.
-- @param hollow=false boolean Whether the generated torus should be hollow or not.
-- @returns number The number of nodes replaced.
function worldeditadditions.torus(position, major_radius, minor_radius, target_node, axes, hollow)
2021-05-17 19:41:52 +02:00
local matrix = {x='yz', y='xz', z='xy'}
2021-05-17 20:18:09 +02:00
if type(axes) ~= "string" then axes = "xz" end
2021-05-18 02:16:04 +02:00
if #axes == 1 and axes:match('[xyz]') then axes = matrix[axes] end
2018-06-10 14:48:11 +02:00
-- position = { x, y, z }
local total_radius = major_radius + minor_radius
2018-06-10 14:50:24 +02:00
local inner_minor_radius = minor_radius - 2
2018-06-10 14:48:11 +02:00
local major_radius_sq = major_radius*major_radius
local minor_radius_sq = minor_radius*minor_radius
local inner_minor_radius_sq = inner_minor_radius*inner_minor_radius
-- Fetch the nodes in the specified area
-- OPTIMIZE: We should be able to calculate a more efficient box-area here
2021-05-15 03:18:41 +02:00
-- This is complicated by the multiple possible axes though
2018-06-10 14:48:11 +02:00
local manip, area = worldedit.manip_helpers.init_radius(position, total_radius)
local data = manip:get_data()
local node_id = minetest.get_content_id(target_node)
local node_id_air = minetest.get_content_id("air")
local stride_z, stride_y = area.zstride, area.ystride
local count = 0 -- The number of nodes replaced
local idx_z_base = area:index(position.x - total_radius, position.y - total_radius, position.z - total_radius) -- initial z offset
for z = -total_radius, total_radius do
local z_sq = z*z
local idx_y_base = idx_z_base
for y = -total_radius, total_radius do
local y_sq = y*y
local i = idx_y_base
for x = -total_radius, total_radius do
local x_sq = x*x
2022-09-18 23:58:33 +02:00
local sq = Vector3.new(x_sq, y_sq, z_sq)
2021-05-15 03:09:43 +02:00
-- Default: xy
if axes == "xz" then
2021-05-15 03:09:43 +02:00
sq.x, sq.y, sq.z = sq.x, sq.z, sq.y
elseif axes == "yz" then
sq.x, sq.y, sq.z = sq.y, sq.z, sq.x
end
2018-06-10 14:48:11 +02:00
-- (x^2+y^2+z^2-(a^2+b^2))^2-4 a b (b^2-z^2)
-- Where:
-- (x, y, z) is the point
-- a is the major radius (centre of the ring to the centre of the torus)
-- b is the minor radius (radius of the ring)
2021-05-15 03:09:43 +02:00
local comp_a = (sq.x+sq.y+sq.z - (major_radius_sq+minor_radius_sq))
local test_value = comp_a*comp_a - 4*major_radius*minor_radius*(minor_radius_sq-sq.z)
2018-06-10 14:48:11 +02:00
-- If we're inside the torus, then fill it in
if test_value <= 1 then
local place_ok = not hollow;
if not place_ok then
-- It must be hollow! Do some additional calculations.
2021-05-15 03:09:43 +02:00
local inner_comp_a = (sq.x+sq.y+sq.z - (major_radius_sq+inner_minor_radius_sq))
local inner_test_value = inner_comp_a*inner_comp_a - 4*major_radius*inner_minor_radius*(inner_minor_radius_sq-sq.z)
2018-06-10 14:48:11 +02:00
-- It's only ok to place it if it's outside our inner torus
place_ok = inner_test_value >= 0
end
if place_ok then
data[i] = node_id
count = count + 1
end
end
i = i + 1
end
idx_y_base = idx_y_base + stride_y
end
idx_z_base = idx_z_base + stride_z
end
-- Save the modified nodes back to disk & return
worldedit.manip_helpers.finish(manip, data)
return count
end