MeshLib C++ Docs
Loading...
Searching...
No Matches
MRSymMatrix2.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector2.h"
4#include "MRMatrix2.h"
5#include <limits>
6
7namespace MR
8{
11
12
15template <typename T>
17{
18 using ValueType = T;
19
21 T xx = 0, xy = 0, yy = 0;
22
23 constexpr SymMatrix2() noexcept = default;
24
27 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
28 constexpr explicit SymMatrix2( const SymMatrix2<U> & m ) : xx( T( m.xx ) ), xy( T( m.xy ) ), yy( T( m.yy ) ) { }
29
30 static constexpr SymMatrix2 identity() noexcept { SymMatrix2 res; res.xx = res.yy = 1; return res; }
31 static constexpr SymMatrix2 diagonal( T diagVal ) noexcept { SymMatrix2 res; res.xx = res.yy = diagVal; return res; }
32
34 constexpr T trace() const noexcept { return xx + yy; }
36 constexpr T normSq() const noexcept { return sqr( xx ) + 2 * sqr( xy ) + sqr( yy ); }
38 constexpr T det() const noexcept;
40 constexpr SymMatrix2<T> inverse() const noexcept { return inverse( det() ); }
42 constexpr SymMatrix2<T> inverse( T det ) const noexcept;
43
44 SymMatrix2 & operator +=( const SymMatrix2<T> & b ) { xx += b.xx; xy += b.xy; yy += b.yy; return * this; }
45 SymMatrix2 & operator -=( const SymMatrix2<T> & b ) { xx -= b.xx; xy -= b.xy; yy -= b.yy; return * this; }
46 SymMatrix2 & operator *=( T b ) { xx *= b; xy *= b; yy *= b; return * this; }
48 {
49 if constexpr ( std::is_integral_v<T> )
50 { xx /= b; xy /= b; yy /= b; return * this; }
51 else
52 return *this *= ( 1 / b );
53 }
54
58 Vector2<T> eigens( Matrix2<T> * eigenvectors = nullptr ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
60 Vector2<T> eigenvector( T eigenvalue ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
62 Vector2<T> maxEigenvector() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
63
69 SymMatrix2<T> pseudoinverse( T tol = std::numeric_limits<T>::epsilon(), int * rank = nullptr, Vector2<T> * space = nullptr ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
70};
71
74
76template <typename T>
77inline Vector2<T> operator *( const SymMatrix2<T> & a, const Vector2<T> & b )
78{
79 return
80 {
81 a.xx * b.x + a.xy * b.y,
82 a.xy * b.x + a.yy * b.y
83 };
84}
85
87template <typename T>
89{
90 SymMatrix2<T> res;
91 res.xx = a.x * a.x;
92 res.xy = a.x * a.y;
93 res.yy = a.y * a.y;
94 return res;
95}
96
98template <typename T>
99inline SymMatrix2<T> outerSquare( T k, const Vector2<T> & a )
100{
101 const auto ka = k * a;
102 SymMatrix2<T> res;
103 res.xx = ka.x * a.x;
104 res.xy = ka.x * a.y;
105 res.yy = ka.y * a.y;
106 return res;
107}
108
109template <typename T>
110inline bool operator ==( const SymMatrix2<T> & a, const SymMatrix2<T> & b )
111 { return a.xx = b.xx && a.xy = b.xy && a.yy = b.yy; }
112
113template <typename T>
114inline bool operator !=( const SymMatrix2<T> & a, const SymMatrix2<T> & b )
115 { return !( a == b ); }
116
117template <typename T>
118inline SymMatrix2<T> operator +( const SymMatrix2<T> & a, const SymMatrix2<T> & b )
119 { SymMatrix2<T> res{ a }; res += b; return res; }
120
121template <typename T>
122inline SymMatrix2<T> operator -( const SymMatrix2<T> & a, const SymMatrix2<T> & b )
123 { SymMatrix2<T> res{ a }; res -= b; return res; }
124
125template <typename T>
126inline SymMatrix2<T> operator *( T a, const SymMatrix2<T> & b )
127 { SymMatrix2<T> res{ b }; res *= a; return res; }
128
129template <typename T>
130inline SymMatrix2<T> operator *( const SymMatrix2<T> & b, T a )
131 { SymMatrix2<T> res{ b }; res *= a; return res; }
132
133template <typename T>
135 { b /= a; return b; }
136
137template <typename T>
138constexpr T SymMatrix2<T>::det() const noexcept
139{
140 return xx * yy - xy * xy;
141}
142
143template <typename T>
144constexpr SymMatrix2<T> SymMatrix2<T>::inverse( T det ) const noexcept
145{
146 if ( det == 0 )
147 return {};
148 SymMatrix2<T> res;
149 res.xx = yy / det;
150 res.xy = -xy / det;
151 res.yy = xx / det;
152 return res;
153}
154
155template <typename T>
156Vector2<T> SymMatrix2<T>::eigens( Matrix2<T> * eigenvectors ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
157{
159 const auto tr = trace();
160 const auto q = tr / 2;
161 const auto p = std::sqrt( std::max( T(0), sqr( tr ) - 4 * det() ) ) / 2;
162 Vector2<T> eig;
163 if ( p <= std::abs( q ) * std::numeric_limits<T>::epsilon() )
164 {
166 eig = { q, q };
167 if ( eigenvectors )
168 *eigenvectors = Matrix2<T>{};
169 return eig;
170 }
171 eig[0] = q - p;
172 eig[1] = q + p;
173 if ( eigenvectors )
174 {
175 const auto x = eigenvector( eig[0] ).normalized();
176 *eigenvectors = Matrix2<T>::fromRows( x, x.perpendicular() );
177 }
178 return eig;
179}
180
181template <typename T>
182Vector2<T> SymMatrix2<T>::eigenvector( T eigenvalue ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
183{
184 const Vector2<T> row0( xx - eigenvalue, xy );
185 const Vector2<T> row1( xy, yy - eigenvalue );
187 const T lsq0 = row0.lengthSq();
188 const T lsq1 = row1.lengthSq();
189 return lsq0 >= lsq1 ? row0.perpendicular() : row1.perpendicular();
190}
191
192template <typename T>
193Vector2<T> SymMatrix2<T>::maxEigenvector() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
194{
195 const auto tr = trace();
196 const auto q = tr / 2;
197 const auto p = std::sqrt( std::max( T(0), sqr( tr ) - 4 * det() ) ) / 2;
198 if ( p <= std::abs( q ) * std::numeric_limits<T>::epsilon() )
199 {
201 return Vector2<T>( T(1), T(0) );
202 }
203 return eigenvector( q + p );
204}
205
206template <typename T>
207SymMatrix2<T> SymMatrix2<T>::pseudoinverse( T tol, int * rank, Vector2<T> * space ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
208{
209 SymMatrix2<T> res;
210 Matrix2<T> eigenvectors;
211 const auto eigenvalues = eigens( &eigenvectors );
212 const auto threshold = std::max( std::abs( eigenvalues[0] ), std::abs( eigenvalues[1] ) ) * tol;
213 int myRank = 0;
214 for ( int i = 0; i < 2; ++i )
215 {
216 if ( std::abs( eigenvalues[i] ) <= threshold )
217 continue;
218 res += outerSquare( 1 / eigenvalues[i], eigenvectors[i] );
219 ++myRank;
220 if ( space )
221 {
222 if ( myRank == 1 )
223 *space = eigenvectors[i];
224 else
225 *space = Vector2<T>{};
226 }
227 }
228 if ( rank )
229 *rank = myRank;
230 return res;
231}
232
234
235}
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:457
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...
SymMatrix2 & operator-=(const SymMatrix2< T > &b)
Definition MRSymMatrix2.h:45
constexpr SymMatrix2() noexcept=default
constexpr SymMatrix2< T > inverse() const noexcept
computes inverse matrix
Definition MRSymMatrix2.h:40
T ValueType
Definition MRSymMatrix2.h:18
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRSymMatrix2.h:34
T xy
Definition MRSymMatrix2.h:21
static constexpr SymMatrix2 diagonal(T diagVal) noexcept
Definition MRSymMatrix2.h:31
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:52
T yy
Definition MRSymMatrix2.h:21
static constexpr SymMatrix2 identity() noexcept
Definition MRSymMatrix2.h:30
constexpr SymMatrix2< T > inverse(T det) const noexcept
computes inverse matrix given determinant of this
bool operator!=(const Color &a, const Color &b)
Definition MRColor.h:104
SymMatrix2 & operator*=(T b)
Definition MRSymMatrix2.h:46
Color operator/(const Color &b, float a)
Definition MRColor.h:129
SymMatrix2< T > outerSquare(T k, const Vector2< T > &a)
x = k * a * a^T
Definition MRSymMatrix2.h:99
SymMatrix2< T > outerSquare(const Vector2< T > &a)
x = a * a^T
Definition MRSymMatrix2.h:88
Vector2< T > operator*(const SymMatrix2< T > &a, const Vector2< T > &b)
x = a * b
Definition MRSymMatrix2.h:77
SymMatrix2 & operator/=(T b)
Definition MRSymMatrix2.h:47
constexpr T det() const noexcept
computes determinant of the matrix
SymMatrix2 & operator+=(const SymMatrix2< T > &b)
Definition MRSymMatrix2.h:44
constexpr T normSq() const noexcept
computes the squared norm of the matrix, which is equal to the sum of 4 squared elements
Definition MRSymMatrix2.h:36
T xx
zero matrix by default
Definition MRSymMatrix2.h:21
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:109
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRMatrix2.h:24
Definition MRSymMatrix2.h:17
Definition MRVector2.h:29
T x
Definition MRVector2.h:35
T y
Definition MRVector2.h:35