From cb4eb6f5dfb72aa69bba579dcd6890b0ca08a257 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 8 Apr 2022 11:35:29 +0800 Subject: [PATCH] add maze3d_sphere --- examples/maze/maze3d_sphere.scad | 86 ++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 examples/maze/maze3d_sphere.scad diff --git a/examples/maze/maze3d_sphere.scad b/examples/maze/maze3d_sphere.scad new file mode 100644 index 00000000..bb17d8a3 --- /dev/null +++ b/examples/maze/maze3d_sphere.scad @@ -0,0 +1,86 @@ +use ; +use ; +use ; +use ; +use ; + +radius = 6; +cell_width = 15; +road_width = 6; +$fn = 4; // [4, 8, 12] + +maze3d_sphere(); + +module maze3d_sphere() { + function mask_sphere(radius) = + let(range = [-radius: radius - 1]) + [ + for(z = range) + [ + for(y = range) + [ + for(x = range) + let(v = [x, y, z]) + if(v * v < radius ^ 2) 1 + else 0 + ] + ] + + ]; + + mz = mz_cube_initialize(mask = mask_sphere(radius)); + cells = mz_cube(start = [radius, radius, radius] / 2, init_cells = mz); + + layers = len(cells); + rows = len(cells[0]); + columns = len(cells[0][0]); + + for(z = [0:layers - 1], y = [0:rows - 1], x = [0:columns - 1]) { + cell = cells[z][y][x]; + type = mz_cube_get(cell, "t"); + + if(type != "MASK") { + channels = [ + z_road(type) , + z != 0 && z_road(mz_cube_get(cells[z - 1][y][x], "t")), + y_road(type), + x_road(type), + y != 0 && y_road(mz_cube_get(cells[z][y - 1][x], "t")), + x != 0 && x_road(mz_cube_get(cells[z][y][x - 1], "t")) + ]; + + translate([x, y, z] * cell_width) + drawCell(cell_width, road_width, channels); + } + } + + function z_road(type) = !has(["Z_WALL", "Z_Y_WALL", "Z_X_WALL", "Z_Y_X_WALL", "MASK"], type); + + function y_road(type) = !has(["Y_WALL", "Y_X_WALL", "Z_Y_WALL", "Z_Y_X_WALL", "MASK"], type); + + function x_road(type) = !has(["X_WALL", "Y_X_WALL", "Z_X_WALL", "Z_Y_X_WALL", "MASK"], type); + + module drawCell(cell_width, road_width, channels) { + half_cw = cell_width / 2; + half_rw = road_width / 2; + + crystal_ball(half_rw); + + rots = [ + [0, 0, 0], + [180, 0, 0], + [-90, 0, 0], + [0, 90, 0], + [90, 0, 0], + [0, -90, 0] + ]; + + for(i = [0:5]) { + if(channels[i]) { + rotate(rots[i]) + linear_extrude(half_cw) + circle(half_rw); + } + } + } +} \ No newline at end of file