From a4bb82698a6f48063f6f5a51c2fb364e945d61b6 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 5 May 2022 09:29:03 +0800 Subject: [PATCH] refactor --- src/matrix/_impl/_m_rotation_impl.scad | 47 +++++++++-------------- src/matrix/_impl/_m_scaling_impl.scad | 3 +- src/matrix/_impl/_m_shearing_impl.scad | 3 +- src/matrix/_impl/_m_translation_impl.scad | 3 +- 4 files changed, 25 insertions(+), 31 deletions(-) diff --git a/src/matrix/_impl/_m_rotation_impl.scad b/src/matrix/_impl/_m_rotation_impl.scad index 9b731c65..8c308918 100644 --- a/src/matrix/_impl/_m_rotation_impl.scad +++ b/src/matrix/_impl/_m_rotation_impl.scad @@ -1,34 +1,25 @@ use <../../__comm__/__to_ang_vect.scad>; +FINAL_ROW = [0, 0, 0, 1]; function __m_rotation_q_rotation(a, v) = let( - half_a = a / 2, - axis = v / norm(v), - s = sin(half_a), - x = s * axis.x, - y = s * axis.y, - z = s * axis.z, - w = cos(half_a), - - x2 = x + x, - y2 = y + y, - z2 = z + z, + uv = v / norm(v), + s = sin(a / 2) * uv, + w = sin(a) * uv, - 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 + xx = 2 * s.x ^ 2, + yy = 2 * s.y ^ 2, + zz = 2 * s.z ^ 2, + + xy = 2 * s.x * s.y, + xz = 2 * s.x * s.z, + yz = 2 * s.y * s.z ) [ - [1 - yy - zz, yx - wz, zx + wy, 0], - [yx + wz, 1 - xx - zz, zy - wx, 0], - [zx - wy, zy + wx, 1 - xx - yy, 0], - [0, 0, 0, 1] + [1 - yy - zz, xy - w.z, xz + w.y, 0], + [xy + w.z, 1 - xx - zz, yz - w.x, 0], + [xz - w.y, yz + w.x, 1 - xx - yy, 0], + FINAL_ROW ]; function __m_rotation_xRotation(a) = @@ -37,7 +28,7 @@ function __m_rotation_xRotation(a) = [1, 0, 0, 0], [0, c, -s, 0], [0, s, c, 0], - [0, 0, 0, 1] + FINAL_ROW ]; function __m_rotation_yRotation(a) = @@ -46,7 +37,7 @@ function __m_rotation_yRotation(a) = [c, 0, s, 0], [0, 1, 0, 0], [-s, 0, c, 0], - [0, 0, 0, 1] + FINAL_ROW ]; function __m_rotation_zRotation(a) = @@ -55,7 +46,7 @@ function __m_rotation_zRotation(a) = [c, -s, 0, 0], [s, c, 0, 0], [0, 0, 1, 0], - [0, 0, 0, 1] + FINAL_ROW ]; function __m_rotation_xyz_rotation(a) = @@ -67,5 +58,5 @@ function _m_rotation_impl(a, v) = [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], - [0, 0, 0, 1] + FINAL_ROW ] : (is_undef(v) ? __m_rotation_xyz_rotation(a) : __m_rotation_q_rotation(a, v)); \ No newline at end of file diff --git a/src/matrix/_impl/_m_scaling_impl.scad b/src/matrix/_impl/_m_scaling_impl.scad index 56a835f5..a04c3423 100644 --- a/src/matrix/_impl/_m_scaling_impl.scad +++ b/src/matrix/_impl/_m_scaling_impl.scad @@ -5,11 +5,12 @@ function __m_scaling_to_3_elems_scaling_vect(s) = function __m_scaling_to_scaling_vect(s) = is_num(s) ? [s, s, s] : __m_scaling_to_3_elems_scaling_vect(s); +FINAL_ROW = [0, 0, 0, 1]; function _m_scaling_impl(s) = let(v = __m_scaling_to_scaling_vect(s)) [ [v.x, 0, 0, 0], [0, v.y, 0, 0], [0, 0, v.z, 0], - [0, 0, 0, 1] + FINAL_ROW ]; \ No newline at end of file diff --git a/src/matrix/_impl/_m_shearing_impl.scad b/src/matrix/_impl/_m_shearing_impl.scad index f4f10a2d..f13a256c 100644 --- a/src/matrix/_impl/_m_shearing_impl.scad +++ b/src/matrix/_impl/_m_shearing_impl.scad @@ -1,3 +1,4 @@ +FINAL_ROW = [0, 0, 0, 1]; function _m_shearing_impl(sx, sy, sz) = let( sx_along_y = sx[0], @@ -11,5 +12,5 @@ function _m_shearing_impl(sx, sy, sz) = [1, sx_along_y, sx_along_z, 0], [sy_along_x, 1, sy_along_z, 0], [sz_along_x, sz_along_y, 1, 0], - [0, 0, 0, 1] + FINAL_ROW ]; \ No newline at end of file diff --git a/src/matrix/_impl/_m_translation_impl.scad b/src/matrix/_impl/_m_translation_impl.scad index b9b7fed0..e6f91778 100644 --- a/src/matrix/_impl/_m_translation_impl.scad +++ b/src/matrix/_impl/_m_translation_impl.scad @@ -5,11 +5,12 @@ function _to_3_elems_translation_vect(v) = function _to_translation_vect(v) = is_num(v) ? [v, 0, 0] : _to_3_elems_translation_vect(v); +FINAL_ROW = [0, 0, 0, 1]; function _m_translation_impl(v) = let(vt = _to_translation_vect(v)) [ [1, 0, 0, vt.x], [0, 1, 0, vt.y], [0, 0, 1, vt.z], - [0, 0, 0, 1] + FINAL_ROW ]; \ No newline at end of file