Out Of Topic : Converting local Axis Angle to Quaternion

Hello All,

This is a bit (100%) out of Panda3D . But this is the place where i know the more talented Python guys so…

I need to code in pure python (not using P3D no using numerics) a function to convert Axis Angle to a single quaternion.

I have 2 bones (Master and Child). I know the child “position” relative to the parent position by the 3 angles around x,y,z axis. (in degrees).
I must provide 1 single quaternion that provide the child position in parent space.

From what i remember from quaternion i will need to :

  1. convert angles to radian
  2. calculate 1 quaternion (x,y,z,w) for each axis angle by
    a)getting a normalized axis vector
    b) doing some cos/sin calculation on each member of the quaternion
    c) normalize the resulting quaternion
  3. add the 3 resulting quaternion to make the single one.

Is there any trick i did not forecasted? if you any sourcecode on the topic i’m very interrested…

This might help you. It’s pure python implementation of several useful 2D and 3D math classes, including quaternions:
http://partiallydisassembled.net/euclid.html

I found it in the “Python Package Index” (pypi), formerly known as the “Python Cheeseshop”. Not the same as CPAN for Perl, but still it has more than 2000 packages listed. Always a good idea to search here for something you need in Python:
http://cheeseshop.python.org/pypi
http://pypi.python.org/pypi

enn0x

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Class name : Quaternion
#
# Description:
#
#	Encapsulates a quaternion and provides methods for its use.
#
# Author: Paul H. Leopard
#
# $Modtime: 8/08/06 10:37a $
#
# $Revision: 6 $ 
#
#-------0---------0---------0---------0---------0---------0---------0---------0

class Quaternion:
	# .........................................................................
	#
	# Description:
	#
	#    Construct an identity quaternion.
	#
	# Input(s):
	#
	#	None
	#
	# Output(s):
	#
	#	None
	#
	def __init__(self):
		self.mW = 1
		self.mX = 0
		self.mY = 0
		self.mZ = 0

	# .........................................................................
	#
	# Description:
	#
	#    Set the components of this instance given a rotation axis.
	#
	# Input(s):
	#
	#    angleRad - Axial rotation angle.
	#    xAxis - X axis component.
	#    yAxis - Y axis component.
	#    zAxis - Z axis component.
	#
	# Output(s):
	#
	#	None
	#
	def fromAngleAxis(self,angleRad,xAxis,yAxis,zAxis) :
		halfAngle = angleRad*0.5
		sinHalf = math.sin(halfAngle)
		self.mW = math.cos(halfAngle)
		self.mX = xAxis.mX*sinHalf
		self.mY = yAxis.mY*sinHalf
		self.mZ = zAxis.mZ*sinHalf