From a9938bfcfddfa194c2027cba36dc0b9695e7a15c Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Mon, 17 Jan 2022 16:57:18 +0800 Subject: [PATCH] add voronoi_sphere --- examples/voronoi/voronoi_sphere.scad | 77 ++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/voronoi/voronoi_sphere.scad diff --git a/examples/voronoi/voronoi_sphere.scad b/examples/voronoi/voronoi_sphere.scad new file mode 100644 index 00000000..d099dcf3 --- /dev/null +++ b/examples/voronoi/voronoi_sphere.scad @@ -0,0 +1,77 @@ +use ; +use ; +use ; +use ; +use ; + +n = 60; +radius = 5; + +// pts = fibonacci_lattice(n, radius); +pts = [ + for(i = [0:n]) + let( + theta = rands(0, 360, 1)[0], + phi = rands(5, 175, 1)[0], + rs = radius * sin(phi) + ) + [ + rs * cos(theta), + rs * sin(theta), + radius * cos(phi) + ] +]; + +region_hollow = false; +region_offset = 0.2; +region_height = 1; + +voronoi_sphere(pts, region_hollow, region_offset, region_height); + +module voronoi_sphere(pts, region_hollow, region_offset, region_height) { + cells = vrn_sphere(pts); + r = norm(pts[0]); + + for(i = [0:len(cells) - 1]) { + cell = cells[i]; + cell_cp = convex_center_p(cell); + cell_inner = [ + for(p = cell) + let( + v = cell_cp - p, + uv = v / norm(v) + ) + p + uv * region_offset + ]; + + double_cell_cp = cell_cp / norm(cell_cp) * r * 2; + cell2 = [ + for(p = cell) + let( + v = double_cell_cp - p, + uv = v / norm(v) + ) + p + uv * region_height + ]; + + if(region_hollow) { + cell2_cp = convex_center_p(cell2); + cell2_inner = [ + for(p = cell2) + let( + v = cell2_cp - p, + uv = v / norm(v) + ) + p + uv * region_offset + ]; + + sweep([concat(cell, cell_inner), concat(cell2, cell2_inner)], "HOLLOW"); + } else { + sweep([cell, cell2]); + } + + + polyline_join(concat(cell, [cell[0]])) + sphere(region_offset / 2, $fn = 4); + } +} \ No newline at end of file