These macros provide operations on matrices, transformation matrices, as well as vector transformations (rotations and translations).
Definition in file macros.h.
#include <math.h>
Include dependency graph for macros.h:
Go to the source code of this file.
Matrix and Vector Element Extractors | |
These macros extract elements from matrices and vectors. They should be used wherever a macro parameter specifies an "extractor". | |
#define | MATVEC_MAT3X3_AS_MDARRAY(matrix) |
Extract the elements of the upper left 3x3 (sub)matrix from a two-dimensional C array. | |
#define | MATVEC_MAT4X4_AS_MDARRAY(matrix) |
Extract the elements of the upper left 4x4 (sub)matrix from a two-dimensional C array. | |
#define | MATVEC_MAT3X3_AS_ARRAY(matrix, ncols) |
Extract elements for the upper left 3x3 (sub)matrix from a matrix represented as a flat C array. | |
#define | MATVEC_MAT4X4_AS_ARRAY(matrix, ncols) |
Extract elements for the upper left 4x4 matrix from a matrix represented as a flat C array. | |
#define | MATVEC_VEC_AS_ARRAY(vector, stride) |
Extract components for a 3 component vector from an array. | |
#define | MATVEC_VEC_AS_STRUCT(vector, x, y, z) |
Extract components for a 3 component vector from a structure. | |
Basic Matrix Operations | |
#define | MATVEC_MAT3X3_ASSIGN(dst, src) |
#define | MATVEC_MAT4X4_ASSIGN(dst, src) |
#define | MATVEC_MAT3X3_SET(matrix, value) |
#define | MATVEC_MAT4X4_SET(matrix, value) |
#define | MATVEC_MAT3X3_IDENTITY(matrix) |
#define | MATVEC_MAT4X4_IDENTITY(matrix) |
#define | MATVEC_MAT3X3_TRANSPOSE(res, src) |
#define | MATVEC_MAT4X4_TRANSPOSE(res, src) |
#define | MATVEC_MAT3X3_MULT(res, a, b) |
#define | MATVEC_MAT4X4_MULT(res, a, b) |
Transformation Matrix Operations | |
#define | MATVEC_MAT3X3_ROT_X(matrix, theta) |
#define | MATVEC_MAT3X3_ROT_Y(matrix, theta) |
#define | MATVEC_MAT3X3_ROT_Z(matrix, theta) |
#define | MATVEC_MAT3X3_ROT_YMXZ(matrix, azimuth, elevation, clock) |
#define | MATVEC_MAT3X3_ROT_ZXZ(matrix, phi, theta, psi) |
Basic Vector Operations | |
#define | MATVEC_VEC_ASSIGN(dst, src) |
#define | MATVEC_VEC_SCALE(res, vin, fac) |
#define | MATVEC_VEC_ADD(sum, veca, vecb) |
#define | MATVEC_VEC_SUB(res, veca, vecb) |
#define | MATVEC_VEC_PROJECT(res, pos, dir, s) |
#define | MATVEC_VEC_DOTPROD(v, w) |
#define | MATVEC_VEC_CROSSPROD(res, v, w) |
#define | MATVEC_VEC_NORM(vector) |
Vector Transformations | |
These macros operate on vectors. Depending upon the convention used when constructing the rotation matrices and translation vectors, these operations will either transform the coordinate frames of the vectors and specify the vector in the new coordinate frame, or will transform the vector within the existing coordinate frame. Be sure to understand which convention was used, and don't mix 'em up! | |
#define | MATVEC_VEC_ROTATE_TRANSLATE(res, vin, matrix, vtrans) |
Apply a rotation followed by a translation to a vector. | |
#define | MATVEC_VEC_TRANSLATE_ROTATE(type, vout, vin, vtrans, matrix) |
Apply a translation followed by a rotation to a vector. |
#define MATVEC_MAT3X3_AS_ARRAY | ( | matrix, | |||
ncols | ) |
Extract elements for the upper left 3x3 (sub)matrix from a matrix represented as a flat C array.
matrix | the flat (one-dimensional) array containing the matrix | |
ncols | the number of columns in the matrix. this parameter is used multiple times, so should not cause any side effects |
matrix
should resolve into an array specification which can be indexed using the standard C bracket operators. For example:
double matrix[3*3]; MATVEC_MAT3X3_AS_ARRAY(matrix,3) double matrix[4*4]; MATVEC_MAT3X3_AS_ARRAY(matrix,4) void func( double *matrixa, double matrixb[] ) { MATVEC_MAT3X3_AS_ARRAY(matrixa,3) MATVEC_MAT3X3_AS_ARRAY(matrixb,3) }
#define MATVEC_MAT3X3_AS_MDARRAY | ( | matrix | ) |
Extract the elements of the upper left 3x3 (sub)matrix from a two-dimensional C array.
matrix | the variable containing the matrix |
matrix
should resolve into an array specification which can be indexed using the standard C bracket operators. For example: double matrix[3][3]; MATVEC_MAT3X3_AS_MDARRAY(matrix) void func( double matrix[][3] ) { MATVEC_MAT3X3_AS_MDARRAY(matrix) }
#define MATVEC_MAT3X3_ASSIGN | ( | dst, | |||
src | ) |
Assign a 3X3 upper-left (sub)matrix
dst | the destination matrix extractor | |
src | the source matrix extractor |
Example:
double mata[3][3]; double *matb = malloc((3*3) * sizeof(double)); MATVEC_MAT3X3_ASSIGN( MATVEC_MAT3X3_AS_ARRAY(matb), MATVEC_MAT3X3_AS_MDARRAY(mata) );
#define MATVEC_MAT3X3_IDENTITY | ( | matrix | ) |
#define MATVEC_MAT3X3_MULT | ( | res, | |||
a, | |||||
b | ) |
#define MATVEC_MAT3X3_ROT_X | ( | matrix, | |||
theta | ) |
#define MATVEC_MAT3X3_ROT_Y | ( | matrix, | |||
theta | ) |
#define MATVEC_MAT3X3_ROT_YMXZ | ( | matrix, | |||
azimuth, | |||||
elevation, | |||||
clock | ) |
Set a 3x3 (sub)matrix to a rotation matrix representing rotation of the coordinate system through a modified YXZ Euler angle rotation sequence (YMXZ
stands for Y
, minus X, Z
).
This system can be thought of as representing an alt-az system with the horizon in the X-Z plane. The system differs from the standard Euler YXZ system in that the sense of the rotation about the X axis is reversed (Z rotates towarsd Y) to match the common definition of altitude.
matrix | result 3x3 (sub)matrix extractor | |
azimuth | The azimuth angle (in radians). This is the rotation angle about the current Y axis. Positive azimuth rotates Z towards X. | |
elevation | The elevation angle (in radians). This is the rotation angle about the new X axis. Positive elevation rotates Z towards Y, which is opposite from the standard convention. | |
clock | The clock angle (in radians). This is the rotation angle about the new Z axis. Positive clocking rotates X towards Y. |
azimuth
-elevation
clock
so that
In terms of the MATVEC rotation macros, this would be where:
MATVEC_MAT3X3_ROT_Y(D, azimuth); MATVEC_MAT3X3_ROT_X(C, -elevation); MATVEC_MAT3X3_ROT_Z(B, clocking);
#define MATVEC_MAT3X3_ROT_Z | ( | matrix, | |||
theta | ) |
#define MATVEC_MAT3X3_ROT_ZXZ | ( | matrix, | |||
phi, | |||||
theta, | |||||
psi | ) |
Set a 3x3 (sub)matrix to a rotation matrix representing rotation of the coordinate system through a ZXZ Euler angle rotation sequence.
matrix | result 3x3 (sub)matrix extractor | |
phi | The 1st Euler angle (in radians). This is a rotation about the current Z axis. Positive phi takes Y towards X. | |
theta | The 2nd Euler angle (in radians) This is a rotation about the current X axis. Positive theta takes Z towards Y. | |
psi | The 3rd Euler angle (in radians) This is a rotation about the current Z axis(again). Positive psi takes Y towards X. |
#define MATVEC_MAT3X3_SET | ( | matrix, | |||
value | ) |
#define MATVEC_MAT3X3_TRANSPOSE | ( | res, | |||
src | ) |
#define MATVEC_MAT4X4_AS_ARRAY | ( | matrix, | |||
ncols | ) |
Extract elements for the upper left 4x4 matrix from a matrix represented as a flat C array.
matrix | the flat (one-dimensional) array containing the matrix | |
ncols | the number of columns in the matrix. this parameter is used multiple times, so should not cause any side effects |
matrix
should resolve into an array specification which can be indexed using the standard C bracket operators. For example:
double matrixa[4*4]; MATVEC_MAT4X4_AS_ARRAY(matrixa,4) double matrixb[5*5]; MATVEC_MAT4X4_AS_ARRAY(matrixb,5) void func( double *matrixa, double matrixb[] ) { MATVEC_MAT4X4_AS_ARRAY(matrixa,4) MATVEC_MAT4X4_AS_ARRAY(matrixb,5) }
#define MATVEC_MAT4X4_AS_MDARRAY | ( | matrix | ) |
Extract the elements of the upper left 4x4 (sub)matrix from a two-dimensional C array.
matrix | the variable containing the matrix |
matrix
should resolve into an array specification which can be indexed using the standard C bracket operators. For example: double matrix[4][4]; MATVEC_MAT4X4_AS_MDARRAY(matrix) void func( double matrix[][4] ) { MATVEC_MAT4X4_AS_MDARRAY(matrix) }
#define MATVEC_MAT4X4_ASSIGN | ( | dst, | |||
src | ) |
Assign a 4X4 upper-left (sub)matrix
dst | the destination matrix extractor | |
src | the source matrix extractor |
double mata[4][4]; double *matb = malloc((4*4) * sizeof(double)); MATVEC_MAT4X4_COPY( MATVEC_MAT4X4_AS_ARRAY(matb), MATVEC_MAT4X4_AS_MDARRAY(mata) );
#define MATVEC_MAT4X4_IDENTITY | ( | matrix | ) |
#define MATVEC_MAT4X4_MULT | ( | res, | |||
a, | |||||
b | ) |
#define MATVEC_MAT4X4_SET | ( | matrix, | |||
value | ) |
#define MATVEC_MAT4X4_TRANSPOSE | ( | res, | |||
src | ) |
#define MATVEC_VEC_ADD | ( | sum, | |||
veca, | |||||
vecb | ) |
#define MATVEC_VEC_AS_ARRAY | ( | vector, | |||
stride | ) |
#define MATVEC_VEC_AS_STRUCT | ( | vector, | |||
x, | |||||
y, | |||||
z | ) |
Extract components for a 3 component vector from a structure.
vector | the structure containing the vector. this is not a pointer to the structure. for pointers, pass *ptr | |
x | the name of the x component structure element | |
y | the name of the y component structure element | |
z | the name of the z component structure element |
struct { double x, y, z } vector, *vecptr; MATVEC_VEC_AS_STRUCT(vector,x,y,z) MATVEC_VEC_AS_STRUCT(*vecptr,x,y,z)
#define MATVEC_VEC_ASSIGN | ( | dst, | |||
src | ) |
#define MATVEC_VEC_CROSSPROD | ( | res, | |||
v, | |||||
w | ) |
#define MATVEC_VEC_DOTPROD | ( | v, | |||
w | ) |
#define MATVEC_VEC_NORM | ( | vector | ) |
#define MATVEC_VEC_PROJECT | ( | res, | |||
pos, | |||||
dir, | |||||
s | ) |
Project a point along a vector.
res | the result vector extractor | |
pos | the point position vector extractor | |
dir | the direction vector extractor | |
s | the fractional distance along the direction vector to project. |
#define MATVEC_VEC_ROTATE_TRANSLATE | ( | res, | |||
vin, | |||||
matrix, | |||||
vtrans | ) |
Apply a rotation followed by a translation to a vector.
res | the result vector extractor | |
vin | the initial vector extractor | |
matrix | the rotation matrix extractor | |
vtrans | the translation vector extractor |
Example:
The convention is to use homogenous coordinates and the transformation is stored as a 4x4 C multidimensional array. In this case, the rotation matrix is stored in the upper 3x3 submatrix and the translation vector is stored in the right-most column. The input and output vectors are stored in 3 element C arrays.
double transform[4][4]; double vin[3], res[3]; MATVEC_VEC_ROTATE_TRANSLATE( MATVEC_VEC_AS_ARRAY(res,1), MATVEC_VEC_AS_ARRAY(vin,1), MATVEC_MAT3X3_AS_MDARRAY(transform), MATVEC_VEC_AS_ARRAY(&transform[0][3], 4) );
Example:
The rotation matrix is stored in a 3x3 C multidimensional array and the translation vector is stored in a 3 element C array. The input and output vectors are stored in 3 element C arrays.
double transform[3][3]; double vtrans[3], vin[3], vout[3]; MATVEC_VEC_ROTATE_TRANSLATE( MATVEC_VEC_AS_ARRAY(res,1), MATVEC_VEC_AS_ARRAY(vin,1), MATVEC_MAT3X3_AS_MDARRAY(transform), MATVEC_VEC_AS_ARRAY(vtrans,1) );
#define MATVEC_VEC_SCALE | ( | res, | |||
vin, | |||||
fac | ) |
#define MATVEC_VEC_SUB | ( | res, | |||
veca, | |||||
vecb | ) |
#define MATVEC_VEC_TRANSLATE_ROTATE | ( | type, | |||
vout, | |||||
vin, | |||||
vtrans, | |||||
matrix | ) |
Apply a translation followed by a rotation to a vector.
type | the C type of a vector component (e.g. double) | |
vout | the output vector extractor | |
vin | the input vector extractor | |
vtrans | the translation vector extractor | |
matrix | the rotation matrix extractor |
Example:
The convention is to use homogenous coordinates and the transformation is stored as a 4x4 C multidimensional array. In this case, the rotation matrix is stored in the upper 3x3 submatrix and the translation vector is stored in the right-most column. The input and output vectors are stored in 3 element C arrays.
double transform[4][4]; double vin[3], vout[3]; MATVEC_VEC_TRANSLATE_ROTATE( double, MATVEC_VEC_AS_ARRAY(vout,1), MATVEC_VEC_AS_ARRAY(vin,1), MATVEC_VEC_AS_ARRAY(&transform[0][3], 4), MATVEC_MAT3X3_AS_MDARRAY(transform) );
Example:
The rotation matrix is stored in a 3x3 C multidimensional array and the translation vector is stored in a 3 element C array. The input and output vectors are stored in 3 element C arrays.
double transform[3][3]; double vtrans[3], vin[3], vout[3]; MATVEC_VEC_TRANSLATE_ROTATE( double, MATVEC_VEC_AS_ARRAY(vout,1), MATVEC_VEC_AS_ARRAY(vin,1), MATVEC_VEC_AS_ARRAY(vtrans,1), MATVEC_MAT3X3_AS_MDARRAY(transform) );