14#pragma warning(disable: 4804)
15#pragma warning(disable: 4146)
50 template <MR_SAME_TYPE_TEMPLATE_PARAM(T, TT)>
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 } ); }
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
88 T
det() const noexcept;
99 constexpr T*
data() {
return (T*) (&
x); };
100 constexpr const T*
data()
const {
return (T*) (&
x); };
102 template <MR_SAME_TYPE_TEMPLATE_PARAM(T, TT)>
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 );
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;
131 if constexpr ( std::is_integral_v<T> )
132 return { b.x / a, b.y / a, b.z / a, b.w / a };
134 return b * ( 1 / a );
142 if constexpr ( std::is_integral_v<T> )
143 { a.
x /= b; a.
y /= b; a.
z /= b; a.
w /= b;
return a; }
145 return a *= ( 1 / b );
151 return {
dot( a.x, b ),
dot( a.y, b ),
dot( a.z, b ),
dot( a.w, b ) };
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) );
166 return s << mat.
x <<
'\n' << mat.
y <<
'\n' << mat.
z <<
'\n' << mat.
w <<
'\n';
171 return s >> mat.
x >> mat.
y >> mat.
z >> mat.
w;
182 return dot( a.x, b.x ) + dot( a.y, b.y ) + dot( a.z, b.z ) + dot( a.w, b.w );
188 return ( *
this *
Vector4<T>{ b.
x, b.y, b.z, T(1) } ).proj3d();
195 return { a.
x * b, a.
y * b, a.
z * b, a.
w * b };
203 for (
int m = 0; m < 4; m++ )
207 auto & row = res[nrow++];
209 for (
int n = 0; n < 4; n++ )
213 row[ncol++] = ( *this )[m][n];
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();
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 },
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];
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];
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] -
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] +
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];
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];
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] +
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] -
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] -
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] +
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] -
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] +
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] -
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] +
348 inv[2][3] = -m[0] * m[5] * m[11] +
350 m[4] * m[1] * m[11] -
355 inv[3][3] = m[0] * m[5] * m[10] -
357 m[4] * m[1] * m[10] +
362 det = m[0] * inv[0][0] + m[1] * inv[1][0] + m[2] * inv[2][0] + m[3] * inv[3][0];
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;
398 return Vector3<T>{ x.w, y.w, z.w };
404 x.w = t.x; y.w = t.y; z.w = t.z;
#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