-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
221 lines (185 loc) · 4.22 KB
/
Matrix.cpp
File metadata and controls
221 lines (185 loc) · 4.22 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "Matrix.h"
#include <cassert>
#include <cmath>
namespace dae {
Matrix::Matrix(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis, const Vector3& t) :
Matrix({ xAxis, 0 }, { yAxis, 0 }, { zAxis, 0 }, { t, 1 })
{
}
Matrix::Matrix(const Vector4& xAxis, const Vector4& yAxis, const Vector4& zAxis, const Vector4& t)
{
data[0] = xAxis;
data[1] = yAxis;
data[2] = zAxis;
data[3] = t;
}
Matrix::Matrix(const Matrix& m)
{
data[0] = m[0];
data[1] = m[1];
data[2] = m[2];
data[3] = m[3];
}
Vector3 Matrix::TransformVector(const Vector3& v) const
{
return TransformVector(v[0], v[1], v[2]);
}
Vector3 Matrix::TransformVector(float x, float y, float z) const
{
return Vector3{
data[0].x * x + data[1].x * y + data[2].x * z,
data[0].y * x + data[1].y * y + data[2].y * z,
data[0].z * x + data[1].z * y + data[2].z * z
};
}
Vector3 Matrix::TransformPoint(const Vector3& p) const
{
return TransformPoint(p[0], p[1], p[2]);
}
Vector3 Matrix::TransformPoint(float x, float y, float z) const
{
return Vector3{
data[0].x * x + data[1].x * y + data[2].x * z + data[3].x,
data[0].y * x + data[1].y * y + data[2].y * z + data[3].y,
data[0].z * x + data[1].z * y + data[2].z * z + data[3].z,
};
}
const Matrix& Matrix::Transpose()
{
Matrix result{};
for (int r{ 0 }; r < 4; ++r)
{
for (int c{ 0 }; c < 4; ++c)
{
result[r][c] = data[c][r];
}
}
data[0] = result[0];
data[1] = result[1];
data[2] = result[2];
data[3] = result[3];
return *this;
}
Matrix Matrix::Transpose(const Matrix& m)
{
Matrix out{ m };
out.Transpose();
return out;
}
Vector3 Matrix::GetAxisX() const
{
return data[0];
}
Vector3 Matrix::GetAxisY() const
{
return data[1];
}
Vector3 Matrix::GetAxisZ() const
{
return data[2];
}
Vector3 Matrix::GetTranslation() const
{
return data[3];
}
Matrix Matrix::CreateTranslation(float x, float y, float z)
{
return Matrix{
Vector3{Vector3::UnitX},
Vector3{Vector3::UnitY},
Vector3{Vector3::UnitZ},
Vector3{x,y,z} };
}
Matrix Matrix::CreateTranslation(const Vector3& t)
{
return { Vector3::UnitX, Vector3::UnitY, Vector3::UnitZ, t };
}
Matrix Matrix::CreateRotationX(float pitch)
{
const auto cosValue{ cos(pitch) };
const auto sinValue{ sin(pitch) };
return Matrix{
Vector3{Vector3::UnitX},
Vector3{0.f,cosValue,-sinValue},
Vector3{0.f,sinValue,cosValue},
Vector3{} };
}
Matrix Matrix::CreateRotationY(float yaw)
{
const auto cosValue{ cos(yaw) };
const auto sinValue{ sin(yaw) };
return Matrix{
Vector3{cosValue,0.f,sinValue},
Vector3{Vector3::UnitY},
Vector3{-sinValue,0.f,cosValue},
Vector3{} };
}
Matrix Matrix::CreateRotationZ(float roll)
{
const auto cosValue{ cos(roll) };
const auto sinValue{ sin(roll) };
return Matrix{
Vector3{cosValue,0.f,sinValue},
Vector3{-sinValue,0.f,cosValue},
Vector3{Vector3::UnitZ},
Vector3{} };
}
Matrix Matrix::CreateRotation(const Vector3& r)
{
return { CreateRotationX(r.x) * CreateRotationY(r.y) * CreateRotationZ(r.z) };
}
Matrix Matrix::CreateRotation(float pitch, float yaw, float roll)
{
return CreateRotation({ pitch, yaw, roll });
}
Matrix Matrix::CreateScale(float sx, float sy, float sz)
{
return Matrix{
Vector3{sx,0.f,0.f},
Vector3{0.f,sy,0.f},
Vector3{0.f,0.f,sz},
Vector3{} };
}
Matrix Matrix::CreateScale(const Vector3& s)
{
return CreateScale(s[0], s[1], s[2]);
}
#pragma region Operator Overloads
Vector4& Matrix::operator[](int index)
{
assert(index <= 3 && index >= 0);
return data[index];
}
Vector4 Matrix::operator[](int index) const
{
assert(index <= 3 && index >= 0);
return data[index];
}
Matrix Matrix::operator*(const Matrix& m) const
{
Matrix result{};
Matrix m_transposed = Transpose(m);
for (int r{ 0 }; r < 4; ++r)
{
for (int c{ 0 }; c < 4; ++c)
{
result[r][c] = Vector4::Dot(data[r], m_transposed[c]);
}
}
return result;
}
const Matrix& Matrix::operator*=(const Matrix& m)
{
Matrix copy{ *this };
Matrix m_transposed = Transpose(m);
for (int r{ 0 }; r < 4; ++r)
{
for (int c{ 0 }; c < 4; ++c)
{
data[r][c] = Vector4::Dot(copy[r], m_transposed[c]);
}
}
return *this;
}
#pragma endregion
}