4x4 Matrices

Overview

A 4x4 matrix acting on homogeneous coordinates (see 4D Vectors) can represent a rigid body transformation,

\[\begin{split}\begin{bmatrix} R & \underline{t} \\ 0 & 1 \end{bmatrix}\end{split}\]

as described in SE(3), and also transformations that are not rigid, such as the perspective projection of a camera or the scaling used in computer graphics.

KSL does its rigid body arithmetic with ksl_SE3_t, which stores only \(R\) and \(\underline{t}\) (12 numbers instead of 16) and never multiplies the constant bottom row. ksl_mat4x4_t is provided for exchanging data with software that expects a full 4x4 matrix, such as graphics interfaces and model file formats. ksl_SE3_toMat4x4() and ksl_mat4x4_toSE3() convert between the two. ksl_mat4x4_toSE3() copies the rotation and translation and assumes the bottom row is \([0\;0\;0\;1]\).

Storage. ksl_mat4x4_t holds sixteen double values, 128 bytes (ksl_mat4x4f_t: 64 bytes), in column major order, the same convention as 3x3 Matrices: at[column][row], m<row><column>, columns v[0] to v[3] as ksl_vec4_t, and as_array for the sixteen values in order. Column major order is also what OpenGL expects, so the as_array member of a ksl_mat4x4f_t can be passed directly to OpenGL functions such as glUniformMatrix4fv without transposing.

Double precision

ksl_mat4x4_t

A general double precision 4x4 matrix.

ksl_mat4x4()

ksl_mat4x4_t constructor.

ksl_mat4x4_cmo()

alternative ksl_mat4x4_t constructor.

ksl_mat4x4_fromSE3()

alternative ksl_mat4x4_t constructor that allows creating a ksl_mat4x4_t datastructure from a ksl_SE3_t datastructure.

ksl_mat4x4_fromRt()

alternative ksl_mat4x4_t constructor that allows creating a ksl_mat4x4_t datastructure from ksl_mat3x3_t and ksl_vec3_t datastructures.

ksl_mat4x4_alloc()

allocates n ksl_mat4x4_t datastructures on the heap. Must be freed by the user.

ksl_mat4x4_set()

Set a value in a double precision 4x4 matrix at specified row and column index.

ksl_mat4x4_setIdentity()

Set a double precision 4x4 matrix to identity.

ksl_mat4x4_getTranslation()

Obtain the 3D translation component of a double precision 4x4 matrix.

ksl_mat4x4_get()

Basic get function for a double precision 4x4 matrix.

ksl_mat4x4_toSE3()

Convert a 4x4 homogeneous transformation matrix to an SE3 transformation.

union ksl_mat4x4_t
#include <types.h>

A general double precision 4x4 matrix.

This union allows accessing double precision mat4x4 quantities by field name, as a singly dimensioned array, as a doubly dimensioned array, or by accessing a specific column. If using the at or as_array operators, quantities are accessed in column major order.

The following examples illustrate how to access row 3, column 2 (using 0 based indexing) of matrix M. The following access methods are equivalent.

M.as_array[3 + 2 * 4];
M.at[2][3];
M.m32;
M.v2.w;
M.v2.at[3];
M.v[2].w;
M.v[2].at[3];

See also the ksl_mat4x4_get and ksl_mat4x4_set functions.

Public Members

double as_array[16]
struct ksl_mat4x4_t

anonymous union allows accessing quantities at specified index in linear array, where entities are specified in column major order

double at[4][4]
struct ksl_mat4x4_t

anonymous union allows accessing mat3x3 quantities at specified index in a doubly dimensioned array, where entities are specified in column major order

double m00
double m10
double m20
double m30
double m01
double m11
double m21
double m31
double m02
double m12
double m22
double m32
double m03
double m13
double m23
double m33
struct ksl_mat4x4_t

anonymous union allows accessing rotation matrix quantities by field name

ksl_vec4_t v0
ksl_vec4_t v1
ksl_vec4_t v2
ksl_vec4_t v3
struct ksl_mat4x4_t

anonymous union allows accessing columns of rotation matrix quantities by field name

ksl_vec4_t v[4]
struct ksl_mat4x4_t

anonymous union allows accessing columns of rotation matrix quantities at specified index

static inline ksl_mat4x4_t ksl_mat4x4(const double m00, const double m01, const double m02, const double m03, const double m10, const double m11, const double m12, const double m13, const double m20, const double m21, const double m22, const double m23, const double m30, const double m31, const double m32, const double m33)

ksl_mat4x4_t constructor.

Note that in the constructor, fields are specified in row major order. Internally, ksl_mat4x4_t datastructures are stored in column major order.

