From 008ee11526709ea0c3f9ad3b4a813dc499380afd Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 8 Apr 2017 08:37:47 +0800 Subject: [PATCH] added bezier_surface.scad --- src/bezier_surface.scad | 10 +++++++ src/test.scad | 66 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/bezier_surface.scad create mode 100644 src/test.scad diff --git a/src/bezier_surface.scad b/src/bezier_surface.scad new file mode 100644 index 00000000..febd7318 --- /dev/null +++ b/src/bezier_surface.scad @@ -0,0 +1,10 @@ +function bezier_surface(t_step, ctrl_pts) = + let(pts = [ + for(i = [0:len(ctrl_pts) - 1]) + bezier_curve(t_step, ctrl_pts[i]) + ]) + [for(x = [0:len(pts[0]) - 1]) + bezier_curve(t_step, + [for(y = [0:len(pts) - 1]) pts[y][x]] + ) + ]; \ No newline at end of file diff --git a/src/test.scad b/src/test.scad new file mode 100644 index 00000000..aca9303a --- /dev/null +++ b/src/test.scad @@ -0,0 +1,66 @@ +include ; +include ; +include ; +include ; + +t_step = 0.05; +thickness = 0.5; + +ctrl_pts = [ + [[0, 0, 20], [60, 0, -35], [90, 0, 60], [200, 0, 5]], + [[0, 50, 30], [100, 60, -25], [120, 50, 120], [200, 50, 5]], + [[0, 100, 0], [60, 120, 35], [90, 100, 60], [200, 100, 45]], + [[0, 150, 0], [60, 150, -35], [90, 180, 60], [200, 150, 45]] +]; + +g = bezier_surface(t_step, ctrl_pts); +function_grapher(g, thickness); + +demo = "YES"; +// demo +if(demo == "YES") { + width = 2; + r = 3; + + pts = [ + for(i = [0:len(ctrl_pts) - 1]) + bezier_curve(t_step, ctrl_pts[i]) + ]; + + // first bezier curve + for(i = [0:len(ctrl_pts) - 1]) { + color("green") union() { + for(j = [0:len(ctrl_pts[i]) - 1]) { + translate(ctrl_pts[i][j]) + sphere(r = r); + } + + for(j = [0:len(ctrl_pts[i]) - 1]) { + hull_polyline3d( + ctrl_pts[i] + , width); + } + } + + color("red") + hull_polyline3d( + bezier_curve(t_step, ctrl_pts[i]), width + ); + } + + step = len(g[0]); + echo(step); + + // second bezier curve + ctrl_pts2 = [for(i = [0:len(pts) - 1]) pts[i][$t * step]]; + color("blue") union() { + for(pt = ctrl_pts2) { + translate(pt) + sphere(r = r); + } + hull_polyline3d(ctrl_pts2, width); + } + + color("black") + hull_polyline3d(g[$t * step], width); +}