ASL
|
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.
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:
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.
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:
The matrix can then be converted to other rotation representations:
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 > | |
T | 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. | |
Matrix_<T> asl::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.
If there are more equations than unknowns (f larger than x0) then a least-squares solution is sought.
Example:
Solve for (x, y) in: