diff --git a/context/mirror.lua b/context/mirror.lua new file mode 100644 index 0000000..c42b491 --- /dev/null +++ b/context/mirror.lua @@ -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 \ No newline at end of file diff --git a/context/new.lua b/context/new.lua index adaf845..edd955a 100644 --- a/context/new.lua +++ b/context/new.lua @@ -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, diff --git a/context/set_node.lua b/context/set_node.lua index fc6c6c6..c6f707c 100644 --- a/context/set_node.lua +++ b/context/set_node.lua @@ -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 diff --git a/context/translate.lua b/context/translate.lua index e9f44bf..e876c58 100644 --- a/context/translate.lua +++ b/context/translate.lua @@ -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 diff --git a/init.lua b/init.lua index 9529161..8348f9d 100644 --- a/init.lua +++ b/init.lua @@ -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") diff --git a/test/mod/init.lua b/test/mod/init.lua index f9c8ab3..3d31ac0 100644 --- a/test/mod/init.lua +++ b/test/mod/init.lua @@ -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 diff --git a/test/mod/mirror.lua b/test/mod/mirror.lua new file mode 100644 index 0000000..325956e --- /dev/null +++ b/test/mod/mirror.lua @@ -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 \ No newline at end of file diff --git a/test/workspace/mirror.lua b/test/workspace/mirror.lua new file mode 100644 index 0000000..58df5ad --- /dev/null +++ b/test/workspace/mirror.lua @@ -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 \ No newline at end of file