From 69976d834c2f581ec80b0a721280cb9cf180b2f1 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 29 Oct 2020 17:28:11 +0800 Subject: [PATCH] add catmull_rom_spline --- src/experimental/catmull_rom_spline.scad | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/experimental/catmull_rom_spline.scad diff --git a/src/experimental/catmull_rom_spline.scad b/src/experimental/catmull_rom_spline.scad new file mode 100644 index 00000000..ad66aee5 --- /dev/null +++ b/src/experimental/catmull_rom_spline.scad @@ -0,0 +1,28 @@ +use ; + +function _catmull_rom_spline_4pts(t_step, points, tightness) = + let( + p1x_0tightness = (points[2] - points[0]) / 4 + points[1], + v_p1x = points[1] - p1x_0tightness, + p1x = p1x_0tightness + v_p1x * tightness, + p2x_0tightness = (points[1] - points[3]) / 4 + points[2], + v_p2x = points[2] - p2x_0tightness, + p2x = p2x_0tightness + v_p2x * tightness + ) + bezier_curve(t_step, [points[1], p1x, p2x, points[2]]); + +function catmull_rom_spline(t_step, points, tightness = 0) = + let( + leng = len(points) + ) + concat( + [ + for(i = [0:leng - 4]) + let( + pts = _catmull_rom_spline_4pts(t_step, [for(j = [i:i + 3]) points[j]], tightness) + ) + for(i = [0:len(pts) - 2]) pts[i] + ], + [points[leng - 2]] + ); + \ No newline at end of file