static inline ksl_mat4x4_t ksl_mat4x4_cmo(const double m00, const double m10, const double m20, const double m30, const double m01, const double m11, const double m21, const double m31, const double m02, const double m12, const double m22, const double m32, const double m03, const double m13, const double m23, const double m33)

alternative ksl_mat4x4_t constructor.

In this alternate constructor, fields are specified in column major order to match the internal layout of ksl_mat4x4_t datastructures.

static inline ksl_mat4x4_t ksl_mat4x4_fromSE3(const ksl_SE3_t D)

alternative ksl_mat4x4_t constructor that allows creating a ksl_mat4x4_t datastructure from a ksl_SE3_t datastructure.

static inline ksl_mat4x4_t ksl_mat4x4_fromRt(const ksl_mat3x3_t R, const ksl_vec3_t t)

alternative ksl_mat4x4_t constructor that allows creating a ksl_mat4x4_t datastructure from ksl_mat3x3_t and ksl_vec3_t datastructures.

ksl_mat4x4_t *ksl_mat4x4_alloc(int n)

allocates n ksl_mat4x4_t datastructures on the heap. Must be freed by the user.

static inline void ksl_mat4x4_set(ksl_mat4x4_t *M, const int row, const int column, const double value)

Set a value in a double precision 4x4 matrix at specified row and column index.

Bounds checking are performed in debug mode. Bounds checking can be disabled by compiling in release mode using the compiler flag -DNDEBUG.

Parameters:
  • M – [in/out]

  • row – [in] row index, \(0 \le row \lt 4\)

  • column – [in] column index, \(0 \le column \lt 4\)

  • value – [in] value to set

static inline void ksl_mat4x4_setIdentity(ksl_mat4x4_t *M)

Set a double precision 4x4 matrix to identity.

\( I \rightarrow M \)

Parameters:
  • M – [in/out] input 4x4 matrix

static inline void ksl_mat4x4_getTranslation(const ksl_mat4x4_t *Mi, ksl_vec3_t *to)

Obtain the 3D translation component of a double precision 4x4 matrix.

Parameters:
  • Mi – [in] input 4x4 matrix

  • to – [out] translation vector returned in to

static inline double ksl_mat4x4_get(const ksl_mat4x4_t *Mi, const int row, const int column)

Basic get function for a double precision 4x4 matrix.

Bounds checking are performed in debug mode. Bounds checking can be disabled by compiling in release mode using the compiler flag -DNDEBUG.

Parameters:
  • Mi – [in] input 4x4 matrix

  • row – [in] row index

  • column – [in] column index

Returns:

value at specified row and column index

static inline void ksl_mat4x4_toSE3(const ksl_mat4x4_t *M, ksl_SE3_t *D)

Convert a 4x4 homogeneous transformation matrix to an SE3 transformation.

Copies the rotation and translation (the top three rows); the bottom row of M is ignored.

Parameters:
  • M – [in] 4x4 homogeneous transformation matrix

  • D – [out] the same transformation as an SE3

Single precision

ksl_mat4x4f_t

A general single precision 4x4 matrix.

ksl_mat4x4f()

ksl_mat4x4f_t constructor.

ksl_mat4x4f_cmo()

alternative ksl_mat4x4f_t constructor.

ksl_mat4x4f_fromSE3f()

alternative ksl_mat4x4f_t constructor that allows creating a ksl_mat4x4f_t datastructure from a ksl_SE3f_t datastructure.

ksl_mat4x4f_fromRt()

alternative ksl_mat4x4f_t constructor that allows creating a ksl_mat4x4f_t datastructure from ksl_mat3x3f_t and ksl_vec3f_t datastructures.

ksl_mat4x4f_alloc()

allocates n ksl_mat4x4f_t datastructures on the heap. Must be freed by the user.

ksl_mat4x4f_set()

Set a value in a single precision 4x4 matrix at specified row and column index.

ksl_mat4x4f_setIdentity()

Set a single precision 4x4 matrix to identity.

ksl_mat4x4f_getTranslation()

Obtain the 3D translation component of a single precision 4x4 matrix.

ksl_mat4x4f_get()

Basic get function for a single precision 4x4 matrix.

ksl_mat4x4f_toSE3f()

Single precision version of ksl_mat4x4_toSE3().

union ksl_mat4x4f_t
#include <types.h>

A general single precision 4x4 matrix.

This union allows accessing single precision mat4x4 quantities by field name, as a singly dimensioned array, as a doubly dimensioned array, or by accessing a specific column. If using the at or as_array operators, quantities are accessed in column major order.

