mirror(x,y,z)

This commit is contained in:
BuckarooBanzay 2022-06-14 11:07:40 +02:00
parent 1898d12be3
commit 954bd4ab43
8 changed files with 72 additions and 1 deletions

16
context/mirror.lua Normal file
View File

@ -0,0 +1,16 @@
local function apply_axis(ctx, axis, v)
assert(v <= 1)
if v == 1 then
-- invert axis
ctx.pos_factor[axis] = ctx.pos_factor[axis] * -1
end
end
function mtscad.Context:mirror(x, y, z)
local ctx = self:clone()
apply_axis(ctx, "x", x)
apply_axis(ctx, "y", y)
apply_axis(ctx, "z", z)
return ctx
end

View File

@ -13,6 +13,7 @@ function mtscad.create_context(opts)
local self = {
pos = opts.pos and vector.copy(opts.pos) or vector.zero(),
pos_factor = opts.pos_factor and vector.copy(opts.pos_factor) or vector.new(1,1,1),
rotation = opts.rotation or mtscad.rotation_matrix_x(0),
nodefactory = opts.nodefactory,
param2 = opts.param2 or 0,

View File

@ -4,6 +4,13 @@ function mtscad.Context:set_node()
node.param2 = self.param2
end
local tnode = mtscad.transform_node(node, self.rotation)
if self.pos_factor.x == -1 or self.pos_factor.z == -1 then
tnode.param2 = mtscad.rotate_facedir(2, "y-", tnode.param2)
elseif self.pos_factor.y == -1 then
tnode.param2 = mtscad.rotate_facedir(2, "z-", tnode.param2)
end
minetest.set_node(self.pos, tnode)
mtscad.extents(self.session.min, self.session.max, self.pos)
return self

View File

@ -7,7 +7,14 @@ function mtscad.Context:translate(x, y, z)
z = z or 0
}
local ctx = self:clone()
-- rotate
local m = mtscad.multiply_matrix(self.rotation, mtscad.pos_to_matrix(opos))
ctx.pos = vector.add(self.pos, mtscad.matrix_to_pos(m))
local rel_pos = mtscad.matrix_to_pos(m)
-- apply pos factor (mirror)
rel_pos = vector.multiply(self.pos_factor, rel_pos)
ctx.pos = vector.add(self.pos, rel_pos)
return ctx
end

View File

@ -16,6 +16,7 @@ dofile(MP .. "/context/dome.lua")
dofile(MP .. "/context/slope.lua")
dofile(MP .. "/context/cylinder.lua")
dofile(MP .. "/context/pattern.lua")
dofile(MP .. "/context/mirror.lua")
dofile(MP .. "/util/extents.lua")
dofile(MP .. "/util/matrix.lua")
dofile(MP .. "/util/origin.lua")

View File

@ -13,6 +13,7 @@ table.insert(jobs, loadfile(MP .. "/load_module.lua")({x=0, y=0, z=20}))
table.insert(jobs, loadfile(MP .. "/draw_line.lua")({x=0, y=0, z=0}))
table.insert(jobs, loadfile(MP .. "/draw_async.lua")({x=20, y=0, z=0}))
table.insert(jobs, loadfile(MP .. "/translate_rotate.lua")({x=20, y=0, z=20}))
table.insert(jobs, loadfile(MP .. "/mirror.lua")({x=-20, y=0, z=20}))
local job_index = 1

23
test/mod/mirror.lua Normal file
View File

@ -0,0 +1,23 @@
local origin = ...
return function(callback)
print("mirror")
local ctx = mtscad.create_context({ pos = origin })
ctx.job_context.register_on_done(function(_, err_msg)
if err_msg then
error(err_msg)
end
assert(minetest.get_node(vector.add(origin, {x=5+2,y=5,z=5})).name == "default:mese")
assert(minetest.get_node(vector.add(origin, {x=5+2,y=5,z=5})).param2 == 3)
assert(minetest.get_node(vector.add(origin, {x=5-2,y=5,z=5})).name == "default:mese")
assert(minetest.get_node(vector.add(origin, {x=5-2,y=5,z=5})).param2 == 1)
callback()
end)
local mod = mtscad.load_module("mirror")
mod(ctx)
-- process async jobs
ctx.job_context.process()
end

15
test/workspace/mirror.lua Normal file
View File

@ -0,0 +1,15 @@
local function fn(ctx)
ctx
:translate(2, 0, 0)
:with("default:mese")
:slope(1, 1, 0)
:set_node()
end
return function(ctx)
ctx
:translate(5, 5, 5)
:execute(fn)
:mirror(1, 0, 0)
:execute(fn)
end