[Bf-python] AngleBetweenVecs still buggy.
Campbell Barton
cbarton at metavr.com
Tue Sep 27 01:17:37 CEST 2005
Hi Joseph, thanks for committing Theeths patch,
From recent testing AngleBetweenVecs only returns NAN when given a zero
length vector.
Useually Id say that it could raise an exception, but faces with a very
small area can return there normal as (0,0,0) - so maybe it would be
good to check if either vector is 0,0,0 and return 0 as the result. -
Its probably not worth be writing a patch for this.
- Cam
> This would be a nice issue to be submitted to the bug tracker. Please
> describe the conditions under which your getting NAN returned.
>
> Campbell Barton wrote:
>
>> Hi, There are a number of existing fixes for
>> Mathutils.AngleBetweenVecs being buggy. For the Nth time I'm using a
>> python workaround, but its frustrating that there have been 4 people
>> who have released fixes, some ~5 months old.
>>
>> Heres what I'm currently using as a python wrapper that works around
>> Anglebetween Vecs returning NAN.
>>
>>
>> # VEC ANGLE NEVER RETURNS NAN!
>> #=============================================================================#
>>
>> # Math functions
>> #=============================================================================#
>>
>> X,Y,Z = 0,1,2
>>
>> def vecMagnitude(vec):
>> '''Calculates the magnitude of a vector and returns the result.
>>
>> Assumes the vector is an XYZ vector.
>> '''
>> return math.sqrt(vec[X]**2 + vec[Y]**2 + vec[Z]**2)
>>
>>
>> def vecDotProduct(vec1, vec2):
>> """Calculates the dot product of two vectors and returns it.
>>
>> Assumes all the vectors are XYZ vectors.
>> """
>> return vec1[X]*vec2[X] + vec1[Y]*vec2[Y] + vec1[Z]*vec2[Z]
>>
>>
>> def vecAngle(vec1, vec2):
>> """Calculates the angle between two vectors (in degrees) and
>> returns it.
>>
>> Assumes all the vectors are XYZ vectors.
>> """
>> # Assume they could be the same first.
>> if vec1.x == vec2.x and\
>> vec1.y == vec2.y and\
>> vec1.z == vec2.z:
>> return 0
>> # Try NAN returning mathutils first
>> ang = Mathutils.AngleBetweenVecs(vec1, vec2)
>> if ang == ang: # Not nan?
>> return ang
>> # Ick Blender cant do it se we have to.
>> denom = vecMagnitude(vec1) * vecMagnitude(vec2)
>> if denom != 0:
>> quotient = vecDotProduct(vec1, vec2) / denom
>>
>> #clip the quotient to acceptable values for acos
>> if quotient > 1:
>> quotient = 1
>> if quotient < -1:
>> quotient = -1
>>
>> return math.acos(quotient) / math.pi * 180.0
>> else:
>> #the vectors are perpendicular
>> return 90.0
>>
>> _______________________________________________
>> Bf-python mailing list
>> Bf-python at projects.blender.org
>> http://projects.blender.org/mailman/listinfo/bf-python
>
>
--
Campbell J Barton
133 Hope Street
Geelong West, Victoria 3218 Australia
URL: http://www.metavr.com
e-mail: cbarton at metavr.com
phone: AU (03) 5229 0241
More information about the Bf-python
mailing list