MeshLib C++ Docs
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{
10
11
14template <typename T>
16{
17 using ValueType = T;
18
20 T xx = 0, xy = 0, xz = 0, xw = 0,
21 yy = 0, yz = 0, yw = 0,
22 zz = 0, zw = 0,
23 ww = 0;
24
25 constexpr SymMatrix4() noexcept = default;
26 template <typename U>
27 constexpr explicit SymMatrix4( const SymMatrix4<U> & m ) :
28 xx( T( m.xx ) ), xy( T( m.xy ) ), xz( T( m.xz ) ), xw( T( m.xw ) ),
29 yy( T( m.yy ) ), yz( T( m.yz ) ), yw( T( m.yw ) ),
30 zz( T( m.zz ) ), zw( T( m.zw ) ),
31 ww( T( m.ww ) ) {}
32 static constexpr SymMatrix4 identity() noexcept { SymMatrix4 res; res.xx = res.yy = res.zz = res.ww = 1; return res; }
33 static constexpr SymMatrix4 diagonal( T diagVal ) noexcept { SymMatrix4 res; res.xx = res.yy = res.zz = res.ww = diagVal; return res; }
34
36 constexpr T trace() const noexcept { return xx + yy + zz + ww; }
38 constexpr T normSq() const noexcept;
39
40 SymMatrix4 & operator +=( const SymMatrix4<T> & b );
41 SymMatrix4 & operator -=( const SymMatrix4<T> & b );
42 SymMatrix4 & operator *=( T b );
43 SymMatrix4 & operator /=( T b );
44
45 bool operator ==( const SymMatrix4<T> & ) const = default;
46};
47
50
52template <typename T>
53inline Vector4<T> operator *( const SymMatrix4<T> & a, const Vector4<T> & b )
54{
55 return
56 {
57 a.xx * b.x + a.xy * b.y + a.xz * b.z + a.xw * b.w,
58 a.xy * b.x + a.yy * b.y + a.yz * b.z + a.yw * b.w,
59 a.xz * b.x + a.yz * b.y + a.zz * b.z + a.zw * b.w,
60 a.xw * b.x + a.yw * b.y + a.zw * b.z + a.ww * b.w
61 };
62}
63
65template <typename T>
67{
68 SymMatrix4<T> res;
69 res.xx = a.x * a.x; res.xy = a.x * a.y; res.xz = a.x * a.z; res.xw = a.x * a.w;
70 res.yy = a.y * a.y; res.yz = a.y * a.z; res.yw = a.y * a.w;
71 res.zz = a.z * a.z; res.zw = a.z * a.w;
72 res.ww = a.w * a.w;
73 return res;
74}
75
77template <typename T>
78inline SymMatrix4<T> outerSquare( T k, const Vector4<T> & a )
79{
80 const auto ka = k * a;
81 SymMatrix4<T> res;
82 res.xx = ka.x * a.x; res.xy = ka.x * a.y; res.xz = ka.x * a.z; res.xw = ka.x * a.w;
83 res.yy = ka.y * a.y; res.yz = ka.y * a.z; res.yw = ka.y * a.w;
84 res.zz = ka.z * a.z; res.zw = ka.z * a.w;
85 res.ww = ka.w * a.w;
86 return res;
87}
88
89template <typename T>
90inline SymMatrix4<T> operator +( const SymMatrix4<T> & a, const SymMatrix4<T> & b )
91 { SymMatrix4<T> res{ a }; res += b; return res; }
92
93template <typename T>
94inline SymMatrix4<T> operator -( const SymMatrix4<T> & a, const SymMatrix4<T> & b )
95 { SymMatrix4<T> res{ a }; res -= b; return res; }
96
97template <typename T>
98inline SymMatrix4<T> operator *( T a, const SymMatrix4<T> & b )
99 { SymMatrix4<T> res{ b }; res *= a; return res; }
100
101template <typename T>
102inline SymMatrix4<T> operator *( const SymMatrix4<T> & b, T a )
103 { SymMatrix4<T> res{ b }; res *= a; return res; }
104
105template <typename T>
107 { b /= a; return b; }
108
109template <typename T>
110constexpr T SymMatrix4<T>::normSq() const noexcept
111{
112 return sqr( xx ) + sqr( yy ) + sqr( zz ) + sqr( ww ) +
113 2 * ( sqr( xy ) + sqr( xz ) + sqr( xw ) + sqr( yz ) + sqr( yw ) + sqr( zw ) );
114}
115
116template <typename T>
118{
119 xx += b.xx; xy += b.xy; xz += b.xz; xw += b.xw;
120 yy += b.yy; yz += b.yz; yw += b.yw;
121 zz += b.zz; zw += b.zw;
122 zz += b.zz;
123 return * this;
124}
125
126template <typename T>
128{
129 xx -= b.xx; xy -= b.xy; xz -= b.xz; xw -= b.xw;
130 yy -= b.yy; yz -= b.yz; yw -= b.yw;
131 zz -= b.zz; zw -= b.zw;
132 zz -= b.zz;
133 return * this;
134}
135
136template <typename T>
138{
139 xx *= b; xy *= b; xz *= b; xw *= b;
140 yy *= b; yz *= b; yw *= b;
141 zz *= b; zw *= b;
142 zz *= b;
143 return * this;
144}
145
146template <typename T>
148{
149 if constexpr ( std::is_integral_v<T> )
150 {
151 xx /= b; xy /= b; xz /= b; xw /= b;
152 yy /= b; yz /= b; yw /= b;
153 zz /= b; zw /= b;
154 zz /= b;
155 }
156 else
157 *this *= ( 1 / b );
158 return * this;
159}
160
162
163}
BitSet operator-(const BitSet &a, const BitSet &b)
Definition MRBitSet.h:457
T xz
Definition MRSymMatrix4.h:20
SymMatrix4 & operator-=(const SymMatrix4< T > &b)
T y
Definition MRVector4.h:32
T z
Definition MRVector4.h:32
T x
Definition MRVector4.h:32
SymMatrix4< T > outerSquare(const Vector4< T > &a)
x = a * a^T
Definition MRSymMatrix4.h:66
T w
Definition MRVector4.h:32
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:33
T zw
Definition MRSymMatrix4.h:22
T xx
zero matrix by default
Definition MRSymMatrix4.h:20
T ww
Definition MRSymMatrix4.h:23
Color operator/(const Color &b, float a)
Definition MRColor.h:129
SymMatrix4< T > outerSquare(T k, const Vector4< T > &a)
x = k * a * a^T
Definition MRSymMatrix4.h:78
T zz
Definition MRSymMatrix4.h:22
T xy
Definition MRSymMatrix4.h:20
constexpr SymMatrix4() noexcept=default
T ValueType
Definition MRSymMatrix4.h:17
SymMatrix4 & operator*=(T b)
constexpr T trace() const noexcept
computes trace of the matrix
Definition MRSymMatrix4.h:36
T yz
Definition MRSymMatrix4.h:21
T xw
Definition MRSymMatrix4.h:20
T yy
Definition MRSymMatrix4.h:21
SymMatrix4 & operator/=(T b)
Color operator+(const Color &a, const Color &b)
Definition MRColor.h:109
T yw
Definition MRSymMatrix4.h:21
static constexpr SymMatrix4 identity() noexcept
Definition MRSymMatrix4.h:32
Vector4< T > operator*(const SymMatrix4< T > &a, const Vector4< T > &b)
x = a * b
Definition MRSymMatrix4.h:53
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRSymMatrix4.h:16
Definition MRVector4.h:26