[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [54660] trunk/blender/source/blender/ blenlib: make asserts that check for unit length vectors into a macro.

Campbell Barton ideasman42 at gmail.com
Tue Feb 19 14:15:34 CET 2013


Revision: 54660
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=54660
Author:   campbellbarton
Date:     2013-02-19 13:15:34 +0000 (Tue, 19 Feb 2013)
Log Message:
-----------
make asserts that check for unit length vectors into a macro.
this was really not nice logic to try to fit into an assert.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_math_base.h
    trunk/blender/source/blender/blenlib/intern/math_geom.c
    trunk/blender/source/blender/blenlib/intern/math_vector.c

Modified: trunk/blender/source/blender/blenlib/BLI_math_base.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_math_base.h	2013-02-19 12:05:38 UTC (rev 54659)
+++ trunk/blender/source/blender/blenlib/BLI_math_base.h	2013-02-19 13:15:34 UTC (rev 54660)
@@ -217,5 +217,25 @@
 
 double double_round(double x, int ndigits);
 
+/* asserts, some math functions expect normalized inputs
+ * check the vector is unit length, or zero length (which can't be helped in some cases).
+ */
+#ifdef DEBUG
+#  define BLI_ASSERT_UNIT_EPSILON 0.0001f
+#  define BLI_ASSERT_UNIT_V3(v)  {                                            \
+	const float _test_unit = len_squared_v3(v);                               \
+	BLI_assert((fabsf(_test_unit - 1.0f) < BLI_ASSERT_UNIT_EPSILON) ||        \
+	           (fabsf(_test_unit)        < BLI_ASSERT_UNIT_EPSILON));         \
+} (void)0
+
+#  define BLI_ASSERT_UNIT_V2(v)  {                                            \
+	const float _test_unit = len_squared_v2(v);                               \
+	BLI_assert((fabsf(_test_unit - 1.0f) < BLI_ASSERT_UNIT_EPSILON) ||        \
+	           (fabsf(_test_unit)        < BLI_ASSERT_UNIT_EPSILON));         \
+} (void)0
+#else
+#  define BLI_ASSERT_UNIT_V2(v) (void)0
+#  define BLI_ASSERT_UNIT_V3(v) (void)0
+#endif
+
 #endif /* __BLI_MATH_BASE_H__ */
-

Modified: trunk/blender/source/blender/blenlib/intern/math_geom.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_geom.c	2013-02-19 12:05:38 UTC (rev 54659)
+++ trunk/blender/source/blender/blenlib/intern/math_geom.c	2013-02-19 13:15:34 UTC (rev 54660)
@@ -2001,10 +2001,7 @@
 	float angle;
 
 	/* double check they are normalized */
-#ifdef DEBUG
-	float test;
-	BLI_assert(fabsf((test = len_squared_v3(normal)) - 1.0f) < 0.0001f || fabsf(test) < 0.0001f);
-#endif
+	BLI_ASSERT_UNIT_V3(normal);
 
 	cross_v3_v3v3(axis, normal, up);
 	angle = saacos(dot_v3v3(normal, up));

Modified: trunk/blender/source/blender/blenlib/intern/math_vector.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/math_vector.c	2013-02-19 12:05:38 UTC (rev 54659)
+++ trunk/blender/source/blender/blenlib/intern/math_vector.c	2013-02-19 13:15:34 UTC (rev 54660)
@@ -235,11 +235,8 @@
 float angle_normalized_v3v3(const float v1[3], const float v2[3])
 {
 	/* double check they are normalized */
-#ifdef DEBUG
-	float test;
-	BLI_assert(fabsf((test = len_squared_v3(v1)) - 1.0f) < 0.0001f || fabsf(test) < 0.0001f);
-	BLI_assert(fabsf((test = len_squared_v3(v2)) - 1.0f) < 0.0001f || fabsf(test) < 0.0001f);
-#endif
+	BLI_ASSERT_UNIT_V3(v1);
+	BLI_ASSERT_UNIT_V3(v2);
 
 	/* this is the same as acos(dot_v3v3(v1, v2)), but more accurate */
 	if (dot_v3v3(v1, v2) < 0.0f) {
@@ -258,11 +255,8 @@
 float angle_normalized_v2v2(const float v1[2], const float v2[2])
 {
 	/* double check they are normalized */
-#ifdef DEBUG
-	float test;
-	BLI_assert(fabsf((test = len_squared_v2(v1)) - 1.0f) < 0.0001f || fabsf(test) < 0.0001f);
-	BLI_assert(fabsf((test = len_squared_v2(v2)) - 1.0f) < 0.0001f || fabsf(test) < 0.0001f);
-#endif
+	BLI_ASSERT_UNIT_V2(v1);
+	BLI_ASSERT_UNIT_V2(v2);
 
 	/* this is the same as acos(dot_v3v3(v1, v2)), but more accurate */
 	if (dot_v2v2(v1, v2) < 0.0f) {
@@ -449,10 +443,7 @@
 	const float sintheta = sin(angle);
 
 	/* double check they are normalized */
-#ifdef DEBUG
-	float test;
-	BLI_assert(fabsf((test = len_squared_v3(axis)) - 1.0f) < 0.0001f || fabsf(test) < 0.0001f);
-#endif
+	BLI_ASSERT_UNIT_V3(axis);
 
 	r[0] = ((costheta + (1 - costheta) * axis[0] * axis[0]) * p[0]) +
 	       (((1 - costheta) * axis[0] * axis[1] - axis[2] * sintheta) * p[1]) +




More information about the Bf-blender-cvs mailing list