BLI_math: add invert_qt_normalized
authorCampbell Barton <ideasman42@gmail.com>
Fri, 23 Oct 2015 16:51:00 +0000 (03:51 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 23 Oct 2015 16:51:00 +0000 (03:51 +1100)
When the quat is known to be unit length, so we can avoid scaling
(just conjugate_qt which asserts on non unit quats).

source/blender/blenkernel/intern/fcurve.c
source/blender/blenkernel/intern/object.c
source/blender/blenkernel/intern/particle_system.c
source/blender/blenlib/BLI_math_rotation.h
source/blender/blenlib/intern/math_rotation.c
source/blender/editors/space_view3d/view3d_edit.c
source/blender/editors/space_view3d/view3d_view.c

index d747fb0cea26764c976a1214806eb3ece689e113..02b95c89be40a48197a20d60c6fa08fd75d888e3 100644 (file)
@@ -1235,7 +1235,7 @@ static float dvar_eval_rotDiff(ChannelDriver *driver, DriverVar *dvar)
        mat4_to_quat(q1, pchan->pose_mat);
        mat4_to_quat(q2, pchan2->pose_mat);
        
-       invert_qt(q1);
+       invert_qt_normalized(q1);
        mul_qt_qtqt(quat, q1, q2);
        angle = 2.0f * (saacos(quat[0]));
        angle = ABS(angle);
index ffb614fdf1144f34ccaa73aa5a266a72b2dd4a27..61c4073a2fd6f688ebe6988e17cd3d8888c718f5 100644 (file)
@@ -1951,7 +1951,7 @@ void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
                        float dquat[4];
                        mat3_to_quat(ob->quat, mat);
                        normalize_qt_qt(dquat, ob->dquat);
-                       invert_qt(dquat);
+                       invert_qt_normalized(dquat);
                        mul_qt_qtqt(ob->quat, dquat, ob->quat);
                        break;
                }
@@ -1963,7 +1963,7 @@ void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
                        /* without drot we could apply 'mat' directly */
                        mat3_to_quat(quat, mat);
                        axis_angle_to_quat(dquat, ob->drotAxis, ob->drotAngle);
-                       invert_qt(dquat);
+                       invert_qt_normalized(dquat);
                        mul_qt_qtqt(quat, dquat, quat);
                        quat_to_axis_angle(ob->rotAxis, &ob->rotAngle, quat);
                        break;
@@ -1977,7 +1977,7 @@ void BKE_object_mat3_to_rot(Object *ob, float mat[3][3], bool use_compat)
                        /* without drot we could apply 'mat' directly */
                        mat3_to_quat(quat, mat);
                        eulO_to_quat(dquat, ob->drot, ob->rotmode);
-                       invert_qt(dquat);
+                       invert_qt_normalized(dquat);
                        mul_qt_qtqt(quat, dquat, quat);
                        quat_to_mat3(tmat, quat);
                        /* end drot correction */
