1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-11 09:14:29 +02:00

refactor: extract constants

This commit is contained in:
Justin Lin
2022-04-19 16:06:33 +08:00
parent 68f73a808a
commit 1e2d5f5d39
16 changed files with 122 additions and 99 deletions

View File

@@ -1,12 +1,4 @@
// NO_WALL = 0; include <_mz_cube_constants.scad>;
// Y_WALL = 1;
// X_WALL = 2;
// Y_X_WALL = 3;
// Z_WALL = 4;
// Z_Y_WALL = 5;
// Z_X_WALL = 6;
// Z_Y_X_WALL = 7;
// MASK = 8;
function no_wall(cell) = get_type(cell) == 0; function no_wall(cell) = get_type(cell) == 0;
function y_wall(cell) = get_type(cell) == 1; function y_wall(cell) = get_type(cell) == 1;

View File

@@ -0,0 +1,15 @@
NO_WALL = 0;
Y_WALL = 1;
X_WALL = 2;
Y_X_WALL = 3;
Z_WALL = 4;
Z_Y_WALL = 5;
Z_X_WALL = 6;
Z_Y_X_WALL = 7;
MASK = 8;
VISITED = true;
UNVISITED = false;
// enumeration
CELL_TYPE = ["NO_WALL", "Y_WALL", "X_WALL", "Y_X_WALL", "Z_WALL", "Z_Y_WALL", "Z_X_WALL", "Z_Y_X_WALL", "MASK"];

View File

@@ -2,6 +2,8 @@ use <_mz_cube_comm.scad>;
use <../../util/shuffle.scad>; use <../../util/shuffle.scad>;
use <../../matrix/m_replace.scad>; use <../../matrix/m_replace.scad>;
include <_mz_cube_constants.scad>;
function update(cells, cell) = function update(cells, cell) =
let( let(
x = cell.x, x = cell.x,
@@ -22,7 +24,7 @@ function visitable(x, y, z, cells, layers, rows, columns) =
!visited(x, y, z, cells); // unvisited !visited(x, y, z, cells); // unvisited
// setting (x, y) as being visited // setting (x, y) as being visited
function set_visited(x, y, z, cells) = update(cells, [x, y, z, get_type(cells[z][y][x]), true]); function set_visited(x, y, z, cells) = update(cells, [x, y, z, get_type(cells[z][y][x]), VISITED]);
// 0(right), 1(top), 2(left), 3(bottom), 4(up), 5(down) // 0(right), 1(top), 2(left), 3(bottom), 4(up), 5(down)
function rand_dirs(c, seed) = function rand_dirs(c, seed) =
@@ -52,9 +54,9 @@ function carve_right(x, y, z, cells) =
let(cell = cells[z][y][x]) let(cell = cells[z][y][x])
update( update(
cells, cells,
z_y_x_wall(cell) ? [x, y, z, 5, true] : z_y_x_wall(cell) ? [x, y, z, Z_Y_WALL, VISITED] :
z_x_wall(cell) ? [x, y, z, 4, true] : z_x_wall(cell) ? [x, y, z, Z_WALL, VISITED] :
y_x_wall(cell) ? [x, y, z, 1, true] : [x, y, z, 0, true] y_x_wall(cell) ? [x, y, z, Y_WALL, VISITED] : [x, y, z, NO_WALL, VISITED]
); );
// go top and carve the top wall // go top and carve the top wall
@@ -62,9 +64,9 @@ function carve_top(x, y, z, cells) =
let(cell = cells[z][y][x]) let(cell = cells[z][y][x])
update( update(
cells, cells,
z_y_x_wall(cell) ? [x, y, z, 6, true] : z_y_x_wall(cell) ? [x, y, z, Z_X_WALL, VISITED] :
z_y_wall(cell) ? [x, y, z, 4, true] : z_y_wall(cell) ? [x, y, z, Z_WALL, VISITED] :
y_x_wall(cell) ? [x, y, z, 2, true] : [x, y, z, 0, true] y_x_wall(cell) ? [x, y, z, X_WALL, VISITED] : [x, y, z, NO_WALL, VISITED]
); );
// go up and carve the up wall // go up and carve the up wall
@@ -72,9 +74,9 @@ function carve_up(x, y, z, cells) =
let(cell = cells[z][y][x]) let(cell = cells[z][y][x])
update( update(
cells, cells,
z_y_x_wall(cell) ? [x, y, z, 3, true] : z_y_x_wall(cell) ? [x, y, z, Y_X_WALL, VISITED] :
z_y_wall(cell) ? [x, y, z, 1, true] : z_y_wall(cell) ? [x, y, z, Y_WALL, VISITED] :
z_x_wall(cell) ? [x, y, z, 2, true] : [x, y, z, 0, true] z_x_wall(cell) ? [x, y, z, X_WALL, VISITED] : [x, y, z, NO_WALL, VISITED]
); );
// go left and carve the right wall of the left cell // go left and carve the right wall of the left cell
@@ -85,7 +87,7 @@ function carve_left(x, y, z, cells, columns) =
) )
update( update(
cells, cells,
[nx, y, z, 5, false] [nx, y, z, Z_Y_WALL, UNVISITED]
); );
// go bottom and carve the top wall of the bottom cell // go bottom and carve the top wall of the bottom cell
@@ -96,7 +98,7 @@ function carve_bottom(x, y, z, cells, rows) =
) )
update( update(
cells, cells,
[x, ny, z, 6, false] [x, ny, z, Z_X_WALL, UNVISITED]
); );
// go down and carve the up wall of the down cell // go down and carve the up wall of the down cell
@@ -107,7 +109,7 @@ function carve_down(x, y, z, cells, layers) =
) )
update( update(
cells, cells,
[x, y, nz, 3, false] [x, y, nz, Y_X_WALL, UNVISITED]
); );
// 0(right), 1(top), 2(left), 3(bottom), 4(up), 5(down) // 0(right), 1(top), 2(left), 3(bottom), 4(up), 5(down)

