From 640b54f9484bc3dc22bd7480e0063f17c96745c7 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Mon, 27 Jan 2020 15:51:14 +0800 Subject: [PATCH] refactor deps --- src/_impl/_rotate_p_impl.scad | 82 ++++++++++++++++++++++++++++++++++ src/rotate_p.scad | 83 +---------------------------------- test/test_rotate_p.scad | 4 +- 3 files changed, 86 insertions(+), 83 deletions(-) create mode 100644 src/_impl/_rotate_p_impl.scad diff --git a/src/_impl/_rotate_p_impl.scad b/src/_impl/_rotate_p_impl.scad new file mode 100644 index 00000000..b714a4fb --- /dev/null +++ b/src/_impl/_rotate_p_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/rotate_p.scad b/src/rotate_p.scad index 327ba777..de155460 100644 --- a/src/rotate_p.scad +++ b/src/rotate_p.scad @@ -8,85 +8,6 @@ * **/ -include <__comm__/__to2d.scad>; -include <__comm__/__to3d.scad>; -include <__comm__/__to_ang_vect.scad>; +use <_impl/_rotate_p_impl.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(point, a, v) = - is_undef(v) ? _rotate_p(point, a) : _q_rotate_p(point, a, v); +function rotate_p(point, a, v) = _rotate_p_impl(point, a, v); \ No newline at end of file diff --git a/test/test_rotate_p.scad b/test/test_rotate_p.scad index c81a6dae..ed51d6aa 100644 --- a/test/test_rotate_p.scad +++ b/test/test_rotate_p.scad @@ -1,5 +1,5 @@ -include ; -include ; +use ; +use ; module test_rotate_p() { echo("==== test_rotate_p ====");