-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.h
214 lines (166 loc) · 4.28 KB
/
geometry.h
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
#include <algorithm>
#include <vector>
#include <fstream>
#include <string>
#include "utils.h"
#ifndef RASTERIZER_GEOMETRY_H
#define RASTERIZER_GEOMETRY_H
struct Vec2 {
float x, y;
Vec2() {}
Vec2(float _x, float _y) {x = _x, y = _y;}
Vec2 operator+(const Vec2 v) {
return {x + v.x, y + v.y};
}
Vec2& operator+=(const Vec2 v) {
this->x += v.x;
this->y += v.y;
return *this;
}
bool operator==(const Vec2 v) {
return x == v.x && y == v.y;
}
bool operator!=(const Vec2 v) {
return x != v.x || y != v.y;
}
};
struct Vec3 {
float x, y, z;
Vec3() {
x = y = z = 0;
}
Vec3(float _x, float _y, float _z) { x = _x, y = _y; z = _z; }
float length() {
return sqrt(x * x + y * y + z * z);
}
void normalize() {
float l = length();
if (l != 0) {
x /= l;
y /= l;
z /= l;
}
}
float dotProduct(const Vec3& v) {
return x * v.x + y * v.y + z * v.z;
}
Vec3 crossProduct(const Vec3& v) {
return {
y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x
};
}
Vec3 operator+(const Vec3 v) {
return {x + v.x, y + v.y, z + v.z};
}
Vec3 operator-(const Vec3 v) {
return {x - v.x, y - v.y, z - v.z};
}
Vec3 operator*(const float v) {
return {x * v, y * v, z * v};
}
Vec3& operator+=(const Vec3 v) {
this->x += v.x;
this->y += v.y;
this->z += v.z;
return *this;
}
Vec3& operator+=(const float v) {
this->x += v;
this->y += v;
this->z += v;
return *this;
}
bool operator==(const Vec3 v) {
return x == v.x && y == v.y && z == v.z;
}
bool operator!=(const Vec3 v) {
return x != v.x || y != v.y || z != v.z;
}
};
class Triangle2d {
public:
Vec2 v[3];
Triangle2d() {}
Triangle2d(Vec2 _v0, Vec2 _v1, Vec2 _v2) {
v[0] = _v0;
v[1] = _v1;
v[2] = _v2;
}
Triangle2d(Vec2 _v[3]) {
v[0] = _v[0];
v[1] = _v[1];
v[2] = _v[2];
}
bool operator==(const Triangle2d t) {
return v[0] == t.v[0] && v[1] == t.v[1] && v[2] == t.v[2];
}
bool operator!=(const Triangle2d t) {
return v[0] != t.v[0] || v[1] != t.v[1] || v[2] != t.v[2];
}
};
struct Triangle {
Vec3 v[3];
Triangle() {}
Triangle(Vec3 _v0, Vec3 _v1, Vec3 _v2) {
v[0] = _v0;
v[1] = _v1;
v[2] = _v2;
}
bool operator==(const Triangle t) {
return v[0] == t.v[0] && v[1] == t.v[1] && v[2] == t.v[2];
}
bool operator!=(const Triangle t) {
return v[0] != t.v[0] || v[1] != t.v[1] || v[2] != t.v[2];
}
Triangle2d toTriangle2d() {
return {
{v[0].x, v[0].y},
{v[1].x, v[1].y},
{v[2].x, v[2].y}
};
}
};
struct Plane {
Vec3 point, normal;
Plane() {}
Plane(Vec3 _point, Vec3 _normal) {
point = _point;
normal = _normal;
}
};
struct Mesh {
std::vector<Triangle> tris;
Mesh() {}
Mesh(std::vector<Triangle> v) {
tris.swap(v);
}
static Mesh loadFromFile(const char *filename) {
std::ifstream obj(filename);
if (!obj.is_open())
return Mesh();
Mesh ret;
std::string line;
std::vector<Vec3> vertices;
while(std::getline(obj, line)) {
if (line[0] == 'v') {
// read a vertex
std::vector <std::string> values = splitString(line, " ");
float v0 = std::stof(values[1]);
float v1 = std::stof(values[2]);
float v2 = std::stof(values[3]);
vertices.emplace_back(Vec3(v0, v1, v2));
} else if (line[0] == 'f') {
std::vector <std::string> values = splitString(line, " ");
int v0 = std::stoi(values[1]);
int v1 = std::stoi(values[2]);
int v2 = std::stoi(values[3]);
ret.tris.emplace_back(Triangle(vertices[v0 - 1], vertices[v1 - 1], vertices[v2 - 1]));
}
}
obj.close();
return ret;
}
};
#endif //RASTERIZER_GEOMETRY_H