View File

@@ -1,5 +1,7 @@
use <_mz_cube_comm.scad>; use <_mz_cube_comm.scad>;
include <_mz_cube_constants.scad>;
// create a starting maze for being visited later. // create a starting maze for being visited later.
function _lrc_maze(layers, rows, columns) = function _lrc_maze(layers, rows, columns) =
[ [
@@ -11,9 +13,9 @@ function _lrc_maze(layers, rows, columns) =
cell( cell(
x, y, z, x, y, z,
// all cells have up/top/right walls // all cells have up/top/right walls
7, Z_Y_X_WALL,
// unvisited // unvisited
false UNVISITED
) )
] ]
] ]
@@ -32,14 +34,14 @@ function _mz_mask(mask) =
mask[layers - z - 1][rows - y - 1][x] == 0 ? mask[layers - z - 1][rows - y - 1][x] == 0 ?
cell( cell(
x, y, z, x, y, z,
8, // mask MASK,
true // visited VISITED // visited
) )
: :
cell( cell(
x, y, z, x, y, z,
7, // all cells have up/top/right walls Z_Y_X_WALL,
false // unvisited UNVISITED // unvisited
) )
] ]
] ]

View File

@@ -1,12 +1,4 @@
// NO_WALL = 0; include <_impl/_mz_cube_constants.scad>;
// Y_WALL = 1;
// X_WALL = 2;
// Y_X_WALL = 3;
// Z_WALL = 4;
// Z_Y_WALL = 5;
// Z_X_WALL = 6;
// Z_Y_X_WALL = 7;
// MASK = 8;
function mz_cube_get(cell, query) = function mz_cube_get(cell, query) =
let( let(
@@ -17,4 +9,4 @@ function mz_cube_get(cell, query) =
["t", 3] ["t", 3]
])[0] ])[0]
) )
i != 3 ? cell[i] : ["NO_WALL", "Y_WALL", "X_WALL", "Y_X_WALL", "Z_WALL", "Z_Y_WALL", "Z_X_WALL", "Z_Y_X_WALL", "MASK"][cell[i]]; i != 3 ? cell[i] : CELL_TYPE[cell[i]];

View File

