MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMatrix3.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMacros.h"
4#include "MRVector3.h"
5#include "MRConstants.h"
6#include <iosfwd>
7
8namespace MR
9{
12
13
14#ifdef _MSC_VER
15#pragma warning(push)
16#pragma warning(disable: 4804)
17#pragma warning(disable: 4146)
18#endif
19
22template <typename T>
23struct Matrix3
24{
25 using ValueType = T;
27
29 Vector3<T> x{ 1, 0, 0 };
30 Vector3<T> y{ 0, 1, 0 };
31 Vector3<T> z{ 0, 0, 1 };
32
33 constexpr Matrix3() noexcept
34 {
35 static_assert( sizeof( Matrix3<ValueType> ) == 3 * sizeof( VectorType ), "Struct size invalid" );
36 }
38 constexpr Matrix3( const Vector3<T> & x, const Vector3<T> & y, const Vector3<T> & z ) : x( x ), y( y ), z( z ) { }
39
42 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
43 constexpr explicit Matrix3( const Matrix3<U> & m ) : x( m.x ), y( m.y ), z( m.z ) { }
44
45 static constexpr Matrix3 zero() noexcept { return Matrix3( Vector3<T>(), Vector3<T>(), Vector3<T>() ); }
46 static constexpr Matrix3 identity() noexcept { return Matrix3(); }
48 static constexpr Matrix3 scale( T s ) noexcept { return Matrix3( { s, T(0), T(0) }, { T(0), s, T(0) }, { T(0), T(0), s } ); }
50 static constexpr Matrix3 scale( T sx, T sy, T sz ) noexcept { return Matrix3( { sx, T(0), T(0) }, { T(0), sy, T(0) }, { T(0), T(0), sz } ); }
51 static constexpr Matrix3 scale( const Vector3<T> & s ) noexcept { return Matrix3( { s.x, T(0), T(0) }, { T(0), s.y, T(0) }, { T(0), T(0), s.z } ); }
53 static constexpr Matrix3 rotation( const Vector3<T> & axis, T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> );
55 static constexpr Matrix3 rotation( const Vector3<T> & from, const Vector3<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> );
58 static constexpr Matrix3 rotationFromEuler( const Vector3<T> & eulerAngles ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
60 static constexpr Matrix3 approximateLinearRotationMatrixFromEuler( const Vector3<T> & eulerAngles ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
62 static constexpr Matrix3 fromRows( const Vector3<T> & x, const Vector3<T> & y, const Vector3<T> & z ) noexcept { return Matrix3( x, y, z ); }
65 static constexpr Matrix3 fromColumns( const Vector3<T> & x, const Vector3<T> & y, const Vector3<T> & z ) noexcept { return Matrix3( x, y, z ).transposed(); }
66
68 constexpr const Vector3<T> & operator []( int row ) const noexcept { return *( ( VectorType* )this + row ); }
69 constexpr Vector3<T> & operator []( int row ) noexcept { return *( ( VectorType* )this + row ); }
70
72 constexpr Vector3<T> col( int i ) const noexcept { return { x[i], y[i], z[i] }; }
73
75 constexpr T trace() const noexcept { return x.x + y.y + z.z; }
77 constexpr T normSq() const noexcept { return x.lengthSq() + y.lengthSq() + z.lengthSq(); }
78 constexpr auto norm() const noexcept
79 {
82 using std::sqrt;
83 return sqrt( normSq() );
84 }
86 constexpr T det() const noexcept;
88 constexpr Matrix3<T> inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> );
90 constexpr Matrix3<T> transposed() const noexcept;
92 constexpr Vector3<T> toEulerAngles() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
93
94 struct QR
95 {
97 };
99 QR qr() const noexcept MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> );
100
101 [[nodiscard]] friend constexpr bool operator ==( const Matrix3<T> & a, const Matrix3<T> & b ) { return a.x == b.x && a.y == b.y && a.z == b.z; }
102 [[nodiscard]] friend constexpr bool operator !=( const Matrix3<T> & a, const Matrix3<T> & b ) { return !( a == b ); }
103
105
106 [[nodiscard]] friend constexpr auto operator +( const Matrix3<T> & a, const Matrix3<T> & b ) -> Matrix3<decltype( std::declval<T>() + std::declval<T>() )> { return { a.x + b.x, a.y + b.y, a.z + b.z }; }
107 [[nodiscard]] friend constexpr auto operator -( const Matrix3<T> & a, const Matrix3<T> & b ) -> Matrix3<decltype( std::declval<T>() - std::declval<T>() )> { return { a.x - b.x, a.y - b.y, a.z - b.z }; }
108 [[nodiscard]] friend constexpr auto operator *( T a, const Matrix3<T> & b ) -> Matrix3<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y, a * b.z }; }
109 [[nodiscard]] friend constexpr auto operator *( const Matrix3<T> & b, T a ) -> Matrix3<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y, a * b.z }; }
110 [[nodiscard]] friend constexpr auto operator /( Matrix3<T> b, T a ) -> Matrix3<decltype( std::declval<T>() / std::declval<T>() )>
111 {
112 if constexpr ( std::is_integral_v<T> )
113 return { b.x / a, b.y / a, b.z / a };
114 else
115 return b * ( 1 / a );
116 }
117
118 friend constexpr Matrix3<T> & operator +=( Matrix3<T> & a, const Matrix3<T> & b ) { a.x += b.x; a.y += b.y; a.z += b.z; return a; }
119 friend constexpr Matrix3<T> & operator -=( Matrix3<T> & a, const Matrix3<T> & b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; }
120 friend constexpr Matrix3<T> & operator *=( Matrix3<T> & a, T b ) { a.x *= b; a.y *= b; a.z *= b; return a; }
121 friend constexpr Matrix3<T> & operator /=( Matrix3<T> & a, T b )
122 {
123 if constexpr ( std::is_integral_v<T> )
124 { a.x /= b; a.y /= b; a.z /= b; return a; }
125 else
126 return a *= ( 1 / b );
127 }
128
130 [[nodiscard]] friend constexpr auto operator *( const Matrix3<T> & a, const Vector3<T> & b ) -> Vector3<decltype( dot( std::declval<Vector3<T>>(), std::declval<Vector3<T>>() ) )>
131 {
132 return { dot( a.x, b ), dot( a.y, b ), dot( a.z, b ) };
133 }
134
136 [[nodiscard]] friend constexpr auto operator *( const Matrix3<T> & a, const Matrix3<T> & b ) -> Matrix3<decltype( dot( std::declval<Vector3<T>>(), std::declval<Vector3<T>>() ) )>
137 {
138 Matrix3<decltype( dot( std::declval<Vector3<T>>(), std::declval<Vector3<T>>() ) )> res;
139 for ( int i = 0; i < 3; ++i )
140 for ( int j = 0; j < 3; ++j )
141 res[i][j] = dot( a[i], b.col(j) );
142 return res;
143 }
144
145 friend std::ostream& operator<<( std::ostream& s, const Matrix3& mat )
146 {
147 return s << mat.x << '\n' << mat.y << '\n' << mat.z << '\n';
148 }
149
150 friend std::istream& operator>>( std::istream& s, Matrix3& mat )
151 {
152 return s >> mat.x >> mat.y >> mat.z;
153 }
154};
155
158
160template <typename T>
161inline auto dot( const Matrix3<T> & a, const Matrix3<T> & b ) -> decltype( dot( a.x, b.x ) )
162{
163 return dot( a.x, b.x ) + dot( a.y, b.y ) + dot( a.z, b.z );
164}
165
167template <typename T>
168inline Matrix3<T> outer( const Vector3<T> & a, const Vector3<T> & b )
169{
170 return { a.x * b, a.y * b, a.z * b };
171}
172
173template <typename T>
174constexpr Matrix3<T> Matrix3<T>::rotation( const Vector3<T> & axis, T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
175{
177 auto u = axis.normalized();
178 T c = cos( angle );
179 T oc = 1 - c;
180 T s = sin( angle );
181 return {
182 { c + u.x * u.x * oc, u.x * u.y * oc - u.z * s, u.x * u.z * oc + u.y * s },
183 { u.y * u.x * oc + u.z * s, c + u.y * u.y * oc, u.y * u.z * oc - u.x * s },
184 { u.z * u.x * oc - u.y * s, u.z * u.y * oc + u.x * s, c + u.z * u.z * oc }
185 };
186}
187
188template <typename T>
189constexpr Matrix3<T> Matrix3<T>::rotation( const Vector3<T> & from, const Vector3<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
190{
191 auto axis = cross( from, to );
192 if ( axis.lengthSq() > 0 )
193 return rotation( axis, angle( from, to ) );
194 if ( dot( from, to ) >= 0 )
195 return {};
196 return rotation( cross( from, from.furthestBasisVector() ), T( PI ) );
197}
198
199template <typename T>
200constexpr Matrix3<T> Matrix3<T>::rotationFromEuler( const Vector3<T> & eulerAngles ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
201{
203 const auto cx = std::cos( eulerAngles.x );
204 const auto cy = std::cos( eulerAngles.y );
205 const auto cz = std::cos( eulerAngles.z );
206 const auto sx = std::sin( eulerAngles.x );
207 const auto sy = std::sin( eulerAngles.y );
208 const auto sz = std::sin( eulerAngles.z );
209 return {
210 { cy * cz, cz * sx * sy - cx * sz, cx * cz * sy + sx * sz },
211 { cy * sz, cx * cz + sx * sy * sz, -cz * sx + cx * sy * sz },
212 { -sy, cy * sx, cx * cy }
213 };
214}
215
216template <typename T>
217constexpr Matrix3<T> Matrix3<T>::approximateLinearRotationMatrixFromEuler( const Vector3<T> & eulerAngles ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
218{
219 const auto alpha = eulerAngles.x;
220 const auto beta = eulerAngles.y;
221 const auto gamma = eulerAngles.z;
222 return {
223 { T(1), -gamma, beta },
224 { gamma, T(1), -alpha },
225 { -beta, alpha, T(1) }
226 };
227}
228
229template <typename T>
230constexpr T Matrix3<T>::det() const noexcept
231{
232 return
233 x.x * ( y.y * z.z - y.z * z.y )
234 - x.y * ( y.x * z.z - y.z * z.x )
235 + x.z * ( y.x * z.y - y.y * z.x );
236}
237
238template <typename T>
239constexpr Matrix3<T> Matrix3<T>::inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
240{
241 auto det = this->det();
242 if ( det == 0 )
243 return {};
244 return Matrix3<T>
245 {
246 { y.y * z.z - y.z * z.y, x.z * z.y - x.y * z.z, x.y * y.z - x.z * y.y },
247 { y.z * z.x - y.x * z.z, x.x * z.z - x.z * z.x, x.z * y.x - x.x * y.z },
248 { y.x * z.y - y.y * z.x, x.y * z.x - x.x * z.y, x.x * y.y - x.y * y.x }
249 } / det;
250}
251
252template <typename T>
253constexpr Matrix3<T> Matrix3<T>::transposed() const noexcept
254{
255 return Matrix3<T>
256 {
257 { x.x, y.x, z.x },
258 { x.y, y.y, z.y },
259 { x.z, y.z, z.z }
260 };
261}
262
263template <typename T>
264constexpr Vector3<T> Matrix3<T>::toEulerAngles() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
265{
267 return {
268 std::atan2( z.y, z.z ),
269 std::atan2( -z.x, std::sqrt( z.y * z.y + z.z * z.z ) ),
270 std::atan2( y.x, x.x )
271 };
272}
273
274template <typename T>
275auto Matrix3<T>::qr() const noexcept -> QR MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
276{
278 const auto a0 = col( 0 );
279 auto a1 = col( 1 );
280 auto a2 = col( 2 );
281 const auto r00 = a0.length();
282 const auto e0 = r00 > 0 ? a0 / r00 : Vector3<T>{};
283 const auto r01 = dot( e0, a1 );
284 const auto r02 = dot( e0, a2 );
285 a1 -= r01 * e0;
286 const auto r11 = a1.length();
287 const auto e1 = r11 > 0 ? a1 / r11 : Vector3<T>{};
288 const auto r12 = dot( e1, a2 );
289 a2 -= r02 * e0 + r12 * e1;
290 const auto r22 = a2.length();
291 const auto e2 = r22 > 0 ? a2 / r22 : Vector3<T>{};
292 return QR
293 {
294 Matrix3::fromColumns( e0, e1, e2 ),
295 Matrix3::fromRows( { r00, r01, r02 }, { T(0), r11, r12 }, { T(0), T(0), r22 } )
296 };
297}
298
300
301#ifdef _MSC_VER
302#pragma warning(pop)
303#endif
304
305}
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
static constexpr Matrix3 scale(T sx, T sy, T sz) noexcept
returns a matrix that has its own scale along each axis
Definition MRMatrix3.h:50
Matrix3< T > outer(const Vector3< T > &a, const Vector3< T > &b)
x = a * b^T
Definition MRMatrix3.h:168
T ValueType
Definition MRMatrix3.h:25
static constexpr Matrix3 identity() noexcept
Definition MRMatrix3.h:46
friend constexpr auto operator*(T a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >() *std::declval< T >())>
Definition MRMatrix3.h:108
Matrix3 q
Definition MRMatrix3.h:96
constexpr Matrix3< T > inverse() const noexcept MR_REQUIRES_IF_SUPPORTED(!std constexpr Matrix3< T > transposed() const noexcept
computes inverse matrix
constexpr T det() const noexcept
computes determinant of the matrix
Vector3< T > x
rows, identity matrix by default
Definition MRMatrix3.h:29
constexpr const Vector3< T > & operator[](int row) const noexcept
row access
Definition MRMatrix3.h:68
friend constexpr Matrix3< T > & operator-=(Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMatrix3.h:119
constexpr Matrix3(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z)
initializes matrix from its 3 rows
Definition MRMatrix3.h:38
T x
Definition MRVector3.h:39
constexpr T normSq() const noexcept
compute sum of squared matrix elements
Definition MRMatrix3.h:77
T y
Definition MRVector3.h:39
friend constexpr Matrix3< T > & operator*=(Matrix3< T > &a, T b)
Definition MRMatrix3.h:120
friend constexpr Matrix3< T > & operator+=(Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMatrix3.h:118
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Matrix3(const Matrix3< U > &m)
Definition MRMatrix3.h:42
constexpr auto norm() const noexcept
Definition MRMatrix3.h:78
constexpr Vector3< T > col(int i) const noexcept
column access
Definition MRMatrix3.h:72
static constexpr Matrix3 scale(T s) noexcept
returns a matrix that scales uniformly
Definition MRMatrix3.h:48
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRMatrix3.h:75
friend constexpr bool operator!=(const Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMatrix3.h:102
static constexpr Matrix3 fromColumns(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z) noexcept
Definition MRMatrix3.h:65
constexpr Matrix3() noexcept
Definition MRMatrix3.h:33
friend std::istream & operator>>(std::istream &s, Matrix3 &mat)
Definition MRMatrix3.h:150
Vector3< T > y
Definition MRMatrix3.h:30
static constexpr Matrix3 zero() noexcept
Definition MRMatrix3.h:45
auto dot(const Matrix3< T > &a, const Matrix3< T > &b) -> decltype(dot(a.x, b.x))
double-dot product: x = a : b
Definition MRMatrix3.h:161
friend constexpr auto operator-(const Matrix3< T > &a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >() - std::declval< T >())>
Definition MRMatrix3.h:107
friend std::ostream & operator<<(std::ostream &s, const Matrix3 &mat)
Definition MRMatrix3.h:145
friend constexpr auto operator+(const Matrix3< T > &a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >()+std::declval< T >())>
NOTE: We use std::declval() in the operators below because libclang 18 in our binding generator is bu...
Definition MRMatrix3.h:106
friend constexpr auto operator/(Matrix3< T > b, T a) -> Matrix3< decltype(std::declval< T >()/std::declval< T >())>
Definition MRMatrix3.h:110
static constexpr Matrix3 static rotation(const Vector3< T > &axis, T angle) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix3 static rotation(const Vector3< T > &from, const Vector3< T > &to) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix3 static rotationFromEuler(const Vector3< T > &eulerAngles) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix3 static approximateLinearRotationMatrixFromEuler(const Vector3< T > &eulerAngles) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix fromRows)(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z) noexcept
creates matrix representing rotation around given axis on given angle
Definition MRMatrix3.h:62
Vector3< T > z
Definition MRMatrix3.h:31
T z
Definition MRVector3.h:39
friend constexpr Matrix3< T > & operator/=(Matrix3< T > &a, T b)
Definition MRMatrix3.h:121
static constexpr Matrix3 scale(const Vector3< T > &s) noexcept
Definition MRMatrix3.h:51
@ angle
Direction, normally Vector3f.
constexpr auto dot(A a, A b)
Definition MRImGuiVectorOperators.h:129
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
returns 3 Euler angles, assuming this is a rotation matrix composed as follows: R=R(z)*R(y)*R(x)
Definition MRMatrix3.h:95
Definition MRMatrix3.h:24
Definition MRVector3.h:33