ASL
Math

Detailed Description

Vectors and matrices for 2D/3D geometry

Classes and functions for math, including vector and matrix types useful in 2D/3D geometric computations and graphics.

These classes are templates that can be used with elements of any numeric type, but some functions oly make sense with floating point types.

Vec3_<int> ivec(1, 2, 3);
Matrix4_<float> m;

They all have two predefined specializations, for float and double elements. The type for double has a d suffix, and the type for float has no suffix:

Vec3 v; // = Vec3_<float>
Vec3d w; // = Vec3_<double>

Classes Vec2_, Vec3_ and Vec4_ represent 2D, 3D and 4D vectors. Classes Matrix3_ and Matrix4_ represent 3x3 and 4x4 matrices, useful to work with spatial transformations. A Matrix4_ can multiply a 3D vector to apply an affine transform (the vector will be assumed to have a 4th component with value 1). Additionally, class Quaternion_ represent a quaternion and is useful to work with rotations in 3D.

Matrix4 transform = Matrix4::translate(position) * Matrix4::rotateX(angleX);
Vec3 worldPos = transform * position;
static Matrix4_ translate(const Vec3_< T > &t)
Returns a translation matrix for the given vector.
Definition: Matrix4.h:281
static Matrix4_ rotateX(T angle)
Returns a rotation matrix of the given angle in radians around the x axis.
Definition: Matrix4.h:312

Representation of 3D rotations

Rotations in 3D space can be represented in different ways and they can be converted to one another (via a Matrix4).

A rotation matrix can be constructed these ways:

m = Matrix4::rotateX(alpha); // a rotation angle around the X axis (or Y or Z)
m = Matrix4::rotate(axis, angle); // rotation angle around an arbitrary axis
m = Matrix4::rotate(axisAngle); // axis-angle: the axis vector with magnitude equal to the rotation angle
m = Matrix4::rotateE({ alpha, beta, gamma }, "XYZ*"); // Euler angles with an arbitrary axis sequence
m = quaternion.matrix(); // a quaternion
static Matrix4_ rotate(const Vec3_< T > &axis, T angle)
Returns a rotation matrix of an angle in radians around a given axis.
static Matrix4_ rotateE(const Vec3_< T > &r, int a0, int a1, int a2)
Returns a rotation matrix created from Euler angles rotating the components of r in axes a0,...
Definition: Matrix4.h:367

The matrix can then be converted to other rotation representations:

auto angles = m.eulerAngles("XYZ*"); // to Euler angles
auto aa = m.axisAngle(); // to axis-angle
Quaternion q = m.rotation(); // to a quaternion

Vectors and matrices of any size

Class Matrix_ represents a matrix of any size (predefined Matrix and Matrixd for float/double). It uses dynamic memory and is less efficient than the fixed size types above. Matrix_ can be used to represent systems of linear equations or arbitrary vectors.

Functions solve() and solveZero() can be used to solve linear or non-linear systems with equal number of equations and unknowns, or more equations (least-squares).

Classes

class  Matrix_< T >
 A matrix supporting basic arithmetic operations. More...
 
class  Matrix3_< T >
 A Matrix3 is a 3x3 matrix useful for representing affine transformations in 2D space. More...
 
class  Matrix4_< T >
 A Matrix4 is a 4x4 matrix useful for representing affine transformations in 3D space. More...
 
class  Quaternion_< T >
 A Quaternion representing an orientation or rotation in 3D space. More...
 
class  Vec2_< T >
 A Vec2 represents a vector in 2D space. More...
 
class  Vec3_< T >
 A Vec3_ represents a vector in 3D space. More...
 
class  Vec4_< T >
 A Vec4 is a 4-dimensional vector usually representing homogenous coordinates. More...
 

Functions

template<class T >
Matrix_< T > solve (const Matrix_< T > &A, const Matrix_< T > &b)
 Solves the matrix equation A*x=b and returns x; if b is a matrix (not a column) then the equation is solved for each of b's columns and solutions returned as the columns of the returned matrix; if A is not square (more equations than unknowns), then a least-squares solution is computed.
 
template<class T , class F >
Matrix_< T > solveZero (F f, const Matrix_< T > &x0, const SolveParams &p=SolveParams())
 Solves a system of equations F(x)=[0], given by functor f, which returns a vector of function values for an input vector x; and using x0 as initial guess. More...
 
template<class T , class F >
solveZero (F f, T x0, const SolveParams &p=SolveParams())
 Solves the equation f(x)=0 given by functor f using an iterative method starting at x0 as initial estimate.
 
template<class T >
asl::Matrix4_< T > orthonormalize (const asl::Matrix4_< T > &m)
 Returns an orthonormal approximation of this transform matrix.
 

Function Documentation

◆ solveZero()

Matrix_<T> asl::solveZero ( f,
const Matrix_< T > &  x0,
const SolveParams p = SolveParams() 
)

Solves a system of equations F(x)=[0], given by functor f, which returns a vector of function values for an input vector x; and using x0 as initial guess.

If there are more equations than unknowns (f larger than x0) then a least-squares solution is sought.

Example:

Solve for (x, y) in:

  • (x-1)2 + (y-1)2 - 1 = 0
  • sin(x) + sin(y) - x = 0
auto x = solveZero([](const Matrixd& x) { // unknowns are x[0], x[1]
return Matrixd{
sqr(x[0] - 1) + sqr(x[1] - 1) - 1,
sin(x[0]) + sin(x[1]) - x[0]
};
}, { 0.0, 0.0 }); // initial estimation
T sqr(T x)
Returns x squared.
Definition: defs.h:193
Matrix_< T > solveZero(F f, const Matrix_< T > &x0, const SolveParams &p=SolveParams())
Solves a system of equations F(x)=[0], given by functor f, which returns a vector of function values ...
Definition: Matrix.h:479