@@ -1,13 +1,4 @@
use <../mz_square_get.scad>; use <_mz_square_comm.scad>;
function _get_x(cell) = mz_square_get(cell, "x");
function _get_y(cell) = mz_square_get(cell, "y");
function _get_type(cell) = mz_square_get(cell, "t");
function _is_top_wall(cell) = _get_type(cell) == "TOP_WALL";
function _is_right_wall(cell) = _get_type(cell) == "RIGHT_WALL";
function _is_top_right_wall(cell) = _get_type(cell) == "TOP_RIGHT_WALL";
function _is_mask(cell) = _get_type(cell) == "MASK";
function _cell_position(cell_radius, x_cell, y_cell) = function _cell_position(cell_radius, x_cell, y_cell) =
let( let(
@@ -16,30 +7,33 @@ function _cell_position(cell_radius, x_cell, y_cell) =
) )
[grid_w * x_cell, grid_h * y_cell + (x_cell % 2 == 0 ? 0 : grid_h / 2), 0]; [grid_w * x_cell, grid_h * y_cell + (x_cell % 2 == 0 ? 0 : grid_h / 2), 0];
function _hex_seg(cell_radius, begin, end) = [for(a = [begin:60:end]) function _hex_seg(cell_radius, begin, end) =
[cell_radius * cos(a), cell_radius * sin(a)]]; [
for(a = [begin:60:end])
[cell_radius * cos(a), cell_radius * sin(a)]
];
function _top_right(cell_radius) = _hex_seg(cell_radius, 0, 60); function _build_top_right(cell_radius) = _hex_seg(cell_radius, 0, 60);
function _top(cell_radius) = _hex_seg(cell_radius, 60, 120); function _build_top(cell_radius) = _hex_seg(cell_radius, 60, 120);
function _top_left(cell_radius) = _hex_seg(cell_radius, 120, 180); function _build_top_left(cell_radius) = _hex_seg(cell_radius, 120, 180);
function _bottom_left(cell_radius) = _hex_seg(cell_radius, 180, 240); function _build_bottom_left(cell_radius) = _hex_seg(cell_radius, 180, 240);
function _bottom(cell_radius) = _hex_seg(cell_radius, 240, 300); function _build_bottom(cell_radius) = _hex_seg(cell_radius, 240, 300);
function _bottom_right(cell_radius) = _hex_seg(cell_radius, 300, 360); function _build_bottom_right(cell_radius) = _hex_seg(cell_radius, 300, 360);
function _right_wall(cell_radius, x_cell) = function _right_wall(cell_radius, x_cell) =
(x_cell % 2 != 0) ? _bottom_right(cell_radius) : _top_right(cell_radius); (x_cell % 2 != 0) ? _build_bottom_right(cell_radius) : _build_top_right(cell_radius);
function _row_wall(cell_radius, x_cell, y_cell) = function _row_wall(cell_radius, x_cell, y_cell) =
x_cell % 2 != 0 ? [_top_right(cell_radius), _top_left(cell_radius)] : [_bottom_right(cell_radius)]; x_cell % 2 != 0 ? [_build_top_right(cell_radius), _build_top_left(cell_radius)] : [_build_bottom_right(cell_radius)];
function _build_cell(cell_radius, cell) = function _build_cell(cell_radius, cell) =
let( let(
x = _get_x(cell), x = get_x(cell),
y = _get_y(cell), y = get_y(cell),
walls = [ walls = [
if(!_is_mask(cell)) each _row_wall(cell_radius, x, y), if(!mask(cell)) each _row_wall(cell_radius, x, y),
if(_is_top_wall(cell) || _is_top_right_wall(cell)) _top(cell_radius), if(top_wall(cell) || top_right_wall(cell)) _build_top(cell_radius),
if(_is_right_wall(cell) || _is_top_right_wall(cell)) _right_wall(cell_radius, x) if(right_wall(cell) || top_right_wall(cell)) _right_wall(cell_radius, x)
], ],
cell_p = _cell_position(cell_radius, x, y) cell_p = _cell_position(cell_radius, x, y)
) )

View File

@@ -0,0 +1,11 @@
NO_WALL = 0;
TOP_WALL = 1;
RIGHT_WALL = 2;
TOP_RIGHT_WALL = 3;
MASK = 4;
VISITED = true;
UNVISITED = false;
// emuneration
CELL_TYPE = ["NO_WALL", "TOP_WALL", "RIGHT_WALL", "TOP_RIGHT_WALL", "MASK"];

View File

