MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMesh/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{
10
11#ifdef _MSC_VER
12#pragma warning(push)
13#pragma warning(disable: 4804) // unsafe use of type 'bool' in operation
14#pragma warning(disable: 4146) // unary minus operator applied to unsigned type, result still unsigned
15#endif
16
19template <typename T>
20struct Matrix3
21{
22 using ValueType = T;
24
26 Vector3<T> x{ 1, 0, 0 };
27 Vector3<T> y{ 0, 1, 0 };
28 Vector3<T> z{ 0, 0, 1 };
29
30 constexpr Matrix3() noexcept
31 {
32 static_assert( sizeof( Matrix3<ValueType> ) == 3 * sizeof( VectorType ), "Struct size invalid" );
33 }
35 constexpr Matrix3( const Vector3<T> & x, const Vector3<T> & y, const Vector3<T> & z ) : x( x ), y( y ), z( z ) { }
36
37 // 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
38 // when generating the bindings, and results in duplicate functions in C#.
39 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
40 constexpr explicit Matrix3( const Matrix3<U> & m ) : x( m.x ), y( m.y ), z( m.z ) { }
41
42 static constexpr Matrix3 zero() noexcept { return Matrix3( Vector3<T>(), Vector3<T>(), Vector3<T>() ); }
43 static constexpr Matrix3 identity() noexcept { return Matrix3(); }
45 static constexpr Matrix3 scale( T s ) noexcept { return Matrix3( { s, T(0), T(0) }, { T(0), s, T(0) }, { T(0), T(0), s } ); }
47 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 } ); }
48 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 } ); }
50 static constexpr Matrix3 rotation( const Vector3<T> & axis, T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> );
52 static constexpr Matrix3 rotation( const Vector3<T> & from, const Vector3<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> );
55 static constexpr Matrix3 rotationFromEuler( const Vector3<T> & eulerAngles ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
57 static constexpr Matrix3 approximateLinearRotationMatrixFromEuler( const Vector3<T> & eulerAngles ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
59 static constexpr Matrix3 fromRows( const Vector3<T> & x, const Vector3<T> & y, const Vector3<T> & z ) noexcept { return Matrix3( x, y, z ); }
62 static constexpr Matrix3 fromColumns( const Vector3<T> & x, const Vector3<T> & y, const Vector3<T> & z ) noexcept { return Matrix3( x, y, z ).transposed(); }
63
65 constexpr const Vector3<T> & operator []( int row ) const noexcept { return *( ( VectorType* )this + row ); }
66 constexpr Vector3<T> & operator []( int row ) noexcept { return *( ( VectorType* )this + row ); }
67
69 constexpr Vector3<T> col( int i ) const noexcept { return { x[i], y[i], z[i] }; }
70
72 constexpr T trace() const noexcept { return x.x + y.y + z.z; }
74 constexpr T normSq() const noexcept { return x.lengthSq() + y.lengthSq() + z.lengthSq(); }
75 constexpr auto norm() const noexcept
76 {
77 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
78 // Returning `auto` to not break on integral types.
79 using std::sqrt;
80 return sqrt( normSq() );
81 }
83 constexpr T det() const noexcept;
85 constexpr Matrix3<T> inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> );
87 constexpr Matrix3<T> transposed() const noexcept;
89 constexpr Vector3<T> toEulerAngles() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
90
91 struct QR
92 {
94 };
96 QR qr() const noexcept MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> );
97
98 [[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; }
99 [[nodiscard]] friend constexpr bool operator !=( const Matrix3<T> & a, const Matrix3<T> & b ) { return !( a == b ); }
100
101 // 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.
102
103 [[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 }; }
104 [[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 }; }
105 [[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 }; }
106 [[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 }; }
107 [[nodiscard]] friend constexpr auto operator /( Matrix3<T> b, T a ) -> Matrix3<decltype( std::declval<T>() / std::declval<T>() )>
108 {
109 if constexpr ( std::is_integral_v<T> )
110 return { b.x / a, b.y / a, b.z / a };
111 else
112 return b * ( 1 / a );
113 }
114
115 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; }
116 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; }
117 friend constexpr Matrix3<T> & operator *=( Matrix3<T> & a, T b ) { a.x *= b; a.y *= b; a.z *= b; return a; }
118 friend constexpr Matrix3<T> & operator /=( Matrix3<T> & a, T b )
119 {
120 if constexpr ( std::is_integral_v<T> )
121 { a.x /= b; a.y /= b; a.z /= b; return a; }
122 else
123 return a *= ( 1 / b );
124 }
125
127 [[nodiscard]] friend constexpr auto operator *( const Matrix3<T> & a, const Vector3<T> & b ) -> Vector3<decltype( dot( std::declval<Vector3<T>>(), std::declval<Vector3<T>>() ) )>
128 {
129 return { dot( a.x, b ), dot( a.y, b ), dot( a.z, b ) };
130 }
131
133 [[nodiscard]] friend constexpr auto operator *( const Matrix3<T> & a, const Matrix3<T> & b ) -> Matrix3<decltype( dot( std::declval<Vector3<T>>(), std::declval<Vector3<T>>() ) )>
134 {
135 Matrix3<decltype( dot( std::declval<Vector3<T>>(), std::declval<Vector3<T>>() ) )> res;
136 for ( int i = 0; i < 3; ++i )
137 for ( int j = 0; j < 3; ++j )
138 res[i][j] = dot( a[i], b.col(j) );
139 return res;
140 }
141
142 friend std::ostream& operator<<( std::ostream& s, const Matrix3& mat )
143 {
144 return s << mat.x << '\n' << mat.y << '\n' << mat.z << '\n';
145 }
146
147 friend std::istream& operator>>( std::istream& s, Matrix3& mat )
148 {
149 return s >> mat.x >> mat.y >> mat.z;
150 }
151};
152
155
157template <typename T>
158inline auto dot( const Matrix3<T> & a, const Matrix3<T> & b ) -> decltype( dot( a.x, b.x ) )
159{
160 return dot( a.x, b.x ) + dot( a.y, b.y ) + dot( a.z, b.z );
161}
162
164template <typename T>
165inline Matrix3<T> outer( const Vector3<T> & a, const Vector3<T> & b )
166{
167 return { a.x * b, a.y * b, a.z * b };
168}
169
170template <typename T>
171constexpr Matrix3<T> Matrix3<T>::rotation( const Vector3<T> & axis, T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
172{
173 // https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
174 auto u = axis.normalized();
175 T c = cos( angle );
176 T oc = 1 - c;
177 T s = sin( angle );
178 return {
179 { c + u.x * u.x * oc, u.x * u.y * oc - u.z * s, u.x * u.z * oc + u.y * s },
180 { u.y * u.x * oc + u.z * s, c + u.y * u.y * oc, u.y * u.z * oc - u.x * s },
181 { u.z * u.x * oc - u.y * s, u.z * u.y * oc + u.x * s, c + u.z * u.z * oc }
182 };
183}
184
185template <typename T>
186constexpr Matrix3<T> Matrix3<T>::rotation( const Vector3<T> & from, const Vector3<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::floating_point<T> )
187{
188 auto axis = cross( from, to );
189 if ( axis.lengthSq() > 0 )
190 return rotation( axis, angle( from, to ) );
191 if ( dot( from, to ) >= 0 )
192 return {}; // identity matrix
193 return rotation( cross( from, from.furthestBasisVector() ), T( PI ) );
194}
195
196template <typename T>
197constexpr Matrix3<T> Matrix3<T>::rotationFromEuler( const Vector3<T> & eulerAngles ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
198{
199 // https://www.geometrictools.com/Documentation/EulerAngles.pdf (36)
200 const auto cx = std::cos( eulerAngles.x );
201 const auto cy = std::cos( eulerAngles.y );
202 const auto cz = std::cos( eulerAngles.z );
203 const auto sx = std::sin( eulerAngles.x );
204 const auto sy = std::sin( eulerAngles.y );
205 const auto sz = std::sin( eulerAngles.z );
206 return {
207 { cy * cz, cz * sx * sy - cx * sz, cx * cz * sy + sx * sz },
208 { cy * sz, cx * cz + sx * sy * sz, -cz * sx + cx * sy * sz },
209 { -sy, cy * sx, cx * cy }
210 };
211}
212
213template <typename T>
214constexpr Matrix3<T> Matrix3<T>::approximateLinearRotationMatrixFromEuler( const Vector3<T> & eulerAngles ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
215{
216 const auto alpha = eulerAngles.x;
217 const auto beta = eulerAngles.y;
218 const auto gamma = eulerAngles.z;
219 return {
220 { T(1), -gamma, beta },
221 { gamma, T(1), -alpha },
222 { -beta, alpha, T(1) }
223 };
224}
225
226template <typename T>
227constexpr T Matrix3<T>::det() const noexcept
228{
229 return
230 x.x * ( y.y * z.z - y.z * z.y )
231 - x.y * ( y.x * z.z - y.z * z.x )
232 + x.z * ( y.x * z.y - y.y * z.x );
233}
234
235template <typename T>
236constexpr Matrix3<T> Matrix3<T>::inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
237{
238 auto det = this->det();
239 if ( det == 0 )
240 return {};
241 return Matrix3<T>
242 {
243 { y.y * z.z - y.z * z.y, x.z * z.y - x.y * z.z, x.y * y.z - x.z * y.y },
244 { y.z * z.x - y.x * z.z, x.x * z.z - x.z * z.x, x.z * y.x - x.x * y.z },
245 { y.x * z.y - y.y * z.x, x.y * z.x - x.x * z.y, x.x * y.y - x.y * y.x }
246 } / det;
247}
248
249template <typename T>
250constexpr Matrix3<T> Matrix3<T>::transposed() const noexcept
251{
252 return Matrix3<T>
253 {
254 { x.x, y.x, z.x },
255 { x.y, y.y, z.y },
256 { x.z, y.z, z.z }
257 };
258}
259
260template <typename T>
261constexpr Vector3<T> Matrix3<T>::toEulerAngles() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
262{
263 // https://stackoverflow.com/questions/15022630/how-to-calculate-the-angle-from-rotation-matrix
264 return {
265 std::atan2( z.y, z.z ),
266 std::atan2( -z.x, std::sqrt( z.y * z.y + z.z * z.z ) ),
267 std::atan2( y.x, x.x )
268 };
269}
270
271template <typename T>
272auto Matrix3<T>::qr() const noexcept -> QR MR_REQUIRES_IF_SUPPORTED( !std::is_integral_v<T> )
273{
274 // https://en.wikipedia.org/wiki/QR_decomposition#Computing_the_QR_decomposition
275 const auto a0 = col( 0 );
276 auto a1 = col( 1 );
277 auto a2 = col( 2 );
278 const auto r00 = a0.length();
279 const auto e0 = r00 > 0 ? a0 / r00 : Vector3<T>{};
280 const auto r01 = dot( e0, a1 );
281 const auto r02 = dot( e0, a2 );
282 a1 -= r01 * e0;
283 const auto r11 = a1.length();
284 const auto e1 = r11 > 0 ? a1 / r11 : Vector3<T>{};
285 const auto r12 = dot( e1, a2 );
286 a2 -= r02 * e0 + r12 * e1;
287 const auto r22 = a2.length();
288 const auto e2 = r22 > 0 ? a2 / r22 : Vector3<T>{};
289 return QR
290 {
291 Matrix3::fromColumns( e0, e1, e2 ),
292 Matrix3::fromRows( { r00, r01, r02 }, { T(0), r11, r12 }, { T(0), T(0), r22 } )
293 };
294}
295
297
298#ifdef _MSC_VER
299#pragma warning(pop)
300#endif
301
302} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
Definition MRCameraOrientationPlugin.h:8
float dot(Vector2f a, Vector2f b)
float cross(Vector2f a, Vector2f b)
returns 3 Euler angles, assuming this is a rotation matrix composed as follows: R=R(z)*R(y)*R(x)
Definition MRMesh/MRMatrix3.h:92
Matrix3 q
Definition MRMesh/MRMatrix3.h:93
Definition MRMesh/MRMatrix3.h:21
static constexpr Matrix3 scale(T sx, T sy, T sz) noexcept
returns a matrix that has its own scale along each axis
Definition MRMesh/MRMatrix3.h:47
Matrix3< T > outer(const Vector3< T > &a, const Vector3< T > &b)
x = a * b^T
Definition MRMesh/MRMatrix3.h:165
T ValueType
Definition MRMesh/MRMatrix3.h:22
static constexpr Matrix3 identity() noexcept
Definition MRMesh/MRMatrix3.h:43
friend constexpr auto operator*(T a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >() *std::declval< T >())>
Definition MRMesh/MRMatrix3.h:105
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 MRMesh/MRMatrix3.h:26
constexpr const Vector3< T > & operator[](int row) const noexcept
row access
Definition MRMesh/MRMatrix3.h:65
friend constexpr Matrix3< T > & operator-=(Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMesh/MRMatrix3.h:116
constexpr Matrix3(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z)
initializes matrix from its 3 rows
Definition MRMesh/MRMatrix3.h:35
constexpr T normSq() const noexcept
compute sum of squared matrix elements
Definition MRMesh/MRMatrix3.h:74
friend constexpr Matrix3< T > & operator*=(Matrix3< T > &a, T b)
Definition MRMesh/MRMatrix3.h:117
friend constexpr Matrix3< T > & operator+=(Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMesh/MRMatrix3.h:115
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Matrix3(const Matrix3< U > &m)
Definition MRMesh/MRMatrix3.h:39
constexpr auto norm() const noexcept
Definition MRMesh/MRMatrix3.h:75
constexpr Vector3< T > col(int i) const noexcept
column access
Definition MRMesh/MRMatrix3.h:69
static constexpr Matrix3 scale(T s) noexcept
returns a matrix that scales uniformly
Definition MRMesh/MRMatrix3.h:45
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRMesh/MRMatrix3.h:72
friend constexpr bool operator!=(const Matrix3< T > &a, const Matrix3< T > &b)
Definition MRMesh/MRMatrix3.h:99
static constexpr Matrix3 fromColumns(const Vector3< T > &x, const Vector3< T > &y, const Vector3< T > &z) noexcept
Definition MRMesh/MRMatrix3.h:62
constexpr Matrix3() noexcept
Definition MRMesh/MRMatrix3.h:30
friend std::istream & operator>>(std::istream &s, Matrix3 &mat)
Definition MRMesh/MRMatrix3.h:147
Vector3< T > y
Definition MRMesh/MRMatrix3.h:27
static constexpr Matrix3 zero() noexcept
Definition MRMesh/MRMatrix3.h:42
auto dot(const Matrix3< T > &a, const Matrix3< T > &b) -> decltype(dot(a.x, b.x))
double-dot product: x = a : b
Definition MRMesh/MRMatrix3.h:158
friend constexpr auto operator-(const Matrix3< T > &a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >() - std::declval< T >())>
Definition MRMesh/MRMatrix3.h:104
friend std::ostream & operator<<(std::ostream &s, const Matrix3 &mat)
Definition MRMesh/MRMatrix3.h:142
friend constexpr auto operator+(const Matrix3< T > &a, const Matrix3< T > &b) -> Matrix3< decltype(std::declval< T >()+std::declval< T >())>
Definition MRMesh/MRMatrix3.h:103
friend constexpr auto operator/(Matrix3< T > b, T a) -> Matrix3< decltype(std::declval< T >()/std::declval< T >())>
Definition MRMesh/MRMatrix3.h:107
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 MRMesh/MRMatrix3.h:59
Vector3< T > z
Definition MRMesh/MRMatrix3.h:28
friend constexpr Matrix3< T > & operator/=(Matrix3< T > &a, T b)
Definition MRMesh/MRMatrix3.h:118
static constexpr Matrix3 scale(const Vector3< T > &s) noexcept
Definition MRMesh/MRMatrix3.h:48
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