1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-17 06:08:31 +01:00

refactor: move funcs into comm

This commit is contained in:
Justin Lin 2022-05-08 11:27:34 +08:00
parent bd34a907c2
commit 07170ed1f0
5 changed files with 65 additions and 110 deletions

View File

@ -1,43 +0,0 @@
use <_lsystem_comm.scad>;
use <../turtle2d.scad>;
function _lsystem2_join(str_lt) = _join(str_lt);
function _lsystem2_derive(axiom, rules, n, rule_prs, seed) = _derive(axiom, rules, n, rule_prs, seed);
function _next_stack(t, code, stack) =
code == "[" ? [t, stack] :
code == "]" && stack != [] ? stack[1] : stack;
function _next_t1(t1, t2, code, stack) =
code == "[" ? t1 :
code == "]" ? stack[0] : t2;
function _next_t2(t, code, angle, leng) =
is_undef(code) || code == "[" || code == "]" ? t :
code == "F" || code == "f" ? turtle2d("forward", t, leng) :
code == "+" ? turtle2d("turn", t, angle) :
code == "-" ? turtle2d("turn", t, -angle) :
code == "|" ? turtle2d("turn", t, 180) : t;
// It doesn't use recursion to avoid recursion error.
function _lines(t, codes, angle, leng) =
let(codes_leng = len(codes))
[
for(
i = 0,
stack = [],
t1 = t,
t2 = _next_t2(t1, codes[i], angle, leng);
i < codes_leng;
t1 = _next_t1(t1, t2, codes[i], stack),
stack = _next_stack(t1, codes[i], stack),
i = i + 1,
t2 = _next_t2(t1, codes[i], angle, leng)
)
let(p1 = turtle2d("pt", t1), p2 = turtle2d("pt", t2))
if(search(codes[i], "F+-") != [] && p1 != p2)
[p1, p2]
];

View File

@ -1,50 +0,0 @@
use <_lsystem_comm.scad>;
use <../turtle3d.scad>;
function _lsystem3_join(str_lt) = _join(str_lt);
function _lsystem3_derive(axiom, rules, n, rule_prs, seed) = _derive(axiom, rules, n, rule_prs, seed);
function _next_stack(t, code, stack) =
code == "[" ? [t, each stack] :
let(leng = len(stack))
code == "]" ?
(leng > 1 ? [for(i = [1:leng - 1]) stack[i]] : []) :
stack;
function _next_t1(t1, t2, code, stack) =
code == "[" ? t1 :
code == "]" ? stack[0] : t2;
function _next_t2(t, code, angle, leng) =
is_undef(code) || code == "[" || code == "]" ? t :
code == "F" || code == "f" ? turtle3d("forward", t, leng) :
code == "+" ? turtle3d("turn", t, angle) :
code == "-" ? turtle3d("turn", t, -angle) :
code == "|" ? turtle3d("turn", t, 180) :
code == "&" ? turtle3d("pitch", t, -angle) :
code == "^" ? turtle3d("pitch", t, angle) :
code == "\\" ? turtle3d("roll", t, angle) :
code == "/" ? turtle3d("roll", t, -angle) : t;
// It doesn't use recursion to avoid recursion error.
function _lines(t, codes, angle, leng) =
let(codes_leng = len(codes))
[
for(
i = 0,
stack = [],
t1 = t,
t2 = _next_t2(t1, codes[i], angle, leng);
i < codes_leng;
t1 = _next_t1(t1, t2, codes[i], stack),
stack = _next_stack(t1, codes[i], stack),
i = i + 1,
t2 = _next_t2(t1, codes[i], angle, leng)
)
let(p1 = turtle3d("pt", t1), p2 = turtle3d("pt", t2))
if(search(codes[i], "F+-") != [] && p1 != p2)
[turtle3d("pt", t1), turtle3d("pt", t2)]
];

View File

