CellModules
matrix4x4.h
Go to the documentation of this file.
1#ifndef MECACELL_MATRIX4X4_HPP
2#define MECACELL_MATRIX4X4_HPP
3#include <array>
5#include "rotation.h"
6
7namespace MecaCell {
8
9struct Matrix4x4 {
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) {}
14
22 template <typename V> void scale(const V &s) {
23 Matrix4x4 sm(
24 {{{{s.x(), 0, 0, 0}}, {{0, s.y(), 0, 0}}, {{0, 0, s.z(), 0}}, {{0, 0, 0, 1}}}});
25 *this = sm * (*this);
26 }
27
35 template <typename V> void translate(const V &v) {
36 Matrix4x4 t(
37 {{{{1, 0, 0, v.x()}}, {{0, 1, 0, v.y()}}, {{0, 0, 1, v.z()}}, {{0, 0, 0, 1}}}});
38 *this = t * (*this);
39 }
40
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);
57
58 Matrix4x4 rm(
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}},
65 {{0, 0, 0, 1}}}});
66 *this = rm * (*this);
67 }
68
69 inline Matrix4x4 operator*(const Matrix4x4 &N) {
70 return Matrix4x4({{{{m[0][0] * N.m[0][0] + m[0][1] * N.m[1][0] + m[0][2] * N.m[2][0] +
71 m[0][3] * N.m[3][0],
72 m[0][0] * N.m[0][1] + m[0][1] * N.m[1][1] + m[0][2] * N.m[2][1] +
73 m[0][3] * N.m[3][1],
74 m[0][0] * N.m[0][2] + m[0][1] * N.m[1][2] + m[0][2] * N.m[2][2] +
75 m[0][3] * N.m[3][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] +
79 m[1][3] * N.m[3][0],
80 m[1][0] * N.m[0][1] + m[1][1] * N.m[1][1] + m[1][2] * N.m[2][1] +
81 m[1][3] * N.m[3][1],
82 m[1][0] * N.m[0][2] + m[1][1] * N.m[1][2] + m[1][2] * N.m[2][2] +
83 m[1][3] * N.m[3][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] +
87 m[2][3] * N.m[3][0],
88 m[2][0] * N.m[0][1] + m[2][1] * N.m[1][1] + m[2][2] * N.m[2][1] +
89 m[2][3] * N.m[3][1],
90 m[2][0] * N.m[0][2] + m[2][1] * N.m[1][2] + m[2][2] * N.m[2][2] +
91 m[2][3] * N.m[3][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] +
95 m[3][3] * N.m[3][0],
96 m[3][0] * N.m[0][1] + m[3][1] * N.m[1][1] + m[3][2] * N.m[2][1] +
97 m[3][3] * N.m[3][1],
98 m[3][0] * N.m[0][2] + m[3][1] * N.m[1][2] + m[3][2] * N.m[2][2] +
99 m[3][3] * N.m[3][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]}}}});
102 }
103 template <typename V> inline V operator*(const V &v) {
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]);
107 }
108
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 {
111 return m[index];
112 }
113 friend ostream &operator<<(ostream &, const Matrix4x4 &);
114};
115
116inline ostream &operator<<(ostream &out, const Matrix4x4 &M) {
117 out << endl;
118 for (auto &i : M.m) {
119 out << "|";
120 for (auto &j : i) {
121 out << std::setw(5) << j << "|";
122 }
123 out << endl;
124 }
125 return out;
126}
127} // namespace MecaCell
128#endif
this file contains various miscellanious utility functions & helpers *
Vector3D Vec
alias for Vector3D
Definition: utils.hpp:22
std::ostream & operator<<(std::ostream &out, const Basis< T > &b)
Definition: basis.hpp:38
double sin(double x)
Computes the sine of a number.
Definition: std.hpp:53
iostream endl
End-of-line manipulator.
Definition: std.hpp:274
double cos(double x)
Computes the cosine of a number.
Definition: std.hpp:63
const std::array< double, 4 > & operator[](const size_t index) const
Definition: matrix4x4.h:110
Matrix4x4 operator*(const Matrix4x4 &N)
Definition: matrix4x4.h:69
Matrix4x4(std::array< std::array< double, 4 >, 4 > a)
Definition: matrix4x4.h:13
friend ostream & operator<<(ostream &, const Matrix4x4 &)
Definition: matrix4x4.h:116
std::array< double, 4 > & operator[](const size_t index)
Definition: matrix4x4.h:109
std::array< std::array< double, 4 >, 4 > m
Definition: matrix4x4.h:10
void scale(const V &s)
Multiplies this matrix by another that scales coordinates by the components of the given vector.
Definition: matrix4x4.h:22
void rotate(const R &r)
Multiplies this matrix by another that rotates coordinates by the according to the given rotation.
Definition: matrix4x4.h:48
V operator*(const V &v)
Definition: matrix4x4.h:103
void translate(const V &v)
Multiplies this matrix by another that translates coordinates by the components of the given vector.
Definition: matrix4x4.h:35