1 #ifndef IV_MATH_MATRIX_H 2 #define IV_MATH_MATRIX_H 10 template<
typename number_t,
int size>
32 static const size_t size = 9;
35 number_t a11, a21,
a31;
36 number_t a12, a22,
a32;
37 number_t a13, a23,
a33;
40 number_t*
data() {
return &a11; }
41 const number_t*
data()
const {
return &a11; }
42 std::pair<int, int>
shape()
const {
return std::make_pair(3, 3); }
62 template<
typename Generator>
65 for(
int i = 0; i < size; i++) r.
data()[i] = generator();
96 return vector_t(a11 * v.
x + a12 * v.
y + a13 * v.
z,
97 a21 * v.
x + a22 * v.
y + a23 * v.
z,
98 a31 * v.
x + a32 * v.
y + a33 * v.
z);
117 r.
a11 = a11 * s; r.
a12 = a12 * s; r.
a13 = a13 * s;
118 r.
a21 = a21 * s; r.
a22 = a22 * s; r.
a23 = a23 * s;
119 r.
a31 = a31 * s; r.
a32 = a32 * s; r.
a33 = a33 * s;
137 return a11 * (a22 * a33 - a32 * a23) +
138 a12 * (a23 * a31 - a21 * a33) +
139 a13 * (a21 * a32 - a22 * a31);
144 internal::matrix_inversion<number_t, 3>(data(), r.
data());
179 number_t
max()
const {
return *std::max_element(data(), data() + size); }
180 number_t
min()
const {
return *std::min_element(data(), data() + size); }
192 for(
int i = 0; i < size; i++) { r.
data()[i] =
std::abs(data()[i]); }
202 static_assert(
sizeof(Matrix3d) ==
sizeof(
double) * 9,
"Matrix3d alignment error.");
203 static_assert(
sizeof(Matrix3f) ==
sizeof(
float) * 9,
"Matrix3f alignment error.");
205 template <
typename T>
210 static const size_t size = 16;
212 number_t a11, a21, a31,
a41;
213 number_t a12, a22, a32,
a42;
214 number_t a13, a23, a33,
a43;
215 number_t a14, a24, a34,
a44;
217 number_t*
data() {
return &a11; }
218 const number_t*
data()
const {
return &a11; }
219 std::pair<int, int>
shape()
const {
return std::make_pair(4, 4); }
242 template<
typename Generator>
245 for(
int i = 0; i < size; i++) r.
data()[i] = generator();
262 static Matrix4_ fromColumns(
const vector_t& e1,
const vector_t& e2,
const vector_t& e3,
const vector_t& e4) {
270 static Matrix4_ fromRows(
const vector_t& e1,
const vector_t& e2,
const vector_t& e3,
const vector_t& e4) {
278 template<
typename T1>
280 return vector_t(a11 * v.
x + a12 * v.
y + a13 * v.
z + a14 * v.
w,
281 a21 * v.
x + a22 * v.
y + a23 * v.
z + a24 * v.
w,
282 a31 * v.
x + a32 * v.
y + a33 * v.
z + a34 * v.
w,
283 a41 * v.
x + a42 * v.
y + a43 * v.
z + a44 * v.
w);
303 r.
a11 = a11 * s; r.
a12 = a12 * s; r.
a13 = a13 * s; r.
a14 = a14 * s;
304 r.
a21 = a21 * s; r.
a22 = a22 * s; r.
a23 = a23 * s; r.
a24 = a24 * s;
305 r.
a31 = a31 * s; r.
a32 = a32 * s; r.
a33 = a33 * s; r.
a34 = a34 * s;
306 r.
a41 = a41 * s; r.
a42 = a42 * s; r.
a43 = a43 * s; r.
a44 = a44 * s;
332 + a44 * (a11 * (a22 * a33 - a32 * a23) + a12 * (a23 * a31 - a21 * a33) + a13 * (a21 * a32 - a22 * a31))
333 - a34 * (a11 * (a22 * a43 - a42 * a23) + a12 * (a23 * a41 - a21 * a43) + a13 * (a21 * a42 - a22 * a41))
334 + a24 * (a11 * (a32 * a43 - a42 * a33) + a12 * (a33 * a41 - a31 * a43) + a13 * (a31 * a42 - a32 * a41))
335 - a14 * (a21 * (a32 * a43 - a42 * a33) + a22 * (a33 * a41 - a31 * a43) + a23 * (a31 * a42 - a32 * a41));
340 internal::matrix_inversion<number_t, 4>(data(), r.
data());
367 number_t
max()
const {
return *std::max_element(data(), data() + size); }
368 number_t
min()
const {
return *std::min_element(data(), data() + size); }
380 for(
int i = 0; i < size; i++) { r.
data()[i] =
std::abs(data()[i]); }
390 static_assert(
sizeof(Matrix4d) ==
sizeof(
double) * 16,
"Matrix4d alignment error.");
391 static_assert(
sizeof(Matrix4f) ==
sizeof(
float) * 16,
"Matrixf alignment error.");
number_t a12
Definition: matrix.h:213
number_t a12
Definition: matrix.h:36
Matrix3_ t() const
Definition: matrix.h:168
number_t a13
Definition: matrix.h:37
static Matrix3_ zero()
Definition: matrix.h:46
number_t z
Definition: vector.h:157
number_t determinant() const
Definition: matrix.h:330
friend Matrix3_ abs(const Matrix3_ &m)
Definition: matrix.h:195
number_t min() const
Definition: matrix.h:368
number_t a33
Definition: matrix.h:37
number_t x
Definition: vector.h:110
number_t y
Definition: vector.h:157
number_t a34
Definition: matrix.h:215
number_t a44
Definition: matrix.h:215
static Matrix4_ eye()
Definition: matrix.h:232
number_t a33
Definition: matrix.h:214
number_t a21
Definition: matrix.h:212
Matrix3_ transpose() const
Definition: matrix.h:161
number_t * data()
Definition: matrix.h:40
Matrix4d Matrix4
Definition: matrix.h:388
number_t max() const
Definition: matrix.h:179
Vector4_< T > vector_t
Definition: matrix.h:209
number_t a24
Definition: matrix.h:215
number_t a21
Definition: matrix.h:35
static Matrix4_ fromRows(const vector_t &e1, const vector_t &e2, const vector_t &e3, const vector_t &e4)
Definition: matrix.h:270
static Matrix3_ skew(const vector_t &v)
Definition: matrix.h:170
number_t a11
Definition: matrix.h:35
static Matrix4_ fromColumns(const vector_t &e1, const vector_t &e2, const vector_t &e3, const vector_t &e4)
Definition: matrix.h:262
Matrix4_ i() const
Definition: matrix.h:343
template void matrix_inversion< double, 4 >(const double *, double *)
number_t w
Definition: vector.h:157
template void matrix_inversion< double, 3 >(const double *, double *)
number_t a42
Definition: matrix.h:213
Matrix4_ transpose() const
Definition: matrix.h:357
const number_t * data() const
Definition: matrix.h:41
T number_t
Definition: matrix.h:30
friend number_t max(const Matrix3_ &m)
Definition: matrix.h:182
T number_t
Definition: matrix.h:208
number_t a32
Definition: matrix.h:213
Matrix4_< double > Matrix4d
Definition: matrix.h:387
static Matrix4_ random(Generator generator)
Definition: matrix.h:243
number_t a23
Definition: matrix.h:214
Matrix4_ abs() const
Definition: matrix.h:378
static Matrix3_ eye()
Definition: matrix.h:54
number_t a41
Definition: matrix.h:212
number_t * data()
Definition: matrix.h:217
friend number_t min(const Matrix3_ &m)
Definition: matrix.h:186
Vector3_< T > vector_t
Definition: matrix.h:31
number_t a43
Definition: matrix.h:214
Matrix3_< float > Matrix3f
Definition: matrix.h:198
Matrix3_ i() const
Definition: matrix.h:147
std::pair< int, int > shape() const
Definition: matrix.h:219
Matrix4_ inversion() const
Definition: matrix.h:338
Matrix3_ abs() const
Definition: matrix.h:190
number_t a23
Definition: matrix.h:37
static Matrix3_ fromColumns(const vector_t &e1, const vector_t &e2, const vector_t &e3)
Definition: matrix.h:79
void matrix_inversion(const number_t *data, number_t *output)
template void matrix_inversion< float, 3 >(const float *, float *)
number_t a14
Definition: matrix.h:215
number_t a31
Definition: matrix.h:212
number_t y
Definition: vector.h:110
number_t max() const
Definition: matrix.h:367
number_t min() const
Definition: matrix.h:180
friend number_t max(const Matrix4_ &m)
Definition: matrix.h:370
static Matrix4_ zero()
Definition: matrix.h:223
static Matrix3_ fromRows(const vector_t &e1, const vector_t &e2, const vector_t &e3)
Definition: matrix.h:87
Matrix3_ inversion() const
Definition: matrix.h:142
number_t a22
Definition: matrix.h:213
number_t z
Definition: vector.h:110
Matrix3_< double > Matrix3d
Definition: matrix.h:199
template void matrix_inversion< float, 4 >(const float *, float *)
number_t a22
Definition: matrix.h:36
number_t abs(number_t a)
Compute absolute value.
Definition: basic.h:27
std::pair< int, int > shape() const
Definition: matrix.h:42
Matrix4_ t() const
Definition: matrix.h:365
friend number_t min(const Matrix4_ &m)
Definition: matrix.h:374
friend Matrix4_ abs(const Matrix4_ &m)
Definition: matrix.h:383
const number_t * data() const
Definition: matrix.h:218
Vector3_< T1 > operator*(const Vector3_< T1 > &v, const Matrix3_< T2 > &m)
Definition: quaternion.h:85
number_t x
Definition: vector.h:157
number_t determinant() const
Definition: matrix.h:136
Matrix3d Matrix3
Definition: matrix.h:200
static Matrix3_ random(Generator generator)
Definition: matrix.h:63
number_t a31
Definition: matrix.h:35
number_t a11
Definition: matrix.h:212
static Matrix3_ fromDiagonal(const vector_t &diag)
Definition: matrix.h:69
number_t a32
Definition: matrix.h:36
Matrix4_< float > Matrix4f
Definition: matrix.h:386
number_t a13
Definition: matrix.h:214
static Matrix4_ fromDiagonal(const vector_t &diag)
Definition: matrix.h:249