opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
matrix3x3.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstddef>
7#include <cstdint>
8
9namespace osc
10{
11 // a 3x3 column-major matrix
12 template<typename T>
13 struct Matrix<T, 3, 3> {
17 using element_type = T;
23 using const_pointer = const column_type*;
26
27 constexpr Matrix() = default;
28
29 explicit constexpr Matrix(T s) :
30 value{
31 column_type{s, T{}, T{}},
32 column_type{T{}, s, T{}},
33 column_type{T{}, T{}, s}
34 }
35 {}
36
37 constexpr Matrix(
38 T x0, T y0, T z0,
39 T x1, T y1, T z1,
40 T x2, T y2, T z2) :
41
42 value{
46 }
47 {}
48
49 constexpr Matrix(
50 const column_type& v0,
51 const column_type& v1,
52 const column_type& v2) :
53
54 value{v0, v1, v2}
55 {}
56
57 template<
58 typename X0, typename Y0, typename Z0,
59 typename X1, typename Y1, typename Z1,
60 typename X2, typename Y2, typename Z2>
61 constexpr Matrix(
62 X0 x0, Y0 y0, Z0 z0,
63 X1 x1, Y1 y1, Z1 z1,
64 X2 x2, Y2 y2, Z2 z2) :
65
66 value{
70 }
71 {}
72
73 template<typename V1, typename V2, typename V3>
74 constexpr Matrix(
75 const Vector<V1, 3>& v1,
76 const Vector<V2, 3>& v2,
77 const Vector<V3, 3>& v3) :
78
79 value{
83 }
84 {}
85
86 template<typename U>
87 explicit constexpr Matrix(const Matrix<U, 3, 3>& m) :
88 value{
89 column_type{m[0]},
90 column_type{m[1]},
91 column_type{m[2]}
92 }
93 {}
94
95 explicit constexpr Matrix(const Matrix<T, 4, 4>& m) :
96 value{
97 column_type{m[0]},
98 column_type{m[1]},
99 column_type{m[2]}
100 }
101 {}
102
103 template<typename U>
105 {
106 this->value[0] = m[0];
107 this->value[1] = m[1];
108 this->value[2] = m[2];
109 return *this;
110 }
111
112 constexpr size_type size() const { return 3; }
113 constexpr pointer data() { return value; }
114 constexpr const_pointer data() const { return value; }
115 constexpr iterator begin() { return data(); }
116 constexpr const_iterator begin() const { return data(); }
117 constexpr iterator end() { return data() + size(); }
118 constexpr const_iterator end() const { return data() + size(); }
119 constexpr reference operator[](size_type pos) { return begin()[pos]; }
120 constexpr const_reference operator[](size_type pos) const { return begin()[pos]; }
121
122 friend constexpr bool operator==(const Matrix&, const Matrix&) = default;
123
124 template<typename U>
126 {
127 this->value[0] += s;
128 this->value[1] += s;
129 this->value[2] += s;
130 return *this;
131 }
132
133 template<typename U>
135 {
136 this->value[0] += m[0];
137 this->value[1] += m[1];
138 this->value[2] += m[2];
139 return *this;
140 }
141
142 template<typename U>
144 {
145 this->value[0] -= s;
146 this->value[1] -= s;
147 this->value[2] -= s;
148 return *this;
149 }
150
151 template<typename U>
153 {
154 this->value[0] -= m[0];
155 this->value[1] -= m[1];
156 this->value[2] -= m[2];
157 return *this;
158 }
159
160 template<typename U>
162 {
163 this->value[0] *= s;
164 this->value[1] *= s;
165 this->value[2] *= s;
166 return *this;
167 }
168
169 template<typename U>
171 {
172 return (*this = *this * m);
173 }
174
175 template<typename U>
177 {
178 this->value[0] /= s;
179 this->value[1] /= s;
180 this->value[2] /= s;
181 return *this;
182 }
183
184 template<typename U>
186 {
187 return *this /= inverse(m);
188 }
189
191 {
192 ++this->value[0];
193 ++this->value[1];
194 ++this->value[2];
195 return *this;
196 }
197
199 {
200 --this->value[0];
201 --this->value[1];
202 --this->value[2];
203 return *this;
204 }
205
207 {
208 Matrix<T, 3, 3> copy{*this};
209 ++*this;
210 return copy;
211 }
212
214 {
215 Matrix<T, 3, 3> copy{*this};
216 --*this;
217 return copy;
218 }
219
220 private:
221 column_type value[3];
222 };
223
224 template<typename T>
226 {
227 return m;
228 }
229
230 template<typename T>
232 {
233 return Matrix<T, 3, 3>{-m[0], -m[1], -m[2]};
234 }
235
236 template<typename T>
238 {
239 return Matrix<T, 3, 3>{m[0] + scalar, m[1] + scalar, m[2] + scalar};
240 }
241
242 template<typename T>
244 {
245 return Matrix<T, 3, 3>{scalar + m[0], scalar + m[1], scalar + m[2]};
246 }
247
248 template<typename T>
250 {
251 return Matrix<T, 3, 3>{m1[0] + m2[0], m1[1] + m2[1], m1[2] + m2[2]};
252 }
253
254 template<typename T>
256 {
257 return Matrix<T, 3, 3>{m[0] - scalar, m[1] - scalar, m[2] - scalar};
258 }
259
260 template<typename T>
262 {
263 return Matrix<T, 3, 3>{scalar - m[0], scalar - m[1], scalar - m[2]};
264 }
265
266 template<typename T>
268 {
269 return Matrix<T, 3, 3>{m1[0] - m2[0], m1[1] - m2[1], m1[2] - m2[2]};
270 }
271
272 template<typename T>
274 {
275 return Matrix<T, 3, 3>{m[0] * scalar, m[1] * scalar, m[2] * scalar};
276 }
277
278 template<typename T>
280 {
281 return Matrix<T, 3, 3>{scalar * m[0], scalar * m[1], scalar * m[2]};
282 }
283
284 template<typename T>
286 {
287 return typename Matrix<T, 3, 3>::column_type{
288 m[0][0] * v.x() + m[1][0] * v.y() + m[2][0] * v.z(),
289 m[0][1] * v.x() + m[1][1] * v.y() + m[2][1] * v.z(),
290 m[0][2] * v.x() + m[1][2] * v.y() + m[2][2] * v.z(),
291 };
292 }
293
294 template<typename T>
296 {
297 return typename Matrix<T, 3, 3>::row_type{
298 m[0][0] * v.x() + m[0][1] * v.y() + m[0][2] * v.z(),
299 m[1][0] * v.x() + m[1][1] * v.y() + m[1][2] * v.z(),
300 m[2][0] * v.x() + m[2][1] * v.y() + m[2][2] * v.z(),
301 };
302 }
303
304 template<typename T>
306 {
307 const T& a00 = a[0][0];
308 const T& a01 = a[0][1];
309 const T& a02 = a[0][2];
310 const T& a10 = a[1][0];
311 const T& a11 = a[1][1];
312 const T& a12 = a[1][2];
313 const T& a20 = a[2][0];
314 const T& a21 = a[2][1];
315 const T& a22 = a[2][2];
316
317 const T& b00 = b[0][0];
318 const T& b01 = b[0][1];
319 const T& b02 = b[0][2];
320 const T& b10 = b[1][0];
321 const T& b11 = b[1][1];
322 const T& b12 = b[1][2];
323 const T& b20 = b[2][0];
324 const T& b21 = b[2][1];
325 const T& b22 = b[2][2];
326
328 rv[0][0] = a00 * b00 + a10 * b01 + a20 * b02;
329 rv[0][1] = a01 * b00 + a11 * b01 + a21 * b02;
330 rv[0][2] = a02 * b00 + a12 * b01 + a22 * b02;
331 rv[1][0] = a00 * b10 + a10 * b11 + a20 * b12;
332 rv[1][1] = a01 * b10 + a11 * b11 + a21 * b12;
333 rv[1][2] = a02 * b10 + a12 * b11 + a22 * b12;
334 rv[2][0] = a00 * b20 + a10 * b21 + a20 * b22;
335 rv[2][1] = a01 * b20 + a11 * b21 + a21 * b22;
336 rv[2][2] = a02 * b20 + a12 * b21 + a22 * b22;
337 return rv;
338 }
339
340 template<typename T>
342 {
343 return Matrix<T, 3, 3>{m[0] / scalar, m[1] / scalar, m[2] / scalar};
344 }
345
346 template<typename T>
348 {
349 return Matrix<T, 3, 3>{scalar / m[0], scalar / m[1], scalar / m[2]};
350 }
351
352 template<typename T>
354 {
355 return inverse(m) * v;
356 }
357
358 template<typename T>
360 {
361 return v * inverse(m);
362 }
363
364 template<typename T>
370
378
379 template<typename T>
380 constexpr T identity();
381
382 template<>
384 {
385 return Matrix3x3{1.0f};
386 }
387}
Definition custom_decoration_generator.h:5
constexpr std::common_type_t< Angle< Rep1, Units1 >, Angle< Rep2, Units2 > > operator+(const Angle< Rep1, Units1 > &lhs, const Angle< Rep2, Units2 > &rhs)
Definition angle.h:99
constexpr Matrix3x3 identity< Matrix3x3 >()
Definition matrix3x3.h:383
constexpr T identity()
Matrix< T, 3, 3 > operator/(const Matrix< T, 3, 3 > &m, T scalar)
Definition matrix3x3.h:341
constexpr U to(T &&value)
Definition conversion.h:56
T inverse(const Matrix< T, 3, 3 > &m)
Definition matrix_functions.h:158
Matrix< T, 3, 3 > operator*(const Matrix< T, 3, 3 > &m, T scalar)
Definition matrix3x3.h:273
constexpr std::common_type_t< Angle< Rep1, Units1 >, Angle< Rep2, Units2 > > operator-(const Angle< Rep1, Units1 > &lhs, const Angle< Rep2, Units2 > &rhs)
Definition angle.h:114
constexpr Matrix()=default
Matrix< T, 3, 3 > & operator/=(const Matrix< U, 3, 3 > &m)
Definition matrix3x3.h:185
constexpr reference operator[](size_type pos)
Definition matrix3x3.h:119
constexpr const_iterator end() const
Definition matrix3x3.h:118
constexpr Matrix(T s)
Definition matrix3x3.h:29
Matrix< T, 3, 3 > & operator+=(U s)
Definition matrix3x3.h:125
size_t size_type
Definition matrix3x3.h:18
Matrix< T, 3, 3 > & operator++()
Definition matrix3x3.h:190
Matrix< T, 3, 3 > operator--(int)
Definition matrix3x3.h:213
Matrix< T, 3, 3 > operator++(int)
Definition matrix3x3.h:206
Matrix< T, 3, 3 > & operator*=(const Matrix< U, 3, 3 > &m)
Definition matrix3x3.h:170
constexpr iterator begin()
Definition matrix3x3.h:115
Matrix< T, 3, 3 > & operator*=(U s)
Definition matrix3x3.h:161
Matrix< T, 3, 3 > & operator-=(const Matrix< U, 3, 3 > &m)
Definition matrix3x3.h:152
friend constexpr bool operator==(const Matrix &, const Matrix &)=default
constexpr size_type size() const
Definition matrix3x3.h:112
Matrix< T, 3, 3 > & operator+=(const Matrix< U, 3, 3 > &m)
Definition matrix3x3.h:134
Matrix< T, 3, 3 > & operator=(const Matrix< U, 3, 3 > &m)
Definition matrix3x3.h:104
constexpr Matrix(const Matrix< U, 3, 3 > &m)
Definition matrix3x3.h:87
constexpr pointer data()
Definition matrix3x3.h:113
Matrix< T, 3, 3 > & operator--()
Definition matrix3x3.h:198
constexpr const_pointer data() const
Definition matrix3x3.h:114
ptrdiff_t difference_type
Definition matrix3x3.h:19
constexpr Matrix(const Vector< V1, 3 > &v1, const Vector< V2, 3 > &v2, const Vector< V3, 3 > &v3)
Definition matrix3x3.h:74
Matrix< T, 3, 3 > & operator/=(U s)
Definition matrix3x3.h:176
constexpr Matrix(T x0, T y0, T z0, T x1, T y1, T z1, T x2, T y2, T z2)
Definition matrix3x3.h:37
constexpr Matrix(const Matrix< T, 4, 4 > &m)
Definition matrix3x3.h:95
constexpr const_reference operator[](size_type pos) const
Definition matrix3x3.h:120
constexpr Matrix(const column_type &v0, const column_type &v1, const column_type &v2)
Definition matrix3x3.h:49
constexpr Matrix(X0 x0, Y0 y0, Z0 z0, X1 x1, Y1 y1, Z1 z1, X2 x2, Y2 y2, Z2 z2)
Definition matrix3x3.h:61
constexpr iterator end()
Definition matrix3x3.h:117
Matrix< T, 3, 3 > & operator-=(U s)
Definition matrix3x3.h:143
T element_type
Definition matrix3x3.h:17
constexpr const_iterator begin() const
Definition matrix3x3.h:116
Definition matrix.h:15