-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
103 lines (88 loc) · 2.29 KB
/
Copy pathmatrix.cpp
File metadata and controls
103 lines (88 loc) · 2.29 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
#include "matrix.hpp"
Mat::Mat(std::initializer_list<float> list) {
int i = 0;
for (float val : list) {
if (i < D * D) values[i++] = val;
}
}
float& Mat::operator()(int r, int c) {
return values[r * D + c];
}
Mat Mat::operator*(Mat other) {
Mat result;
int D = result.D;
for(int i=0; i < D*D; i++) result.values[i] = 0.0f;
for (int i = 0; i < D; i++) {
for (int j = 0; j < D; j++) {
for (int k = 0; k < D; k++) {
result(i, j) += (*this)(i, k) * other(k, j);
}
}
}
return result;
}
Vec4 Mat::operator*(Vec4 vec) {
Vec4 res;
for (int i = 0; i < D; i++) {
res.v[i]=0;
for (int j = 0; j < D; j++) {
res.v[i] += (*this)(i, j) * vec.v[j];
}
}
return res;
}
void Mat::print() {
for (int r = 0; r < D; r++) {
for (int c = 0; c < D; c++) {
std::cout << (*this)(r, c) << "\t";
}
std::cout << "\n";
}
};
void Vec4::print() {
std::cout << "(" << (*this).x << " " << (*this).y << " " << (*this).z << " " << (*this).w << ")\n";
}
Vec4 intersect(Vec4 inside, Vec4 outside, float zn) {
float t = (zn - inside.w) / (outside.w - inside.w);
return inside + (outside - inside) * t;
}
Vec4 Vec4::norm() {
Vec4 curr = (*this);
Vec4 vec = Vec4(0, 0, 0, (*this).w);
float length = std::sqrt(curr.x*curr.x + curr.y*curr.y + curr.z*curr.z);
vec.x = curr.x/length;
vec.y = curr.y/length;
vec.z = curr.z/length;
return vec;
}
Vec4 Vec4::operator+(const Vec4 other) {
return Vec4((*this).x+other.x, (*this).y+other.y, (*this).z+other.z, 1);
}
Vec4 Vec4::operator-(const Vec4 other) {
return Vec4((*this).x-other.x, (*this).y-other.y, (*this).z-other.z, 1);
}
Vec4 Vec4::operator*(const float coef) {
float values[4];
for (int i = 0; i < 4; i++) {
values[i] = (*this).v[i] * coef;
}
return Vec4(values[0], values[1], values[2], values[3]);
}
Vec2 Vec4::project(Mat comp_matrix, std::pair<int, int> screen_size) {
Vec4 V_screen = comp_matrix * (*this);
float x = V_screen.x;
float y = V_screen.y;
float w = V_screen.w;
return Vec2((x/w + 1)/2 * screen_size.first, (1 - y/w)/2 * screen_size.second, w);
}
float dot(Vec4 u, Vec4 v) {
return u.x*v.x + u.y*v.y + u.z*v.z;
}
Vec4 cross(Vec4 u, Vec4 v) {
return Vec4(
u.y * v.z - u.z * v.y,
u.z * v.x - u.x * v.z,
u.x * v.y - u.y * v.x,
0.0f
);
}