Optimisation problem
Simon Wittber (Maptek)
Simon.Wittber at perth.maptek.com.au
Tue Nov 12 02:44:28 EST 2002
I have created a quaternion class which I am using for rotations in
OpenGL.
Usage goes something like this:
q = Quaternion(zRot, 0.0, 0.0, 1.0)
glMultMatrixf(q.matrix)
Does anyone have any idea how I can optimise this code? It is going to
be used in a very tight rendering loop. I would prefer to keep all the
code in Python, and avoid writing any external modules.
class Quaternion:
def __init__(self, degree, x, y, z):
self.matrix = range(16)
angle = (degree / 180.0) *
3.1415926535897932384626433832795
result = math.sin(angle / 2.0)
self.w = math.cos(angle / 2.0)
self.x = x * result
self.y = y * result
self.z = z * result
self.__createMatrix()
def __createMatrix(self):
self.matrix[0] = 1.0 - 2.0 * ( self.y * self.y + self.z
* self.z )
self.matrix[1] = 2.0 * ( self.x * self.y - self.w *
self.z )
self.matrix[2] = 2.0 * ( self.x * self.z + self.w *
self.y )
self.matrix[3] = 0.0
self.matrix[4] = 2.0 * ( self.x * self.y + self.w *
self.z )
self.matrix[5] = 1.0 - 2.0 * ( self.x * self.x + self.z
* self.z )
self.matrix[6] = 2.0 * ( self.y * self.z - self.w *
self.x )
self.matrix[7] = 0.0
self.matrix[8] = 2.0 * ( self.x * self.z - self.w *
self.y )
self.matrix[9] = 2.0 * ( self.y * self.z + self.w *
self.x )
self.matrix[10] = 1.0 - 2.0 * ( self.x * self.x + self.y
* self.y )
self.matrix[11] = 0.0
self.matrix[12] = 0.0
self.matrix[13] = 0.0
self.matrix[14] = 0.0
self.matrix[15] = 1.0
--
simon . wittber @ perth . maptek . com .au
--
More information about the Python-list
mailing list