From 3fd7fd60d9eed5a604537755b5dc12f5302197f9 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 5 May 2022 15:54:08 +0800 Subject: [PATCH] optimization --- .../_impl/_geom_platonic_polyhedra.scad | 67 +++++++++---------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/polyhedra/_impl/_geom_platonic_polyhedra.scad b/src/polyhedra/_impl/_geom_platonic_polyhedra.scad index 3d1d97f8..292dcafd 100644 --- a/src/polyhedra/_impl/_geom_platonic_polyhedra.scad +++ b/src/polyhedra/_impl/_geom_platonic_polyhedra.scad @@ -1,45 +1,44 @@ -function _tri_subdivide(points, detail) = +function _tri_subdivide_pts(points, radius, rows) = let( - rows = detail + 1, p0 = points[0], - basis = [points[2] - p0, points[1] - p0] / rows, - pts = [ - for(ri = [0:rows], ci = [0:rows - ri]) - p0 + [ri, ci] * basis - ], - ri_base = [for(ri = 0, row_2 = rows + 2; ri <= rows; ri = ri + 1) ri * row_2 - ri * (ri + 1) * 0.5], - faces = [ - for(ri = [0:rows - 1], base_i = ri_base[ri], base_i1 = ri_base[ri + 1]) - let(cols = rows - ri - 1) - for(ci = [0:cols]) - if(ci != cols) - each [ - [ci + 1 + base_i, ci + base_i1, ci + base_i], - [ci + 1 + base_i1, ci + base_i1, ci + 1 + base_i] - ] - else - [ci + 1 + base_i, ci + base_i1, ci + base_i] - ] + basis = [points[2] - p0, points[1] - p0] / rows ) - [pts, faces]; + [ + for(ri = [0:rows], ci = [0:rows - ri]) + let(p = p0 + [ri, ci] * basis) + radius * p / norm(p) + ]; + +function _tri_subdivide_faces(rows) = + let( + ri_base = [for(ri = 0, row_2 = rows + 2; ri <= rows; ri = ri + 1) ri * row_2 - ri * (ri + 1) * 0.5] + ) + [ + for(ri = [0:rows - 1], base_i = ri_base[ri], base_i1 = ri_base[ri + 1]) + let(cols = rows - ri - 1) + for(ci = [0:cols]) + if(ci != cols) + each [ + [ci + 1 + base_i, ci + base_i1, ci + base_i], + [ci + 1 + base_i1, ci + base_i1, ci + 1 + base_i] + ] + else + [ci + 1 + base_i, ci + base_i1, ci + base_i] + ]; function _subdivide_project(points, faces, radius, detail) = - detail == 0 ? [[for(p = points) p / norm(p) * radius], faces] : + detail == 0 ? [points * radius / norm(points[0]), faces] : let( - subdivided_all = [ + rows = detail + 1, + subdivided_pts = [ for(face = faces) - _tri_subdivide([for(i = face) points[i]], detail) + each _tri_subdivide_pts([for(i = face) points[i]], radius, rows) ], - flatten_points = [ - for(pts_faces = subdivided_all, p = pts_faces[0]) - p / norm(p) * radius - ], - pts_number_per_tri = len(subdivided_all[0][0]), + pts_number_per_tri = (rows + 1) * (rows + 2) / 2, + subdivided_faces = _tri_subdivide_faces(rows), flatten_faces = [ - for(i = [0:len(subdivided_all) - 1]) - let(faces = subdivided_all[i][1], off = [i, i, i] * pts_number_per_tri) - for(face = faces) - face + off + for(i = [0:len(faces) - 1], face = subdivided_faces) + face + [i, i, i] * pts_number_per_tri ] ) - [flatten_points, flatten_faces]; \ No newline at end of file + [subdivided_pts, flatten_faces]; \ No newline at end of file