# Float4x4

A 4x4 matrix of floating-point values.

A `Float4x4` can represent any affine transformation in 3D space including rotations, scales, shears and translations. Unlike `Float3x4`, it can also represent perspective projections.

The matrix is stored in column-major order. In other words, it's an array of four `Float4` column vectors.

`#include <ply-math/Matrix.h>`

Also included from `<ply-math/Base.h>`.

## Constructors

` Float4x4::Float4x4()` [code]

Constructs an uninitialized `Float4x4`.

` Float4x4::Float4x4(const Float4& col0, const Float4& col1, const Float4& col2, const Float4& col3)` [code]

Constructs a 4x4 matrix from the given column vectors.

``````Float4x4 m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
``````
`explicit Float4x4::Float4x4(const Float3x3& m3x3, const Float3& pos = {0, 0, 0})` [code]

Constructs a 4x4 matrix by concatenating a 3x3 matrix with an optional fourth column vector and adding [0 0 0 1] as the fourth row. When the resulting 4x4 matrix transforms a `Float3`, it's equivalent to a transformation by `m3x3` followed by a translation by `pos`.

``````Float4x4 m = {Float3x3::identity(), {5, 0, 0}};
``````

## Conversion Functions

`Float3x3 Float4x4::toFloat3x3() const` [code]

Returns a 3x3 matrix by truncating the fourth column and fourth row of the 4x4 matrix.

`Float3x4 Float4x4::toFloat3x4() const` [code]

Returns a 3x4 matrix by truncating the fourth row of the 4x4 matrix.

## Element Access

`Float4& Float4x4::operator[](ureg i)` [code]
`const Float4& Float4x4::operator[](ureg i) const` [code]

Accesses the column vector at the specified index.

``````Float4x4 m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
m.x = -1;
StdOut::text() << m;  // "{0, 0, 0, 1}"
``````

## Comparison Functions

`bool operator==(const Float4x4& a, const Float4x4& b)` [code]
`bool operator!=(const Float4x4& a, const Float4x4& b)` [code]

Returns `true` if the matrices are equal (or not equal) using floating-point comparison. In particular, a component with a value of `0.f` is equal to a component with a value of `-0.f`.

## Creation Functions

`static Float4x4 Float4x4::identity()` [code]

Returns the identity matrix `{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}`.

`static Float4x4 Float4x4::makeScale(const Float3& arg)` [code]

Returns a scale matrix.

`static Float4x4 Float4x4::makeRotation(const Float3& unitAxis, float radians)` [code]

Returns a matrix that performs a counter-clockwise rotation around the specified axis following the right-hand rule. `unitAxis` must have have unit length and the angle is specified in radians.

`static Float4x4 Float4x4::makeTranslation(const Float3& pos)` [code]

Returns a translation matrix.

`static Float4x4 Float4x4::fromQuaternion(const Quaternion& q, const Float3& pos = {0, 0, 0})` [code]

Returns a matrix that performs the same rotation as `q`. If `pos` is specified, it's used as the fourth column, so that the resulting matrix performs the same rotation as `q` followed by a translation by `pos`.

`static Float4x4 Float4x4::fromQuatPos(const QuatPos& qp)` [code]

Returns a matrix that performs the same transformation as `qp`.

`static Float4x4 Float4x4::makeProjection(const Rect& frustum, float zNear, float zFar)` [code]

Returns a perspective projection matrix that maps a 3D frustum to OpenGL clip space in homogeneous coordinates. OpenGL clip space goes from -1 at the near plane to +1 at the far plane, with x and y coordinates in the range [-1, 1].

Assuming a right-handed coordinate system, the 3D frustum starts at the origin, faces the -z direction, and intersects the z = -1 plane at the rectangle given by `frustum`. Use the `rectFromFov()` helper function to convert a field-of-view angle and aspect ratio to a `frustum` rectangle.

``````Float4x4 p = Float4x4::makeProjection(rectFromFov(Pi / 2, 16.f / 9), 1, 100);
``````

`zNear` and `zFar` are interpreted as distances from the origin and must be positive values. Since the 3D frustum faces the -z direction, the actual near and far clip planes are given by z = `-zNear` and z = `-zFar`.

Note: This type of projection matrix works, but is considered outdated. More modern projection matrices, such as those using reversed z or having an infinite far plane, typically offer better precision, but those aren't implemented in Plywood yet.

`static Float4x4 Float4x4::makeOrtho(const Rect& rect, float zNear, float zFar)` [code]

Returns an orthographic projection matrix that maps a 3D box to OpenGL clip space. OpenGL clip space goes from -1 at the near plane to +1 at the far plane, with x and y coordinates in the range [-1, 1].

Assuming a right-handed coordinate system, the 3D box being transformed faces the -z direction. `zNear` and `zFar` are interpreted as distances from the origin, so the actual near and far clip planes are given by z = `-zNear` and z = `-zFar`.

## Matrix Operations

`Float4x4 Float4x4::transposed() const` [code]

Returns the transpose of the 4x4 matrix.

`Float4x4 Float4x4::invertedOrtho() const` [code]

A fast method to compute the inverse of a 4x4 matrix whose fourth row is [0 0 0 1] and whose first three columns are orthogonal. In other words, the input matrix must only consist of a rotation, translation and/or reflection; no scale or shear is allowed.

`Float4 operator*(const Float4x4& m, const Float4& v)` [code]

Transform a vector using a matrix. `v` is treated as a column vector and premultiplied by the matrix `m`.

`Float4x4 operator*(const Float4x4& a, const Float4x4& b)` [code]
`Float4x4 operator*(const Float3x4& a, const Float4x4& b)` [code]
`Float4x4 operator*(const Float4x4& a, const Float3x4& b)` [code]

Matrix multiplication. `Float3x4` arguments are interpreted as 4x4 matrices with [0 0 0 1] as the implicit fourth row.