@@ -1,6 +1,8 @@
use <_mz_square_comm.scad>; use <_mz_square_comm.scad>;
use <../../matrix/m_replace.scad>; use <../../matrix/m_replace.scad>;
include <_mz_square_cell_constants.scad>;
// is (x, y) visited? // is (x, y) visited?
function visited(x, y, cells) = cells[y][x][3]; function visited(x, y, cells) = cells[y][x][3];
@@ -12,7 +14,7 @@ function visitable(x, y, cells, rows, columns) =
// setting (x, y) as being visited // setting (x, y) as being visited
function set_visited(x, y, cells) = function set_visited(x, y, cells) =
m_replace(cells, x, y, [x, y, get_type(cells[y][x]), true]); m_replace(cells, x, y, [x, y, get_type(cells[y][x]), VISITED]);
// 0(right), 1(top), 2(left), 3(bottom) // 0(right), 1(top), 2(left), 3(bottom)
_rand_dir_table = [ _rand_dir_table = [
@@ -59,11 +61,11 @@ function next_y(y, dir, rows, wrapping) =
// go right and carve the right wall // go right and carve the right wall
function carve_right(x, y, cells) = function carve_right(x, y, cells) =
m_replace(cells, x, y, top_right_wall(cells[y][x]) ? [x, y, 1, true] : [x, y, 0, true]); m_replace(cells, x, y, top_right_wall(cells[y][x]) ? [x, y, TOP_WALL, VISITED] : [x, y, NO_WALL, VISITED]);
// go up and carve the top wall // go up and carve the top wall
function carve_top(x, y, cells) = function carve_top(x, y, cells) =
m_replace(cells, x, y, top_right_wall(cells[y][x]) ? [x, y, 2, true] : [x, y, 0, true]); m_replace(cells, x, y, top_right_wall(cells[y][x]) ? [x, y, RIGHT_WALL, VISITED] : [x, y, NO_WALL, VISITED]);
// go left and carve the right wall of the left cell // go left and carve the right wall of the left cell
function carve_left(x, y, cells, columns) = function carve_left(x, y, cells, columns) =
@@ -71,7 +73,7 @@ function carve_left(x, y, cells, columns) =
x_minus_one = x - 1, x_minus_one = x - 1,
nx = x_minus_one < 0 ? x_minus_one + columns : x_minus_one nx = x_minus_one < 0 ? x_minus_one + columns : x_minus_one
) )
m_replace(cells, nx, y, [nx, y, 1, false]); m_replace(cells, nx, y, [nx, y, TOP_WALL, UNVISITED]);
// go down and carve the top wall of the bottom cell // go down and carve the top wall of the bottom cell
function carve_bottom(x, y, cells, rows) = function carve_bottom(x, y, cells, rows) =
@@ -79,7 +81,7 @@ function carve_bottom(x, y, cells, rows) =
y_minus_one = y - 1, y_minus_one = y - 1,
ny = y_minus_one < 0 ? y_minus_one + rows : y_minus_one ny = y_minus_one < 0 ? y_minus_one + rows : y_minus_one
) )
m_replace(cells, x, ny, [x, ny, 2, false]); m_replace(cells, x, ny, [x, ny, RIGHT_WALL, UNVISITED]);
// 0(right), 1(top), 2(left), 3(bottom) // 0(right), 1(top), 2(left), 3(bottom)
function carve(dir, x, y, cells, rows, columns) = function carve(dir, x, y, cells, rows, columns) =

View File

@@ -1,13 +1,10 @@
// NO_WALL = 0; include <_mz_square_cell_constants.scad>;
// TOP_WALL = 1;
// RIGHT_WALL = 2;
// TOP_RIGHT_WALL = 3;
// MASK = 4;
function no_wall(cell) = get_type(cell) == 0; function no_wall(cell) = get_type(cell) == NO_WALL;
function top_wall(cell) = get_type(cell) == 1; function top_wall(cell) = get_type(cell) == TOP_WALL;
function right_wall(cell) = get_type(cell) == 2; function right_wall(cell) = get_type(cell) == RIGHT_WALL;
function top_right_wall(cell) = get_type(cell) == 3; function top_right_wall(cell) = get_type(cell) == TOP_RIGHT_WALL;
function mask(cell) = get_type(cell) == MASK;
function cell(x, y, type, visited) = [x, y, type, visited]; function cell(x, y, type, visited) = [x, y, type, visited];
function get_x(cell) = cell.x; function get_x(cell) = cell.x;

