CMLibs Maths

CMLibs Maths is a Python package containing math utility functions. These utility functions are general utilities commonly used by downstream packages.

This package provides two modules

  1. vectorops

  2. algorithms

These modules are surfaced under the namespace package cmlibs within the maths package. To use these modules the following import statement can be used:

import cmlibs.maths.vectorops
import cmlibs.maths.algorithms

Vector Operations

The vectorops module is a collection of functions that operate on python lists as if they were vectors. A basic implementation to forgo the need to use numpy.

Algorithms

The algorithms module is a collection of functions that perform calculations on python lists. These functions are commonly used in other CMLibs packages.

Package API

Vector Operations Module

A collection of functions that operate on python lists as if they were vectors. A basic implementation to forgo the need to use numpy.

cmlibs.maths.vectorops.angle(u, v)

Calculate the angle between two non-zero vectors. :return: The angle between them in radians.

cmlibs.maths.vectorops.axis_angle_to_quaternion(axis, theta)
Parameters:
  • axis – Unit vector axis of rotation.

  • theta – Angle of rotation in right hand sense around axis, in radians.

Returns:

Quaternion representing rotation.

cmlibs.maths.vectorops.axis_angle_to_rotation_matrix(axis, theta)

Convert axis angle to a rotation matrix.

Parameters:
  • axis – Unit vector axis of rotation.

  • theta – Angle of rotation in right hand sense around axis, in radians.

Returns:

3x3 rotation matrix suitable for pre-multiplying vector v: i.e. v’ = Mv

cmlibs.maths.vectorops.euler_to_rotation_matrix(euler_angles)

From Zinc graphics_library.cpp, with matrix transposed to row major. Matrix is product RzRyRx, giving rotation about x, then y, then z with positive angles rotating by right hand rule about axis. :param euler_angles: 3 angles in radians, components: 0 = azimuth (about z) 1 = elevation (about y) 2 = roll (about x) :return: 3x3 rotation matrix suitable for pre-multiplying vector v: i.e. v’ = Mv

cmlibs.maths.vectorops.identity_matrix(size)

Create an identity matrix of size size.

cmlibs.maths.vectorops.matrix_constant_mult(m, c)

Multiply components of matrix m by constant c

cmlibs.maths.vectorops.matrix_inv(a)

Invert a square matrix by compouting the determinant and cofactor matrix.

cmlibs.maths.vectorops.matrix_mult(a, b)

Multiply 2 matrices: first index is down row, second is across column. Assumes sizes are compatible (number of columns of a == number of rows of b).

cmlibs.maths.vectorops.matrix_vector_mult(m, v)

Post multiply matrix m by vector v

cmlibs.maths.vectorops.quaternion_to_rotation_matrix(quaternion)

This method takes a quaternion representing a rotation and turns it into a rotation matrix. :return: 3x3 rotation matrix suitable for pre-multiplying vector v: i.e. v’ = Mv

cmlibs.maths.vectorops.rotation_matrix_to_euler(matrix)

From Zinc graphics_library.cpp, with matrix transposed to row major. Inverse function to euler_to_rotation_matrix.

cmlibs.maths.vectorops.vector_matrix_mult(v, m)

Premultiply matrix m by vector v

Algorithms Module

cmlibs.maths.algorithms.calculateExtents(values)

Calculate the maximum and minimum for each coordinate x, y, and z Return the max’s and min’s as:

[x_min, x_max, y_min, y_max, z_min, z_max]

cmlibs.maths.algorithms.calculate_centroid(data_points)

Calculates the centroid of a list of point coordinates.

Parameters:

data_points – A list containing ‘n’ lists (coordinates) of size ‘m’. With ‘n’ denoting the number of points and ‘m’ denoting the number of dimensions of the coordinates.

Returns:

An m-dimensional list containing the coordinates of the centroid.

cmlibs.maths.algorithms.calculate_extents(values)

Calculate the maximum and minimum for each coordinate x, y, and z Return the max’s and min’s as:

[x_min, x_max, y_min, y_max, z_min, z_max]