2011-10-10 20:24:12 +00:00
|
|
|
// This code is in the public domain -- castanyo@yahoo.es
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2011-10-11 18:52:24 +00:00
|
|
|
#include "Vector.h"
|
2011-10-10 20:24:12 +00:00
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
// - Matrices are stored in memory in *column major* order.
|
|
|
|
// - Points are to be though of as column vectors.
|
|
|
|
// - Transformation of a point p by a matrix M is: p' = M * p
|
|
|
|
|
2011-10-10 20:24:12 +00:00
|
|
|
namespace nv
|
|
|
|
{
|
|
|
|
enum identity_t { identity };
|
|
|
|
|
2018-02-06 02:55:07 +00:00
|
|
|
// 2x2 matrix.
|
2020-04-05 19:20:35 +00:00
|
|
|
class Matrix2
|
2018-02-06 02:55:07 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Matrix2();
|
|
|
|
explicit Matrix2(float f);
|
|
|
|
explicit Matrix2(identity_t);
|
|
|
|
Matrix2(const Matrix2 & m);
|
|
|
|
Matrix2(Vector2::Arg v0, Vector2::Arg v1);
|
|
|
|
Matrix2(float a, float b, float c, float d);
|
|
|
|
|
|
|
|
float data(uint idx) const;
|
|
|
|
float & data(uint idx);
|
|
|
|
float get(uint row, uint col) const;
|
|
|
|
float operator()(uint row, uint col) const;
|
|
|
|
float & operator()(uint row, uint col);
|
|
|
|
|
|
|
|
Vector2 row(uint i) const;
|
|
|
|
Vector2 column(uint i) const;
|
|
|
|
|
|
|
|
void operator*=(float s);
|
|
|
|
void operator/=(float s);
|
|
|
|
void operator+=(const Matrix2 & m);
|
|
|
|
void operator-=(const Matrix2 & m);
|
|
|
|
|
|
|
|
void scale(float s);
|
|
|
|
void scale(Vector2::Arg s);
|
|
|
|
float determinant() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
float m_data[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Solve equation system using LU decomposition and back-substitution.
|
|
|
|
extern bool solveLU(const Matrix2 & m, const Vector2 & b, Vector2 * x);
|
|
|
|
|
|
|
|
// Solve equation system using Cramer's inverse.
|
|
|
|
extern bool solveCramer(const Matrix2 & A, const Vector2 & b, Vector2 * x);
|
|
|
|
|
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
// 3x3 matrix.
|
2020-04-05 19:20:35 +00:00
|
|
|
class Matrix3
|
2011-10-10 20:24:12 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Matrix3();
|
|
|
|
explicit Matrix3(float f);
|
|
|
|
explicit Matrix3(identity_t);
|
|
|
|
Matrix3(const Matrix3 & m);
|
|
|
|
Matrix3(Vector3::Arg v0, Vector3::Arg v1, Vector3::Arg v2);
|
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
float data(uint idx) const;
|
|
|
|
float & data(uint idx);
|
2012-01-02 08:49:13 +00:00
|
|
|
float get(uint row, uint col) const;
|
|
|
|
float operator()(uint row, uint col) const;
|
|
|
|
float & operator()(uint row, uint col);
|
2011-10-10 20:24:12 +00:00
|
|
|
|
|
|
|
Vector3 row(uint i) const;
|
|
|
|
Vector3 column(uint i) const;
|
|
|
|
|
|
|
|
void operator*=(float s);
|
|
|
|
void operator/=(float s);
|
|
|
|
void operator+=(const Matrix3 & m);
|
|
|
|
void operator-=(const Matrix3 & m);
|
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
void scale(float s);
|
|
|
|
void scale(Vector3::Arg s);
|
2011-10-10 20:24:12 +00:00
|
|
|
float determinant() const;
|
|
|
|
|
|
|
|
private:
|
2012-01-02 08:49:13 +00:00
|
|
|
float m_data[9];
|
2011-10-10 20:24:12 +00:00
|
|
|
};
|
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
// Solve equation system using LU decomposition and back-substitution.
|
|
|
|
extern bool solveLU(const Matrix3 & m, const Vector3 & b, Vector3 * x);
|
2011-10-10 20:24:12 +00:00
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
// Solve equation system using Cramer's inverse.
|
|
|
|
extern bool solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x);
|
|
|
|
|
2018-02-06 02:55:07 +00:00
|
|
|
extern Matrix3 inverse(const Matrix3 & m);
|
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
|
|
|
|
// 4x4 matrix.
|
2020-04-05 19:20:35 +00:00
|
|
|
class Matrix
|
2011-10-10 20:24:12 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Matrix const & Arg;
|
|
|
|
|
|
|
|
Matrix();
|
|
|
|
explicit Matrix(float f);
|
|
|
|
explicit Matrix(identity_t);
|
2012-07-20 16:32:26 +00:00
|
|
|
Matrix(const Matrix3 & m);
|
2011-10-10 20:24:12 +00:00
|
|
|
Matrix(const Matrix & m);
|
|
|
|
Matrix(Vector4::Arg v0, Vector4::Arg v1, Vector4::Arg v2, Vector4::Arg v3);
|
2012-01-02 08:49:13 +00:00
|
|
|
//explicit Matrix(const float m[]); // m is assumed to contain 16 elements
|
2011-10-10 20:24:12 +00:00
|
|
|
|
2012-01-02 08:49:13 +00:00
|
|
|
float data(uint idx) const;
|
|
|
|
float & data(uint idx);
|
|
|
|
float get(uint row, uint col) const;
|
|
|
|
float operator()(uint row, uint col) const;
|
|
|
|
float & operator()(uint row, uint col);
|
|
|
|
const float * ptr() const;
|
2011-10-10 20:24:12 +00:00
|
|
|
|
|
|
|
Vector4 row(uint i) const;
|
|
|
|
Vector4 column(uint i) const;
|
|
|
|
|
2015-10-29 06:53:08 +00:00
|
|
|
void zero();
|
|
|
|
void identity();
|
|
|
|
|
2012-01-02 08:49:13 +00:00
|
|
|
void scale(float s);
|
2011-10-10 20:24:12 +00:00
|
|
|
void scale(Vector3::Arg s);
|
|
|
|
void translate(Vector3::Arg t);
|
2012-01-02 08:49:13 +00:00
|
|
|
void rotate(float theta, float v0, float v1, float v2);
|
|
|
|
float determinant() const;
|
2011-10-10 20:24:12 +00:00
|
|
|
|
2014-11-04 17:49:29 +00:00
|
|
|
void operator+=(const Matrix & m);
|
|
|
|
void operator-=(const Matrix & m);
|
|
|
|
|
2011-10-10 20:24:12 +00:00
|
|
|
void apply(Matrix::Arg m);
|
|
|
|
|
|
|
|
private:
|
2012-01-02 08:49:13 +00:00
|
|
|
float m_data[16];
|
2011-10-10 20:24:12 +00:00
|
|
|
};
|
|
|
|
|
2012-07-20 16:19:03 +00:00
|
|
|
// Solve equation system using LU decomposition and back-substitution.
|
2014-11-04 17:49:29 +00:00
|
|
|
extern bool solveLU(const Matrix & A, const Vector4 & b, Vector4 * x);
|
2012-07-20 16:19:03 +00:00
|
|
|
|
|
|
|
// Solve equation system using Cramer's inverse.
|
|
|
|
extern bool solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x);
|
|
|
|
|
2014-11-04 17:49:29 +00:00
|
|
|
// Compute inverse using LU decomposition.
|
|
|
|
extern Matrix inverseLU(const Matrix & m);
|
|
|
|
|
|
|
|
// Compute inverse using Gaussian elimination and partial pivoting.
|
|
|
|
extern Matrix inverse(const Matrix & m);
|
|
|
|
|
2011-10-10 20:24:12 +00:00
|
|
|
} // nv namespace
|