View File

@@ -1,5 +1,7 @@
use <_mz_square_comm.scad>; use <_mz_square_comm.scad>;
include <_mz_square_cell_constants.scad>;
// create a starting maze for being visited later. // create a starting maze for being visited later.
function _rc_maze(rows, columns) = [ function _rc_maze(rows, columns) = [
for(y = [0:rows - 1]) for(y = [0:rows - 1])
@@ -8,9 +10,8 @@ function _rc_maze(rows, columns) = [
cell( cell(
x, y, x, y,
// all cells have top and right walls // all cells have top and right walls
3, TOP_RIGHT_WALL,
// unvisited UNVISITED
false
) )
] ]
]; ];
@@ -26,15 +27,14 @@ function _mz_mask(mask) =
mask[rows - y - 1][x] == 0 ? mask[rows - y - 1][x] == 0 ?
cell( cell(
x, y, x, y,
4, // mask MASK,
true // visited VISITED
) )
: :
cell( cell(
x, y, x, y,
// all cells have top and right walls TOP_RIGHT_WALL,
3, // unvisited UNVISITED
false
) )
] ]
]; ];

View File

@@ -0,0 +1,10 @@
NO_WALL = 0;
INWARD_WALL = 1;
CCW_WALL = 2;
INWARD_CCW_WALL = 3;
UNVISITED = true;
VISITED = false;
// emuneration
CELL_TYPE = ["NO_WALL", "INWARD_WALL", "CCW_WALL", "INWARD_CCW_WALL"];

View File

