[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [23037] trunk/blender/source/blender: Mathutils fix: Vector.reflect

Dalai Felinto dfelinto at gmail.com
Sun Sep 6 21:51:57 CEST 2009


Revision: 23037
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=23037
Author:   dfelinto
Date:     2009-09-06 21:51:57 +0200 (Sun, 06 Sep 2009)

Log Message:
-----------
Mathutils fix: Vector.reflect
* correct function for reflection and moving it to arithb.c
* note: 2.5 has an already more elegant solution for it (still wrong, but the code is cleaner).
Therefore the merge may need to be manual in that case.

Specifically in 2.5 we are doing:
if(!BaseMath_ReadCallback(self) || !BaseMath_ReadCallback(value)) return NULL;
And there we don't need to create a VectorObject *mirrvec; only to get the values.

Code used to test it:
http://www.pasteall.org/7654/python

* YoFrankie script probably needs to be fixed too.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_arithb.h
    trunk/blender/source/blender/blenlib/intern/arithb.c
    trunk/blender/source/blender/python/api2_2x/vector.c

Modified: trunk/blender/source/blender/blenlib/BLI_arithb.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_arithb.h	2009-09-06 19:18:12 UTC (rev 23036)
+++ trunk/blender/source/blender/blenlib/BLI_arithb.h	2009-09-06 19:51:57 UTC (rev 23037)
@@ -270,6 +270,7 @@
 void RotationBetweenVectorsToQuat(float *q, float v1[3], float v2[3]);
 void vectoquat(float *vec, short axis, short upflag, float *q);
 
+void VecReflect(float *out, float *v1, float *v2);
 void VecBisect3(float *v, float *v1, float *v2, float *v3);
 float VecAngle2(float *v1, float *v2);
 float VecAngle3(float *v1, float *v2, float *v3);

Modified: trunk/blender/source/blender/blenlib/intern/arithb.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/arithb.c	2009-09-06 19:18:12 UTC (rev 23036)
+++ trunk/blender/source/blender/blenlib/intern/arithb.c	2009-09-06 19:51:57 UTC (rev 23037)
@@ -2981,6 +2981,29 @@
 	Normalize(out);
 }
 
+/* Returns a reflection vector from a vector and a normal vector
+reflect = vec - ((2 * DotVecs(vec, mirror)) * mirror)
+*/
+void VecReflect(float *out, float *v1, float *v2)
+{
+	float vec[3], normal[3];
+	float reflect[3] = {0.0f, 0.0f, 0.0f};
+	float dot2;
+
+	VecCopyf(vec, v1);
+	VecCopyf(normal, v2);
+
+	Normalize(normal);
+
+	dot2 = 2 * Inpf(vec, normal);
+
+	reflect[0] = vec[0] - (dot2 * normal[0]);
+	reflect[1] = vec[1] - (dot2 * normal[1]);
+	reflect[2] = vec[2] - (dot2 * normal[2]);
+
+	VecCopyf(out, reflect);
+}
+
 /* Return the angle in degrees between vecs 1-2 and 2-3 in degrees
    If v1 is a shoulder, v2 is the elbow and v3 is the hand,
    this would return the angle at the elbow */

Modified: trunk/blender/source/blender/python/api2_2x/vector.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/vector.c	2009-09-06 19:18:12 UTC (rev 23036)
+++ trunk/blender/source/blender/python/api2_2x/vector.c	2009-09-06 19:51:57 UTC (rev 23037)
@@ -277,20 +277,15 @@
 
 /*----------------------------Vector.reflect(mirror) ----------------------
   return a reflected vector on the mirror normal
-  ((2 * DotVecs(vec, mirror)) * mirror) - vec
-  using arithb.c would be nice here */
+   vec - ((2 * DotVecs(vec, mirror)) * mirror)
+*/
 PyObject *Vector_Reflect( VectorObject * self, PyObject * value )
 {
 	VectorObject *mirrvec;
 	float mirror[3];
 	float vec[3];
-	float reflect[4] = {0.0f, 0.0f, 0.0f, 0.0f};
-	float dot2;
+	float reflect[3] = {0.0f, 0.0f, 0.0f};
 	
-	/* for normalizing */
-	int i;
-	float norm = 0.0f;
-	
 	if (!VectorObject_Check(value)) {
 		PyErr_SetString( PyExc_TypeError, "vec.reflect(value): expected a vector argument" );
 		return NULL;
@@ -302,27 +297,13 @@
 	if (mirrvec->size > 2)	mirror[2] = mirrvec->vec[2];
 	else					mirror[2] = 0.0;
 	
-	/* normalize, whos idea was it not to use arithb.c? :-/ */
-	for(i = 0; i < 3; i++) {
-		norm += mirror[i] * mirror[i];
-	}
-	norm = (float) sqrt(norm);
-	for(i = 0; i < 3; i++) {
-		mirror[i] /= norm;
-	}
-	/* done */
-	
 	vec[0] = self->vec[0];
 	vec[1] = self->vec[1];
 	if (self->size > 2)		vec[2] = self->vec[2];
 	else					vec[2] = 0.0;
-	
-	dot2 = 2 * vec[0]*mirror[0]+vec[1]*mirror[1]+vec[2]*mirror[2];
-	
-	reflect[0] = (dot2 * mirror[0]) - vec[0];
-	reflect[1] = (dot2 * mirror[1]) - vec[1];
-	reflect[2] = (dot2 * mirror[2]) - vec[2];
-	
+
+	VecReflect(reflect, vec, mirror);
+
 	return newVectorObject(reflect, self->size, Py_NEW);
 }
 





More information about the Bf-blender-cvs mailing list