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