Matrix22

#include <Imath/ImathMatrix.h>

The Matrix22 class template represents a 2x2 matrix, with predefined typedefs for float and double.

There are also various utility functions that operate on matrices defined in ImathMatrixAlgo.h and described in Matrix Functions.

Individual components of a matrix M may be referenced as either M[j][i] or M.x[j][i]. While the latter is a little awkward, it has an advantage when used in loops that may be auto-vectorized or explicitly vectorized by #pragma omp simd or other such hints, because the function call and pointer casting of operator[] can confuse the compiler just enough to prevent vectorization of the loop, whereas directly addressing the real underlying array (M.x[j][i]) does not.

Example:

#include <Imath/ImathMatrix.h>
#include <Imath/ImathMatrixAlgo.h>
#include <cassert>

void
matrix22_example ()
{
    Imath::M22f M (Imath::UNINITIALIZED); // uninitialized

    M.makeIdentity ();
    assert (M[0][0] == 1.0f);
    assert (M[0][1] == 0.0f);

    Imath::M22f Minv = M.inverse ();

    Imath::M22f R;
    assert (R == Imath::identity22f);

    R.rotate (M_PI / 4);

    M = R * M;

    Imath::V2f v2 (1.0f, 0.0f);
    Imath::V2f r2 = v2 * M;

    assert (r2.equalWithAbsError (Imath::V2f (0.707107f, 0.707107f), 1e-6f));
}
typedef Matrix22<float> Imath::M22f

2x2 matrix of float

typedef Matrix22<double> Imath::M22d

2x2 matrix of double

template<class T>
class Matrix22

2x2 transformation matrix

Direct access to elements

T x[2][2]

Matrix elements.

Constructors and Assignment

inline Matrix22(Uninitialized) noexcept

Uninitialized.

inline constexpr Matrix22() noexcept

Default constructor: initialize to identity.

1 0 0 1

inline constexpr Matrix22(T a) noexcept

Initialize to scalar constant:

a a a a

inline constexpr Matrix22(const T a[2][2]) noexcept

Construct from 2x2 array:

a[0][0] a[0][1] a[1][0] a[1][1]

inline constexpr Matrix22(T a, T b, T c, T d) noexcept

Construct from given scalar values:

a b c d

inline constexpr Matrix22(const Matrix22 &v) noexcept

Copy constructor.

template<class S>
inline explicit constexpr Matrix22(const Matrix22<S> &v) noexcept

Construct from Matrix22 of another base type.

inline constexpr const Matrix22 &operator=(const Matrix22 &v) noexcept

Assignment.

inline constexpr const Matrix22 &operator=(T a) noexcept

Assignment from scalar.

~Matrix22() noexcept = default

Destructor.

Compatibility with Sb

inline T *getValue() noexcept

Return a raw pointer to the array of values.

inline const T *getValue() const noexcept

Return a raw pointer to the array of values.

template<class S>
inline void getValue(Matrix22<S> &v) const noexcept

Return the value in v

template<class S>
constexpr Matrix22 &setValue(const Matrix22<S> &v) noexcept

Set the value.

template<class S>
constexpr Matrix22 &setTheMatrix(const Matrix22<S> &v) noexcept

Set the value.

Arithmetic and Comparison

inline constexpr bool operator==(const Matrix22 &v) const noexcept

Equality.

inline constexpr bool operator!=(const Matrix22 &v) const noexcept

Inequality.

inline constexpr bool equalWithAbsError(const Matrix22<T> &v, T e) const noexcept

Compare two matrices and test if they are “approximately equal”:

Returns

True if the coefficients of this and m are the same with an absolute error of no more than e, i.e., for all i, j:

abs (this[i][j] - m[i][j]) <= e

inline constexpr bool equalWithRelError(const Matrix22<T> &v, T e) const noexcept

Compare two matrices and test if they are “approximately equal”:

Returns

True if the coefficients of this and m are the same with a relative error of no more than e, i.e., for all i, j:

abs (this[i] - v[i][j]) <= e * abs (this[i][j])

inline constexpr const Matrix22 &operator+=(const Matrix22 &v) noexcept

Component-wise addition.

inline constexpr const Matrix22 &operator+=(T a) noexcept

Component-wise addition.

inline constexpr Matrix22 operator+(const Matrix22 &v) const noexcept

Component-wise addition.

inline constexpr const Matrix22 &operator-=(const Matrix22 &v) noexcept

