MCPcopy Create free account
hub / github.com/MeshInspector/MeshLib / Matrix2

Class Matrix2

source/MRMesh/MRMatrix2.h:20–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18/// \ingroup MatrixGroup
19template <typename T>
20struct Matrix2
21{
22 using ValueType = T;
23 using VectorType = Vector2<T>;
24
25 /// rows, identity matrix by default
26 Vector2<T> x{ 1, 0 };
27 Vector2<T> y{ 0, 1 };
28
29 constexpr Matrix2() noexcept
30 {
31 static_assert( sizeof( Matrix2<ValueType> ) == 2 * sizeof( VectorType ), "Struct size invalid" );
32 }
33 /// initializes matrix from its 2 rows
34 constexpr Matrix2( const Vector2<T> & x, const Vector2<T> & y ) : x( x ), y( y ) { }
35 template <typename U>
36 constexpr explicit Matrix2( const Matrix2<U> & m ) : x( m.x ), y( m.y ) { }
37 static constexpr Matrix2 zero() noexcept { return Matrix2( Vector2<T>(), Vector2<T>() ); }
38 static constexpr Matrix2 identity() noexcept { return Matrix2(); }
39 /// returns a matrix that scales uniformly
40 static constexpr Matrix2 scale( T s ) noexcept { return Matrix2( { s, T(0) }, { T(0), s } ); }
41 /// returns a matrix that has its own scale along each axis
42 static constexpr Matrix2 scale( T sx, T sy ) noexcept { return Matrix2( { sx, T(0) }, { T(0), sy } ); }
43 static constexpr Matrix2 scale( const Vector2<T> & s ) noexcept { return Matrix2( { s.x, T(0) }, { T(0), s.y } ); }
44 /// creates matrix representing rotation around origin on given angle
45 static constexpr Matrix2 rotation( T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
46 /// creates matrix representing rotation that after application to (from) makes (to) vector
47 static constexpr Matrix2 rotation( const Vector2<T> & from, const Vector2<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
48 /// constructs a matrix from its 2 rows
49 static constexpr Matrix2 fromRows( const Vector2<T> & x, const Vector2<T> & y ) noexcept { return Matrix2( x, y ); }
50 /// constructs a matrix from its 2 columns;
51 /// use this method to get the matrix that transforms basis vectors ( plusX, plusY ) into vectors ( x, y ) respectively
52 static constexpr Matrix2 fromColumns( const Vector2<T> & x, const Vector2<T> & y ) noexcept { return Matrix2( x, y ).transposed(); }
53
54 /// row access
55 constexpr const Vector2<T> & operator []( int row ) const noexcept { return *( ( VectorType* )this + row ); }
56 constexpr Vector2<T> & operator []( int row ) noexcept { return *( ( VectorType* )this + row ); }
57
58 /// column access
59 constexpr Vector2<T> col( int i ) const noexcept { return { x[i], y[i] }; }
60
61 /// computes trace of the matrix
62 constexpr T trace() const noexcept { return x.x + y.y; }
63 /// compute sum of squared matrix elements
64 constexpr T normSq() const noexcept { return x.lengthSq() + y.lengthSq(); }
65 constexpr auto norm() const noexcept
66 {
67 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
68 // Returning `auto` to not break on integral types.
69 using std::sqrt;
70 return sqrt( normSq() );
71 }
72 /// computes determinant of the matrix
73 constexpr T det() const noexcept;
74 /// computes inverse matrix
75 constexpr Matrix2<T> inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
76 /// computes transposed matrix
77 constexpr Matrix2<T> transposed() const noexcept;

Callers 5

zeroMethod · 0.85
identityMethod · 0.85
scaleMethod · 0.85
fromRowsMethod · 0.85
fromColumnsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected