MeshLib
 
Loading...
Searching...
No Matches
MRMatrix2.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector2.h"
4#include "MRConstants.h"
5
6namespace MR
7{
8
11template <typename T>
12struct Matrix2
13{
14 using ValueType = T;
16
18 Vector2<T> x{ 1, 0 };
19 Vector2<T> y{ 0, 1 };
20
21 constexpr Matrix2() noexcept = default;
23 constexpr Matrix2( const Vector2<T> & x, const Vector2<T> & y ) : x( x ), y( y ) { }
24 template <typename U>
25 constexpr explicit Matrix2( const Matrix2<U> & m ) : x( m.x ), y( m.y ) { }
26 static constexpr Matrix2 zero() noexcept { return Matrix2( Vector2<T>(), Vector2<T>() ); }
27 static constexpr Matrix2 identity() noexcept { return Matrix2(); }
29 static constexpr Matrix2 scale( T s ) noexcept { return Matrix2( { s, T(0) }, { T(0), s } ); }
31 static constexpr Matrix2 scale( T sx, T sy ) noexcept { return Matrix2( { sx, T(0) }, { T(0), sy } ); }
32 static constexpr Matrix2 scale( const Vector2<T> & s ) noexcept { return Matrix2( { s.x, T(0) }, { T(0), s.y } ); }
34 static constexpr Matrix2 rotation( T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
36 static constexpr Matrix2 rotation( const Vector2<T> & from, const Vector2<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
38 static constexpr Matrix2 fromRows( const Vector2<T> & x, const Vector2<T> & y ) noexcept { return Matrix2( x, y ); }
41 static constexpr Matrix2 fromColumns( const Vector2<T> & x, const Vector2<T> & y ) noexcept { return Matrix2( x, y ).transposed(); }
42
44 constexpr const Vector2<T> & operator []( int row ) const noexcept { return *( &x + row ); }
45 constexpr Vector2<T> & operator []( int row ) noexcept { return *( &x + row ); }
46
48 constexpr Vector2<T> col( int i ) const noexcept { return { x[i], y[i] }; }
49
51 constexpr T trace() const noexcept { return x.x + y.y; }
53 constexpr T normSq() const noexcept { return x.lengthSq() + y.lengthSq(); }
54 constexpr auto norm() const noexcept
55 {
56 // Calling `sqrt` this way to hopefully support boost.multiprecision numbers.
57 // Returning `auto` to not break on integral types.
58 using std::sqrt;
59 return sqrt( normSq() );
60 }
62 constexpr T det() const noexcept;
64 constexpr Matrix2<T> inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
66 constexpr Matrix2<T> transposed() const noexcept;
67
68 Matrix2 & operator +=( const Matrix2<T> & b ) { x += b.x; y += b.y; return * this; }
69 Matrix2 & operator -=( const Matrix2<T> & b ) { x -= b.x; y -= b.y; return * this; }
70 Matrix2 & operator *=( T b ) { x *= b; y *= b; return * this; }
72 {
73 if constexpr ( std::is_integral_v<T> )
74 { x /= b; y /= b; return * this; }
75 else
76 return *this *= ( 1 / b );
77 }
78};
79
82
84template <typename T>
85inline Vector2<T> operator *( const Matrix2<T> & a, const Vector2<T> & b )
86{
87 return { dot( a.x, b ), dot( a.y, b ) };
88}
89
91template <typename T>
92inline T dot( const Matrix2<T> & a, const Matrix2<T> & b )
93{
94 return dot( a.x, b.x ) + dot( a.y, b.y );
95}
96
98template <typename T>
99inline Matrix2<T> operator *( const Matrix2<T> & a, const Matrix2<T> & b )
100{
101 Matrix2<T> res;
102 for ( int i = 0; i < 2; ++i )
103 for ( int j = 0; j < 2; ++j )
104 res[i][j] = dot( a[i], b.col(j) );
105 return res;
106}
107
109template <typename T>
110inline Matrix2<T> outer( const Vector2<T> & a, const Vector2<T> & b )
111{
112 return { a.x * b, a.y * b };
113}
114
115template <typename T>
116inline bool operator ==( const Matrix2<T> & a, const Matrix2<T> & b )
117 { return a.x == b.x && a.y == b.y; }
118
119template <typename T>
120inline bool operator !=( const Matrix2<T> & a, const Matrix2<T> & b )
121 { return !( a == b ); }
122
123template <typename T>
124inline Matrix2<T> operator +( const Matrix2<T> & a, const Matrix2<T> & b )
125 { return { a.x + b.x, a.y + b.y }; }
126
127template <typename T>
128inline Matrix2<T> operator -( const Matrix2<T> & a, const Matrix2<T> & b )
129 { return { a.x - b.x, a.y - b.y }; }
130
131template <typename T>
132inline Matrix2<T> operator *( T a, const Matrix2<T> & b )
133 { return { a * b.x, a * b.y }; }
134
135template <typename T>
136inline Matrix2<T> operator *( const Matrix2<T> & b, T a )
137 { return { a * b.x, a * b.y }; }
138
139template <typename T>
140inline Matrix2<T> operator /( Matrix2<T> b, T a )
141 { b /= a; return b; }
142
143template <typename T>
144constexpr Matrix2<T> Matrix2<T>::rotation( T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
145{
146 T c = cos( angle );
147 T s = sin( angle );
148 return {
149 { c, -s },
150 { s, c }
151 };
152}
153
154template <typename T>
155constexpr Matrix2<T> Matrix2<T>::rotation( const Vector2<T> & from, const Vector2<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
156{
157 const auto x = cross( from, to );
158 if ( x > 0 )
159 return rotation( angle( from, to ) );
160 if ( x < 0 )
161 return rotation( -angle( from, to ) );
162 if ( dot( from, to ) >= 0 )
163 return {}; // identity matrix
164 return rotation( T( PI ) );
165}
166
167template <typename T>
168constexpr T Matrix2<T>::det() const noexcept
169{
170 return x.x * y.y - x.y * y.x;
171}
172
173template <typename T>
174constexpr Matrix2<T> Matrix2<T>::inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
175{
176 auto det = this->det();
177 if ( det == 0 )
178 return {};
179 return Matrix2<T>
180 {
181 { y.y, - x.y },
182 { - y.x, x.x }
183 } / det;
184}
185
186template <typename T>
187constexpr Matrix2<T> Matrix2<T>::transposed() const noexcept
188{
189 return Matrix2<T>
190 {
191 { x.x, y.x },
192 { x.y, y.y }
193 };
194}
195
197
198} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:26
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:325
MRMESH_API bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
bool operator!=(const SetBitIteratorT< T > &a, const SetBitIteratorT< T > &b)
Definition MRMesh/MRBitSet.h:259
constexpr auto dot(A a, A b)
Definition MRImGuiVectorOperators.h:129
Definition MRCameraOrientationPlugin.h:7
MRMESH_CLASS Vector3< double > Matrix2
Definition MRMesh/MRMeshFwd.h:152
Color operator/(const Color &b, float a)
Definition MRColor.h:128
Color operator*(float a, const Color &b)
Definition MRColor.h:118
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:108
Definition MRMatrix2.h:13
constexpr Matrix2(const Matrix2< U > &m)
Definition MRMatrix2.h:25
Vector2< T > x
rows, identity matrix by default
Definition MRMatrix2.h:18
static constexpr Matrix2 scale(T s) noexcept
returns a matrix that scales uniformly
Definition MRMatrix2.h:29
constexpr T normSq() const noexcept
compute sum of squared matrix elements
Definition MRMatrix2.h:53
Matrix2< T > outer(const Vector2< T > &a, const Vector2< T > &b)
x = a * b^T
Definition MRMatrix2.h:110
static constexpr Matrix2 static rotation(T angle) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix2 static rotation(const Vector2< T > &from, const Vector2< T > &to) noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix fromRows)(const Vector2< T > &x, const Vector2< T > &y) noexcept
creates matrix representing rotation around origin on given angle
Definition MRMatrix2.h:38
constexpr Vector2< T > col(int i) const noexcept
column access
Definition MRMatrix2.h:48
static constexpr Matrix2 identity() noexcept
Definition MRMatrix2.h:27
Vector2< T > y
Definition MRMatrix2.h:19
T ValueType
Definition MRMatrix2.h:14
constexpr T det() const noexcept
computes determinant of the matrix
static constexpr Matrix2 scale(const Vector2< T > &s) noexcept
Definition MRMatrix2.h:32
static constexpr Matrix2 scale(T sx, T sy) noexcept
returns a matrix that has its own scale along each axis
Definition MRMatrix2.h:31
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRMatrix2.h:51
constexpr const Vector2< T > & operator[](int row) const noexcept
row access
Definition MRMatrix2.h:44
Matrix2 & operator*=(T b)
Definition MRMatrix2.h:70
constexpr Matrix2() noexcept=default
T dot(const Matrix2< T > &a, const Matrix2< T > &b)
double-dot product: x = a : b
Definition MRMatrix2.h:92
Matrix2 & operator-=(const Matrix2< T > &b)
Definition MRMatrix2.h:69
constexpr Matrix2< T > inverse() const noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix2< T > transposed() const noexcept
computes inverse matrix
constexpr auto norm() const noexcept
Definition MRMatrix2.h:54
static constexpr Matrix2 zero() noexcept
Definition MRMatrix2.h:26
Matrix2 & operator/=(T b)
Definition MRMatrix2.h:71
static constexpr Matrix2 fromColumns(const Vector2< T > &x, const Vector2< T > &y) noexcept
Definition MRMatrix2.h:41
Definition MRVector2.h:18
T x
Definition MRVector2.h:24
T y
Definition MRVector2.h:24