From e5af7b3027ac352d4fcbfb9c24c8362e82d16322 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Tue, 7 Sep 2021 10:12:29 +0800 Subject: [PATCH] refactor --- src/experimental/geom_isosphere.scad | 30 ++++++++++++++++++++++++ src/experimental/isosphere.scad | 35 +++------------------------- 2 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 src/experimental/geom_isosphere.scad diff --git a/src/experimental/geom_isosphere.scad b/src/experimental/geom_isosphere.scad new file mode 100644 index 00000000..21456a5a --- /dev/null +++ b/src/experimental/geom_isosphere.scad @@ -0,0 +1,30 @@ +use ; + +function _prj2sphere(t, r) = [for(p = t) p / norm(p) * r]; + +function geom_isosphere(radius, detail = 0) = + let( + t = (1 + sqrt(5)) / 2 , + icosahedron_points = [ + [- 1, t, 0], [1, t, 0], [- 1, - t, 0], [1, - t, 0], + [0, - 1, t], [0, 1, t], [0, - 1, - t], [0, 1, - t], + [t, 0, - 1], [t, 0, 1], [- t, 0, - 1], [- t, 0, 1] + ], + icosahedron_faces = [ + [5, 11, 0], [1, 5, 0], [7, 1, 0], [10, 7, 0], [11, 10, 0], + [9, 5, 1], [4, 11, 5], [2, 10, 11], [6, 7, 10], [8, 1, 7], + [4, 9, 3], [2, 4, 3], [6, 2, 3], [8, 6, 3], [9, 8, 3], + [5, 9, 4], [11, 4, 2], [10, 2, 6], [7, 6, 8], [1, 8, 9] + ], + tris = [ + for(face = icosahedron_faces) + [for(i = face) icosahedron_points[i]] + ], + 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]; + \ No newline at end of file diff --git a/src/experimental/isosphere.scad b/src/experimental/isosphere.scad index f8694c56..f18c6069 100644 --- a/src/experimental/isosphere.scad +++ b/src/experimental/isosphere.scad @@ -1,35 +1,6 @@ -use ; +use ; module isosphere(radius, detail = 0) { - function prj2sphere(t, r) = [for(p = t) p / norm(p) * r]; - - // Icosahedron - t = (1 + sqrt(5)) / 2 ; - - icosahedron_points = [ - [- 1, t, 0], [1, t, 0], [- 1, - t, 0], [1, - t, 0], - [0, - 1, t], [0, 1, t], [0, - 1, - t], [0, 1, - t], - [t, 0, - 1], [t, 0, 1], [- t, 0, - 1], [- t, 0, 1] - ]; - - icosahedron_faces = [ - [5, 11, 0], [1, 5, 0], [7, 1, 0], [10, 7, 0], [11, 10, 0], - [9, 5, 1], [4, 11, 5], [2, 10, 11], [6, 7, 10], [8, 1, 7], - [4, 9, 3], [2, 4, 3], [6, 2, 3], [8, 6, 3], [9, 8, 3], - [5, 9, 4], [11, 4, 2], [10, 2, 6], [7, 6, 8], [1, 8, 9] - ]; - - tris = [ - for(face = icosahedron_faces) - [for(i = face) icosahedron_points[i]] - ]; - - 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]]; - - polyhedron(points, faces); + points_faces = geom_isosphere(radius, detail); + polyhedron(points_faces[0], points_faces[1]); } \ No newline at end of file