MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMatrix4.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector4.h"
4#include <limits>
5#include <cassert>
6
7namespace MR
8{
9
10#ifdef _MSC_VER
11#pragma warning(push)
12#pragma warning(disable: 4804) // unsafe use of type 'bool' in operation
13#pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
14#endif
15
18template <typename T>
19struct Matrix4
20{
21 using ValueType = T;
23
25 Vector4<T> x{ 1, 0, 0, 0 };
26 Vector4<T> y{ 0, 1, 0, 0 };
27 Vector4<T> z{ 0, 0, 1, 0 };
28 Vector4<T> w{ 0, 0, 0, 1 };
29
30 constexpr Matrix4() noexcept
31 {
32 static_assert( sizeof( Matrix4<ValueType> ) == 4 * sizeof( VectorType ), "Struct size invalid" );
33 }
35 constexpr Matrix4( const Vector4<T>& x, const Vector4<T>& y, const Vector4<T>& z, const Vector4<T>& w ) : x( x ), y( y ), z( z ), w( w ) { }
36
38 constexpr Matrix4( const Matrix3<T>& r, const Vector3<T>& t )
39 {
40 x = Vector4<T>( r.x.x, r.x.y, r.x.z, t.x );
41 y = Vector4<T>( r.y.x, r.y.y, r.y.z, t.y );
42 z = Vector4<T>( r.z.x, r.z.y, r.z.z, t.z );
43 w = Vector4<T>( 0, 0, 0, 1 );
44 }
45
46 // Currently `AffineXf3<long long>` doesn't seem to compile, so we disable this constructor for `Matrix4<long long>`, because otherwise
47 // mrbind instantiates the entire `AffineXf3<long long>` and chokes on it.
48 template <MR_SAME_TYPE_TEMPLATE_PARAM(T, TT)>
49 constexpr Matrix4( const AffineXf3<TT>& xf ) MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> ) : Matrix4( xf.A, xf.b ) {}
50
51 template <typename U>
52 constexpr explicit Matrix4( const Matrix4<U> & m ) : x( m.x ), y( m.y ), z( m.z ), w( m.w ) { }
53 static constexpr Matrix4 zero() noexcept { return Matrix4( Vector4<T>(), Vector4<T>(), Vector4<T>(), Vector4<T>() ); }
54 static constexpr Matrix4 identity() noexcept { return Matrix4(); }
56 static constexpr Matrix4 scale( T s ) noexcept { return Matrix4( { s, T(0), T(0), T(0) }, { T(0), s, T(0), T(0) }, { T(0), T(0), s, T(0) }, { T(0), T(0), T(0), s } ); }
57
59 constexpr const T& operator ()( int row, int col ) const noexcept { return operator[]( row )[col]; }
60 constexpr T& operator ()( int row, int col ) noexcept { return operator[]( row )[col]; }
61
63 constexpr const Vector4<T> & operator []( int row ) const noexcept { return *( ( VectorType* )this + row ); }
64 constexpr Vector4<T> & operator []( int row ) noexcept { return *( ( VectorType* )this + row ); }
65
67 constexpr Vector4<T> col( int i ) const noexcept { return { x[i], y[i], z[i], w[i] }; }
68
70 constexpr T trace() const noexcept { return x.x + y.y + z.z + w.w; }
72 constexpr T normSq() const noexcept { return x.lengthSq() + y.lengthSq() + z.lengthSq() + w.lengthSq(); }
73 constexpr auto norm() const noexcept
74 {
75 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
76 // Returning `auto` to not break on integral types.
77 using std::sqrt;
78 return sqrt( normSq() );
79 }
81 Matrix3<T> submatrix3( int i, int j ) const noexcept;
83 T det() const noexcept;
85 constexpr Matrix4<T> inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> );
87 constexpr Matrix4<T> transposed() const noexcept;
88
89 constexpr Matrix3<T> getRotation() const noexcept;
90 void setRotation( const Matrix3<T>& rot) noexcept;
91 constexpr Vector3<T> getTranslation() const noexcept;
92 void setTranslation( const Vector3<T>& t ) noexcept;
93
94 constexpr T* data() { return (T*) (&x); };
95 constexpr const T* data() const { return (T*) (&x); };
96
97 template <MR_SAME_TYPE_TEMPLATE_PARAM(T, TT)>
98 operator AffineXf3<TT>() const MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
99 {
100 assert( std::abs( w.x ) < std::numeric_limits<T>::epsilon() * 1000 );
101 assert( std::abs( w.y ) < std::numeric_limits<T>::epsilon() * 1000 );
102 assert( std::abs( w.z ) < std::numeric_limits<T>::epsilon() * 1000 );
103 assert( std::abs( 1 - w.w ) < std::numeric_limits<T>::epsilon() * 1000 );
104 AffineXf3<T> res;
105 res.A.x.x = x.x; res.A.x.y = x.y; res.A.x.z = x.z; res.b.x = x.w;
106 res.A.y.x = y.x; res.A.y.y = y.y; res.A.y.z = y.z; res.b.y = y.w;
107 res.A.z.x = z.x; res.A.z.y = z.y; res.A.z.z = z.z; res.b.z = z.w;
108 return res;
109 }
110
113 Vector3<T> operator ()( const Vector3<T> & b ) const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> );
114
115 [[nodiscard]] friend constexpr bool operator ==( const Matrix4<T> & a, const Matrix4<T> & b ) { return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; }
116 [[nodiscard]] friend constexpr bool operator !=( const Matrix4<T> & a, const Matrix4<T> & b ) { return !( a == b ); }
117
118 // NOTE: We use `std::declval()` in the operators below because libclang 18 in our binding generator is bugged and chokes on decltyping `a.x` and such. TODO fix this when we update libclang.
119
120 [[nodiscard]] friend constexpr auto operator +( const Matrix4<T> & a, const Matrix4<T> & b ) -> Matrix4<decltype( std::declval<T>() + std::declval<T>() )> { return { a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w }; }
121 [[nodiscard]] friend constexpr auto operator -( const Matrix4<T> & a, const Matrix4<T> & b ) -> Matrix4<decltype( std::declval<T>() - std::declval<T>() )> { return { a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w }; }
122 [[nodiscard]] friend constexpr auto operator *( T a, const Matrix4<T> & b ) -> Matrix4<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y, a * b.z, a * b.w }; }
123 [[nodiscard]] friend constexpr auto operator *( const Matrix4<T> & b, T a ) -> Matrix4<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y, a * b.z, a * b.z }; }
124 [[nodiscard]] friend constexpr auto operator /( Matrix4<T> b, T a ) -> Matrix4<decltype( std::declval<T>() / std::declval<T>() )>
125 {
126 if constexpr ( std::is_integral_v<T> )
127 return { b.x / a, b.y / a, b.z / a, b.w / a };
128 else
129 return b * ( 1 / a );
130 }
131
132 friend constexpr Matrix4<T> & operator +=( Matrix4<T> & a, const Matrix4<T> & b ) { a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; return a; }
133 friend constexpr Matrix4<T> & operator -=( Matrix4<T> & a, const Matrix4<T> & b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; return a; }
134 friend constexpr Matrix4<T> & operator *=( Matrix4<T> & a, T b ) { a.x *= b; a.y *= b; a.z *= b; a.w *= b; return a; }
135 friend constexpr Matrix4<T> & operator /=( Matrix4<T> & a, T b )
136 {
137 if constexpr ( std::is_integral_v<T> )
138 { a.x /= b; a.y /= b; a.z /= b; a.w /= b; return a; }
139 else
140 return a *= ( 1 / b );
141 }
142
144 [[nodiscard]] friend constexpr auto operator *( const Matrix4<T> & a, const Vector4<T> & b ) -> Vector4<decltype( dot( std::declval<Vector4<T>>(), std::declval<Vector4<T>>() ) )>
145 {
146 return { dot( a.x, b ), dot( a.y, b ), dot( a.z, b ), dot( a.w, b ) };
147 }
148
150 [[nodiscard]] friend constexpr auto operator *( const Matrix4<T> & a, const Matrix4<T> & b ) -> Matrix4<decltype( dot( std::declval<Vector4<T>>(), std::declval<Vector4<T>>() ) )>
151 {
152 Matrix4<decltype( dot( std::declval<Vector4<T>>(), std::declval<Vector4<T>>() ) )> res;
153 for ( int i = 0; i < 4; ++i )
154 for ( int j = 0; j < 4; ++j )
155 res[i][j] = dot( a[i], b.col(j) );
156 return res;
157 }
158};
159
162
164template <typename T>
165inline auto dot( const Matrix4<T> & a, const Matrix4<T> & b ) -> decltype( dot( a.x, b.x ) )
166{
167 return dot( a.x, b.x ) + dot( a.y, b.y ) + dot( a.z, b.z ) + dot( a.w, b.w );
168}
169
170template <typename T>
171inline Vector3<T> Matrix4<T>::operator ()( const Vector3<T> & b ) const MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
172{
173 return ( *this * Vector4<T>{ b.x, b.y, b.z, T(1) } ).proj3d();
174}
175
177template <typename T>
178inline Matrix4<T> outer( const Vector4<T> & a, const Vector4<T> & b )
179{
180 return { a.x * b, a.y * b, a.z * b, a.w * b };
181}
182
183template <typename T>
184Matrix3<T> Matrix4<T>::submatrix3( int i, int j ) const noexcept
185{
186 Matrix3<T> res;
187 int nrow = 0;
188 for ( int m = 0; m < 4; m++ )
189 {
190 if ( m == i )
191 continue;
192 auto & row = res[nrow++];
193 int ncol = 0;
194 for ( int n = 0; n < 4; n++ )
195 {
196 if ( n == j )
197 continue;
198 row[ncol++] = ( *this )[m][n];
199 }
200 assert( ncol == 3 );
201 }
202 assert( nrow == 3 );
203 return res;
204}
205
206template <typename T>
207T Matrix4<T>::det() const noexcept
208{
209 return
210 x.x * submatrix3( 0, 0 ).det()
211 - x.y * submatrix3( 0, 1 ).det()
212 + x.z * submatrix3( 0, 2 ).det()
213 - x.w * submatrix3( 0, 3 ).det();
214}
215
216template <typename T>
217constexpr Matrix4<T> Matrix4<T>::transposed() const noexcept
218{
219 return Matrix4<T>
220 {
221 { x.x, y.x, z.x, w.x },
222 { x.y, y.y, z.y, w.y },
223 { x.z, y.z, z.z, w.z },
224 { x.w, y.w, z.w, w.w },
225 };
226}
227
228template <typename T>
229constexpr Matrix4<T> Matrix4<T>::inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
230{
231 Matrix4<T> inv;
232 T* m = (T*) (&x);
233 T det;
234
235 inv[0][0] = m[5] * m[10] * m[15] -
236 m[5] * m[11] * m[14] -
237 m[9] * m[6] * m[15] +
238 m[9] * m[7] * m[14] +
239 m[13] * m[6] * m[11] -
240 m[13] * m[7] * m[10];
241
242 inv[1][0] = -m[4] * m[10] * m[15] +
243 m[4] * m[11] * m[14] +
244 m[8] * m[6] * m[15] -
245 m[8] * m[7] * m[14] -
246 m[12] * m[6] * m[11] +
247 m[12] * m[7] * m[10];
248
249 inv[2][0] = m[4] * m[9] * m[15] -
250 m[4] * m[11] * m[13] -
251 m[8] * m[5] * m[15] +
252 m[8] * m[7] * m[13] +
253 m[12] * m[5] * m[11] -
254 m[12] * m[7] * m[9];
255
256 inv[3][0] = -m[4] * m[9] * m[14] +
257 m[4] * m[10] * m[13] +
258 m[8] * m[5] * m[14] -
259 m[8] * m[6] * m[13] -
260 m[12] * m[5] * m[10] +
261 m[12] * m[6] * m[9];
262
263 inv[0][1] = -m[1] * m[10] * m[15] +
264 m[1] * m[11] * m[14] +
265 m[9] * m[2] * m[15] -
266 m[9] * m[3] * m[14] -
267 m[13] * m[2] * m[11] +
268 m[13] * m[3] * m[10];
269
270 inv[1][1] = m[0] * m[10] * m[15] -
271 m[0] * m[11] * m[14] -
272 m[8] * m[2] * m[15] +
273 m[8] * m[3] * m[14] +
274 m[12] * m[2] * m[11] -
275 m[12] * m[3] * m[10];
276
277 inv[2][1] = -m[0] * m[9] * m[15] +
278 m[0] * m[11] * m[13] +
279 m[8] * m[1] * m[15] -
280 m[8] * m[3] * m[13] -
281 m[12] * m[1] * m[11] +
282 m[12] * m[3] * m[9];
283
284 inv[3][1] = m[0] * m[9] * m[14] -
285 m[0] * m[10] * m[13] -
286 m[8] * m[1] * m[14] +
287 m[8] * m[2] * m[13] +
288 m[12] * m[1] * m[10] -
289 m[12] * m[2] * m[9];
290
291 inv[0][2] = m[1] * m[6] * m[15] -
292 m[1] * m[7] * m[14] -
293 m[5] * m[2] * m[15] +
294 m[5] * m[3] * m[14] +
295 m[13] * m[2] * m[7] -
296 m[13] * m[3] * m[6];
297
298 inv[1][2] = -m[0] * m[6] * m[15] +
299 m[0] * m[7] * m[14] +
300 m[4] * m[2] * m[15] -
301 m[4] * m[3] * m[14] -
302 m[12] * m[2] * m[7] +
303 m[12] * m[3] * m[6];
304
305 inv[2][2] = m[0] * m[5] * m[15] -
306 m[0] * m[7] * m[13] -
307 m[4] * m[1] * m[15] +
308 m[4] * m[3] * m[13] +
309 m[12] * m[1] * m[7] -
310 m[12] * m[3] * m[5];
311
312 inv[3][2] = -m[0] * m[5] * m[14] +
313 m[0] * m[6] * m[13] +
314 m[4] * m[1] * m[14] -
315 m[4] * m[2] * m[13] -
316 m[12] * m[1] * m[6] +
317 m[12] * m[2] * m[5];
318
319 inv[0][3] = -m[1] * m[6] * m[11] +
320 m[1] * m[7] * m[10] +
321 m[5] * m[2] * m[11] -
322 m[5] * m[3] * m[10] -
323 m[9] * m[2] * m[7] +
324 m[9] * m[3] * m[6];
325
326 inv[1][3] = m[0] * m[6] * m[11] -
327 m[0] * m[7] * m[10] -
328 m[4] * m[2] * m[11] +
329 m[4] * m[3] * m[10] +
330 m[8] * m[2] * m[7] -
331 m[8] * m[3] * m[6];
332
333 inv[2][3] = -m[0] * m[5] * m[11] +
334 m[0] * m[7] * m[9] +
335 m[4] * m[1] * m[11] -
336 m[4] * m[3] * m[9] -
337 m[8] * m[1] * m[7] +
338 m[8] * m[3] * m[5];
339
340 inv[3][3] = m[0] * m[5] * m[10] -
341 m[0] * m[6] * m[9] -
342 m[4] * m[1] * m[10] +
343 m[4] * m[2] * m[9] +
344 m[8] * m[1] * m[6] -
345 m[8] * m[2] * m[5];
346
347 det = m[0] * inv[0][0] + m[1] * inv[1][0] + m[2] * inv[2][0] + m[3] * inv[3][0];
348
349 if( det == 0 )
350 {
351 // impossible to invert singular matrix
352 assert( false );
353 return Matrix4<T>();
354 }
355
356 inv /= det;
357
358 return inv;
359}
360
361template <typename T>
362constexpr Matrix3<T> Matrix4<T>::getRotation() const noexcept
363{
364 return Matrix3<T>
365 {
366 { x.x, x.y, x.z },
367 { y.x, y.y, y.z },
368 { z.x, z.y, z.z }
369 };
370}
371
372template <typename T>
373void Matrix4<T>::setRotation( const Matrix3<T>& rot ) noexcept
374{
375 x.x = rot.x.x; x.y = rot.x.y; x.z = rot.x.z;
376 y.x = rot.y.x; y.y = rot.y.y; y.z = rot.y.z;
377 z.x = rot.z.x; z.y = rot.z.y; z.z = rot.z.z;
378}
379
380template <typename T>
381constexpr Vector3<T> Matrix4<T>::getTranslation() const noexcept
382{
383 return Vector3<T>{ x.w, y.w, z.w };
384}
385
386template <typename T>
387void Matrix4<T>::setTranslation( const Vector3<T>& t ) noexcept
388{
389 x.w = t.x; y.w = t.y; z.w = t.z;
390}
391
393
394#ifdef _MSC_VER
395#pragma warning(pop)
396#endif
397
398} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
Definition MRCameraOrientationPlugin.h:8
AffineXf< Vector3< T > > AffineXf3
Definition MRMesh/MRMeshFwd.h:306
Definition MRMesh/MRAffineXf.h:21
V b
Definition MRMesh/MRAffineXf.h:26
M A
Definition MRMesh/MRAffineXf.h:25
Definition MRMesh/MRMatrix3.h:19
Vector3< T > x
rows, identity matrix by default
Definition MRMesh/MRMatrix3.h:24
Vector3< T > y
Definition MRMesh/MRMatrix3.h:25
Vector3< T > z
Definition MRMesh/MRMatrix3.h:26
Definition MRMatrix4.h:20
friend constexpr bool operator!=(const Matrix4< T > &a, const Matrix4< T > &b)
Definition MRMatrix4.h:116
Vector4< T > z
Definition MRMatrix4.h:27
Vector3< T > operator()(const Vector3< T > &b) const MR_REQUIRES_IF_SUPPORTED(!std friend constexpr bool operator==(const Matrix4< T > &a, const Matrix4< T > &b)
Definition MRMatrix4.h:115
constexpr T * data()
Definition MRMatrix4.h:94
T ValueType
Definition MRMatrix4.h:21
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRMatrix4.h:70
void setTranslation(const Vector3< T > &t) noexcept
friend constexpr auto operator*(T a, const Matrix4< T > &b) -> Matrix4< decltype(std::declval< T >() *std::declval< T >())>
Definition MRMatrix4.h:122
Matrix3< T > submatrix3(int i, int j) const noexcept
computes submatrix of the matrix with excluded i-th row and j-th column
static constexpr Matrix4 zero() noexcept
Definition MRMatrix4.h:53
constexpr T normSq() const noexcept
compute sum of squared matrix elements
Definition MRMatrix4.h:72
constexpr const T & operator()(int row, int col) const noexcept
element access
Definition MRMatrix4.h:59
Vector4< T > y
Definition MRMatrix4.h:26
static constexpr Matrix4 scale(T s) noexcept
returns a matrix that scales uniformly
Definition MRMatrix4.h:56
void setRotation(const Matrix3< T > &rot) noexcept
constexpr Matrix3< T > getRotation() const noexcept
constexpr Matrix4(const Matrix3< T > &r, const Vector3< T > &t)
construct from rotation matrix and translation vector
Definition MRMatrix4.h:38
friend constexpr auto operator/(Matrix4< T > b, T a) -> Matrix4< decltype(std::declval< T >()/std::declval< T >())>
Definition MRMatrix4.h:124
T det() const noexcept
computes determinant of the matrix
Vector4< T > w
Definition MRMatrix4.h:28
constexpr auto norm() const noexcept
Definition MRMatrix4.h:73
constexpr Matrix4(const AffineXf3< TT > &xf) MR_REQUIRES_IF_SUPPORTED(std
Definition MRMatrix4.h:49
constexpr Matrix4< T > inverse() const noexcept MR_REQUIRES_IF_SUPPORTED(!std constexpr Matrix4< T > transposed() const noexcept
computes inverse matrix
auto dot(const Matrix4< T > &a, const Matrix4< T > &b) -> decltype(dot(a.x, b.x))
double-dot product: x = a : b
Definition MRMatrix4.h:165
constexpr Matrix4() noexcept
Definition MRMatrix4.h:30
friend constexpr auto operator+(const Matrix4< T > &a, const Matrix4< T > &b) -> Matrix4< decltype(std::declval< T >()+std::declval< T >())>
Definition MRMatrix4.h:120
constexpr Matrix4(const Vector4< T > &x, const Vector4< T > &y, const Vector4< T > &z, const Vector4< T > &w)
initializes matrix from 4 row-vectors
Definition MRMatrix4.h:35
constexpr Matrix4(const Matrix4< U > &m)
Definition MRMatrix4.h:52
constexpr const Vector4< T > & operator[](int row) const noexcept
row access
Definition MRMatrix4.h:63
friend constexpr Matrix4< T > & operator+=(Matrix4< T > &a, const Matrix4< T > &b)
Definition MRMatrix4.h:132
constexpr Vector4< T > col(int i) const noexcept
column access
Definition MRMatrix4.h:67
friend constexpr Matrix4< T > & operator*=(Matrix4< T > &a, T b)
Definition MRMatrix4.h:134
friend constexpr Matrix4< T > & operator-=(Matrix4< T > &a, const Matrix4< T > &b)
Definition MRMatrix4.h:133
friend constexpr auto operator-(const Matrix4< T > &a, const Matrix4< T > &b) -> Matrix4< decltype(std::declval< T >() - std::declval< T >())>
Definition MRMatrix4.h:121
constexpr const T * data() const
Definition MRMatrix4.h:95
static constexpr Matrix4 identity() noexcept
Definition MRMatrix4.h:54
friend constexpr Matrix4< T > & operator/=(Matrix4< T > &a, T b)
Definition MRMatrix4.h:135
constexpr Vector3< T > getTranslation() const noexcept
Vector4< T > x
rows, identity matrix by default
Definition MRMatrix4.h:25
Matrix4< T > outer(const Vector4< T > &a, const Vector4< T > &b)
x = a * b^T
Definition MRMatrix4.h:178
Definition MRMesh/MRVector3.h:29
T x
Definition MRMesh/MRVector3.h:35
T y
Definition MRMesh/MRVector3.h:35
T z
Definition MRMesh/MRVector3.h:35
Definition MRVector4.h:22
T y
Definition MRVector4.h:28
T z
Definition MRVector4.h:28
T x
Definition MRVector4.h:28
T w
Definition MRVector4.h:28