Page MenuHome

angleBetweenVecs_bugfix.txt

angleBetweenVecs_bugfix.txt

Index: source/blender/python/api2_2x/Mathutils.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/python/api2_2x/Mathutils.c,v
retrieving revision 1.8
diff -u -p -r1.8 Mathutils.c
--- source/blender/python/api2_2x/Mathutils.c 21 Feb 2005 18:26:53 -0000 1.8
+++ source/blender/python/api2_2x/Mathutils.c 28 Apr 2005 05:49:23 -0000
@@ -369,8 +369,19 @@ static PyObject *M_Mathutils_DotVecs( Py
static PyObject *M_Mathutils_AngleBetweenVecs( PyObject * self,
PyObject * args )
{
+ // original vectors, makea copy of these
VectorObject *vec1;
VectorObject *vec2;
+
+ /* copy of the 2 input vectors, these can be normalized
+ without input vectors being normalized. bugfix
+ no need to use fector objects, just use floats
+ No Chance of 4D vectors getting in.
+
+ Use doubles, since floats will return nan when input vecs are large.*/
+ double vec1copy[3];
+ double vec2copy[3];
+
float norm;
double dot, angleRads;
int x;
@@ -386,30 +397,43 @@ static PyObject *M_Mathutils_AngleBetwee
if( vec1->size > 3 || vec2->size > 3 )
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
"only 2D,3D vectors are supported\n" ) );
-
- //normalize vec1
+
+ /* Check for 2 vectors being the same */
+ if (vec1->size == 3 &&
+ vec1->vec[0] == vec2->vec[0] &&
+ vec1->vec[1] == vec2->vec[1] &&
+ vec1->vec[2] == vec2->vec[2])
+ return PyFloat_FromDouble( dot ); /* 2 points are the same, return zero */
+ else if (vec1->size == 2 &&
+ vec1->vec[0] == vec2->vec[0] &&
+ vec1->vec[1] == vec2->vec[1])
+ return PyFloat_FromDouble( dot ); /* 2 points are the same, return zero */
+
+ //normalize vec1copy
norm = 0.0f;
for( x = 0; x < vec1->size; x++ ) {
- norm += vec1->vec[x] * vec1->vec[x];
+ vec1copy[x] = vec1->vec[x]; /* Assign new vector in the loop */
+ norm += vec1copy[x] * vec1copy[x];
}
norm = ( float ) sqrt( norm );
for( x = 0; x < vec1->size; x++ ) {
- vec1->vec[x] /= norm;
+ vec1copy[x] /= norm;
}
- //normalize vec2
+ //normalize vec2copy
norm = 0.0f;
for( x = 0; x < vec2->size; x++ ) {
- norm += vec2->vec[x] * vec2->vec[x];
+ vec2copy[x] = vec2->vec[x]; /* Assign new vector in the loop */
+ norm += vec2copy[x] * vec2copy[x];
}
norm = ( float ) sqrt( norm );
for( x = 0; x < vec2->size; x++ ) {
- vec2->vec[x] /= norm;
+ vec2copy[x] /= norm;
}
//dot product
for( x = 0; x < vec1->size; x++ ) {
- dot += vec1->vec[x] * vec2->vec[x];
+ dot += vec1copy[x] * vec2copy[x];
}
//I believe saacos checks to see if the vectors are normalized

File Metadata

Mime Type
text/x-diff
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
c8/64/626955ac50dc94f73fb8423ceb27

Event Timeline