-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
254 lines (194 loc) · 4.35 KB
/
matrix.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "matrix.h"
#include "util.h"
#include <cstring>
#include <cmath>
Mat4 Mat4::ZeroMat() {
Mat4 temp;
for(int i = 0; i < 16; i++)
temp.mat[i] = 0;
return temp;
}
Mat4 Mat4::IdentityMat() {
Mat4 temp = ZeroMat();
temp.mat[index(0,0)] = 1;
temp.mat[index(1,1)] = 1;
temp.mat[index(2,2)] = 1;
temp.mat[index(3,3)] = 1;
return temp;
}
Mat4 Mat4::ScaleMat(Vect4 v) {
Mat4 temp = IdentityMat();
double x = v[0] / v[3];
double y = v[1] / v[3];
double z = v[2] / v[3];
temp.mat[index(0,0)] = x;
temp.mat[index(1,1)] = y;
temp.mat[index(2,2)] = z;
return temp;
}
Mat4 Mat4::TranslateMat(Vect4 v) {
Mat4 temp = IdentityMat();
double x = v[0] / v[3];
double y = v[1] / v[3];
double z = v[2] / v[3];
temp.mat[index(0,3)] = x;
temp.mat[index(1,3)] = y;
temp.mat[index(2,3)] = z;
return temp;
}
Mat4 Mat4::RotateXMat(double theta) {
Mat4 temp = IdentityMat();
temp.mat[index(1,1)] = cos(theta);
temp.mat[index(1,2)] = sin(theta);
temp.mat[index(2,1)] = -1 * sin(theta);
temp.mat[index(2,2)] = cos(theta);
return temp;
}
Mat4 Mat4::RotateYMat(double theta) {
Mat4 temp = IdentityMat();
temp.mat[index(0,0)] = cos(theta);
temp.mat[index(0,2)] = sin(theta);
temp.mat[index(2,0)] = -1 * sin(theta);
temp.mat[index(2,2)] = cos(theta);
return temp;
}
Mat4 Mat4::RotateZMat(double theta) {
Mat4 temp = IdentityMat();
temp.mat[index(0,0)] = cos(theta);
temp.mat[index(0,1)] = sin(theta);
temp.mat[index(1,0)] = -1 * sin(theta);
temp.mat[index(1,1)] = cos(theta);
return temp;
}
Mat4 Mat4::ProjectPersp() {//Needs to be normalized after this
Mat4 temp = IdentityMat();
temp.mat[index(3,3)] = 0;
temp.mat[index(3,2)] = 1;
return temp;
}
/*
projectortho
*/
Mat4 Mat4::mult(Mat4 left, Mat4 right) {
Mat4 temp = ZeroMat();
for(int r = 0; r < 4; r++) {
for(int c = 0; c < 4; c++) {
double sum = 0;
for(int i = 0; i < 4; i++) {
sum += left.mat[index(r, i)] * right.mat[index(i, c)];
}
temp.mat[index(r, c)] = sum;
}
}
return temp;
}
std::string Mat4::to_string() {
std::string temp;
temp.append("[\n");
for(int c = 0; c < 4; c++) {
for(int r = 0; r < 4; r++) {
temp.append(std::to_string(mat[index(r, c)]));
temp.append(", ");
}
temp.append("\n");
}
temp.append("]\n");
return temp;
}
Vect4::Vect4() {coord[3] = 1;}
Vect4::Vect4(double x, double y, double z) {
coord[0] = x;
coord[1] = y;
coord[2] = z;
coord[3] = 1;
}
Vect4 Mat4::mult(Vect4 v) { //Multiply a vect4 by this
Vect4 temp;
for(int r = 0; r < 4; r++) {
temp.coord[r] = 0;
for(int i = 0; i < 4; i++) {
temp.coord[r] += mat[index(r, i)] * v.coord[i];
}
}
return temp;
}
int Mat4::index(int r, int c) { //0 indexed
return r + c * 4;
}
Vect4 Vect4::Zero() {
Vect4 temp;
temp.coord[0] = 0;
temp.coord[1] = 0;
temp.coord[2] = 0;
temp.coord[3] = 1;
return temp;
}
double& Vect4::operator[] (const int& i) {
return coord[i];
}
const Vect4 Vect4::operator+ (const Vect4 &rhs) {
return Vect4(
coord[0] + rhs.coord[0],
coord[1] + rhs.coord[1],
coord[2] + rhs.coord[2]);
}
const Vect4 Vect4::operator- (const Vect4 &rhs) {
return Vect4(
coord[0] - rhs.coord[0],
coord[1] - rhs.coord[1],
coord[2] - rhs.coord[2]);
}
const Vect4 Vect4::operator* (const double &rhs) {
return Vect4(
coord[0] * rhs,
coord[1] * rhs,
coord[2] * rhs);
}
double Vect4::magnitude() {
return sqrt(coord[0] * coord[0] +
coord[1] * coord[1] +
coord[2] * coord[2]);
}
Vect4 Vect4::inverse() {
return Vect4 (-1 * coord[0], -1 * coord[1], -1 * coord[2]);
}
std::string Vect4::to_string() {
std::string temp;
temp.append("");
temp.append(std::to_string(coord[0]));
temp.append(", ");
temp.append(std::to_string(coord[1]));
temp.append(", ");
temp.append(std::to_string(coord[2]));
temp.append(", ");
temp.append(std::to_string(coord[3]));
return temp;
}
Vect4 Vect4::vLerp(Vect4 a, Vect4 b, double t) {
return Vect4(
lerp(a[0],b[0], t),
lerp(a[1],b[1], t),
lerp(a[2],b[2], t));
}
//+X x +Z
//0 - (1 0)
//(0 * 1) - (1 1)
//(1 0) - (0 0)
Vect4 Vect4::cross(Vect4 l, Vect4 r) {
return Vect4(
l[1] * r[2] - l[2] * r[1],
l[2] * r[0] - l[0] * r[2],
l[0] * r[1] - l[1] * r[0]);
}
double Vect4::crossZ(Vect4 l, Vect4 r) {
return l[0] * r[1] - l[1] * r[0];
}
double Vect4::dot(Vect4 l, Vect4 r) {
return
l[0] * r[0] +
l[1] * r[1] +
l[2] * r[2];
}
Vect4 Vect4::unit(Vect4 v) {
return v * (1 / v.magnitude());
}