-
Notifications
You must be signed in to change notification settings - Fork 18
pymath
The pymath library contains mathematical helper functions
Vectors are represented as arrays (Lua tables) of numbers, e.g. {1, 2, 3}. The pymath functions do not make a difference between row and column vectors.
A matrix is represented as an array of row-vectors, e.g.
local mat = {
{1, 2},
{3, 4}
}with its layout as printed.
Even higher tensors may be formed as arrays of lower tensors (with the lower indices to the right)
-
pymath.cross— cross product of two 3-vectors -
pymath.length— length of a vector, or the distance between two points -
pymath.normalize— scale a vector to unit length
For matrices or arrays-of-vectors, the Lua representation required a large number of tables. For enhanced performance, *_inplace variants are provided where applicable, that write the result back into the first parameter.
pymath.rotation_matrix is a convenience constructor for rotation matrices, with the form selected by its arguments:
-
pymath.rotation_matrix(axis, angle)— 3×3 matrix for a rotation aboutaxis(a vector or an axis keyword) byangledegrees -
pymath.rotation_matrix(angle)— 2×2 matrix for a planar rotation byangledegrees
Unit quaternions are widely used in 3D CAD because of their tight correspondence with 3D rotation matrices. A quaternion is represented in Lua as an array of four numbers {x, y, z, w}.
Functions that require a unit quaternion (e.g. pymath.matrix_from_quat) normalize their input internally, so you rarely need to normalize by hand. See the individual pages for the exact behaviour.
-
pymath.quat_from_rotation— from an axis and angle -
pymath.quat_from_matrix— from a 3×3 rotation matrix -
pymath.rotation_from_quat— to an axis and angle -
pymath.matrix_from_quat— to a 3×3 rotation matrix
and Quaternion operations
-
pymath.multiply_quat— compose two rotations -
pymath.invert_quat— invert a rotation -
pymath.interpolate_quat— spherical linear interpolation (SLERP) -
pymath.rotate_by_quat— rotate a 3-vector with the rotation given by a quaternion
A rotation can also be described by three Euler angles. pymath uses the ZXZ convention
mat = rot(z, alpha) · rot(x, beta) · rot(z, gamma)
-
pymath.matrix_from_euler_angles— 3×3 rotation matrix fromalpha, beta, gamma -
pymath.euler_angles_from_matrix—alpha, beta, gammafrom a 3×3 rotation matrix
with all angles in degrees (like every PYTHA Lua API function)
Bezier curves are a versatile tool to represent spatial splines and for interpolation:
-
pymath.eval_bezier— the curve point at a parametert -
pymath.eval_bezier_deriv— the curve derivative (tangent) att -
pymath.interpolate_cubic— a smooth cubic curve through a list of points, as Bézier segments
-
pymath.interpolate_linear— linear interpolationv + (w - v) * tbetween two numbers or vectors -
pymath.clamp— clamp a number or vector into an interval (default[0, 1]) -
pymath.smooth_step— smooth 0-to-1 ramp over an interval (numbers only)