Skip to content

Commit

Permalink
* Merged in content from old project structure's trunk/enthought.math…
Browse files Browse the repository at this point in the history
…ematics_2.1.

* Updated project name in README.txt.
* Updated project dependencies in setup.py.
  • Loading branch information
dpeterson committed Jan 8, 2008
1 parent 6e7f095 commit 99be704
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.txt
@@ -1,2 +1,2 @@
The interpolate package, a part of the Enthought Tool Suite.
The SciMath project, a part of the Enthought Tool Suite.

Empty file.
9 changes: 9 additions & 0 deletions enthought/mathematics/api.py
@@ -0,0 +1,9 @@
#-----------------------------------------------------------------------------
#
# Copyright (c) 2006 by Enthought, Inc.
# All rights reserved.
#
#-----------------------------------------------------------------------------

from quaternion import normq, qmult, rotmat, rotquat

133 changes: 133 additions & 0 deletions enthought/mathematics/quaternion.py
@@ -0,0 +1,133 @@
#-----------------------------------------------------------------------------
#
# Copyright (c) 2006 by Enthought, Inc.
# All rights reserved.
#
#-----------------------------------------------------------------------------

""" Provides functions that manipulate quaternions.
Quaternions are frequently used to avoid 'gimbal lock' and/or 'twisting'
when rotating polygons in 3D space.
"""

# Major library imports.
import numpy as np


def normq(quat):
"""
Normalize a quaternion.
"""

quat = np.asarray(quat)
return quat / np.sqrt(np.dot(quat, quat))


def qmult(q1, q2):
"""
Multiply two quaternions.
"""

s1, v1 = q1[0], q1[1:]
s2, v2 = q2[0], q2[1:]
scalar = s1*s2 - np.dot(v1, v2)
vector = s2*v1 + s1*v2 + _crossv(v1, v2)[0]
return np.hstack((scalar, vector))


def rotmat(q):
"""
Transform a unit quaternion into its corresponding rotation matrix (to
be applied on the right side).
"""

w, x, y, z = q
xx2 = 2*x*x
yy2 = 2*y*y
zz2 = 2*z*z
xy2 = 2*x*y
wz2 = 2*w*z
zx2 = 2*z*x
wy2 = 2*w*y
yz2 = 2*y*z
wx2 = 2*w*x

rmat = np.empty((3, 3), float)
rmat[0,0] = 1. - yy2 - zz2
rmat[0,1] = xy2 - wz2
rmat[0,2] = zx2 + wy2
rmat[1,0] = xy2 + wz2
rmat[1,1] = 1. - xx2 - zz2
rmat[1,2] = yz2 - wx2
rmat[2,0] = zx2 - wy2
rmat[2,1] = yz2 + wx2
rmat[2,2] = 1. - xx2 - yy2

return rmat


def rotquat(vhat1, vhat2):
"""
Compute the quaternion that rotates the normalized vector *vhat1* to *vhat2*.
The quaternion is repesented as a tuple (scalar part, vector part).
"""

# Compute the bisectors
bisector = _normv(vhat1 + vhat2)

# Compute the scalar part
cost2 = _dotv(vhat1, bisector)

# Compute the vector part
sint2v = _crossv(vhat1, bisector)

# NOTE: This is an expanded version of the old version of numpy's
# 'column_stack' function.
tup = (cost2, np.transpose(sint2v))
arrays = map(np.transpose, map(np.atleast_2d, tup))
return np.concatenate(arrays, 1)


def _crossv(vertices1, vertices2):
"""
Perform the cross product on arrays of 3-vectors.
"""

v10, v11, v12 = np.transpose(vertices1)
v20, v21, v22 = np.transpose(vertices2)

# NOTE: This is an expanded version of the old version of numpy's
# 'column_stack' function.
tup = (
v11*v22-v12*v21,
v12*v20-v10*v22,
v10*v21-v11*v20)
arrays = map(np.transpose, map(np.atleast_2d, tup))
return np.concatenate(arrays, 1)


def _dotv(vertices1, vertices2):
"""
Perform the dot product on arrays of 3-vectors.
"""

return np.sum(vertices1*vertices2, axis=-1)


def _normv(vertices):
"""
Normalize an array of 3-vectors.
"""

vertices = np.asarray(vertices)
return vertices / np.sqrt(_dotv(vertices, vertices))[:,np.newaxis]

9 changes: 4 additions & 5 deletions setup.py
Expand Up @@ -34,17 +34,16 @@ def etsdep(p, min, max=None, literal=False):


# Declare our ETS project dependencies.
TRAITS = etsdep('enthought.traits', '2.0b1')
TRAITS = etsdep('Traits', '3.0.0b1')


setup(
author = 'Enthought, Inc',
author_email = 'info@enthought.com',
dependency_links = [
'http://code.enthought.com/enstaller/eggs/source',
'http://code.enthought.com/enstaller/eggs/source/unstable',
],
description = "Array interpolation/extrapolation",
description = "Science and mathematics features",
extras_require = {
# All non-ets dependencies should be in this extra to ensure users can
# decide whether to require them or not.
Expand All @@ -57,7 +56,7 @@ def etsdep(p, min, max=None, literal=False):
TRAITS,
],
license = "BSD",
name = 'enthought.interpolate',
name = 'SciMath',
namespace_packages = [
"enthought",
],
Expand All @@ -66,7 +65,7 @@ def etsdep(p, min, max=None, literal=False):
],
test_suite = 'nose.collector',
url = 'http://code.enthought.com/ets',
version = '2.1.0a1',
version = '3.0.0b1',
**configuration().todict()
)

0 comments on commit 99be704

Please sign in to comment.