-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMat3.cpp
123 lines (110 loc) · 3.44 KB
/
Mat3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* @file: Mat3.cpp
* @author: Ricard Bitriá Ribes (https://github.com/dracir9)
* Created Date: 2021-11-14
* -----
* Last Modified: 16-05-2023
* Modified By: Ricard Bitriá Ribes
* -----
* @copyright (c) 2021 Ricard Bitriá Ribes
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Mat3.h"
#include <cmath>
#include <cassert>
#include <cstring>
Mat3& Mat3::operator=(const Mat3& m)
{
memcpy(data, m.data, sizeof(data));
return *this;
}
Mat3& Mat3::operator*=(float scalar)
{
for(auto& row : data)
{
for(float& e : row)
{
e *= scalar;
}
}
return *this;
}
Mat3 Mat3::operator*(float scalar) const
{
Mat3 result = *this;
return result *= scalar;
}
Mat3& Mat3::operator*=(const Mat3& m)
{
return *this = *this * m;
}
Mat3 Mat3::operator*(const Mat3& m) const
{
Mat3 result;
result.data[0][0] = data[0][0] * m.data[0][0] + data[0][1] * m.data[1][0] + data[0][2] * m.data[2][0];
result.data[0][1] = data[0][0] * m.data[0][1] + data[0][1] * m.data[1][1] + data[0][2] * m.data[2][1];
result.data[0][2] = data[0][0] * m.data[0][2] + data[0][1] * m.data[1][2] + data[0][2] * m.data[2][2];
result.data[1][0] = data[1][0] * m.data[0][0] + data[1][1] * m.data[1][0] + data[1][2] * m.data[2][0];
result.data[1][1] = data[1][0] * m.data[0][1] + data[1][1] * m.data[1][1] + data[1][2] * m.data[2][1];
result.data[1][2] = data[1][0] * m.data[0][2] + data[1][1] * m.data[1][2] + data[1][2] * m.data[2][2];
result.data[2][0] = data[2][0] * m.data[0][0] + data[2][1] * m.data[1][0] + data[2][2] * m.data[2][0];
result.data[2][1] = data[2][0] * m.data[0][1] + data[2][1] * m.data[1][1] + data[2][2] * m.data[2][1];
result.data[2][2] = data[2][0] * m.data[0][2] + data[2][1] * m.data[1][2] + data[2][2] * m.data[2][2];
return result;
}
Mat3 Mat3::operator!() const
{
Mat3 m;
m.data[0][0] = data[0][0];
m.data[0][1] = data[1][0];
m.data[0][2] = data[2][0];
m.data[1][0] = data[0][1];
m.data[1][1] = data[1][1];
m.data[1][2] = data[2][1];
m.data[2][0] = data[0][2];
m.data[2][1] = data[1][2];
m.data[2][2] = data[2][2];
return m;
}
Mat3 Mat3::RotationZ(float theta)
{
const float sinTheta = sinf(theta);
const float cosTheta = cosf(theta);
return {
cosTheta, sinTheta, 0.0f,
-sinTheta, cosTheta, 0.0f,
0.0f, 0.0f, 1.0f,
};
}
Mat3 Mat3::RotationY(float theta)
{
const float sinTheta = sinf(theta);
const float cosTheta = cosf(theta);
return {
cosTheta, 0.0f, -sinTheta,
0.0f, 1.0f, 0.0f,
sinTheta, 0.0f, cosTheta
};
}
Mat3 Mat3::RotationX(float theta)
{
const float sinTheta = sinf(theta);
const float cosTheta = cosf(theta);
return {
1.0f, 0.0f, 0.0f,
0.0f, cosTheta, sinTheta,
0.0f, -sinTheta, cosTheta,
};
}