Skip to main content

Homogeneous Transformation Matrices and Quaternions

Project description

A collection of functions and classes for calculating 4x4 matrices for translating, rotating, reflecting, scaling, shearing, projecting, orthogonalizing, and superimposing arrays of 3D homogeneous coordinates as well as for converting between rotation matrices, Euler angles, and quaternions. Also includes an Arcball control object and functions to decompose transformation matrices.

Author:

Christoph Gohlke

Organization:

Laboratory for Fluorescence Dynamics. University of California, Irvine

Version:

2018.9.5

Requirements

Notes

Transformations.py is no longer actively developed and has a few known issues and numerical instabilities. The module is mostly superseded by other modules for 3D transformations and quaternions:

The API is not stable yet and is expected to change between revisions.

Python 2.7 and 3.4 are deprecated.

This Python code is not optimized for speed. Refer to the transformations.c module for a faster implementation of some functions.

Documentation in HTML format can be generated with epydoc.

Matrices (M) can be inverted using numpy.linalg.inv(M), be concatenated using numpy.dot(M0, M1), or transform homogeneous coordinate arrays (v) using numpy.dot(M, v) for shape (4, *) column vectors, respectively numpy.dot(v, M.T) for shape (*, 4) row vectors (“array of points”).

This module follows the “column vectors on the right” and “row major storage” (C contiguous) conventions. The translation components are in the right column of the transformation matrix, i.e. M[:3, 3]. The transpose of the transformation matrices may have to be used to interface with other graphics systems, e.g. OpenGL’s glMultMatrixd(). See also [16].

Calculations are carried out with numpy.float64 precision.

Vector, point, quaternion, and matrix function arguments are expected to be “array like”, i.e. tuple, list, or numpy arrays.

Return types are numpy arrays unless specified otherwise.

Angles are in radians unless specified otherwise.

Quaternions w+ix+jy+kz are represented as [w, x, y, z].

A triple of Euler angles can be applied/interpreted in 24 ways, which can be specified using a 4 character string or encoded 4-tuple:

Axes 4-string: e.g. ‘sxyz’ or ‘ryxy’

  • first character : rotations are applied to ‘s’tatic or ‘r’otating frame

  • remaining characters : successive rotation axis ‘x’, ‘y’, or ‘z’

Axes 4-tuple: e.g. (0, 0, 0, 0) or (1, 1, 1, 1)

  • inner axis: code of axis (‘x’:0, ‘y’:1, ‘z’:2) of rightmost matrix.

  • parity : even (0) if inner axis ‘x’ is followed by ‘y’, ‘y’ is followed by ‘z’, or ‘z’ is followed by ‘x’. Otherwise odd (1).

  • repetition : first and last axis are same (1) or different (0).

  • frame : rotations are applied to static (0) or rotating (1) frame.

References

  1. Matrices and transformations. Ronald Goldman. In “Graphics Gems I”, pp 472-475. Morgan Kaufmann, 1990.

  2. More matrices and transformations: shear and pseudo-perspective. Ronald Goldman. In “Graphics Gems II”, pp 320-323. Morgan Kaufmann, 1991.

  3. Decomposing a matrix into simple transformations. Spencer Thomas. In “Graphics Gems II”, pp 320-323. Morgan Kaufmann, 1991.

  4. Recovering the data from the transformation matrix. Ronald Goldman. In “Graphics Gems II”, pp 324-331. Morgan Kaufmann, 1991.

  5. Euler angle conversion. Ken Shoemake. In “Graphics Gems IV”, pp 222-229. Morgan Kaufmann, 1994.

  6. Arcball rotation control. Ken Shoemake. In “Graphics Gems IV”, pp 175-192. Morgan Kaufmann, 1994.

  7. Representing attitude: Euler angles, unit quaternions, and rotation vectors. James Diebel. 2006.

  8. A discussion of the solution for the best rotation to relate two sets of vectors. W Kabsch. Acta Cryst. 1978. A34, 827-828.

  9. Closed-form solution of absolute orientation using unit quaternions. BKP Horn. J Opt Soc Am A. 1987. 4(4):629-642.

  10. Quaternions. Ken Shoemake. http://www.sfu.ca/~jwa3/cmpt461/files/quatut.pdf

  11. From quaternion to matrix and back. JMP van Waveren. 2005. http://www.intel.com/cd/ids/developer/asmo-na/eng/293748.htm

  12. Uniform random rotations. Ken Shoemake. In “Graphics Gems III”, pp 124-132. Morgan Kaufmann, 1992.

  13. Quaternion in molecular modeling. CFF Karney. J Mol Graph Mod, 25(5):595-604

  14. New method for extracting the quaternion from a rotation matrix. Itzhack Y Bar-Itzhack, J Guid Contr Dynam. 2000. 23(6): 1085-1087.

  15. Multiple View Geometry in Computer Vision. Hartley and Zissermann. Cambridge University Press; 2nd Ed. 2004. Chapter 4, Algorithm 4.7, p 130.

  16. Column Vectors vs. Row Vectors. http://steve.hollasch.net/cgindex/math/matrix/column-vec.html

