diff --git a/src/experimental/_impl/_geom_platonic_polyhedra.scad b/src/experimental/_impl/_geom_platonic_polyhedra.scad new file mode 100644 index 00000000..4527f314 --- /dev/null +++ b/src/experimental/_impl/_geom_platonic_polyhedra.scad @@ -0,0 +1,56 @@ +use <__comm__/_pt3_hash.scad>; +use ; +use ; +use ; +use ; + +function _geom_prj2sphere(t, r) = [for(p = t) p / norm(p) * r]; + +function _pimap_pts(radius, points, leng, hash, m, deduped_pts = [], n = -1, i = 0) = + i == leng ? [m, deduped_pts] : + let(v = hashmap_get(m, points[i], hash = hash)) + is_undef(v) ? + _pimap_pts(radius, points, leng, hash, hashmap_put(m, points[i], n + 1, hash = hash), concat(deduped_pts, [points[i] / norm(points[i]) * radius]), n + 1, i + 1) : + _pimap_pts(radius, points, leng, hash, m, deduped_pts, n, i + 1); + +function _geom_pts_faces(points, radius) = + let( + number_of_buckets = ceil(sqrt(len(points)) * 1.5), + hash = function(p) _pt3_hash(p), + leng = len(points), + m_pts = _pimap_pts( + radius, + points, + leng, + hash, + hashmap(number_of_buckets = number_of_buckets) + ), + faces = [ + for(i = [0:3:leng - 3]) + [ + hashmap_get(m_pts[0], points[i], hash = hash), + hashmap_get(m_pts[0], points[i + 1], hash = hash), + hashmap_get(m_pts[0], points[i + 2], hash = hash) + ] + ] + ) + [m_pts[1], faces]; + +function _geom_info(tris, radius, detail) = + let( + points = detail == 0 ? [for(tri = tris) each tri] : [ + for(tri = tris) + each [for(t = tri_subdivide(tri, detail)) each t] + ] + ) + _geom_pts_faces(points, radius); + +function _geom_info_quick(tris, radius, detail) = + let( + points = detail == 0 ? [for(tri = tris) each _geom_prj2sphere(tri, radius)] : [ + for(tri = tris) + each [for(t = tri_subdivide(tri, detail)) each _geom_prj2sphere(t, radius)] + ], + faces = [for(i = [0:3:len(points) - 3]) [i, i + 1, i + 2]] + ) + [points, faces]; \ No newline at end of file diff --git a/src/experimental/geom_icosahedron.scad b/src/experimental/geom_icosahedron.scad index b3010379..ed4b0a4b 100644 --- a/src/experimental/geom_icosahedron.scad +++ b/src/experimental/geom_icosahedron.scad @@ -1,55 +1,5 @@ -use <__comm__/_pt3_hash.scad>; -use ; -use ; -use ; -use ; -use ; -function _prj2sphere(t, r) = [for(p = t) p / norm(p) * r]; - -function _pimap_pts(radius, points, leng, hash, m, deduped_pts = [], n = -1, i = 0) = - i == leng ? [m, deduped_pts] : - let(v = hashmap_get(m, points[i], hash = hash)) - is_undef(v) ? - _pimap_pts(radius, points, leng, hash, hashmap_put(m, points[i], n + 1, hash = hash), concat(deduped_pts, [points[i] / norm(points[i]) * radius]), n + 1, i + 1) : - _pimap_pts(radius, points, leng, hash, m, deduped_pts, n, i + 1); - -function _geom_icosahedron(icosahedron_points, icosahedron_faces, tris, radius, detail) = - let( - points = detail == 0 ? [for(tri = tris) each tri] : [ - for(tri = tris) - each [for(t = tri_subdivide(tri, detail)) each t] - ], - number_of_buckets = ceil(sqrt(len(points)) * 1.5), - hash = function(p) _pt3_hash(p), - leng = len(points), - m_pts = _pimap_pts( - radius, - points, - leng, - hash, - hashmap(number_of_buckets = number_of_buckets) - ), - faces = [ - for(i = [0:3:leng - 3]) - [ - hashmap_get(m_pts[0], points[i], hash = hash), - hashmap_get(m_pts[0], points[i + 1], hash = hash), - hashmap_get(m_pts[0], points[i + 2], hash = hash) - ] - ] - ) - [m_pts[1], faces]; - -function _geom_icosahedron_quick(icosahedron_points, icosahedron_faces, tris, radius, detail) = - let( - points = detail == 0 ? [for(tri = tris) each _prj2sphere(tri, radius)] : [ - for(tri = tris) - each [for(t = tri_subdivide(tri, detail)) each _prj2sphere(t, radius)] - ], - faces = [for(i = [0:3:len(points) - 3]) [i, i + 1, i + 2]] - ) - [points, faces]; +use ; function geom_icosahedron(radius, detail = 0, quick_mode = true) = let( @@ -70,6 +20,6 @@ function geom_icosahedron(radius, detail = 0, quick_mode = true) = [for(i = face) icosahedron_points[i]] ] ) - quick_mode ? _geom_icosahedron_quick(icosahedron_points, icosahedron_faces, tris, radius, detail) : - _geom_icosahedron(icosahedron_points, icosahedron_faces, tris, radius, detail); + quick_mode ? _geom_info_quick(tris, radius, detail) : + _geom_info(tris, radius, detail); \ No newline at end of file