Cumulia Illustrator Rendering Engine v1.0.0
A Rendering engine for industrial CAD/CAE model and optimized for greatest performance
 
Loading...
Searching...
No Matches
matrix.h
1//##################################################################################################
2//
3// Copyright (c) 2024 Beijing Qiongqi Tech Co.,Ltd. All rights reserved.
4
5// This source code is confidential and proprietary to Beijing Qiongqi Tech Co.,Ltd(The Holder).
6// Any unauthorized use, copying, modification, or distribution of the code is strictly prohibited.
7// Any user shall obtain authorizaition from the Holder before modifying the source code. And the user shall not
8// sublicense, sell, distribute, or transfer the source code, whether in original or modified form, to any third party
9// without the prior written consent of the Holder.
10
11// This copyright notice and permission grant shall be included in all copies or substantial portions of the source code.
12
13// Author Cumulia Illustrator
14// Date 2024-05-01
15// Version V1.0.0
16//##################################################################################################
17
18#pragma once
19
20#include "tuple.h"
21
22namespace cil
23{
24 class Vector2f;
25 class Vector3f;
26 class Vector4f;
27 class Quaternion;
28 class Matrix2f;
29 class Matrix3f;
30 class Matrix4f;
31
34 template<typename S>
35 class Matrix : public Tuple<float>
36 {
37 protected:
38 int m_dim;
39
40 public:
42 Matrix(int dim)
43 : Tuple<float>(dim* dim)
44 {
45 this->m_dim = dim;
46
47 for (int i = 0; i < this->m_dim; i++)
48 this->m_data[i * this->m_dim + i] = 1;
49 }
51 const float* operator[](int index) const
52 {
53 return &this->m_data[index * this->m_dim];
54 }
56 float* operator[](int index)
57 {
58 return &this->m_data[index * this->m_dim];
59 }
60
63 Matrix& operator+=(const Matrix& other)
64 {
65 int size = this->m_size;
66 for (int i = 0; i < size; i++)
67 this->m_data[i] += other.data()[i];
68
69 return *this;
70 }
71
75 {
76 int size = this->m_size;
77 for (int i = 0; i < size; i++)
78 this->m_data[i] -= other.data()[i];
79
80 return *this;
81 }
85 {
86 auto a = this->m_data;
87 auto b = other.data();
88
89 int size = this->m_size;
90 auto t = new float[size] {};
91 int dim = this->m_dim;
92 for (int i = 0; i < dim; i++)
93 for (int j = 0; j < dim; j++)
94 for (int k = 0; k < dim; k++)
95 t[i * dim + j] += a[k * dim + j] * b[i * dim + k];
96
97 for (int i = 0; i < size; i++)
98 this->m_data[i] = t[i];
99
100 delete[] t;
101
102 return *this;
103 }
105 S operator+(const S& other) const
106 {
107 auto v = S();
108 int size = this->m_size;
109 for (int i = 0; i < size; i++)
110 v.data()[i] = this->m_data[i] + other.data()[i];
111
112 return v;
113 }
115 S operator-(const S& other) const
116 {
117 auto v = S();
118 int size = this->m_size;
119 for (int i = 0; i < size; i++)
120 v.data()[i] = this->m_data[i] - other.data()[i];
121
122 return v;
123 }
125 S operator*(const S& other) const
126 {
127 auto v = S();
128 auto a = this->m_data;
129 auto b = other.data();
130 auto c = v.data();
131
132 int size = v.size();
133 for (int i = 0; i < size; i++)
134 c[i] = 0;
135
136 int dim = this->m_dim;
137 for (int i = 0; i < dim; i++)
138 for (int j = 0; j < dim; j++)
139 for (int k = 0; k < dim; k++)
140 c[i * dim + j] += a[k * dim + j] * b[i * dim + k];
141
142 return v;
143 }
146 {
147 for (int i = 0; i < this->m_size; i++)
148 this->m_data[i] = 0;
149 for (int i = 0; i < this->m_dim; i++)
150 this->m_data[i * this->m_dim + i] = 1;
151 }
154 {
155 auto data = this->m_data;
156 int dim = this->m_dim;
157 for (int i = 0; i < dim; i++)
158 for (int j = 0; j < i; j++)
159 {
160 float t = data[i * dim + j];
161 data[i * dim + j] = data[j * dim + i];
162 data[j * dim + i] = t;
163 }
164 }
165 };
166
168 class Matrix2f final : public Matrix<Matrix2f>
169 {
170 public:
176 Matrix2f(float e00, float e01,
177 float e10, float e11);
180 const Vector2f& c1);
182 Matrix2f(const Matrix2f& other);
184 Matrix2f(const Matrix3f& other);
186 Matrix2f(const Matrix4f& other);
189 void set(float e00, float e01,
190 float e10, float e11);
191 };
193 class Matrix3f final : public Matrix<Matrix3f>
194 {
195 public:
201 Matrix3f(float e00, float e01, float e02,
202 float e10, float e11, float e12,
203 float e20, float e21, float e22);
206 const Vector3f& c1,
207 const Vector3f& c2);
208
210 Matrix3f(const Matrix2f& other);
212 Matrix3f(const Matrix3f& other);
214 Matrix3f(const Matrix4f& other);
217 void set(float e00, float e01, float e02,
218 float e10, float e11, float e12,
219 float e20, float e21, float e22);
220 float determinant() const;
222 bool invert();
223 };
225 class Matrix4f final : public Matrix<Matrix4f>
226 {
227 public:
233 Matrix4f(float e00, float e01, float e02, float e03,
234 float e10, float e11, float e12, float e13,
235 float e20, float e21, float e22, float e23,
236 float e30, float e31, float e32, float e33);
239 const Vector4f& c1,
240 const Vector4f& c2,
241 const Vector4f& c3);
243 Matrix4f(const Matrix2f& other);
245 Matrix4f(const Matrix3f& other);
247 Matrix4f(const Matrix4f& other);
248
251 Matrix4f operator*(const Matrix4f& other) const;
254 Vector4f operator*(const Vector4f& other) const;
257 void set(float e00, float e01, float e02, float e03,
258 float e10, float e11, float e12, float e13,
259 float e20, float e21, float e22, float e23,
260 float e30, float e31, float e32, float e33);
261
262 float determinant() const;
264 bool invert();
265
273 void rotate(const Quaternion& r);
275 void scale(const Vector3f& s);
277 void translate(const Vector3f& t);
278
282 static Matrix4f fromScaling(const Vector3f& s);
285 };
286
287 using mat2 = Matrix2f;
288 using mat3 = Matrix3f;
289 using mat4 = Matrix4f;
290}
The Matrix2f class is 2 X 2 matrix.
Definition matrix.h:169
Matrix2f(float e00, float e01, float e10, float e11)
Constructs a 2 X 2 matrix objects, and sets the elements with the input fload parameters.
void set(float e00, float e01, float e10, float e11)
Sets the elements of this 2 x 2 matrix object.
Matrix2f(const Matrix2f &other)
Constructs a 2 X 2 matrix object by copying the elements of another 2 X 2 matrix.
Matrix2f(const Matrix3f &other)
Constructs a 2 X 2 matrix object by copying the elements of the 2 X 2 submatrix of another 3 X 3 matr...
Matrix2f(const Vector2f &c0, const Vector2f &c1)
Constructs a 2 X 2 matrix object by setting the vector c0 to the first column and c1 to the second co...
Matrix2f(const Matrix4f &other)
Constructs a 2 X 2 matrix object by copying the elements of the 2 X 2 submatrix of another 4 X 4 matr...
Matrix2f()
Constructs a 2 X 2 matrix objects, initializing elements on the diagonal to 1 and all others to 0.
The Matrix3f class is 3 X 3 matrix.
Definition matrix.h:194
Matrix3f(const Matrix2f &other)
Constructs a 3 X 3 matrix object, and sets its 2 X 2 submatrix with another 2 X 2 matrix,...
Matrix3f(float e00, float e01, float e02, float e10, float e11, float e12, float e20, float e21, float e22)
Constructs a 3 X 3 matrix objects, and sets the elements with the input fload parameters.
Matrix3f(const Vector3f &c0, const Vector3f &c1, const Vector3f &c2)
Constructs a 3 X 3 matrix object by setting the vector c0 to the first column, c1 to the second colum...
Matrix3f(const Matrix3f &other)
Constructs a 3 X 3 matrix object by copying the elements of another 3 X 3 matrix.
Matrix3f(const Matrix4f &other)
Constructs a 3 X 3 matrix object by copying the elements of the 3 X 3 submatrix of another 4 X 4 matr...
bool invert()
Inverts this matrix. Returns a boolean value of true if success(non-singular). If the determinant is ...
float determinant() const
Returns the determinant of the Matrix3f object.
Matrix3f()
Constructs a 3 X 3 matrix objects, initializing elements on the diagonal to 1 and all others to 0.
void set(float e00, float e01, float e02, float e10, float e11, float e12, float e20, float e21, float e22)
Sets the elements of this 3 X 3 matrix object.
The Matrix4f class is 4 X 4 matrix.
Definition matrix.h:226
Matrix4f(const Matrix4f &other)
Constructs a 4 X 4 matrix object by copying the elements of another 4 X 4 matrix.
Vector4f operator*(const Vector4f &other) const
Returns a new Vector4 object that is formed by multiplication between this Matrix and a Vector4 objec...
Matrix4f(const Vector4f &c0, const Vector4f &c1, const Vector4f &c2, const Vector4f &c3)
Constructs a 4 X 4 matrix object by setting the vector c0 to the first column, c1 to the second colum...
Quaternion toRotation() const
Extracts the rotation quaternion from this matrix object.
void translate(const Vector3f &t)
Translates this matrix by the specified vector3.
static Matrix4f fromRotation(const Quaternion &r)
Returns a 4 X 4 matrix objects representing a rotation around an axis by a specified angle.
void rotate(const Quaternion &r)
Rotates this matrix by the specified quaternion.
static Matrix4f fromTranslation(const Vector3f &t)
Returns a 4 X 4 matrix objects representing a translation by vector3.
Matrix4f(const Matrix2f &other)
Constructs a 4 X 4 matrix object, and sets its 2 X 2 submatrix with another 2 X 2 matrix,...
Matrix4f(float e00, float e01, float e02, float e03, float e10, float e11, float e12, float e13, float e20, float e21, float e22, float e23, float e30, float e31, float e32, float e33)
Constructs a 4 X 4 matrix objects, and sets the elements with the input fload parameters.
Matrix4f()
Constructs a 4 X 4 matrix objects, initializing elements on the diagonal to 1 and all others to 0.
void set(float e00, float e01, float e02, float e03, float e10, float e11, float e12, float e13, float e20, float e21, float e22, float e23, float e30, float e31, float e32, float e33)
Sets the elements of this 4 X 4 matrix object.
Matrix4f operator*(const Matrix4f &other) const
Returns a new Matrix object that is formed by matrix multiplication between the current Matrix and an...
bool invert()
Inverts this matrix. Returns a boolean value of true if success(non-singular). If the determinant is ...
static Matrix4f fromScaling(const Vector3f &s)
Returns a 4 X 4 matrix objects representing a scaling transformation by vector3.
Vector3f toTranslation() const
Extracts the translation vector3 object from this matrix object.
Matrix4f(const Matrix3f &other)
Constructs a 4 X 4 matrix object, and sets its 3 X 3 submatrix with another 3 X 3 matrix,...
Vector3f toScaling() const
Extracts the scaling vector3 object from this matrix4 object.
float determinant() const
Returns the determinant of the Matrix4f object.
void scale(const Vector3f &s)
Scales this matrix by the specified vector3.
The templated class Matrix inherits from the templated class Tuple which is being instantiated with a...
Definition matrix.h:36
S operator+(const S &other) const
Returns a new Matrix object that is formed by element-wise addition between the current matrix and an...
Definition matrix.h:105
float * operator[](int index)
Returns a pointer to the first element in the column of index of this matrix, providing a read-write ...
Definition matrix.h:56
Matrix & operator+=(const Matrix &other)
Performs element-wise addition between the current Matrix and another matrix.
Definition matrix.h:63
void setIdentity()
Sets elements on the diagonal of this matrix to 1 and all others to 0.
Definition matrix.h:145
Matrix< S > & operator-=(const Matrix &other)
Performs element-wise subtraction between the current Matrix and another matrix.
Definition matrix.h:74
Matrix(int dim)
Constructs a dim X dim Matrix object, initializing elements on the diagonal to 1 and all others to 0.
Definition matrix.h:42
int m_dim
The dimension of the Matrix.
Definition matrix.h:38
const float * operator[](int index) const
Returns a constant pointer to the first element in the column of index of this matrix,...
Definition matrix.h:51
S operator-(const S &other) const
Returns a new Matrix object that is formed by element-wise subtraction between the current matrix and...
Definition matrix.h:115
Matrix< S > & operator*=(const Matrix &other)
Performs right matrix multiplication of the current matrix with another matrix.
Definition matrix.h:84
void transpose()
Transpose this matrix.
Definition matrix.h:153
S operator*(const S &other) const
Returns a new Matrix object that is formed by matrix multiplication between the current Matrix and an...
Definition matrix.h:125
The Quaternion class inherits from the Tuple class, specilizing the templated parameter to float for ...
Definition quaternion.h:28
The Tuple is a templated class inheriting from the most basic class Variant.
Definition tuple.h:27
T * data()
Gets the data array of the Tuple.
Definition tuple.h:60
int m_size
Definition tuple.h:36
float * m_data
Definition tuple.h:35
int size() const
Definition tuple.h:54
The class Vector2f inherits from the templated class of Vector2, and the template parameters are spec...
Definition vector.h:268
The class Vector3f inherits from the templated class of Vector3, and the template parameters are spec...
Definition vector.h:322
The class Vector4f inherits from the templated class of Vector4, and the template parameters are spec...
Definition vector.h:366
Definition decal.h:23