Revision: 35954
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=35954
Author:   campbellbarton
Date:     2011-04-02 03:05:49 +0000 (Sat, 02 Apr 2011)
Log Message:
-----------
add angle wrapping functions: angle_wrap_rad(), angle_wrap_deg().
use with mathutils.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_rotation.h
    trunk/blender/source/blender/blenlib/intern/math_rotation.c
    trunk/blender/source/blender/python/generic/mathutils_Matrix.c
    trunk/blender/source/blender/python/generic/mathutils_Quaternion.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_rotation.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_rotation.h    2011-04-02 
02:08:33 UTC (rev 35953)
+++ trunk/blender/source/blender/blenlib/BLI_math_rotation.h    2011-04-02 
03:05:49 UTC (rev 35954)
@@ -185,6 +185,9 @@
 float lens_to_angle(float lens);
 float angle_to_lens(float angle);
 
+float angle_wrap_rad(float angle);
+float angle_wrap_deg(float angle);
+
 #ifdef __cplusplus
 }
 #endif

Modified: trunk/blender/source/blender/blenlib/intern/math_rotation.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_rotation.c 2011-04-02 
02:08:33 UTC (rev 35953)
+++ trunk/blender/source/blender/blenlib/intern/math_rotation.c 2011-04-02 
03:05:49 UTC (rev 35954)
@@ -1675,3 +1675,19 @@
 {
        return 16.0f / tanf(angle * 0.5f);
 }
+
+/* 'mod_inline(-3,4)= 1', 'fmod(-3,4)= -3' */
+static float mod_inline(float a, float b)
+{
+       return a - (b * floorf(a / b));
+}
+
+float angle_wrap_rad(float angle)
+{
+       return mod_inline(angle + (float)M_PI, (float)M_PI*2.0f) - (float)M_PI;
+}
+
+float angle_wrap_deg(float angle)
+{
+       return mod_inline(angle + 180.0f, 360.0f) - 180.0f;
+}

Modified: trunk/blender/source/blender/python/generic/mathutils_Matrix.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_Matrix.c      
2011-04-02 02:08:33 UTC (rev 35953)
+++ trunk/blender/source/blender/python/generic/mathutils_Matrix.c      
2011-04-02 03:05:49 UTC (rev 35954)
@@ -226,8 +226,7 @@
                }
        }
 
-       /* clamp angle between -360 and 360 in radians */
-       angle= fmod(angle + M_PI*2, M_PI*4) - M_PI*2;
+       angle= angle_wrap_rad(angle);
 
        if(matSize != 2 && matSize != 3 && matSize != 4) {
                PyErr_SetString(PyExc_AttributeError, 
"mathutils.RotationMatrix(): can only return a 2x2 3x3 or 4x4 matrix");

Modified: trunk/blender/source/blender/python/generic/mathutils_Quaternion.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_Quaternion.c  
2011-04-02 02:08:33 UTC (rev 35953)
+++ trunk/blender/source/blender/python/generic/mathutils_Quaternion.c  
2011-04-02 03:05:49 UTC (rev 35954)
@@ -861,7 +861,7 @@
                return -1;
        }
 
-       angle= fmod(angle + M_PI*2, M_PI*4) - M_PI*2;
+       angle= angle_wrap_rad(angle);
 
        /* If the axis of rotation is 0,0,0 set it to 1,0,0 - for zero-degree 
rotations */
        if(     EXPP_FloatsAreEqual(axis[0], 0.0f, 10) &&
@@ -955,7 +955,7 @@
        case 2:
                if (mathutils_array_parse(quat, 3, 3, seq, 
"mathutils.Quaternion()") == -1)
                        return NULL;
-               angle= fmod(angle + M_PI*2, M_PI*4) - M_PI*2; /* clamp because 
of precision issues */
+               angle= angle_wrap_rad(angle); /* clamp because of precision 
issues */
                axis_angle_to_quat(quat, quat, angle);
                break;
        /* PyArg_ParseTuple assures no more then 2 */

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to