From 32478aa942f08643c1836461c5f888f20f49e10a Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 20 Jun 2021 14:41:07 +0800 Subject: [PATCH] add sf_thicken --- src/surface/sf_thicken.scad | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/surface/sf_thicken.scad diff --git a/src/surface/sf_thicken.scad b/src/surface/sf_thicken.scad new file mode 100644 index 00000000..d6833f17 --- /dev/null +++ b/src/surface/sf_thicken.scad @@ -0,0 +1,67 @@ +use ; +use ; + +module sf_thicken(points, thickness, slicing = "SLASH") { + function tri_normal(tri) = + let(v = cross(tri[2] - tri[0], tri[1] - tri[0])) v / norm(v); + + function vertex_normal(sf, xi, yi) = + let( + xy = [xi, yi], + // clockwise + vi = [for(coord_offset = [[1, 0], [0, -1], [-1, 0], [0, 1]]) coord_offset + xy], + normals = [ + for(i = [0:3]) + let( + vi0 = xy, + vi1 = vi[i], + vi2 = vi[(i + 1) % 4], + v1 = sf[vi0[1]][vi0[0]], + v2 = sf[vi1[1]][vi1[0]], + v3 = sf[vi2[1]][vi2[0]] + ) + if(v1 != undef && v2 != undef && v3 != undef) + tri_normal([v1, v2, v3]) + ] + ) + sum(normals) / len(normals); + + vertex_normals = [ + for(y = [0:len(points) - 1]) + [ + for(x = [0:len(points[0]) - 1]) + vertex_normal(points, x, y) + ] + ]; + + + half_thickness = thickness / 2; + surface_top = points + half_thickness * vertex_normals; + surface_bottom = points - half_thickness * vertex_normals; + + sf_solidify(surface_top, surface_bottom, slicing); +} + +/* +use ; + +function f(x, y) = + 30 * ( + cos(sqrt(pow(x, 2) + pow(y, 2))) + + cos(3 * sqrt(pow(x, 2) + pow(y, 2))) + ); + +thickness = 3; +min_value = -200; +max_value = 200; +resolution = 10; + +surface1 = [ + for(y = [min_value:resolution:max_value]) + [ + for(x = [min_value:resolution:max_value]) + [x, y, f(x, y) + 100] + ] +]; +sf_thicken(surface1, thickness); +*/ \ No newline at end of file