Component-wise subtraction.

inline constexpr const Matrix22 &operator-=(T a) noexcept

Component-wise subtraction.

inline constexpr Matrix22 operator-(const Matrix22 &v) const noexcept

Component-wise subtraction.

inline constexpr Matrix22 operator-() const noexcept

Component-wise multiplication by -1.

inline constexpr const Matrix22 &negate() noexcept

Component-wise multiplication by -1.

inline constexpr const Matrix22 &operator*=(T a) noexcept

Component-wise multiplication.

inline constexpr Matrix22 operator*(T a) const noexcept

Component-wise multiplication.

inline constexpr const Matrix22 &operator/=(T a) noexcept

Component-wise division.

inline constexpr Matrix22 operator/(T a) const noexcept

Component-wise division.

inline constexpr const Matrix22 &operator*=(const Matrix22 &v) noexcept

Matrix-matrix multiplication.

inline constexpr Matrix22 operator*(const Matrix22 &v) const noexcept

Matrix-matrix multiplication.

template<class S>
inline void multDirMatrix(const Vec2<S> &src, Vec2<S> &dst) const noexcept

Vector * matrix multiplication.

Parameters
  • src[in] Input vector

  • dst[out] transformed vector

Maniplation

inline void makeIdentity() noexcept

Set to the identity.

inline constexpr const Matrix22 &transpose() noexcept

Transpose.

inline constexpr Matrix22 transposed() const noexcept

Return the transpose.

inline constexpr const Matrix22 &invert(bool singExc)

Invert in place.

Parameters

singExc – If true, throw an exception if the matrix cannot be inverted.

Returns

const reference to this

inline constexpr const Matrix22 &invert() noexcept

Invert in place.

Returns

const reference to this

inline constexpr Matrix22<T> inverse(bool singExc) const

Return the inverse, leaving this unmodified.

Parameters

singExc – If true, throw an exception if the matrix cannot be inverted.

inline constexpr Matrix22<T> inverse() const noexcept

Return the inverse, leaving this unmodified.

inline constexpr T determinant() const noexcept

Determinant.

inline constexpr T trace() const noexcept

Trace.

template<class S>
const Matrix22 &setRotation(S r) noexcept

Set matrix to rotation by r (in radians)

Returns

const referenced to this

template<class S>
constexpr const Matrix22 &rotate(S r) noexcept

Rotate the given matrix by r (in radians)

Returns

const referenced to this

inline constexpr const Matrix22 &setScale(T s) noexcept

Set matrix to scale by given uniform factor.

Returns

const referenced to this

template<class S>
constexpr const Matrix22 &setScale(const Vec2<S> &s) noexcept

Set matrix to scale by given vector.

Returns

const referenced to this

template<class S>
constexpr const Matrix22 &scale(const Vec2<S> &s) noexcept
Returns

const referenced to this

Numeric Limits

static inline constexpr T baseTypeLowest() noexcept

Largest possible negative value.

static inline constexpr T baseTypeMax() noexcept

Largest possible positive value.

static inline constexpr T baseTypeSmallest() noexcept

Smallest possible positive value.

static inline constexpr T baseTypeEpsilon() noexcept

Smallest possible e for which 1+e != 1.

Public Types

typedef T BaseType

The base type: In templates that accept a parameter V, you can refer to T as V::BaseType

typedef Vec2<T> BaseVecType

The base vector type.

Public Functions

inline T *operator[](int i) noexcept

Row access.

inline const T *operator[](int i) const noexcept

Row access.

template<class S>
inline constexpr Matrix22<T> &setValue(const Matrix22<S> &v) noexcept
template<class S>
inline constexpr Matrix22<T> &setTheMatrix(const Matrix22<S> &v) noexcept
template<class S>
inline const Matrix22<T> &setRotation(S r) noexcept
template<class S>
inline constexpr const Matrix22<T> &rotate(S r) noexcept
template<class S>
inline constexpr const Matrix22<T> &setScale(const Vec2<S> &s) noexcept
template<class S>
inline constexpr const Matrix22<T> &scale(const Vec2<S> &s) noexcept

Public Static Functions

static inline constexpr unsigned int dimensions() noexcept

Return the number of the row and column dimensions, i.e. 2.

template<class T>
std::ostream &Imath::operator<<(std::ostream &s, const Matrix22<T> &m)

Stream output, as: (m00 m01 m10 m11)