@ -1,5 +1,21 @@
use <../../util/rand.scad>;
function _codes(axiom, rules, n, forward_chars, rule_prs, seed) =
let(derived = _derive(axiom, rules, n, rule_prs, seed))
forward_chars == "F" ? derived : _join([
for(c = derived)
let(idx = search(c, forward_chars))
idx == [] ? c : "F"
]);
function _next_stack(t, code, stack) =
code == "[" ? [t, stack] :
code == "]" && stack != [] ? stack[1] : stack;
function _next_t1(t1, t2, code, stack) =
code == "[" ? t1 :
code == "]" ? stack[0] : t2;
function _assoc_lookup(array, key) =
array[search([key], array)[0]][1];
@ -35,4 +51,26 @@ function _derive(base, rules, n, rule_prs, seed) =
b
]
)
bs[len(bs) - 1];
bs[len(bs) - 1];
// It doesn't use recursion to avoid recursion error.
function _lines(t, codes, angle, leng, next_t2, turtle_p) =
let(codes_leng = len(codes))
[
for(
i = 0,
stack = [],
t1 = t,
t2 = next_t2(t1, codes[i], angle, leng);
i < codes_leng;
t1 = _next_t1(t1, t2, codes[i], stack),
stack = _next_stack(t1, codes[i], stack),
i = i + 1,
t2 = next_t2(t1, codes[i], angle, leng)
)
let(p1 = turtle_p(t1), p2 = turtle_p(t2))
if(search(codes[i], "F+-") != [] && p1 != p2)
[p1, p2]
];

View File

@ -8,21 +8,24 @@
*
**/
use <_impl/_lsystem2_impl.scad>;
use <_impl/_lsystem_comm.scad>;
use <turtle2d.scad>;
function lsystem2(axiom, rules, n, angle, leng = 1, heading = 0, start = [0, 0], forward_chars = "F", rule_prs, seed) =
let(
derived = _lsystem2_derive(axiom, rules, n, rule_prs, seed),
codes = forward_chars == "F" ? derived : _lsystem2_join([
for(c = derived)
let(idx = search(c, forward_chars))
idx == [] ? c : "F"
])
codes = _codes(axiom, rules, n, forward_chars, rule_prs, seed),
next_t2 = function(t, code, angle, leng)
is_undef(code) || code == "[" || code == "]" ? t :
code == "F" || code == "f" ? turtle2d("forward", t, leng) :
code == "+" ? turtle2d("turn", t, angle) :
code == "-" ? turtle2d("turn", t, -angle) :
code == "|" ? turtle2d("turn", t, 180) : t
)
_lines(
turtle2d("create", start.x, start.y, heading),
codes,
angle,
leng
leng,
next_t2,
function(t) turtle2d("pt", t)
);

View File

@ -8,21 +8,28 @@
*
**/
use <_impl/_lsystem3_impl.scad>;
use <_impl/_lsystem_comm.scad>;
use <turtle3d.scad>;
function lsystem3(axiom, rules, n, angle, leng = 1, heading = 0, start = [0, 0, 0], forward_chars = "F", rule_prs, seed) =
let(
derived = _lsystem3_derive(axiom, rules, n, rule_prs, seed),
codes = forward_chars == "F" ? derived : _lsystem3_join([
for(c = derived)
let(idx = search(c, forward_chars))
idx == [] ? c : "F"
])
codes = _codes(axiom, rules, n, forward_chars, rule_prs, seed),
_next_t2 = function(t, code, angle, leng)
is_undef(code) || code == "[" || code == "]" ? t :
code == "F" || code == "f" ? turtle3d("forward", t, leng) :
code == "+" ? turtle3d("turn", t, angle) :
code == "-" ? turtle3d("turn", t, -angle) :
code == "|" ? turtle3d("turn", t, 180) :
code == "&" ? turtle3d("pitch", t, -angle) :
code == "^" ? turtle3d("pitch", t, angle) :
code == "\\" ? turtle3d("roll", t, angle) :
code == "/" ? turtle3d("roll", t, -angle) : t
)
_lines(
turtle3d("create", start, [[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
codes,
angle,
leng
leng,
_next_t2,
function(t) turtle3d("pt", t)
);