diff --git a/README.md b/README.md index 831c0c00..7f6b5c8e 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,15 @@ See [examples](examples). - [part/connector_peg](https://openhome.cc/eGossip/OpenSCAD/lib2x-connector_peg.html) - [part/cone](https://openhome.cc/eGossip/OpenSCAD/lib2x-cone.html) - [part/joint_T](https://openhome.cc/eGossip/OpenSCAD/lib2-joint_T.html) + +- Point Transformation (2.3 Preview) + - ptf/ptf_rotate + - ptf/ptf_x_twist + - ptf/ptf_y_twist + - ptf/ptf_bend + - ptf/ptf_ring + - ptf/ptf_sphere + - ptf/ptf_torus ## Bugs and Feedback diff --git a/examples/hollow_out/hollow_out_vase.scad b/examples/hollow_out/hollow_out_vase.scad index 104cf1ba..008816ee 100644 --- a/examples/hollow_out/hollow_out_vase.scad +++ b/examples/hollow_out/hollow_out_vase.scad @@ -1,6 +1,6 @@ use ; use ; -use ; +use ; use ; use ; use ; @@ -28,7 +28,7 @@ module hollow_out_vase(ctrl_pts, t_step, width, fn, line_style) { a_step = 360 / fn; sects = paths2sections([ for(a = [0:a_step:360 - a_step]) - [for(p = bezier) rotate_p(p, [0, 0, a])] + [for(p = bezier) ptf_rotate(p, [0, 0, a])] ]); // body diff --git a/examples/nautilus_shell.scad b/examples/nautilus_shell.scad index 6764cc2b..5f463d86 100644 --- a/examples/nautilus_shell.scad +++ b/examples/nautilus_shell.scad @@ -1,4 +1,4 @@ -use ; +use ; use ; use ; use ; @@ -16,7 +16,7 @@ module nautilus_shell(chambered_section_max_angle, steps, thickness) { a_step = chambered_section_max_angle / steps; spiral = [ for(a = [a_step:a_step:chambered_section_max_angle + 450]) - rotate_p([r(a), 0], a) + ptf_rotate([r(a), 0], a) ]; render() { @@ -25,9 +25,9 @@ module nautilus_shell(chambered_section_max_angle, steps, thickness) { for(a = [a_step:a_step * 2:chambered_section_max_angle]) { a2 = a + 360; a3 = a + 420; - p1 = rotate_p([r(a), 0], a); - p2 = rotate_p((p1 + rotate_p([r(a2), 0], a2)) * .6, -5); - p3 = rotate_p([r(a3), 0], a3); + p1 = ptf_rotate([r(a), 0], a); + p2 = ptf_rotate((p1 + ptf_rotate([r(a2), 0], a2)) * .6, -5); + p3 = ptf_rotate([r(a3), 0], a3); hull_polyline2d(bezier_curve(0.1, [p1, p2, p3] diff --git a/examples/simple_seashell.scad b/examples/simple_seashell.scad index 2bbf3431..e0863d40 100644 --- a/examples/simple_seashell.scad +++ b/examples/simple_seashell.scad @@ -1,5 +1,5 @@ use ; -use ; +use ; use ; r1 = 0.1; @@ -24,7 +24,7 @@ module simple_seashell(r1, r2, a1, a2, steps) { ) [ for(p = concat(shape_circle(r), shape_circle(r * 0.9))) - rotate_p([p[0], p[1], 0] + [r, 0, 0], [0, a, 0]) + ptf_rotate([p[0], p[1], 0] + [r, 0, 0], [0, a, 0]) ] ]; diff --git a/examples/superformula_vase.scad b/examples/superformula_vase.scad index e46d1b9c..3e2fb4e9 100644 --- a/examples/superformula_vase.scad +++ b/examples/superformula_vase.scad @@ -2,7 +2,7 @@ use ; use ; use ; use ; -use ; +use ; use ; use ; @@ -35,7 +35,7 @@ module superformula_vase(phi_step, m, n, n3, d, r1, r2, h1, h2, t_step, twist) { for(i = [0:leng - 1]) [ for(p = sects[i]) - rotate_p(p, twist_step * i) + ptf_rotate(p, twist_step * i) ] ]; diff --git a/examples/taiwan/dancing_taiwan.scad b/examples/taiwan/dancing_taiwan.scad index 2c612c21..241faf8d 100644 --- a/examples/taiwan/dancing_taiwan.scad +++ b/examples/taiwan/dancing_taiwan.scad @@ -3,7 +3,7 @@ use ; use ; use ; use ; -use ; +use ; use ; x1 = 4; // [-20:4] @@ -27,7 +27,7 @@ module dancing_formosan(x1, x2, x3, y1, y2, y3, twist, t_step) { for(i = [0:leng - 1]) [ for(p = sects[i]) - rotate_p(p, twist_step * i) + ptf_rotate(p, twist_step * i) ] ]; diff --git a/examples/voronoi/voronoi_fibonacci.scad b/examples/voronoi/voronoi_fibonacci.scad index aa2c6fb1..3816c2a2 100644 --- a/examples/voronoi/voronoi_fibonacci.scad +++ b/examples/voronoi/voronoi_fibonacci.scad @@ -1,5 +1,5 @@ use ; -use ; +use ; use ; use ; @@ -27,7 +27,7 @@ module voronoi_fibonacci() { pts = [ for(a = [0:a_step:360 - a_step]) - each [for(p = spiral) rotate_p(p, a)] + each [for(p = spiral) ptf_rotate(p, a)] ]; half_line_thicness = line_thickness / 2; diff --git a/src/_impl/_ptf_rotate_impl.scad b/src/_impl/_ptf_rotate_impl.scad new file mode 100644 index 00000000..b714a4fb --- /dev/null +++ b/src/_impl/_ptf_rotate_impl.scad @@ -0,0 +1,82 @@ +use <__comm__/__to2d.scad>; +use <__comm__/__to3d.scad>; +use <__comm__/__to_ang_vect.scad>; + +function _q_rotate_p_3d(p, a, v) = + let( + half_a = a / 2, + axis = v / norm(v), + s = sin(half_a), + x = s * axis[0], + y = s * axis[1], + z = s * axis[2], + w = cos(half_a), + + x2 = x + x, + y2 = y + y, + z2 = z + z, + + xx = x * x2, + yx = y * x2, + yy = y * y2, + zx = z * x2, + zy = z * y2, + zz = z * z2, + wx = w * x2, + wy = w * y2, + wz = w * z2 + ) + [ + [1 - yy - zz, yx - wz, zx + wy] * p, + [yx + wz, 1 - xx - zz, zy - wx] * p, + [zx - wy, zy + wx, 1 - xx - yy] * p + ]; + +function _rotx(pt, a) = + a == 0 ? pt : + let(cosa = cos(a), sina = sin(a)) + [ + pt[0], + pt[1] * cosa - pt[2] * sina, + pt[1] * sina + pt[2] * cosa + ]; + +function _roty(pt, a) = + a == 0 ? pt : + let(cosa = cos(a), sina = sin(a)) + [ + pt[0] * cosa + pt[2] * sina, + pt[1], + -pt[0] * sina + pt[2] * cosa, + ]; + +function _rotz(pt, a) = + a == 0 ? pt : + let(cosa = cos(a), sina = sin(a)) + [ + pt[0] * cosa - pt[1] * sina, + pt[0] * sina + pt[1] * cosa, + pt[2] + ]; + +function _rotate_p_3d(point, a) = + _rotz(_roty(_rotx(point, a[0]), a[1]), a[2]); + +function _rotate_p(p, a) = + let(angle = __to_ang_vect(a)) + len(p) == 3 ? + _rotate_p_3d(p, angle) : + __to2d( + _rotate_p_3d(__to3d(p), angle) + ); + + +function _q_rotate_p(p, a, v) = + len(p) == 3 ? + _q_rotate_p_3d(p, a, v) : + __to2d( + _q_rotate_p_3d(__to3d(p), a, v) + ); + +function _rotate_p_impl(point, a, v) = + is_undef(v) ? _rotate_p(point, a) : _q_rotate_p(point, a, v); diff --git a/src/ptf/ptf_rotate.scad b/src/ptf/ptf_rotate.scad new file mode 100644 index 00000000..e118b9b3 --- /dev/null +++ b/src/ptf/ptf_rotate.scad @@ -0,0 +1,13 @@ +/** +* rotate_p.scad +* +* @copyright Justin Lin, 2017 +* @license https://opensource.org/licenses/lgpl-3.0.html +* +* @see https://openhome.cc/eGossip/OpenSCAD/lib2x-rotate_p.html +* +**/ + +use <_impl/_ptf_rotate_impl.scad>; + +function ptf_rotate(point, a, v) = _rotate_p_impl(point, a, v); \ No newline at end of file