@@ -5,8 +5,10 @@ NO_WALL = 0;
INWARD_WALL = 1; INWARD_WALL = 1;
CCW_WALL = 2; CCW_WALL = 2;
INWARD_CCW_WALL = 3; INWARD_CCW_WALL = 3;
UNVISITED = true;
VISITED = false;
function cell(ri, ci, wallType, notVisited = true, inward = undef, outwards = undef, cw = undef, ccw = undef) = function cell(ri, ci, wallType, notVisited = UNVISITED, inward = undef, outwards = undef, cw = undef, ccw = undef) =
[ri, ci, wallType, notVisited, inward, outwards, cw, ccw]; [ri, ci, wallType, notVisited, inward, outwards, cw, ccw];
function get_ri(cell) = cell[0]; function get_ri(cell) = cell[0];
@@ -22,7 +24,7 @@ function set_wallType(cell, wallType) = [
]; ];
function set_visited(cell) = [ function set_visited(cell) = [
cell[0], cell[1], cell[2], false, cell[4], cell[5], cell[6], cell[7] cell[0], cell[1], cell[2], VISITED, cell[4], cell[5], cell[6], cell[7]
]; ];
function set_outwards(cell, outwards) = [ function set_outwards(cell, outwards) = [
@@ -213,12 +215,12 @@ function visitIN(maze, next, currentCell) =
function visitOUT(maze, next, currentCell) = update_maze( function visitOUT(maze, next, currentCell) = update_maze(
maze, maze,
cell(next[0], next[1], CCW_WALL, false, next[4], next[5], next[6], next[7]) cell(next[0], next[1], CCW_WALL, VISITED, next[4], next[5], next[6], next[7])
); );
function visitCW(maze, next, currentCell) = update_maze( function visitCW(maze, next, currentCell) = update_maze(
maze, maze,
cell(next[0], next[1], INWARD_WALL, false, next[4], next[5], next[6], next[7]) cell(next[0], next[1], INWARD_WALL, VISITED, next[4], next[5], next[6], next[7])
); );
function visitCCW(maze, next, currentCell) = function visitCCW(maze, next, currentCell) =

View File

@@ -18,8 +18,8 @@ function mz_hex_walls(cells, rows, columns, cell_radius, left_border = true, bot
for(y = [0:rows - 1]) for(y = [0:rows - 1])
let( let(
cell_p = _cell_position(cell_radius, 0, y), cell_p = _cell_position(cell_radius, 0, y),
walls1 = _top_left(cell_radius), walls1 = _build_top_left(cell_radius),
walls2 = _bottom_left(cell_radius) walls2 = _build_bottom_left(cell_radius)
) )
each [ each [
[walls1[0] + cell_p, walls1[1] + cell_p], [walls1[0] + cell_p, walls1[1] + cell_p],
@@ -31,12 +31,12 @@ function mz_hex_walls(cells, rows, columns, cell_radius, left_border = true, bot
for(x = [0:columns - 1]) for(x = [0:columns - 1])
let( let(
cell_p = _cell_position(cell_radius, x, 0), cell_p = _cell_position(cell_radius, x, 0),
walls1 = _bottom(cell_radius) walls1 = _build_bottom(cell_radius)
) )
each [ each [
[walls1[0] + cell_p, walls1[1] + cell_p], [walls1[0] + cell_p, walls1[1] + cell_p],
if(x % 2 == 0) if(x % 2 == 0)
let(walls2 = [each _bottom_left(cell_radius), each _bottom_right(cell_radius)]) let(walls2 = [each _build_bottom_left(cell_radius), each _build_bottom_right(cell_radius)])
[walls2[0] + cell_p, walls2[1] + cell_p] [walls2[0] + cell_p, walls2[1] + cell_p]
] ]
] ]

View File

@@ -22,8 +22,8 @@ function mz_hexwalls(cells, cell_radius, left_border = true, bottom_border = tru
for(y = [0:rows - 1]) for(y = [0:rows - 1])
let( let(
cell_p = _cell_position(cell_radius, 0, y), cell_p = _cell_position(cell_radius, 0, y),
walls1 = _top_left(cell_radius), walls1 = _build_top_left(cell_radius),
walls2 = _bottom_left(cell_radius) walls2 = _build_bottom_left(cell_radius)
) )
each [ each [
[walls1[0] + cell_p, walls1[1] + cell_p], [walls1[0] + cell_p, walls1[1] + cell_p],
@@ -35,12 +35,12 @@ function mz_hexwalls(cells, cell_radius, left_border = true, bottom_border = tru
for(x = [0:columns - 1]) for(x = [0:columns - 1])
let( let(
cell_p = _cell_position(cell_radius, x, 0), cell_p = _cell_position(cell_radius, x, 0),
walls1 = _bottom(cell_radius) walls1 = _build_bottom(cell_radius)
) )
each [ each [
[walls1[0] + cell_p, walls1[1] + cell_p], [walls1[0] + cell_p, walls1[1] + cell_p],
if(x % 2 == 0) if(x % 2 == 0)
let(walls2 = [each _bottom_left(cell_radius), each _bottom_right(cell_radius)]) let(walls2 = [each _build_bottom_left(cell_radius), each _build_bottom_right(cell_radius)])
[walls2[0] + cell_p, walls2[1] + cell_p] [walls2[0] + cell_p, walls2[1] + cell_p]
] ]
] ]

View File

@@ -8,6 +8,8 @@
* *
**/ **/
include <_impl/_mz_square_cell_constants.scad>;
function mz_square_get(cell, query) = function mz_square_get(cell, query) =
let( let(
i = search(query, [ i = search(query, [
@@ -16,4 +18,4 @@ function mz_square_get(cell, query) =
["t", 2] ["t", 2]
])[0] ])[0]
) )
i != 2 ? cell[i] : ["NO_WALL", "TOP_WALL", "RIGHT_WALL", "TOP_RIGHT_WALL", "MASK"][cell[i]]; i != 2 ? cell[i] : CELL_TYPE[cell[i]];

View File

@@ -8,6 +8,8 @@
* *
**/ **/
include <_impl/_mz_theta_cell_constants.scad>;
function mz_theta_get(cell, query) = function mz_theta_get(cell, query) =
let( let(
i = search(query, [ i = search(query, [
@@ -16,4 +18,4 @@ function mz_theta_get(cell, query) =
["t", 2] ["t", 2]
])[0] ])[0]
) )
i != 2 ? cell[i] : ["NO_WALL", "INWARD_WALL", "CCW_WALL", "INWARD_CCW_WALL"][cell[i]]; i != 2 ? cell[i] : CELL_TYPE[cell[i]];