1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
// This code is in the public domain -- castanyo@yahoo.es
#pragma once
#ifndef NV_MATH_MATRIX_H
#define NV_MATH_MATRIX_H
#include "Vector.h"
// - 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
namespace nv
{
enum identity_t { identity };
// 3x3 matrix.
class NVMATH_CLASS Matrix3
{
public:
Matrix3();
explicit Matrix3(float f);
explicit Matrix3(identity_t);
Matrix3(const Matrix3 & m);
Matrix3(Vector3::Arg v0, Vector3::Arg v1, Vector3::Arg v2);
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);
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);
void scale(float s);
void scale(Vector3::Arg s);
float determinant() const;
private:
float m_data[9];
};
// Solve equation system using LU decomposition and back-substitution.
extern bool solveLU(const Matrix3 & m, const Vector3 & b, Vector3 * x);
// Solve equation system using Cramer's inverse.
extern bool solveCramer(const Matrix3 & A, const Vector3 & b, Vector3 * x);
// 4x4 matrix.
class NVMATH_CLASS Matrix
{
public:
typedef Matrix const & Arg;
Matrix();
explicit Matrix(float f);
explicit Matrix(identity_t);
Matrix(const Matrix3 & m);
Matrix(const Matrix & m);
Matrix(Vector4::Arg v0, Vector4::Arg v1, Vector4::Arg v2, Vector4::Arg v3);
//explicit Matrix(const float m[]); // m is assumed to contain 16 elements
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;
Vector4 row(uint i) const;
Vector4 column(uint i) const;
void zero();
void identity();
void scale(float s);
void scale(Vector3::Arg s);
void translate(Vector3::Arg t);
void rotate(float theta, float v0, float v1, float v2);
float determinant() const;
void operator+=(const Matrix & m);
void operator-=(const Matrix & m);
void apply(Matrix::Arg m);
private:
float m_data[16];
};
// Solve equation system using LU decomposition and back-substitution.
extern bool solveLU(const Matrix & A, const Vector4 & b, Vector4 * x);
// Solve equation system using Cramer's inverse.
extern bool solveCramer(const Matrix & A, const Vector4 & b, Vector4 * x);
// 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);
extern Matrix3 inverse(const Matrix3 & m);
} // nv namespace
#endif // NV_MATH_MATRIX_H
|