MeshLib C++ Docs
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
6#include <iosfwd>
7
8namespace MR
9{
12
13
14#ifdef _MSC_VER
15#pragma warning(push)
16#pragma warning(disable: 4804)
17#pragma warning(disable: 4146)
18#endif
19
22template <typename T>
23struct Matrix2
24{
25 using ValueType = T;
27
29 Vector2<T> x{ 1, 0 };
30 Vector2<T> y{ 0, 1 };
31
32 constexpr Matrix2() noexcept
33 {
34 static_assert( sizeof( Matrix2<ValueType> ) == 2 * sizeof( VectorType ), "Struct size invalid" );
35 }
37 constexpr Matrix2( const Vector2<T> & x, const Vector2<T> & y ) : x( x ), y( y ) { }
38 template <typename U>
39 constexpr explicit Matrix2( const Matrix2<U> & m ) : x( m.x ), y( m.y ) { }
40 static constexpr Matrix2 zero() noexcept { return Matrix2( Vector2<T>(), Vector2<T>() ); }
41 static constexpr Matrix2 identity() noexcept { return Matrix2(); }
43 static constexpr Matrix2 scale( T s ) noexcept { return Matrix2( { s, T(0) }, { T(0), s } ); }
45 static constexpr Matrix2 scale( T sx, T sy ) noexcept { return Matrix2( { sx, T(0) }, { T(0), sy } ); }
46 static constexpr Matrix2 scale( const Vector2<T> & s ) noexcept { return Matrix2( { s.x, T(0) }, { T(0), s.y } ); }
48 static constexpr Matrix2 rotation( T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
50 static constexpr Matrix2 rotation( const Vector2<T> & from, const Vector2<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
52 static constexpr Matrix2 fromRows( const Vector2<T> & x, const Vector2<T> & y ) noexcept { return Matrix2( x, y ); }
55 static constexpr Matrix2 fromColumns( const Vector2<T> & x, const Vector2<T> & y ) noexcept { return Matrix2( x, y ).transposed(); }
56
58 constexpr const Vector2<T> & operator []( int row ) const noexcept { return *( ( VectorType* )this + row ); }
59 constexpr Vector2<T> & operator []( int row ) noexcept { return *( ( VectorType* )this + row ); }
60
62 constexpr Vector2<T> col( int i ) const noexcept { return { x[i], y[i] }; }
63
65 constexpr T trace() const noexcept { return x.x + y.y; }
67 constexpr T normSq() const noexcept { return x.lengthSq() + y.lengthSq(); }
68 constexpr auto norm() const noexcept
69 {
72 using std::sqrt;
73 return sqrt( normSq() );
74 }
76 constexpr T det() const noexcept;
78 constexpr Matrix2<T> inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> );
80 constexpr Matrix2<T> transposed() const noexcept;
81
82 [[nodiscard]] friend constexpr bool operator ==( const Matrix2<T> & a, const Matrix2<T> & b ) { return a.x == b.x && a.y == b.y; }
83 [[nodiscard]] friend constexpr bool operator !=( const Matrix2<T> & a, const Matrix2<T> & b ) { return !( a == b ); }
84
86
87 [[nodiscard]] friend constexpr auto operator +( const Matrix2<T> & a, const Matrix2<T> & b ) -> Matrix2<decltype( std::declval<T>() + std::declval<T>() )> { return { a.x + b.x, a.y + b.y }; }
88 [[nodiscard]] friend constexpr auto operator -( const Matrix2<T> & a, const Matrix2<T> & b ) -> Matrix2<decltype( std::declval<T>() - std::declval<T>() )> { return { a.x - b.x, a.y - b.y }; }
89 [[nodiscard]] friend constexpr auto operator *( T a, const Matrix2<T> & b ) -> Matrix2<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y }; }
90 [[nodiscard]] friend constexpr auto operator *( const Matrix2<T> & b, T a ) -> Matrix2<decltype( std::declval<T>() * std::declval<T>() )> { return { a * b.x, a * b.y }; }
91 [[nodiscard]] friend constexpr auto operator /( Matrix2<T> b, T a ) -> Matrix2<decltype( std::declval<T>() / std::declval<T>() )>
92 {
93 if constexpr ( std::is_integral_v<T> )
94 return { b.x / a, b.y / a };
95 else
96 return b * ( 1 / a );
97 }
98
99 friend constexpr Matrix2<T> & operator +=( Matrix2<T> & a, const Matrix2<T> & b ) { a.x += b.x; a.y += b.y; return a; }
100 friend constexpr Matrix2<T> & operator -=( Matrix2<T> & a, const Matrix2<T> & b ) { a.x -= b.x; a.y -= b.y; return a; }
101 friend constexpr Matrix2<T> & operator *=( Matrix2<T> & a, T b ) { a.x *= b; a.y *= b; return a; }
102 friend constexpr Matrix2<T> & operator /=( Matrix2<T> & a, T b )
103 {
104 if constexpr ( std::is_integral_v<T> )
105 { a.x /= b; a.y /= b; return a; }
106 else
107 return a *= ( 1 / b );
108 }
109
111 [[nodiscard]] friend constexpr auto operator *( const Matrix2<T> & a, const Vector2<T> & b ) -> Vector2<decltype( dot( std::declval<Vector2<T>>(), std::declval<Vector2<T>>() ) )>
112 {
113 return { dot( a.x, b ), dot( a.y, b ) };
114 }
115
117 [[nodiscard]] friend constexpr auto operator *( const Matrix2<T> & a, const Matrix2<T> & b ) -> Matrix2<decltype( dot( std::declval<Vector2<T>>(), std::declval<Vector2<T>>() ) )>
118 {
119 Matrix2<decltype( dot( std::declval<Vector2<T>>(), std::declval<Vector2<T>>() ) )> res;
120 for ( int i = 0; i < 2; ++i )
121 for ( int j = 0; j < 2; ++j )
122 res[i][j] = dot( a[i], b.col(j) );
123 return res;
124 }
125
126 friend std::ostream& operator<<( std::ostream& s, const Matrix2& mat )
127 {
128 return s << mat.x << '\n' << mat.y << '\n';
129 }
130
131 friend std::istream& operator>>( std::istream& s, Matrix2& mat )
132 {
133 return s >> mat.x >> mat.y;
134 }
135};
136
139
141template <typename T>
142inline auto dot( const Matrix2<T> & a, const Matrix2<T> & b ) -> decltype( dot( a.x, b.x ) )
143{
144 return dot( a.x, b.x ) + dot( a.y, b.y );
145}
146
148template <typename T>
149inline Matrix2<T> outer( const Vector2<T> & a, const Vector2<T> & b )
150{
151 return { a.x * b, a.y * b };
152}
153
154template <typename T>
155constexpr Matrix2<T> Matrix2<T>::rotation( T angle ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
156{
157 T c = cos( angle );
158 T s = sin( angle );
159 return {
160 { c, -s },
161 { s, c }
162 };
163}
164
165template <typename T>
166constexpr Matrix2<T> Matrix2<T>::rotation( const Vector2<T> & from, const Vector2<T> & to ) noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
167{
168 const auto x = cross( from, to );
169 if ( x > 0 )
170 return rotation( angle( from, to ) );
171 if ( x < 0 )
172 return rotation( -angle( from, to ) );
173 if ( dot( from, to ) >= 0 )
174 return {};
175 return rotation( T( PI ) );
176}
177
178template <typename T>
179constexpr T Matrix2<T>::det() const noexcept
180{
181 return x.x * y.y - x.y * y.x;
182}
183
184template <typename T>
185constexpr Matrix2<T> Matrix2<T>::inverse() const noexcept MR_REQUIRES_IF_SUPPORTED( std::is_floating_point_v<T> )
186{
187 auto det = this->det();
188 if ( det == 0 )
189 return {};
190 return Matrix2<T>
191 {
192 { y.y, - x.y },
193 { - y.x, x.x }
194 } / det;
195}
196
197template <typename T>
198constexpr Matrix2<T> Matrix2<T>::transposed() const noexcept
199{
200 return Matrix2<T>
201 {
202 { x.x, y.x },
203 { x.y, y.y }
204 };
205}
206
208
209#ifdef _MSC_VER
210#pragma warning(pop)
211#endif
212
213}
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
constexpr Matrix2(const Matrix2< U > &m)
Definition MRMatrix2.h:39
Vector2< T > x
rows, identity matrix by default
Definition MRMatrix2.h:29
constexpr Matrix2() noexcept
Definition MRMatrix2.h:32
static constexpr Matrix2 scale(T s) noexcept
returns a matrix that scales uniformly
Definition MRMatrix2.h:43
constexpr T normSq() const noexcept
compute sum of squared matrix elements
Definition MRMatrix2.h:67
Matrix2< T > outer(const Vector2< T > &a, const Vector2< T > &b)
x = a * b^T
Definition MRMatrix2.h:149
auto dot(const Matrix2< T > &a, const Matrix2< T > &b) -> decltype(dot(a.x, b.x))
double-dot product: x = a : b
Definition MRMatrix2.h:142
friend std::ostream & operator<<(std::ostream &s, const Matrix2 &mat)
Definition MRMatrix2.h:126
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
friend constexpr auto operator/(Matrix2< T > b, T a) -> Matrix2< decltype(std::declval< T >()/std::declval< T >())>
Definition MRMatrix2.h:91
friend constexpr auto operator-(const Matrix2< T > &a, const Matrix2< T > &b) -> Matrix2< decltype(std::declval< T >() - std::declval< T >())>
Definition MRMatrix2.h:88
constexpr Matrix2(const Vector2< T > &x, const Vector2< T > &y)
initializes matrix from its 2 rows
Definition MRMatrix2.h:37
friend constexpr Matrix2< T > & operator-=(Matrix2< T > &a, const Matrix2< T > &b)
Definition MRMatrix2.h:100
friend constexpr Matrix2< T > & operator*=(Matrix2< T > &a, T b)
Definition MRMatrix2.h:101
constexpr Vector2< T > col(int i) const noexcept
column access
Definition MRMatrix2.h:62
static constexpr Matrix2 identity() noexcept
Definition MRMatrix2.h:41
Vector2< T > y
Definition MRMatrix2.h:30
T ValueType
Definition MRMatrix2.h:25
constexpr T det() const noexcept
computes determinant of the matrix
friend constexpr auto operator*(T a, const Matrix2< T > &b) -> Matrix2< decltype(std::declval< T >() *std::declval< T >())>
Definition MRMatrix2.h:89
static constexpr Matrix2 scale(const Vector2< T > &s) noexcept
Definition MRMatrix2.h:46
friend constexpr Matrix2< T > & operator+=(Matrix2< T > &a, const Matrix2< T > &b)
Definition MRMatrix2.h:99
static constexpr Matrix2 scale(T sx, T sy) noexcept
returns a matrix that has its own scale along each axis
Definition MRMatrix2.h:45
friend std::istream & operator>>(std::istream &s, Matrix2 &mat)
Definition MRMatrix2.h:131
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRMatrix2.h:65
constexpr const Vector2< T > & operator[](int row) const noexcept
row access
Definition MRMatrix2.h:58
friend constexpr bool operator!=(const Matrix2< T > &a, const Matrix2< T > &b)
Definition MRMatrix2.h:83
constexpr Matrix2< T > inverse() const noexcept MR_REQUIRES_IF_SUPPORTED(std constexpr Matrix2< T > transposed() const noexcept
computes inverse matrix
friend constexpr Matrix2< T > & operator/=(Matrix2< T > &a, T b)
Definition MRMatrix2.h:102
constexpr auto norm() const noexcept
Definition MRMatrix2.h:68
static constexpr Matrix2 zero() noexcept
Definition MRMatrix2.h:40
friend constexpr auto operator+(const Matrix2< T > &a, const Matrix2< T > &b) -> Matrix2< decltype(std::declval< T >()+std::declval< T >())>
NOTE: We use std::declval() in the operators below because libclang 18 in our binding generator is bu...
Definition MRMatrix2.h:87
static constexpr Matrix2 fromColumns(const Vector2< T > &x, const Vector2< T > &y) noexcept
Definition MRMatrix2.h:55
@ angle
Direction, normally Vector3f.
constexpr auto dot(A a, A b)
Definition MRImGuiVectorOperators.h:129
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRMatrix2.h:24
Definition MRVector2.h:29
T x
Definition MRVector2.h:35
T y
Definition MRVector2.h:35