MeshLib Documentation
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{
9
12template <typename T>
14{
15 using ValueType = T;
16
18 T xx = 0, xy = 0, yy = 0;
19
20 constexpr SymMatrix2() noexcept = default;
21 template <typename U>
22 constexpr explicit SymMatrix2( const SymMatrix2<U> & m ) : xx( T( m.xx ) ), xy( T( m.xy ) ), yy( T( m.yy ) ) { }
23 static constexpr SymMatrix2 identity() noexcept { SymMatrix2 res; res.xx = res.yy = 1; return res; }
24 static constexpr SymMatrix2 diagonal( T diagVal ) noexcept { SymMatrix2 res; res.xx = res.yy = diagVal; return res; }
25
27 constexpr T trace() const noexcept { return xx + yy; }
29 constexpr T normSq() const noexcept { return sqr( xx ) + 2 * sqr( xy ) + sqr( yy ); }
31 constexpr T det() const noexcept;
33 constexpr SymMatrix2<T> inverse() const noexcept { return inverse( det() ); }
35 constexpr SymMatrix2<T> inverse( T det ) const noexcept;
36
37 SymMatrix2 & operator +=( const SymMatrix2<T> & b ) { xx += b.xx; xy += b.xy; yy += b.yy; return * this; }
38 SymMatrix2 & operator -=( const SymMatrix2<T> & b ) { xx -= b.xx; xy -= b.xy; yy -= b.yy; return * this; }
39 SymMatrix2 & operator *=( T b ) { xx *= b; xy *= b; yy *= b; return * this; }
41 {
42 if constexpr ( std::is_integral_v<T> )
43 { xx /= b; xy /= b; yy /= b; return * this; }
44 else
45 return *this *= ( 1 / b );
46 }
47
51 Vector2<T> eigens( Matrix2<T> * eigenvectors = nullptr ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
53 Vector2<T> eigenvector( T eigenvalue ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
55 Vector2<T> maxEigenvector() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
56
62 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> );
63};
64
67
69template <typename T>
70inline Vector2<T> operator *( const SymMatrix2<T> & a, const Vector2<T> & b )
71{
72 return
73 {
74 a.xx * b.x + a.xy * b.y,
75 a.xy * b.x + a.yy * b.y
76 };
77}
78
80template <typename T>
82{
83 SymMatrix2<T> res;
84 res.xx = a.x * a.x;
85 res.xy = a.x * a.y;
86 res.yy = a.y * a.y;
87 return res;
88}
89
91template <typename T>
92inline SymMatrix2<T> outerSquare( T k, const Vector2<T> & a )
93{
94 const auto ka = k * a;
95 SymMatrix2<T> res;
96 res.xx = ka.x * a.x;
97 res.xy = ka.x * a.y;
98 res.yy = ka.y * a.y;
99 return res;
100}
101
102template <typename T>
103inline bool operator ==( const SymMatrix2<T> & a, const SymMatrix2<T> & b )
104 { return a.xx = b.xx && a.xy = b.xy && a.yy = b.yy; }
105
106template <typename T>
107inline bool operator !=( const SymMatrix2<T> & a, const SymMatrix2<T> & b )
108 { return !( a == b ); }
109
110template <typename T>
111inline SymMatrix2<T> operator +( const SymMatrix2<T> & a, const SymMatrix2<T> & b )
112 { SymMatrix2<T> res{ a }; res += b; return res; }
113
114template <typename T>
115inline SymMatrix2<T> operator -( const SymMatrix2<T> & a, const SymMatrix2<T> & b )
116 { SymMatrix2<T> res{ a }; res -= b; return res; }
117
118template <typename T>
119inline SymMatrix2<T> operator *( T a, const SymMatrix2<T> & b )
120 { SymMatrix2<T> res{ b }; res *= a; return res; }
121
122template <typename T>
123inline SymMatrix2<T> operator *( const SymMatrix2<T> & b, T a )
124 { SymMatrix2<T> res{ b }; res *= a; return res; }
125
126template <typename T>
128 { b /= a; return b; }
129
130template <typename T>
131constexpr T SymMatrix2<T>::det() const noexcept
132{
133 return xx * yy - xy * xy;
134}
135
136template <typename T>
137constexpr SymMatrix2<T> SymMatrix2<T>::inverse( T det ) const noexcept
138{
139 if ( det == 0 )
140 return {};
141 SymMatrix2<T> res;
142 res.xx = yy / det;
143 res.xy = -xy / det;
144 res.yy = xx / det;
145 return res;
146}
147
148template <typename T>
149Vector2<T> SymMatrix2<T>::eigens( Matrix2<T> * eigenvectors ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
150{
151 //https://en.wikipedia.org/wiki/Eigenvalue_algorithm#2%C3%972_matrices
152 const auto tr = trace();
153 const auto q = tr / 2;
154 const auto p = std::sqrt( std::max( T(0), sqr( tr ) - 4 * det() ) ) / 2;
155 Vector2<T> eig;
156 if ( p <= std::abs( q ) * std::numeric_limits<T>::epsilon() )
157 {
158 // this is proportional to identity matrix
159 eig = { q, q };
160 if ( eigenvectors )
161 *eigenvectors = Matrix2<T>{};
162 return eig;
163 }
164 eig[0] = q - p;
165 eig[1] = q + p;
166 if ( eigenvectors )
167 {
168 const auto x = eigenvector( eig[0] ).normalized();
169 *eigenvectors = Matrix2<T>::fromRows( x, x.perpendicular() );
170 }
171 return eig;
172}
173
174template <typename T>
175Vector2<T> SymMatrix2<T>::eigenvector( T eigenvalue ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
176{
177 const Vector2<T> row0( xx - eigenvalue, xy );
178 const Vector2<T> row1( xy, yy - eigenvalue );
179 // not-repeating eigenvalue means that one of two rows is not zero
180 const T lsq0 = row0.lengthSq();
181 const T lsq1 = row1.lengthSq();
182 return lsq0 >= lsq1 ? row0.perpendicular() : row1.perpendicular();
183}
184
185template <typename T>
186Vector2<T> SymMatrix2<T>::maxEigenvector() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
187{
188 const auto tr = trace();
189 const auto q = tr / 2;
190 const auto p = std::sqrt( std::max( T(0), sqr( tr ) - 4 * det() ) ) / 2;
191 if ( p <= std::abs( q ) * std::numeric_limits<T>::epsilon() )
192 {
193 // this is proportional to identity matrix
194 return Vector2<T>( T(1), T(0) );
195 }
196 return eigenvector( q + p );
197}
198
199template <typename T>
200SymMatrix2<T> SymMatrix2<T>::pseudoinverse( T tol, int * rank, Vector2<T> * space ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
201{
202 SymMatrix2<T> res;
203 Matrix2<T> eigenvectors;
204 const auto eigenvalues = eigens( &eigenvectors );
205 const auto threshold = std::max( std::abs( eigenvalues[0] ), std::abs( eigenvalues[1] ) ) * tol;
206 int myRank = 0;
207 for ( int i = 0; i < 2; ++i )
208 {
209 if ( std::abs( eigenvalues[i] ) <= threshold )
210 continue;
211 res += outerSquare( 1 / eigenvalues[i], eigenvectors[i] );
212 ++myRank;
213 if ( space )
214 {
215 if ( myRank == 1 )
216 *space = eigenvectors[i];
217 else
218 *space = Vector2<T>{};
219 }
220 }
221 if ( rank )
222 *rank = myRank;
223 return res;
224}
225
227
228} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:29
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:342
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:276
Definition MRCameraOrientationPlugin.h:8
constexpr T sqr(T x) noexcept
Definition MRMesh/MRMeshFwd.h:600
MRMESH_CLASS Vector3< double > Matrix2
Definition MRMesh/MRMeshFwd.h:153
Color operator/(const Color &b, float a)
Definition MRColor.h:128
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:108
Definition MRMatrix2.h:13
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
Definition MRSymMatrix2.h:14
SymMatrix2 & operator-=(const SymMatrix2< T > &b)
Definition MRSymMatrix2.h:38
constexpr SymMatrix2() noexcept=default
constexpr SymMatrix2< T > inverse() const noexcept
computes inverse matrix
Definition MRSymMatrix2.h:33
T ValueType
Definition MRSymMatrix2.h:15
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRSymMatrix2.h:27
T xy
Definition MRSymMatrix2.h:18
static constexpr SymMatrix2 diagonal(T diagVal) noexcept
Definition MRSymMatrix2.h:24
T yy
Definition MRSymMatrix2.h:18
static constexpr SymMatrix2 identity() noexcept
Definition MRSymMatrix2.h:23
constexpr SymMatrix2< T > inverse(T det) const noexcept
computes inverse matrix given determinant of this
SymMatrix2 & operator*=(T b)
Definition MRSymMatrix2.h:39
SymMatrix2< T > outerSquare(T k, const Vector2< T > &a)
x = k * a * a^T
Definition MRSymMatrix2.h:92
SymMatrix2< T > outerSquare(const Vector2< T > &a)
x = a * a^T
Definition MRSymMatrix2.h:81
Vector2< T > operator*(const SymMatrix2< T > &a, const Vector2< T > &b)
x = a * b
Definition MRSymMatrix2.h:70
SymMatrix2 & operator/=(T b)
Definition MRSymMatrix2.h:40
constexpr T det() const noexcept
computes determinant of the matrix
SymMatrix2 & operator+=(const SymMatrix2< T > &b)
Definition MRSymMatrix2.h:37
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:29
T xx
zero matrix by default
Definition MRSymMatrix2.h:18
Definition MRVector2.h:18
T x
Definition MRVector2.h:24
T y
Definition MRVector2.h:24