Examples

>>> alpha, beta, gamma = 0.123, -1.234, 2.345
>>> origin, xaxis, yaxis, zaxis = [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]
>>> I = identity_matrix()
>>> Rx = rotation_matrix(alpha, xaxis)
>>> Ry = rotation_matrix(beta, yaxis)
>>> Rz = rotation_matrix(gamma, zaxis)
>>> R = concatenate_matrices(Rx, Ry, Rz)
>>> euler = euler_from_matrix(R, 'rxyz')
>>> numpy.allclose([alpha, beta, gamma], euler)
True
>>> Re = euler_matrix(alpha, beta, gamma, 'rxyz')
>>> is_same_transform(R, Re)
True
>>> al, be, ga = euler_from_matrix(Re, 'rxyz')
>>> is_same_transform(Re, euler_matrix(al, be, ga, 'rxyz'))
True
>>> qx = quaternion_about_axis(alpha, xaxis)
>>> qy = quaternion_about_axis(beta, yaxis)
>>> qz = quaternion_about_axis(gamma, zaxis)
>>> q = quaternion_multiply(qx, qy)
>>> q = quaternion_multiply(q, qz)
>>> Rq = quaternion_matrix(q)
>>> is_same_transform(R, Rq)
True
>>> S = scale_matrix(1.23, origin)
>>> T = translation_matrix([1, 2, 3])
>>> Z = shear_matrix(beta, xaxis, origin, zaxis)
>>> R = random_rotation_matrix(numpy.random.rand(3))
>>> M = concatenate_matrices(T, R, Z, S)
>>> scale, shear, angles, trans, persp = decompose_matrix(M)
>>> numpy.allclose(scale, 1.23)
True
>>> numpy.allclose(trans, [1, 2, 3])
True
>>> numpy.allclose(shear, [0, math.tan(beta), 0])
True
>>> is_same_transform(R, euler_matrix(axes='sxyz', *angles))
True
>>> M1 = compose_matrix(scale, shear, angles, trans, persp)
>>> is_same_transform(M, M1)
True
>>> v0, v1 = random_vector(3), random_vector(3)
>>> M = rotation_matrix(angle_between_vectors(v0, v1), vector_product(v0, v1))
>>> v2 = numpy.dot(v0, M[:3,:3].T)
>>> numpy.allclose(unit_vector(v1), unit_vector(v2))
True

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transformations-2018.9.5.tar.gz (44.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

transformations-2018.9.5-cp37-cp37m-win_amd64.whl (55.3 kB view details)

Uploaded CPython 3.7mWindows x86-64

transformations-2018.9.5-cp37-cp37m-win32.whl (49.7 kB view details)

Uploaded CPython 3.7mWindows x86

transformations-2018.9.5-cp36-cp36m-win_amd64.whl (54.8 kB view details)

Uploaded CPython 3.6mWindows x86-64

transformations-2018.9.5-cp36-cp36m-win32.whl (49.1 kB view details)

Uploaded CPython 3.6mWindows x86

transformations-2018.9.5-cp35-cp35m-win_amd64.whl (54.8 kB view details)

Uploaded CPython 3.5mWindows x86-64

transformations-2018.9.5-cp35-cp35m-win32.whl (49.1 kB view details)

Uploaded CPython 3.5mWindows x86

transformations-2018.9.5-cp34-cp34m-win_amd64.whl (52.4 kB view details)

Uploaded CPython 3.4mWindows x86-64

transformations-2018.9.5-cp34-cp34m-win32.whl (46.7 kB view details)

Uploaded CPython 3.4mWindows x86

transformations-2018.9.5-cp27-cp27m-win_amd64.whl (52.9 kB view details)

Uploaded CPython 2.7mWindows x86-64

transformations-2018.9.5-cp27-cp27m-win32.whl (47.0 kB view details)

Uploaded CPython 2.7mWindows x86

File details

Details for the file transformations-2018.9.5.tar.gz.

File metadata

  • Download URL: transformations-2018.9.5.tar.gz
  • Upload date:
  • Size: 44.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5.tar.gz
Algorithm Hash digest
SHA256 36bf1cb5f8fba4840be628400ae449b1ea5a81ea3f8fddba6c0e899842e71910
MD5 2ae804b580f5254a452ffb4d0d495aa0
BLAKE2b-256 302f18a95df2e3d3b1ba2673e702d95d807e18fcfa00bdf7c303a0af038874c0

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 55.3 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 d82516e182f341fceef15b057871c1acfe3dc207a498ec17d6e1e8c5a435f3ac
MD5 ea16e848c8a49661ecb93d8e3326e9e9
BLAKE2b-256 ab46cfb19413b363bf42e928c3b6488fd707b582a1f376400b7c90c6c341703b

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp37-cp37m-win32.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 49.7 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 67ea0a4efc9892525594bfb67546ce210febe02de3fb0f10eee0c64382fda402
MD5 43f45d23c0a7ae9893d9e4e4f5c92697
BLAKE2b-256 b8a96fe7263e17e36195c7ddb7b526a146eec9c722fdd1f9b5ee5c3705dd2748

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 54.8 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 cb98c81c710a286b0af272b79f14a730aef1e27e6ebd0392ff2fea303e16d7cd
MD5 da98b3da5b09a82ce08704c1878ce1a4
BLAKE2b-256 069d491dcea00c0dc0f6ca43521aca9563fb11ede71bef4e76dd22646bcdb065

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp36-cp36m-win32.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 49.1 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 1dbbc63148bb6d94ea96c225d800890471c3951b759d9c6069e3398df928bb5b
MD5 61e3c16af09659547db836c98e6f3712
BLAKE2b-256 5e1bce0a75597166aa739d8c2c02d8d0c5af5f90f325e25a9cf4be4519743aea

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp35-cp35m-win_amd64.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 54.8 kB
  • Tags: CPython 3.5m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 d10fcbd334f7bd92ec15841d4c14abbdde6108389be1620d48786bade5329787
MD5 d9b4133e2e2fd126bf6114e8dc4a94b6
BLAKE2b-256 1d54d87c85c1d2e287e9e265f3b13c29c96fca612907de3337d7bb78f7322f1f

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp35-cp35m-win32.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 49.1 kB
  • Tags: CPython 3.5m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 3a1e538e7ffcf2c3dd8c28c84462c2a38100c01efe1eeadc094745d7808b75dd
MD5 dfe13bca28f2391639f9d927c0ab2e2f
BLAKE2b-256 26f57ba3aadb2c51335b75d759186c80b2d18b10ec61a787773af4a00bc5b545

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp34-cp34m-win_amd64.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp34-cp34m-win_amd64.whl
  • Upload date:
  • Size: 52.4 kB
  • Tags: CPython 3.4m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 02b3f678aa73603627e20fdbdc80752384a53c42e7800e55e42a52edeb480a74
MD5 15a200d546fe2c28db0942457a0bc23f
BLAKE2b-256 87b0806d095b3c52a2d7ddbaf268059ce784da32dd733d309484ad6df68382d5

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp34-cp34m-win32.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp34-cp34m-win32.whl
  • Upload date:
  • Size: 46.7 kB
  • Tags: CPython 3.4m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 2fc52a19b5d8e5ab061bf48b94580200d8efbd309f6f863b1851d1566edf35b8
MD5 b3be28bc279b0e89aba0fa4b79585e79
BLAKE2b-256 55eead82e5861b600a63c6a17f2b1add5df87b8f1b828f8da480ae406aa682e7

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp27-cp27m-win_amd64.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp27-cp27m-win_amd64.whl
  • Upload date:
  • Size: 52.9 kB
  • Tags: CPython 2.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 ae1eb2f01a048691bdf09fe36c1c55d210ee8181f1f09c46f537350301a5ad93
MD5 acd6368f08777456e79e2466331b62a8
BLAKE2b-256 6bfd25c5e3a708ff3c6008c80a95c83a89184e627680f44a9ffb83094769bd9b

See more details on using hashes here.

File details

Details for the file transformations-2018.9.5-cp27-cp27m-win32.whl.

File metadata

  • Download URL: transformations-2018.9.5-cp27-cp27m-win32.whl
  • Upload date:
  • Size: 47.0 kB
  • Tags: CPython 2.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for transformations-2018.9.5-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 cdb6e31c217230d782e63725af1d77ed9898be9fbbf90184a52bbe426f271ae9
MD5 11ef90bd5b9eaed1abb3bc01d28709ec
BLAKE2b-256 a5e9df55fa1c9135ad7cf5c330c6af2accaecc709e385bcdedc412a7c17b5966

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page