-
Notifications
You must be signed in to change notification settings - Fork 0
/
HCoord.cpp
187 lines (162 loc) · 5.16 KB
/
HCoord.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
/******************************************************************************
* @file HCoord.cpp
* @author Andrés Gavín Murillo, 716358
* @author Abel Naya Forcano, 544125
* @date Enero 2020
* @coms Informática Gráfica - Trabajo 4: Path tracer
******************************************************************************/
#include "HCoord.hpp"
#include <cmath>
#include <cassert>
#include <vector>
using namespace std;
HCoord hPoint(float x, float y, float z) {
return {{x, y, z, 1.0f}};
}
HCoord hVector(float x, float y, float z) {
return {{x, y, z, 0.0f}};
}
float dot(const HCoord &a, const HCoord &b) {
assert(a.isVector() && b.isVector());
return a.e[0] * b.e[0] + a.e[1] * b.e[1] + a.e[2] * b.e[2];
}
HCoord cross(const HCoord &a, const HCoord &b) {
assert(a.isVector() && b.isVector());
return {{
a.e[1] * b.e[2] - a.e[2] * b.e[1],
a.e[2] * b.e[0] - a.e[0] * b.e[2],
a.e[0] * b.e[1] - a.e[1] * b.e[0],
0
}};
}
HCoord norm(const HCoord &h) {
assert(h.isVector());
float modh = mod(h);
return {{
h.e[0] / modh,
h.e[1] / modh,
h.e[2] / modh,
0
}};
}
float mod(const HCoord &h) {
assert(h.isVector());
return sqrt(h.e[0] * h.e[0] + h.e[1] * h.e[1] + h.e[2] * h.e[2]);
}
ostream &operator<<(ostream &o, const HCoord &h) {
if (h.isVector()) {
o << "Vector(" << h.e[0] << ", " << h.e[1] << ", " << h.e[2] << ")";
} else {
o << "Point(" << h.x() << ", " << h.y() << ", " << h.z() << ")";
}
return o;
}
bool HCoord::operator==(const HCoord &h) const {
// check if one is vector and the other isn't
if (h.isVector() != this->isVector()) return false;
if (h.isVector()) {
// two vectors, no third component
return this->e[0] == h.e[0] && this->e[1] == h.e[1] && this->e[2] == h.e[2];
} else {
// two points, same coordinates
return this->x() == h.x() && this->y() == h.y() && this->z() == h.z();
}
}
HCoord HCoord::operator+(const HCoord &right) const {
if (this->isVector()) {
if (right.isVector()) {
// vector + vector = vector
return {{
this->e[0] + right.e[0],
this->e[1] + right.e[1],
this->e[2] + right.e[2],
0
}};
} else {
// vector + point = point
float w = right.e[3];
return {{
this->e[0] * w + right.e[0],
this->e[1] * w + right.e[1],
this->e[2] * w + right.e[2],
w
}};
}
} else {
if (right.isVector()) {
// point + vector = point
float w = this->e[3];
return {{
this->e[0] + right.e[0] * w,
this->e[1] + right.e[1] * w,
this->e[2] + right.e[2] * w,
w
}};
} else {
// point + point = ERROR
assert(false);
}
}
}
HCoord HCoord::operator-(const HCoord &right) const {
if (this->isVector()) {
if (right.isVector()) {
// vector - vector = vector
return {{
this->e[0] - right.e[0],
this->e[1] - right.e[1],
this->e[2] - right.e[2],
0
}};
} else {
// vector - point = ERROR
assert(false);
}
} else {
if (right.isVector()) {
// point - vector = point
float w = this->e[3];
return {{
this->e[0] - right.e[0] * w,
this->e[1] - right.e[1] * w,
this->e[2] - right.e[2] * w,
w
}};
} else {
// point - point = vector
return {{
this->x() - right.x(),
this->y() - right.y(),
this->z() - right.z(),
0
}};
}
}
}
HCoord HCoord::operator-() const {
return {{-this->e[0], -this->e[1], -this->e[2], this->e[3]}};
}
HCoord HCoord::operator*(float s) const {
assert(this->isVector());
return {{
this->e[0] * s,
this->e[1] * s,
this->e[2] * s,
0
}};
}
float HCoord::x() const {
return this->isVector() ? this->e[0] : this->e[0] / this->e[3];
}
float HCoord::y() const {
return this->isVector() ? this->e[1] : this->e[1] / this->e[3];
}
float HCoord::z() const {
return this->isVector() ? this->e[2] : this->e[2] / this->e[3];
}
bool HCoord::isVector() const {
return this->e[3] == 0;
}
vector<float> HCoord::as_vector() const {
return {this->x(), this->y(), this->z()};
}