1#ifndef MECACELL_MATRIX4X4_HPP
2#define MECACELL_MATRIX4X4_HPP
10 std::array<std::array<double, 4>, 4>
m = {
11 {{{1, 0, 0, 0}}, {{0, 1, 0, 0}}, {{0, 0, 1, 0}}, {{0, 0, 0, 1}}}};
13 Matrix4x4(std::array<std::array<double, 4>, 4> a) :
m(a) {}
22 template <
typename V>
void scale(
const V &s) {
24 {{{{s.x(), 0, 0, 0}}, {{0, s.y(), 0, 0}}, {{0, 0, s.z(), 0}}, {{0, 0, 0, 1}}}});
37 {{{{1, 0, 0, v.x()}}, {{0, 1, 0, v.y()}}, {{0, 0, 1, v.z()}}, {{0, 0, 0, 1}}}});
48 template <
typename R>
void rotate(
const R &r) {
49 double squ = r.n.x() * r.n.x();
50 double sqv = r.n.y() * r.n.y();
51 double sqw = r.n.z() * r.n.z();
52 double uv = r.n.x() * r.n.y();
53 double uw = r.n.x() * r.n.z();
54 double vw = r.n.y() * r.n.z();
55 double costeta =
cos(r.teta);
56 double sinteta =
sin(r.teta);
59 {{{{squ + (1.0f - squ) * costeta, uv * (1.0f - costeta) - r.n.z() * sinteta,
60 uw * (1.0f - costeta) + r.n.y() * sinteta, 0}},
61 {{uv * (1.0f - costeta) + r.n.z() * sinteta, sqv + (1.0f - sqv) * costeta,
62 vw * (1.0f - costeta) - r.n.x() * sinteta, 0}},
63 {{uw * (1.0f - costeta) - r.n.y() * sinteta,
64 vw * (1.0f - costeta) + r.n.x() * sinteta, sqw + (1.0f - sqw) * costeta, 0}},
70 return Matrix4x4({{{{
m[0][0] * N.
m[0][0] +
m[0][1] * N.
m[1][0] +
m[0][2] * N.
m[2][0] +
72 m[0][0] * N.
m[0][1] +
m[0][1] * N.
m[1][1] +
m[0][2] * N.
m[2][1] +
74 m[0][0] * N.
m[0][2] +
m[0][1] * N.
m[1][2] +
m[0][2] * N.
m[2][2] +
76 m[0][0] * N.
m[0][3] +
m[0][1] * N.
m[1][3] +
m[0][2] * N.
m[2][3] +
77 m[0][3] * N.
m[3][3]}},
78 {{
m[1][0] * N.
m[0][0] +
m[1][1] * N.
m[1][0] +
m[1][2] * N.
m[2][0] +
80 m[1][0] * N.
m[0][1] +
m[1][1] * N.
m[1][1] +
m[1][2] * N.
m[2][1] +
82 m[1][0] * N.
m[0][2] +
m[1][1] * N.
m[1][2] +
m[1][2] * N.
m[2][2] +
84 m[1][0] * N.
m[0][3] +
m[1][1] * N.
m[1][3] +
m[1][2] * N.
m[2][3] +
85 m[1][3] * N.
m[3][3]}},
86 {{
m[2][0] * N.
m[0][0] +
m[2][1] * N.
m[1][0] +
m[2][2] * N.
m[2][0] +
88 m[2][0] * N.
m[0][1] +
m[2][1] * N.
m[1][1] +
m[2][2] * N.
m[2][1] +
90 m[2][0] * N.
m[0][2] +
m[2][1] * N.
m[1][2] +
m[2][2] * N.
m[2][2] +
92 m[2][0] * N.
m[0][3] +
m[2][1] * N.
m[1][3] +
m[2][2] * N.
m[2][3] +
93 m[2][3] * N.
m[3][3]}},
94 {{
m[3][0] * N.
m[0][0] +
m[3][1] * N.
m[1][0] +
m[3][2] * N.
m[2][0] +
96 m[3][0] * N.
m[0][1] +
m[3][1] * N.
m[1][1] +
m[3][2] * N.
m[2][1] +
98 m[3][0] * N.
m[0][2] +
m[3][1] * N.
m[1][2] +
m[3][2] * N.
m[2][2] +
100 m[3][0] * N.
m[0][3] +
m[3][1] * N.
m[1][3] +
m[3][2] * N.
m[2][3] +
101 m[3][3] * N.
m[3][3]}}}});
104 return Vec(
m[0][0] * v.x() +
m[0][1] * v.y() +
m[0][2] * v.z() +
m[0][3],
105 m[1][0] * v.x() +
m[1][1] * v.y() +
m[1][2] * v.z() +
m[1][3],
106 m[2][0] * v.x() +
m[2][1] * v.y() +
m[2][2] * v.z() +
m[2][3]);
109 inline std::array<double, 4> &
operator[](
const size_t index) {
return m[index]; }
110 inline const std::array<double, 4> &
operator[](
const size_t index)
const {
118 for (
auto &i : M.
m) {
121 out << std::setw(5) << j <<
"|";
this file contains various miscellanious utility functions & helpers *
Vector3D Vec
alias for Vector3D
std::ostream & operator<<(std::ostream &out, const Basis< T > &b)
double sin(double x)
Computes the sine of a number.
iostream endl
End-of-line manipulator.
double cos(double x)
Computes the cosine of a number.
const std::array< double, 4 > & operator[](const size_t index) const
Matrix4x4 operator*(const Matrix4x4 &N)
Matrix4x4(std::array< std::array< double, 4 >, 4 > a)
friend ostream & operator<<(ostream &, const Matrix4x4 &)
std::array< double, 4 > & operator[](const size_t index)
std::array< std::array< double, 4 >, 4 > m
void scale(const V &s)
Multiplies this matrix by another that scales coordinates by the components of the given vector.
void rotate(const R &r)
Multiplies this matrix by another that rotates coordinates by the according to the given rotation.
void translate(const V &v)
Multiplies this matrix by another that translates coordinates by the components of the given vector.