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 constexpr SymMatrix2 & operator *=( T b ) { xx *= b; xy *= b; yy *= b; return * this; }
47 constexpr SymMatrix2 & operator /=( T b )
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;
150 res.xy = -xy;
151 res.yy = xx;
152 res /= det;
153 return res;
154}
155
156template <typename T>
157Vector2<T> SymMatrix2<T>::eigens( Matrix2<T> * eigenvectors ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
158{
160 const auto tr = trace();
161 const auto q = tr / 2;
162 const auto p = std::sqrt( std::max( T(0), sqr( tr ) - 4 * det() ) ) / 2;
163 Vector2<T> eig;
164 if ( p <= std::abs( q ) * std::numeric_limits<T>::epsilon() )
165 {
167 eig = { q, q };
168 if ( eigenvectors )
169 *eigenvectors = Matrix2<T>{};
170 return eig;
171 }
172 eig[0] = q - p;
173 eig[1] = q + p;
174 if ( eigenvectors )
175 {
176 const auto x = eigenvector( eig[0] ).normalized();
177 *eigenvectors = Matrix2<T>::fromRows( x, x.perpendicular() );
178 }
179 return eig;
180}
181
182template <typename T>
183Vector2<T> SymMatrix2<T>::eigenvector( T eigenvalue ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
184{
185 const Vector2<T> row0( xx - eigenvalue, xy );
186 const Vector2<T> row1( xy, yy - eigenvalue );
188 const T lsq0 = row0.lengthSq();
189 const T lsq1 = row1.lengthSq();
190 return lsq0 >= lsq1 ? row0.perpendicular() : row1.perpendicular();
191}
192
193template <typename T>
194Vector2<T> SymMatrix2<T>::maxEigenvector() const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
195{
196 const auto tr = trace();
197 const auto q = tr / 2;
198 const auto p = std::sqrt( std::max( T(0), sqr( tr ) - 4 * det() ) ) / 2;
199 if ( p <= std::abs( q ) * std::numeric_limits<T>::epsilon() )
200 {
202 return Vector2<T>( T(1), T(0) );
203 }
204 return eigenvector( q + p );
205}
206
207template <typename T>
208SymMatrix2<T> SymMatrix2<T>::pseudoinverse( T tol, int * rank, Vector2<T> * space ) const MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
209{
213 if ( xx > 0 )
214 {
215 const auto d = det();
216 if ( d > 0 && d > std::max( tol, T( 1e-4 ) ) * sqr( trace() ) )
217 {
218 if ( rank )
219 *rank = 2;
220 if ( space )
221 *space = Vector2<T>{};
222 return inverse( d );
223 }
224 }
225
226 SymMatrix2<T> res;
227 Matrix2<T> eigenvectors;
228 const auto eigenvalues = eigens( &eigenvectors );
229 const auto threshold = std::max( std::abs( eigenvalues[0] ), std::abs( eigenvalues[1] ) ) * tol;
230 int myRank = 0;
231 for ( int i = 0; i < 2; ++i )
232 {
233 if ( std::abs( eigenvalues[i] ) <= threshold )
234 continue;
235 res += outerSquare( 1 / eigenvalues[i], eigenvectors[i] );
236 ++myRank;
237 if ( space )
238 {
239 if ( myRank == 1 )
240 *space = eigenvectors[i];
241 else
242 *space = Vector2<T>{};
243 }
244 }
245 if ( rank )
246 *rank = myRank;
247 return res;
248}
249
251
252}
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:33
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:492
SymMatrix2 & operator-=(const SymMatrix2< T > &b)
Definition MRSymMatrix2.h:45
bool operator==(const Allocator< T > &, const Allocator< U > &) noexcept
Definition MRAllocator.h:95
constexpr SymMatrix2() noexcept=default
constexpr SymMatrix2< T > inverse() const noexcept
computes inverse matrix
Definition MRSymMatrix2.h:40
Vector2
Definition MRMeshFwd.h:189
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
constexpr auto sqr(T x) noexcept -> decltype(x *x)
squared value; the result type is the type of x*x, which is int for small integer types (char,...
Definition MRMeshFwd.h:799
constexpr SymMatrix2 & operator/=(T b)
Definition MRSymMatrix2.h:47
static constexpr SymMatrix2 diagonal(T diagVal) noexcept
Definition MRSymMatrix2.h:31
Vector2< T > eigenvector(T eigenvalue) const
computes not-unit eigenvector corresponding to a not-repeating eigenvalue
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:107
Vector2< T > eigens(Matrix2< T > *eigenvectors=nullptr) const
Color operator/(const Color &b, float a)
Definition MRColor.h:132
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
constexpr T det() const noexcept
computes determinant of the matrix
SymMatrix2 & operator+=(const SymMatrix2< T > &b)
Definition MRSymMatrix2.h:44
Matrix2
Definition MRMeshFwd.h:216
constexpr SymMatrix2 & operator*=(T b)
Definition MRSymMatrix2.h:46
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
SymMatrix2< T > pseudoinverse(T tol=std::numeric_limits< T >::epsilon(), int *rank=nullptr, Vector2< T > *space=nullptr) const
Vector2< T > maxEigenvector() const
computes not-unit eigenvector corresponding to maximum eigenvalue
T xx
zero matrix by default
Definition MRSymMatrix2.h:21
static constexpr Matrix2 fromRows(const Vector2< T > &x, const Vector2< T > &y) noexcept
constructs a matrix from its 2 rows
Definition MRMatrix2.h:52
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:112
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
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