index a59a0b73b69ebdff351c86f903d8da8dcb4143ec..dc6249b6a156e2d97cad70d9729421ed477a4ff3 100644 (file)
@@ -894,7 +894,7 @@ void psys_get_birth_coords(ParticleSimulationData *sim, ParticleData *pa, Partic
                                float q_imat[4];
 
                                mat4_to_quat(q_obmat, ob->obmat);
-                               invert_qt_qt(q_imat, q_obmat);
+                               invert_qt_qt_normalized(q_imat, q_obmat);
 
 
                                if (part->rotmode != PART_ROT_NOR_TAN) {
index ac9295c90355b676bc049fa5ce4af8d33cdc3f71..4c4e305c9653dbbf40d6e624ecc3cc80bd11dcdd 100644 (file)
@@ -59,6 +59,8 @@ void sub_qt_qtqt(float q[4], const float a[4], const float b[4]);
 
 void invert_qt(float q[4]);
 void invert_qt_qt(float q1[4], const float q2[4]);
+void invert_qt_normalized(float q[4]);
+void invert_qt_qt_normalized(float q1[4], const float q2[4]);
 void conjugate_qt(float q[4]);
 void conjugate_qt_qt(float q1[4], const float q2[4]);
 float dot_qtqt(const float a[4], const float b[4]);
index 949473a8d86cea50dc06690265dc1d92daf9499f..0ef3f046202f0e443d8412a9821c5fb590a2c78d 100644 (file)
@@ -154,6 +154,24 @@ void invert_qt_qt(float q1[4], const float q2[4])
        invert_qt(q1);
 }
 
+/**
+ * This is just conjugate_qt for cases we know \a q is unit-length.
+ * we could use #conjugate_qt directly, but use this function to show intent,
+ * and assert if its ever becomes non-unit-length.
+ */
+void invert_qt_normalized(float q[4])
+{
+       BLI_ASSERT_UNIT_QUAT(q);
+       conjugate_qt(q);
+
+}
+
+void invert_qt_qt_normalized(float q1[4], const float q2[4])
+{
+       copy_qt_qt(q1, q2);
+       invert_qt_normalized(q1);
+}
+
 /* simple mult */
 void mul_qt_fl(float q[4], const float f)
 {
index 1fa865b585dbe2a94bbeccbb582013205a73fcbc..4665360d0b859d1dae701f20712dabdea63716fe 100644 (file)
@@ -391,14 +391,14 @@ static void view3d_boxview_sync_axis(RegionView3D *rv3d_dst, RegionView3D *rv3d_
        if (UNLIKELY(ED_view3d_quat_from_axis_view(rv3d_src->view, viewinv) == false)) {
                return;
        }
-       invert_qt(viewinv);
+       invert_qt_normalized(viewinv);
        mul_qt_v3(viewinv, view_src_x);
        mul_qt_v3(viewinv, view_src_y);
 
        if (UNLIKELY(ED_view3d_quat_from_axis_view(rv3d_dst->view, viewinv) == false)) {
                return;
        }
-       invert_qt(viewinv);
+       invert_qt_normalized(viewinv);
        mul_qt_v3(viewinv, view_dst_x);
        mul_qt_v3(viewinv, view_dst_y);
 
@@ -614,10 +614,10 @@ static void view3d_orbit_apply_dyn_ofs(
         const float oldquat[4], const float viewquat[4])
 {
        float q1[4];
-       conjugate_qt_qt(q1, oldquat);
+       invert_qt_qt_normalized(q1, oldquat);
        mul_qt_qtqt(q1, q1, viewquat);
 
-       conjugate_qt(q1);  /* conj == inv for unit quat */
+       invert_qt_normalized(q1);
 
        sub_v3_v3(r_ofs, dyn_ofs);
        mul_qt_v3(q1, r_ofs);
@@ -931,7 +931,7 @@ static void viewrotate_apply_snap(ViewOpsData *vod)
        int x, y, z;
        bool found = false;
 
-       invert_qt_qt(viewquat_inv, vod->viewquat);
+       invert_qt_qt_normalized(viewquat_inv, vod->viewquat);
 
        mul_qt_v3(viewquat_inv, zaxis);
        normalize_v3(zaxis);
@@ -969,11 +969,11 @@ static void viewrotate_apply_snap(ViewOpsData *vod)
                normalize_qt(viewquat_align);
                mul_qt_qtqt(viewquat_align, vod->viewquat, viewquat_align);
                normalize_qt(viewquat_align);
-               invert_qt_qt(viewquat_align_inv, viewquat_align);
+               invert_qt_qt_normalized(viewquat_align_inv, viewquat_align);
 
                vec_to_quat(quat_snap, zaxis_best, OB_NEGZ, OB_POSY);
-               invert_qt(quat_snap);
                normalize_qt(quat_snap);
+               invert_qt_normalized(quat_snap);
 
                /* check if we can find the roll */
                found = false;
@@ -992,7 +992,7 @@ static void viewrotate_apply_snap(ViewOpsData *vod)
                        normalize_qt(quat_final);
 
                        /* compare 2 vector angles to find the least roll */
-                       invert_qt_qt(quat_final_inv, quat_final);
+                       invert_qt_qt_normalized(quat_final_inv, quat_final);
                        mul_qt_v3(viewquat_align_inv, xaxis1);
                        mul_qt_v3(quat_final_inv, xaxis2);
                        angle = angle_v3v3(xaxis1, xaxis2);
@@ -1356,7 +1356,7 @@ static float view3d_ndof_pan_speed_calc_from_dist(RegionView3D *rv3d, const floa
 #if 0
        mul_mat3_m4_v3(rv3d->viewinv, tvec);
 #else
-       invert_qt_qt(viewinv, rv3d->viewquat);
+       invert_qt_qt_normalized(viewinv, rv3d->viewquat);
        mul_qt_v3(viewinv, tvec);
 #endif
 
@@ -1425,7 +1425,7 @@ static void view3d_ndof_pan_zoom(const struct wmNDOFMotionData *ndof, ScrArea *s
                mul_v3_fl(pan_vec, speed * ndof->dt);
 
                /* transform motion from view to world coordinates */
-               invert_qt_qt(view_inv, rv3d->viewquat);
+               invert_qt_qt_normalized(view_inv, rv3d->viewquat);
                mul_qt_v3(view_inv, pan_vec);
 
                /* move center of view opposite of hand motion (this is camera mode, not object mode) */
@@ -1453,7 +1453,7 @@ static void view3d_ndof_orbit(const struct wmNDOFMotionData *ndof, ScrArea *sa,
 
        rv3d->view = RV3D_VIEW_USER;
 
-       invert_qt_qt(view_inv, rv3d->viewquat);
+       invert_qt_qt_normalized(view_inv, rv3d->viewquat);
 
        if (U.ndof_flag & NDOF_TURNTABLE) {
                float rot[3];
@@ -1522,7 +1522,7 @@ void view3d_ndof_fly(
        bool has_rotate = NDOF_HAS_ROTATE;
 
        float view_inv[4];
-       invert_qt_qt(view_inv, rv3d->viewquat);
+       invert_qt_qt_normalized(view_inv, rv3d->viewquat);
 
        rv3d->rot_angle = 0.0f;  /* disable onscreen rotation doo-dad */
 
@@ -1595,7 +1595,7 @@ void view3d_ndof_fly(
                                float view_direction[3] = {0.0f, 0.0f, -1.0f}; /* view -z (into screen) */
 
                                /* find new inverse since viewquat has changed */
-                               invert_qt_qt(view_inv, rv3d->viewquat);
+                               invert_qt_qt_normalized(view_inv, rv3d->viewquat);
                                /* could apply reverse rotation to existing view_inv to save a few cycles */
 
                                /* transform view vectors to world coordinates */
@@ -3762,7 +3762,7 @@ static void axis_set_view(bContext *C, View3D *v3d, ARegion *ar,
                        ED_getTransformOrientationMatrix(C, twmat, V3D_ACTIVE);
 
                        mat3_to_quat(obact_quat, twmat);
-                       invert_qt(obact_quat);
+                       invert_qt_normalized(obact_quat);
                        mul_qt_qtqt(quat, quat, obact_quat);
 
                        rv3d->view = view = RV3D_VIEW_USER;
@@ -4999,7 +4999,7 @@ void ED_view3d_distance_set(RegionView3D *rv3d, const float dist)
 #if 0
        mul_mat3_m4_v3(rv3d->viewinv, tvec);
 #else
-       invert_qt_qt(viewinv, rv3d->viewquat);
+       invert_qt_qt_normalized(viewinv, rv3d->viewquat);
        mul_qt_v3(viewinv, tvec);
 #endif
        sub_v3_v3(rv3d->ofs, tvec);
index 0f05f4e52dd9e1b4612569aea5b4795cf44a293b..e3736d902321fc22a6fa72f24c54213d04b836e4 100644 (file)
@@ -255,8 +255,8 @@ void ED_view3d_smooth_view_ex(
                                float vec1[3] = {0, 0, 1}, vec2[3] = {0, 0, 1};
                                float q1[4], q2[4];
 
-                               invert_qt_qt(q1, sms.dst.quat);
-                               invert_qt_qt(q2, sms.src.quat);
+                               invert_qt_qt_normalized(q1, sms.dst.quat);
+                               invert_qt_qt_normalized(q2, sms.src.quat);
 
                                mul_qt_v3(q1, vec1);
                                mul_qt_v3(q2, vec2);