The following examples illustrate how to access row 3, column 2 (using 0 based indexing) of matrix M. The following access methods are equivalent.

M.as_array[3 + 2 * 4];
M.at[2][3];
M.m32;
M.v2.w;
M.v2.at[3];
M.v[2].w;
M.v[2].at[3];

See also the ksl_mat4x4f_get and ksl_mat4x4f_set functions.

Public Members

float as_array[16]
struct ksl_mat4x4f_t

allows accessing quantities at specified index in linear array, where entities are specified in column major order

float at[4][4]
struct ksl_mat4x4f_t

allows accessing mat3x3 quantities at specified index in a doubly dimensioned array, where entities are specified in column major order

float m00
float m10
float m20
float m30
float m01
float m11
float m21
float m31
float m02
float m12
float m22
float m32
float m03
float m13
float m23
float m33
struct ksl_mat4x4f_t

anonymous union allows accessing rotation matrix quantities by field name

ksl_vec4f_t v0
ksl_vec4f_t v1
ksl_vec4f_t v2
ksl_vec4f_t v3
struct ksl_mat4x4f_t

anonymous union allows accessing columns of rotation matrix quantities by field name

ksl_vec4f_t v[4]
struct ksl_mat4x4f_t

anonymous union allows accessing columns of rotation matrix quantities at specified index

static inline ksl_mat4x4f_t ksl_mat4x4f(const float m00, const float m01, const float m02, const float m03, const float m10, const float m11, const float m12, const float m13, const float m20, const float m21, const float m22, const float m23, const float m30, const float m31, const float m32, const float m33)

ksl_mat4x4f_t constructor.

Note that in the constructor, fields are specified in row major order. Internally, ksl_mat4x4f_t datastructures are stored in column major order.

static inline ksl_mat4x4f_t ksl_mat4x4f_cmo(const float m00, const float m10, const float m20, const float m30, const float m01, const float m11, const float m21, const float m31, const float m02, const float m12, const float m22, const float m32, const float m03, const float m13, const float m23, const float m33)

alternative ksl_mat4x4f_t constructor.

In this alternate constructor, fields are specified in column major order to match the internal layout of ksl_mat4x4f_t datastructures.

static inline ksl_mat4x4f_t ksl_mat4x4f_fromSE3f(const ksl_SE3f_t D)

alternative ksl_mat4x4f_t constructor that allows creating a ksl_mat4x4f_t datastructure from a ksl_SE3f_t datastructure.

static inline ksl_mat4x4f_t ksl_mat4x4f_fromRt(const ksl_mat3x3f_t R, const ksl_vec3f_t t)

alternative ksl_mat4x4f_t constructor that allows creating a ksl_mat4x4f_t datastructure from ksl_mat3x3f_t and ksl_vec3f_t datastructures.

ksl_mat4x4f_t *ksl_mat4x4f_alloc(int n)

allocates n ksl_mat4x4f_t datastructures on the heap. Must be freed by the user.

static inline void ksl_mat4x4f_set(ksl_mat4x4f_t *D, int row, int column, float value)

Set a value in a single precision 4x4 matrix at specified row and column index.

Bounds checking are performed in debug mode. Bounds checking can be disabled by compiling in release mode using the compiler flag -DNDEBUG.

Parameters:
  • D – [in,out] matrix to modify

  • row – [in] row index, \(0 \le row \lt 4\)

  • column – [in] column index, \(0 \le column \lt 4\)

  • value – [in] value to set

static inline void ksl_mat4x4f_setIdentity(ksl_mat4x4f_t *M)

Set a single precision 4x4 matrix to identity.

\( I \rightarrow M \)

Parameters:
  • M – [in/out] input 4x4 matrix

static inline void ksl_mat4x4f_getTranslation(const ksl_mat4x4f_t *Mi, ksl_vec3f_t *to)

Obtain the 3D translation component of a single precision 4x4 matrix.

Parameters:
  • Mi – [in] input 4x4 matrix

  • to – [out] translation vector returned in to

static inline float ksl_mat4x4f_get(const ksl_mat4x4f_t *Mi, const int row, const int column)

Basic get function for a single precision 4x4 matrix.

Bounds checking are performed in debug mode. Bounds checking can be disabled by compiling in release mode using the compiler flag -DNDEBUG.

Parameters:
  • Mi – [in] input 4x4 matrix

  • row – [in] row index

  • column – [in] column index

Returns:

value at specified row and column index

static inline void ksl_mat4x4f_toSE3f(const ksl_mat4x4f_t *M, ksl_SE3f_t *D)

Single precision version of ksl_mat4x4_toSE3().