[Bf-python] AngleBetweenVecs still buggy.
Campbell Barton
cbarton at metavr.com
Sat Sep 24 13:39:45 CEST 2005
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
More information about the Bf-python
mailing list