CGVIS Utils
Loading...
Searching...
No Matches
Mat3.h
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <string>
5#include <array>
6#include <sstream>
7#include <cmath>
8
9#include "Vec3.h"
10
24
25template <typename T>
26class Mat3t {
27public:
30 Mat3t{1,0,0,
31 0,1,0,
32 0,0,1}
33 {}
34
39 Mat3t(T m11, T m12, T m13,
40 T m21, T m22, T m23,
41 T m31, T m32, T m33):
42 e{m11,m12,m13,
43 m21,m22,m23,
44 m31,m32,m33}
45 {
46 }
47
52 Mat3t(const std::array<T, 9>& e):
53 e{e}
54 {
55 }
56
63 Mat3t(const Vec3t<T>& e1,
64 const Vec3t<T>& e2,
65 const Vec3t<T>& e3):
66 Mat3t(e1.x,e1.y,e1.z,
67 e2.x,e2.y,e2.z,
68 e3.x,e3.y,e3.z)
69 {
70 }
71
73 Mat3t(const Mat3t& other):
74 e{other.e}
75 {
76 }
77
82 const std::string toString() const{
83 std::stringstream s;
84 s << "[" << e[0] << ", " << e[1] << ", " << e[2] << std::endl <<
85 " " << e[3] << ", " << e[4] << ", " << e[5] << std::endl <<
86 " " << e[6] << ", " << e[7] << ", " << e[8] << "]";
87 return s.str();
88 }
89
93 friend std::ostream& operator<<(std::ostream &os, const Mat3t& m) {
94 os << m.toString() ; return os;
95 }
96
102 operator T*(void){
103 return e.data();
104 }
105
110 operator const T*(void) const{
111 return e.data();
112 }
113
114 // ===== Scalar arithmetic =====
116 Mat3t operator * ( T scalar ) const{
117 return Mat3t{e[0]*scalar,e[1]*scalar,e[2]*scalar,
118 e[3]*scalar,e[4]*scalar,e[5]*scalar,
119 e[6]*scalar,e[7]*scalar,e[8]*scalar};
120 }
121
122 Mat3t operator + ( T scalar ) const{
123 return Mat3t{e[0]+scalar,e[1]+scalar,e[2]+scalar,e[3]+scalar,
124 e[4]+scalar,e[5]+scalar,e[6]+scalar,e[7]+scalar,
125 e[8]+scalar};
126 }
127
128 Mat3t operator - ( T scalar ) const{
129 return Mat3t{e[0]-scalar,e[1]-scalar,e[2]-scalar,e[3]-scalar,
130 e[4]-scalar,e[5]-scalar,e[6]-scalar,e[7]-scalar,
131 e[8]-scalar};
132 }
133
134 Mat3t operator / ( T scalar ) const{
135 return Mat3t{e[0]/scalar,e[1]/scalar,e[2]/scalar,e[3]/scalar,
136 e[4]/scalar,e[5]/scalar,e[6]/scalar,e[7]/scalar,
137 e[8]/scalar};
138 }
139
140 // ===== Products =====
144 Mat3t operator * ( const Mat3t& other ) const{
145 Mat3t result;
146 for (uint8_t x = 0;x<9;x+=3)
147 for (uint8_t y = 0;y<3;y++)
148 result.e[x+y] = e[0+x] * other.e[0+y]+
149 e[1+x] * other.e[3+y]+
150 e[2+x] * other.e[6+y];
151 return result;
152 }
153
157 Vec3t<T> operator * ( const Vec3t<T>& other ) const{
158 return Vec3t<T>{(other.x*e[0]+other.y*e[1]+other.z*e[2]),
159 (other.x*e[3]+other.y*e[4]+other.z*e[5]),
160 (other.x*e[6]+other.y*e[7]+other.z*e[8])};
161 }
162
163 // ===== Factory helpers =====
165 static Mat3t scaling(const Vec3t<T>& scale){
166 return scaling(scale.x, scale.y, scale.z);
167 }
168
169 static Mat3t scaling(T x, T y, T z){
170 return {x, 0, 0,
171 0, y, 0,
172 0, 0, z};
173 }
174
175 static Mat3t rotationX(T degree){
176 const T angle{deg2Rad(degree)};
177 const T cosAngle = cosf(angle);
178 const T sinAngle = sinf(angle);
179
180 return {1, 0, 0,
181 0, cosAngle, sinAngle,
182 0, -sinAngle, cosAngle};
183 }
184
185 static Mat3t rotationY(T degree){
186 const T angle{deg2Rad(degree)};
187 const T cosAngle{cosf(angle)};
188 const T sinAngle{sinf(angle)};
189
190 return {cosAngle, 0, -sinAngle,
191 0, 1, 0,
192 sinAngle, 0, cosAngle};
193 }
194
195 static Mat3t rotationZ(T degree){
196 const T angle{deg2Rad(degree)};
197 const T cosAngle{cosf(angle)};
198 const T sinAngle{sinf(angle)};
199
200 return {cosAngle, sinAngle, 0,
201 -sinAngle, cosAngle, 0,
202 0, 0, 1};
203 }
204
206 static Mat3t transpose(const Mat3t& m){
207 return {m.e[0],m.e[3],m.e[6],
208 m.e[1],m.e[4],m.e[7],
209 m.e[2],m.e[5],m.e[8]};
210 }
211
213 static T det(const Mat3t& m){
214 return m.e[0]*(m.e[4]*m.e[8]-m.e[5]*m.e[7])-m.e[1]*(m.e[3]*m.e[8]-m.e[5]*m.e[6])+m.e[2]*(m.e[3]*m.e[7]-m.e[4]*m.e[6]);
215 }
216
224 static Mat3t inverse(const Mat3t& m, T det) {
225 T Q = T(1.0/det);
226
227 return {
228 (m.e[4]*m.e[8]-m.e[5]*m.e[7])*Q, (m.e[2]*m.e[7]-m.e[1]*m.e[8])*Q, (m.e[1]*m.e[5]-m.e[2]*m.e[4])*Q,
229 (m.e[5]*m.e[6]-m.e[3]*m.e[8])*Q, (m.e[0]*m.e[8]-m.e[2]*m.e[6])*Q, (m.e[2]*m.e[3]-m.e[0]*m.e[5])*Q,
230 (m.e[3]*m.e[7]-m.e[4]*m.e[6])*Q, (m.e[1]*m.e[6]-m.e[0]*m.e[7])*Q, (m.e[0]*m.e[4]-m.e[1]*m.e[3])*Q
231 };
232 }
233
239 static Mat3t inverse(const Mat3t& m) {
240 return Mat3t::inverse(m, Mat3t::det(m));
241 }
242
243
244private:
245 std::array<T, 9> e;
246
248 static T deg2Rad(const T d) {
249 return T(3.14159265358979323846)*d/T(180);
250 }
251};
252
Mat3t< float > Mat3
Single-precision 3×3 matrix.
Definition Mat3.h:254
3D vector template with arithmetic, geometry helpers, and RNG utilities.
Definition Mat3.h:26
Mat3t(const std::array< T, 9 > &e)
Construct from a raw array of 9 elements.
Definition Mat3.h:52
Mat3t()
Construct the identity matrix.
Definition Mat3.h:29
Mat3t(const Vec3t< T > &e1, const Vec3t< T > &e2, const Vec3t< T > &e3)
Construct from three column vectors.
Definition Mat3.h:63
const std::string toString() const
String representation (pretty-printed rows).
Definition Mat3.h:82
Mat3t(const Mat3t &other)
Copy-construct from another matrix.
Definition Mat3.h:73
static Mat3t scaling(const Vec3t< T > &scale)
Non-uniform scaling by components of scale.
Definition Mat3.h:165
static Mat3t inverse(const Mat3t &m, T det)
Matrix inverse with precomputed determinant.
Definition Mat3.h:224
Mat3t operator*(T scalar) const
Component-wise multiplication by scalar.
Definition Mat3.h:116
static Mat3t scaling(T x, T y, T z)
Non-uniform scaling by components.
Definition Mat3.h:169
static Mat3t inverse(const Mat3t &m)
Matrix inverse computed via adjugate and determinant.
Definition Mat3.h:239
static Mat3t rotationZ(T degree)
Rotation about the Z-axis by degree (degrees).
Definition Mat3.h:195
Mat3t(T m11, T m12, T m13, T m21, T m22, T m23, T m31, T m32, T m33)
Construct from 9 elements (column-major order).
Definition Mat3.h:39
Mat3t operator/(T scalar) const
Component-wise division by scalar.
Definition Mat3.h:134
static Mat3t rotationY(T degree)
Rotation about the Y-axis by degree (degrees).
Definition Mat3.h:185
static Mat3t transpose(const Mat3t &m)
Transpose of a 3×3 matrix.
Definition Mat3.h:206
Mat3t operator+(T scalar) const
Component-wise addition of scalar.
Definition Mat3.h:122
Mat3t operator-(T scalar) const
Component-wise subtraction of scalar.
Definition Mat3.h:128
friend std::ostream & operator<<(std::ostream &os, const Mat3t &m)
Stream insertion using toString().
Definition Mat3.h:93
static Mat3t rotationX(T degree)
Rotation about the X-axis by degree (degrees).
Definition Mat3.h:175
static T det(const Mat3t &m)
Determinant of a 3×3 matrix.
Definition Mat3.h:213
Definition Vec3.h:29
T y
Definition Vec3.h:33
T x
Definition Vec3.h:33
T z
Definition Vec3.h:33