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

refactor: optimization

This commit is contained in:
Justin Lin
2022-04-16 21:45:40 +08:00
parent dc6d5f8065
commit 97a7f38969

View File

@@ -8,8 +8,6 @@
*
**/
use <__comm__/__to3d.scad>;
use <matrix/m_scaling.scad>;
use <matrix/m_translation.scad>;
use <matrix/m_rotation.scad>;
@@ -17,24 +15,35 @@ use <matrix/m_rotation.scad>;
function cross_sections(shape_pts, path_pts, angles, twist = 0, scale = 1.0) =
let(
len_path_pts_minus_one = len(path_pts) - 1,
sh_pts = len(shape_pts[0]) == 3 ? shape_pts : [for(p = shape_pts) __to3d(p)],
pth_pts = len(path_pts[0]) == 3 ? path_pts : [for(p = path_pts) __to3d(p)],
scale_step_vt = is_num(scale) ?
[(scale - 1) / len_path_pts_minus_one, (scale - 1) / len_path_pts_minus_one] :
[(scale[0] - 1) / len_path_pts_minus_one, (scale[1] - 1) / len_path_pts_minus_one],
twist_step = twist / len_path_pts_minus_one
sh_pts = len(shape_pts[0]) == 3 ? [for(p = shape_pts) [each p, 1]] : [for(p = shape_pts) [each p, 0, 1]],
pth_pts = len(path_pts[0]) == 3 ? path_pts : [for(p = path_pts) [each p, 0]]
)
twist == 0 && scale == 1.0 ?
[
for(i = [0:len_path_pts_minus_one])
let(transform_m = m_translation(pth_pts[i]) * m_rotation(angles[i]))
[
for(p = sh_pts)
let(transformed = transform_m * p)
[transformed.x, transformed.y, transformed.z]
]
] :
let(
twist_step = twist / len_path_pts_minus_one,
scale_step_vt = (is_num(scale) ? let(s = scale - 1) [s, s, 0] : ([each scale, 0] - [1, 1, 0])) / len_path_pts_minus_one,
one_s = [1, 1, 1]
)
[
for(i = 0; i <= len_path_pts_minus_one; i = i + 1)
for(i = [0:len_path_pts_minus_one])
let(
transform_m = m_translation(pth_pts[i]) *
m_rotation(angles[i]) *
m_rotation(twist_step * i) *
m_scaling([1 + scale_step_vt.x * i, 1 + scale_step_vt.y * i, 1])
m_scaling(scale_step_vt * i + one_s)
)
[
for(p = sh_pts)
let(transformed = transform_m * [each p, 1])
let(transformed = transform_m * p)
[transformed.x, transformed.y, transformed.z]
]
];