MeshLib
 
Loading...
Searching...
No Matches
MRSymMatrix4.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector4.h"
4#include "MRMatrix4.h"
5
6namespace MR
7{
8
11template <typename T>
13{
14 using ValueType = T;
15
17 T xx = 0, xy = 0, xz = 0, xw = 0,
18 yy = 0, yz = 0, yw = 0,
19 zz = 0, zw = 0,
20 ww = 0;
21
22 constexpr SymMatrix4() noexcept = default;
23 template <typename U>
24 constexpr explicit SymMatrix4( const SymMatrix4<U> & m ) :
25 xx( T( m.xx ) ), xy( T( m.xy ) ), xz( T( m.xz ) ), xw( T( m.xw ) ),
26 yy( T( m.yy ) ), yz( T( m.yz ) ), yw( T( m.yw ) ),
27 zz( T( m.zz ) ), zw( T( m.zw ) ),
28 ww( T( m.ww ) ) {}
29 static constexpr SymMatrix4 identity() noexcept { SymMatrix4 res; res.xx = res.yy = res.zz = res.ww = 1; return res; }
30 static constexpr SymMatrix4 diagonal( T diagVal ) noexcept { SymMatrix4 res; res.xx = res.yy = res.zz = res.ww = diagVal; return res; }
31
33 constexpr T trace() const noexcept { return xx + yy + zz + ww; }
35 constexpr T normSq() const noexcept;
36
37 SymMatrix4 & operator +=( const SymMatrix4<T> & b );
38 SymMatrix4 & operator -=( const SymMatrix4<T> & b );
39 SymMatrix4 & operator *=( T b );
40 SymMatrix4 & operator /=( T b );
41
42 bool operator ==( const SymMatrix4<T> & ) const = default;
43};
44
47
49template <typename T>
50inline Vector4<T> operator *( const SymMatrix4<T> & a, const Vector4<T> & b )
51{
52 return
53 {
54 a.xx * b.x + a.xy * b.y + a.xz * b.z + a.xw * b.w,
55 a.xy * b.x + a.yy * b.y + a.yz * b.z + a.yw * b.w,
56 a.xz * b.x + a.yz * b.y + a.zz * b.z + a.zw * b.w,
57 a.xw * b.x + a.yw * b.y + a.zw * b.z + a.ww * b.w
58 };
59}
60
62template <typename T>
64{
65 SymMatrix4<T> res;
66 res.xx = a.x * a.x; res.xy = a.x * a.y; res.xz = a.x * a.z; res.xw = a.x * a.w;
67 res.yy = a.y * a.y; res.yz = a.y * a.z; res.yw = a.y * a.w;
68 res.zz = a.z * a.z; res.zw = a.z * a.w;
69 res.ww = a.w * a.w;
70 return res;
71}
72
74template <typename T>
75inline SymMatrix4<T> outerSquare( T k, const Vector4<T> & a )
76{
77 const auto ka = k * a;
78 SymMatrix4<T> res;
79 res.xx = ka.x * a.x; res.xy = ka.x * a.y; res.xz = ka.x * a.z; res.xw = ka.x * a.w;
80 res.yy = ka.y * a.y; res.yz = ka.y * a.z; res.yw = ka.y * a.w;
81 res.zz = ka.z * a.z; res.zw = ka.z * a.w;
82 res.ww = ka.w * a.w;
83 return res;
84}
85
86template <typename T>
87inline SymMatrix4<T> operator +( const SymMatrix4<T> & a, const SymMatrix4<T> & b )
88 { SymMatrix4<T> res{ a }; res += b; return res; }
89
90template <typename T>
91inline SymMatrix4<T> operator -( const SymMatrix4<T> & a, const SymMatrix4<T> & b )
92 { SymMatrix4<T> res{ a }; res -= b; return res; }
93
94template <typename T>
95inline SymMatrix4<T> operator *( T a, const SymMatrix4<T> & b )
96 { SymMatrix4<T> res{ b }; res *= a; return res; }
97
98template <typename T>
99inline SymMatrix4<T> operator *( const SymMatrix4<T> & b, T a )
100 { SymMatrix4<T> res{ b }; res *= a; return res; }
101
102template <typename T>
104 { b /= a; return b; }
105
106template <typename T>
107constexpr T SymMatrix4<T>::normSq() const noexcept
108{
109 return sqr( xx ) + sqr( yy ) + sqr( zz ) + sqr( ww ) +
110 2 * ( sqr( xy ) + sqr( xz ) + sqr( xw ) + sqr( yz ) + sqr( yw ) + sqr( zw ) );
111}
112
113template <typename T>
115{
116 xx += b.xx; xy += b.xy; xz += b.xz; xw += b.xw;
117 yy += b.yy; yz += b.yz; yw += b.yw;
118 zz += b.zz; zw += b.zw;
119 zz += b.zz;
120 return * this;
121}
122
123template <typename T>
125{
126 xx -= b.xx; xy -= b.xy; xz -= b.xz; xw -= b.xw;
127 yy -= b.yy; yz -= b.yz; yw -= b.yw;
128 zz -= b.zz; zw -= b.zw;
129 zz -= b.zz;
130 return * this;
131}
132
133template <typename T>
135{
136 xx *= b; xy *= b; xz *= b; xw *= b;
137 yy *= b; yz *= b; yw *= b;
138 zz *= b; zw *= b;
139 zz *= b;
140 return * this;
141}
142
143template <typename T>
145{
146 if constexpr ( std::is_integral_v<T> )
147 {
148 xx /= b; xy /= b; xz /= b; xw /= b;
149 yy /= b; yz /= b; yw /= b;
150 zz /= b; zw /= b;
151 zz /= b;
152 }
153 else
154 *this *= ( 1 / b );
155 return * this;
156}
157
159
160} // namespace MR
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRMesh/MRBitSet.h:325
Definition MRCameraOrientationPlugin.h:7
constexpr T sqr(T x) noexcept
Definition MRMesh/MRMeshFwd.h:613
Color operator/(const Color &b, float a)
Definition MRColor.h:128
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:108
Definition MRSymMatrix4.h:13
T xz
Definition MRSymMatrix4.h:17
SymMatrix4 & operator-=(const SymMatrix4< T > &b)
SymMatrix4< T > outerSquare(const Vector4< T > &a)
x = a * a^T
Definition MRSymMatrix4.h:63
SymMatrix4 & operator+=(const SymMatrix4< T > &b)
constexpr T normSq() const noexcept
computes the squared norm of the matrix, which is equal to the sum of 16 squared elements
static constexpr SymMatrix4 diagonal(T diagVal) noexcept
Definition MRSymMatrix4.h:30
T zw
Definition MRSymMatrix4.h:19
T xx
zero matrix by default
Definition MRSymMatrix4.h:17
T ww
Definition MRSymMatrix4.h:20
SymMatrix4< T > outerSquare(T k, const Vector4< T > &a)
x = k * a * a^T
Definition MRSymMatrix4.h:75
T zz
Definition MRSymMatrix4.h:19
T xy
Definition MRSymMatrix4.h:17
constexpr SymMatrix4() noexcept=default
T ValueType
Definition MRSymMatrix4.h:14
SymMatrix4 & operator*=(T b)
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRSymMatrix4.h:33
T yz
Definition MRSymMatrix4.h:18
T xw
Definition MRSymMatrix4.h:17
T yy
Definition MRSymMatrix4.h:18
SymMatrix4 & operator/=(T b)
T yw
Definition MRSymMatrix4.h:18
static constexpr SymMatrix4 identity() noexcept
Definition MRSymMatrix4.h:29
Vector4< T > operator*(const SymMatrix4< T > &a, const Vector4< T > &b)
x = a * b
Definition MRSymMatrix4.h:50
Definition MRVector4.h:13
T y
Definition MRVector4.h:19
T z
Definition MRVector4.h:19
T x
Definition MRVector4.h:19
T w
Definition MRVector4.h:19