|
Aeon Engine c550894
AeonGames Open Source Game Engine
|
Inline functions related to 3D Math. More...
#include <cmath>#include <cassert>#include <cstring>#include <climits>#include <cfloat>#include <cstdio>

Go to the source code of this file.
Functions | |
| float | sabs (float x) |
| Single pressision floating point absolute value. | |
Quaternion Functions \note The quaternion scalar is the first element in any quaternion array | |
| void | RotateVectorByQuat (const float *q, const float *v, float *out) |
| Rotates a vector around the origin by a quaternion. | |
| float * | GetQuaternionInverse (const float *q, float *out) |
| Inverts a unit length quaternion, same as quaternion conjugate. | |
| void | AngleAxisToQuat (float angle, float x, float y, float z, float *quat) |
| Convert axis angle rotation notation to a quaternion. | |
| void | EulerToQuat (float *euler, float *q) |
| Convert euler angle rotation notation to a quaternion. | |
| float * | MultQuats (const float *q1, const float *q2, float *out) |
| Multiply two quaternions. | |
| void | QuatTo4x4Matrix (float *q, float *m) |
| Builds a 3x4 rotation matrix out of a quaternion inside a 4x4 matrix. | |
| void | QuatTo3x3Matrix (const float *q, float *m) |
| Builds a 3x3 rotation matrix out of a quaternion. | |
| void | Matrix4x4ToQuat (float *matrix, float *q) |
| Extracts rotation matrix and converts it into a quaternion. | |
| void | MultQuats4 (float *q1, float *q2, float *out) |
| Multiplies two quaternions. | |
| bool | CapInterpolation (const float *q1, const float *q2, double interpolation, float *out) |
| Caps quaternion interpolation factor to [0,1] and copies the corresponding endpoint. | |
| float * | LerpQuats (const float *q1, const float *q2, double interpolation, float *out) |
| Linearly interpolate between two quaternions. | |
| float * | NlerpQuats (const float *q1, const float *q2, double interp, float *out) |
| Linearly interpolate between two quaternions return the normalized result. | |
| float * | SlerpQuats (float *q1, float *q2, float interpolation, float *out) |
| Spherical Linear interpolation between two quaternions. | |
Vector Functions | |
| float * | Cross3 (float *v1, float *v2, float *dst) |
| Cross product. | |
| float | Length (float const *const v) |
| Calculate vector length. | |
| float | Length4 (float *v) |
| Calculate 4 element vector length. | |
| float | Dot (const float v1[], const float v2[]) |
| 3 element vector Dot Product. | |
| float | Dot4 (const float v1[], const float v2[]) |
| 4 element vector Dot Product. | |
| float * | Normalize (float *v) |
| Normalize vector. | |
| float * | Normalize4 (float *v) |
| Normalize vector. | |
| float * | NormalizePlane (float const *const aPlane, float *aOut) |
| Normalizes a plane. | |
| float * | Add4 (float *v1, float *v2, float *out) |
| adds two 4 element vectors. | |
| float * | Subtract4 (float *v1, float *v2, float *out) |
| adds two 4 element vectors. | |
| float * | ScalarMultiply4 (float *v, float s, float *out) |
| Multiply 4 element vector by a scalar. | |
| void | ClipVelocity (float *v, float *normal, float overbounce) |
| Clips Velocity against impacting surface normal. | |
| int | ClosestAxis (float *v) |
| Returns the closest to paralell axis to the provided vector. | |
| void | InterpolateVectors (float *v1, float *v2, float interpolation, float *out) |
| Interpolates between two vectors. | |
| float | DistanceSquared (const float a[], const float b[]) |
| Returns the squared distance between point a and b. | |
| float | Distance (const float a[], const float b[]) |
| Returns the distance between point a and b. | |
| float * | MultVector4x4Matrix (const float *v, const float *m, float *out) |
| Multiplies a vector and a 4x4 matrix. | |
| float * | MultVectorScalar (float *v, float s, float *out) |
| Multiplies a vector and a Scalar. | |
| void | MultVector3x3Matrix (float *v, float *m, float *out) |
| Multiplies a vector and the rotational 3x3 part of a 4x4 matrix. | |
| float * | GetScaleVectorInverse (const float *v, float *out) |
| Returns the multiplicative inverse of a scale (transform) vector. | |
| float * | GetPositionVectorInverse (const float *v, float *out) |
| Returns the additive inverse of a position (transform) vector. | |
Matrix Functions | |
| float * | SetIdentityMatrix4x4 (float *M) |
| Set a matrix to the Identity. | |
| float * | Extract3x3Matrix (const float *m, float *out) |
| Extracts a 3x3 matrix from a 4x4 matrix. | |
| float * | Extract3x3Into4x4 (const float *m, float *out) |
| Extracts a 3x3 matrix from a 4x4 matrix into a 4x4 matrix. | |
| float * | Convert3x3To4x3 (const float *m, float *out) |
| converts a 3x3 matrix to a 4x3 matrix. | |
| float * | Convert3x3To4x4 (const float *m, float *out) |
| converts a 3x3 matrix to a 4x4 matrix. | |
| float * | Transpose3x3Matrix (const float *src, float *dst) |
| Transposes a 3x3 Matrix. | |
| float | DeterminantMatrix3 (const float *src) |
| Computes the determinant of a 3x3 matrix. | |
| float * | Invert3x3Matrix (const float *src, float *dst) |
| Inverts a 3x3 Matrix. | |
| float * | InvertOrthogonalMatrix (float *src, float *dst) |
| Inverts a RT (rotation and translation) 4x4 matrix. | |
| float * | InvertMatrix (float *mat, float *dest) |
| Invert a 4x4 matrix. | |
| float * | Multiply3x3Matrix (float *A, float *B, float *dst) |
| Multiplies only the 3x3 part of two 4x4 matrices. | |
| void | InterpolateMatrices (float *m1, float *m2, float *o, float i) |
| Linearly interpolate two matrices. | |
| float * | Matrix3x3To4x4 (const float *src, float *dst) |
| Copies the 3x3 rotational part of a 4x4 matrix into a new 4x4 matrix. | |
| float * | TranslateMatrixObjectSpace (float *v, float *src, float *dst) |
| Translates a 4x4 matrix using a vector relative from its current position using the object axis. | |
| float * | TranslateMatrixInertialSpace (float *v, float *src, float *dst) |
| Translates a 4x4 matrix using a vector relative from its current position using the inertial axis. | |
| float * | GetMatrixFromSRT (const float *srt, float *M) |
| Constructs a transformation matrix from SRT vector. | |
| float * | GetMatrixFromSRT (float *s, float *r, float *t, float *M) |
| Constructs a transformation matrix from SRT vectors. | |
| float * | GetInvertedMatrixFromSRT (const float *srt, float *M) |
| Constructs an inverted transformation matrix from SRT vector. | |
SRT Functions | |
| float * | MultSRTs (const float *srt1, const float *srt2, float *out) |
| This function should be equivalent to multiplying the two matrices generated from the SRTs. | |
| float * | InvertSRT (const float *srt, float *out) |
| Inverts an SRT (scale-rotation-translation) transform. | |
Distance functions | |
| float | PointDistanceToPlane (float *plane, const float *point, const float *dimensions=nullptr) |
| Computes the signed distance from a point to a plane. | |
| float | SphereDistanceToPlane (float *plane, const float *point, const float *dimensions) |
| Computes the distance from a bounding sphere to a plane. | |
| float | BoxDistanceToPlane (float *plane, const float *point, const float *dimensions) |
| Computes the distance from an axis-aligned bounding box to a plane. | |
| float | CapsuleDistanceToPlane (float *plane, const float *point, const float *dimensions) |
| Computes the distance from a capsule to a plane. | |
| void | PrintMatrix (const float *M) |
| Prints a 4x4 matrix to stdout in row-major visual layout. | |
Inline functions related to 3D Math.
All math functions have been defined as inline and don't belong to any class, matrices and vectors are represented as float arrays, this is done to avoid castings as well as being able to perform operations on contiguous memory.
Matrices are defined in the same way OpenGL defines them, as a float arrays with 16 elements, matrix notation is done in column mayor format to fit OpenGL documentation as well (see OpenGL FAQ).
The matrix m[16] would then be printed as:
\[\left( \begin{array}{cccc} m[0] & m[4] & m[8] & m[12] \\ m[1] & m[5] & m[9] & m[13] \\ m[2] & m[6] & m[10] & m[14] \\ m[3] & m[7] & m[11] & m[15] \end{array} \right) \]
© 2009-2019 Rodrigo Hernandez
Definition in file 3DMath.h.
|
inline |
|
inline |
Convert axis angle rotation notation to a quaternion.
Converts a 4 element float array representing axis angle notation into a 4 element float array representing a quaternion. The first 3 elements of axis represent the axis itself while the last one represents the angle in degres, the quaternion is given as W,X,Y,Z.
| angle | [in] Angle of rotation in degrees. |
| x | [in] x axis. |
| y | [in] y axis. |
| z | [in] z axis. |
| quat | [out] The resulting quaternion. |
|
inline |
|
inline |
Caps quaternion interpolation factor to [0,1] and copies the corresponding endpoint.
| q1 | Origin quaternion. |
| q2 | Destination quaternion. |
| interpolation | Interpolation factor. |
| out | Resulting quaternion if capped. |
|
inline |
|
inline |
|
inline |
Returns the closest to paralell axis to the provided vector.
Takes a vector and calculates the closest to paralell axis, and returns it as an index where:
| v | [in] Vector to evaluate. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Returns the distance between point a and b.
When relative comparations are needed is faster to use the values returned by DistanceSquared than this function.
| a | [in] Vector for point a. |
| b | [in] Vector for point b. |
|
inline |
|
inline |
|
inline |
|
inline |
Convert euler angle rotation notation to a quaternion.
Converts a 3 element float array representing euler angle notation into a 4 element float array representing a quaternion. The 3 elements of the euler parameter represent, roll, pitch and yaw in that order, the quaternion is given as W,X,Y,Z.
| euler | [in] Three element array repserenting euler angles as roll, pitch, yaw. |
| q | [out] The resulting quaternion. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Linearly interpolate two matrices.
It is not safe to pass m1 or m2 as o, unlike some other functions. If the interpolation factor is less than 0.0 or more than 1.0 the resulting matrix is clipped to be the same as either m1 or m2 depending on the case.
| m1 | [in] Origin matrix. |
| m2 | [in] Destination matrix. |
| o | [out] Resulting matrix. |
| i | [in] Interpolation factor. |
|
inline |
Interpolates between two vectors.
Calculates the location between the two vectors where v1 is the origin and v2 is the destination, interpolation represents the fraction between the two that should be advanced, a value of zero returns v1 while a value of one returns v2, anything over one will return a position past v2 in the direction v2-v1. The out parameter may be the same as either v1 or v2 in which case the values get overwritten.
| v1 | [in] Origin vector. |
| v2 | [in] Destination vector. |
| interpolation | [in] Interpolation factor. |
| out | [out] Resulting position vector. |
|
inline |
|
inline |
Invert a 4x4 matrix.
Inverts a 4x4 matrix, it assumes the matrix is represented by a linear 16 element float array, dest must not be NULL and must contain enought space for 16 floats, it may be the same as math, in which case the original matrix is overwritten.
| mat | [in] A pointer or reference to a 16 element float array containing the 4x4 matrix to invert. |
| dest | [out] A pointer or reference to a 16 element float array in which the inverted matrix will be stored, can be the same as mat. |
For matrices containing rotational and translational tranforms only, using InvertOrthogonalMatrix is much faster.
|
inline |
Inverts a RT (rotation and translation) 4x4 matrix.
This function is faster than InvertMatrix but the rotation part of the matrix must be Orthogonal, otherwise the result is not what is expected.
The operation performed to the matrix is to transpose the rotation matrix and recalculate the translation vector so it goes from:
\[\left( \begin{array}{cccc} X.x & Y.x & Z.x & P.x \\ X.y & Y.y & Z.y & P.y \\ X.z & Y.z & Z.z & P.z \\ 0 & 0 & 0 & 1 \end{array} \right) \]
to:
\[\left( \begin{array}{cccc} X.x & X.y & X.z & -dot(P, X) Y.x & Y.y & Y.z & -dot(P, Y) Z.x & Z.y & Z.z & -dot(P, Z) 0 & 0 & 0 & 1 \end{array} \right) \]
| src | [in] A pointer or reference to a 16 element float array containing the 4x4 matrix to invert. |
| dst | [out] A pointer or reference to a 16 element float array in which the inverted matrix will be stored, can be the same as mat. |
|
inline |
|
inline |
|
inline |
|
inline |
Linearly interpolate between two quaternions.
Each element is interpolated as v' = v1+((v2-v1)*interpolation). The out parameter may be the same as either q1 or q2 in which case the values are overwritten.
| q1 | [in] Origin quaternion. |
| q2 | [in] Destination quaternion. |
| interpolation | [in] Interpolation factor. |
| out | [out] Resulting quaternion. |
|
inline |
|
inline |
Extracts rotation matrix and converts it into a quaternion.
Extracts 3x3 rotation matrix and converts it into a 4 element float array representing a quaternion, the quaternion is represented as W,X,Y,Z.
| matrix | [in] The matrix from which to extract the rotation. |
| q | [out] Resulting quaternion. |
|
inline |
Multiplies only the 3x3 part of two 4x4 matrices.
Multiplies two 4x4 matrices, returning a pointer to the resulting matrix, each of which should be an array of 16 float elements, dst may be the same as either of the two matrices in which case the matrix is overwritten, it must not be NULL, and should point to an array of at least 16 float elements. This function only takes into consideration the rotational part of the matrix, hence 3x3, the operation it performs is as follows:
\f[
\left( \begin{array}{ccc}
A[0] & A[4] & A[8] \\
A[1] & A[5] & A[9] \\
A[2] & A[6] & A[10] \end{array} \right)
\times
\left( \begin{array}{ccc}
B[0] & B[4] & B[8] \\
B[1] & B[5] & B[9] \\
B[2] & B[6] & B[10] \end{array} \right)
\f]
| A | [in] Pointer or reference to left side matrix |
| B | [in] Pointer or reference to right side matrix |
| dst | [out] Pointer or reference to space in memory in which to store the result, may be the same as either m1 or m2. |
|
inline |
|
inline |
Multiplies two quaternions.
Multiplies 2 quaternions together effectively combining both rotations. The out parameter may be the same as either q1 or q2 in which case the values get overwritten.
| q1 | [in] Left Quaternion. |
| q2 | [in] Right Quaternion. |
| out | [out] Resulting Quaternion. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Linearly interpolate between two quaternions return the normalized result.
Each element is interpolated as v' = v1+((v2-v1)*interpolation). The out parameter may be the same as either q1 or q2 in which case the values are overwritten.
| [in] | q1 | Origin quaternion. |
| [in] | q2 | Destination quaternion. |
| [in] | interp | Interpolation factor. |
| [out] | out | Result quaternion. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Builds a 3x4 rotation matrix out of a quaternion inside a 4x4 matrix.
Builds a 3x4 rotation matrix inside a 4x4 matrix, leaving the last row (translation) intact. The contents of the 3x4 matrix are overwritten, but the 4th row is untouched, so it must be set to something meaningful before use.
| q | [in] Quaternion. |
| m | [in/out] Matrix. |
|
inline |
Rotates a vector around the origin by a quaternion.
Applies the rotation stored in a quaternion to a vector, using the origin as pivot point. The out parameter may be the same as v or even q in which case the values are overwritten.
| q | [in] 4 element quaternion [w,x,y,z]. |
| v | [in] 3 element vector [x,y,z]. |
| out | [out] Rotated 3 element vector. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Translates a 4x4 matrix using a vector relative from its current position using the inertial axis.
This is a simplified matrix multiplication \(A \times B\):
\[A = \left( \begin{array}{cccc} 1 & 0 & 0 & v[0] \\ 0 & 1 & 0 & v[1] \\ 0 & 0 & 1 & v[2] \\ 0 & 0 & 0 & 1 \end{array} \right) \times B = \left( \begin{array}{cccc} src[0] & src[4] & src[8] & src[12] \\ src[1] & src[5] & src[9] & src[13] \\ src[2] & src[6] & src[10] & src[14] \\ 0 & 0 & 0 & 1 \end{array} \right) \]
| v | [in] Vector for matrix translation. |
| src | [in] Pointer or reference to source matrix. |
| dst | [out] Pointer or reference to destination matrix, may be same as src. |
|
inline |
Translates a 4x4 matrix using a vector relative from its current position using the object axis.
This is a simplified matrix multiplication \(A \times B\):
\[A = \left( \begin{array}{cccc} src[0] & src[4] & src[8] & src[12] \\ src[1] & src[5] & src[9] & src[13] \\ src[2] & src[6] & src[10] & src[14] \\ 0 & 0 & 0 & 1 \end{array} \right) \times B = \left( \begin{array}{cccc} 1 & 0 & 0 & v[0] \\ 0 & 1 & 0 & v[1] \\ 0 & 0 & 1 & v[2] \\ 0 & 0 & 0 & 1 \end{array} \right) \]
| v | [in] Vector for matrix translation. |
| src | [in] Pointer or reference to source matrix. |
| dst | [out] Pointer or reference to destination matrix, may be same as src. |