-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.cpp
427 lines (323 loc) · 7.86 KB
/
mesh.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "mesh.h"
#include <cstring>
#include <cstdlib>
#include <cmath>
using std::endl;
Face::Face() {}
Face::Face(int a, int b, int c) {
v1 = a;
v2 = b;
v3 = c;
}
std::string Face::to_string() {
std::string temp;
temp.append("");
temp.append(std::to_string(v1));
temp.append(", ");
temp.append(std::to_string(v2));
temp.append(", ");
temp.append(std::to_string(v3));
return temp;
}
Mesh::Mesh()
: Entity() {
}
Mesh::Mesh(int deleteonremove)
: Entity(), deleteOnRemove(deleteonremove) {
}
Mesh::Mesh(const Mesh& other) :
markedForDeath(other.markedForDeath),
deleteOnRemove(other.deleteOnRemove),
verts(std::vector<Vect4>(other.verts)),
faces(std::vector<Face>(other.faces)) {
}
Mesh::~Mesh() {
clear();
}
void Mesh::clear() {
verts.clear();
faces.clear();
}
int Mesh::addVert(Vect4 v) {
verts.push_back(v);
return verts.size() - 1;
}
void Mesh::addFace(Face f) {
faces.push_back(f);
}
//Replaces next occurence of delim with null
//Returns start of next string, or null if end
char *Mesh::myStrtok(char *s, char delim) {
while(1) {
//Checl if hit end
if(!*s)
return NULL;
//Check if hit delimiter
if(*s == delim) {
//null all delimiters
while(1) {
*s = '\0';
s++;
if(!*s)
return NULL;
if(*s != delim) {
return s;
}
}
}
//If not, keep going
s++;
}
}
void Mesh::loadFromObjFile(std::string filename) {
addVert(Vect4(0,0,0));//dummy vert to account for 1-indexing
std::ifstream fs;
int numread = 0;
char buffer[1024];
fs.open(filename);
while(!fs.eof()) {
char *next, *curr;
fs.getline(buffer, 1024);
curr = buffer;
next = myStrtok(curr, ' ');
//Handles blank lines
if(*curr == '\0')
continue;
if(!strcmp(buffer, "#")) {
std::cerr << "Ignoring comment" << endl;
} else if(!strcmp(buffer, "o")) {
//std::cerr << "ignoring o for now" << endl;
} else if(!strcmp(buffer, "v")) {
//std::cerr << "handling v" << endl;
Vect4 v;
char *temp = next; //points to start of first float
for(int i = 0; i < 3; i++)
v.coord[i] = strtod(temp, &temp);
v.coord[3] = 1;
addVert(v);
} else if(!strcmp(buffer, "vt")) {
//std::cerr << "ignoring vt for now" << endl;
} else if(!strcmp(buffer, "vn")) {
//std::cerr << "ignoring vn for now" << endl;
} else if(!strcmp(buffer, "f")) {
Face f;
char *temp = next; //points to start of first int
f.v1 = strtol(temp, &temp, 0);
while(*temp++ != ' ')
;
f.v2 = strtol(temp, &temp, 0);
while(*temp++ != ' ')
;
f.v3 = strtol(temp, &temp, 0);
//std::cerr << f.v1 << ", " << f.v2 << ", " << f.v3 << "\n";
addFace(f);
//std::cerr << "Woo! f" << endl;
} else if(!strcmp(buffer, "s")) {
std::cerr << "ignored s" << endl;
} else {
//Delay
for(int i = 0; i < 100 * 1000 * 1000; i++)
;
std::cerr << "Unkown start word: '" << buffer << "'" << std::endl;
}
}
fs.close();
}
void Mesh::genPrimEdge(Vect4 a, Vect4 b) {
int av = addVert(a);
int bv = addVert(b);
addFace(Face {av, bv, bv});
}
void Mesh::genPrimCircle(Vect4 c, double r) {
double t = 0;
const int edges = 50;
double lastx, lasty, currx, curry;
lasty = sin(t) * r;
lastx = cos(t) * r;
for(int i = 1; i <= edges; i++) {
t = (TWOPI / edges) * i;
currx = cos(t) * r;
curry = sin(t) * r;
genPrimEdge(
Vect4(currx + c[0], curry + c[1], c[2]),
Vect4(lastx + c[0], lasty + c[1], c[2]));
lastx = currx;
lasty = curry;
}
}
//Start, start control, end control, end
void Mesh::genPrimBezier(Vect4 a, Vect4 b, Vect4 c, Vect4 d) {
double t = 0;
const int edges = 50;
double lastx, lasty, currx, curry;
/*
genPrimEdge(a, b);
genPrimEdge(c, d);
*/
Vect4 ab;
Vect4 bc;
Vect4 cd;
Vect4 ab_bc;
Vect4 bc_cd;
Vect4 end;
Vect4 last = a; //starts at a
for(int i = 1; i <= edges; i++) {
t = (1.0 / edges) * i;
ab = Vect4::vLerp(a, b, t);
bc = Vect4::vLerp(b, c, t);
cd = Vect4::vLerp(c, d, t);
ab_bc = Vect4::vLerp(ab, bc, t);
bc_cd = Vect4::vLerp(bc, cd, t);
end = Vect4::vLerp(ab_bc, bc_cd, t);
genPrimEdge(last, end);
last = end;
}
}
void Mesh::genPrimHermite(Vect4 p0, Vect4 p1, Vect4 r0, Vect4 r1) {
const int edges = 80;
double lastx, lasty, currx, curry;
/*
genPrimEdge(p0, p1);
genPrimEdge(p0, p0 + r0);
genPrimEdge(p1, p1 + r1);
*/
Vect4 curr;
Vect4 last = p0; //starts at p0
for(int i = 1; i <= edges; i++) {
double t = (1.0 / edges) * i;
double t2 = t * t;
double t3 = t2 * t;
double h0 = 2 * t3 - 3 * t2 + 1;
double h1 = -2 * t3 + 3 * t2;
double h2 = t3 - 2 * t2 + t;
double h3 = t3 - t2;
//Do this for each coordinate
for(int j = 0; j < 3; j++) {
curr[j] =
h0 * p0[j] +
h1 * p1[j] +
h2 * r0[j] +
h3 * r1[j];
}
genPrimEdge(last, curr);
last = curr;
}
}
void Mesh::genPrimBox(double lx, double ly, double lz) {
lx *= lx > 0 ? 1 : -1;
ly *= ly > 0 ? 1 : -1;
lz *= lz > 0 ? 1 : -1;
int v0 = addVert(Vect4( 0, 0, 0));
int v1 = addVert(Vect4(lx, 0, 0));
int v2 = addVert(Vect4( 0, ly, 0));
int v3 = addVert(Vect4( 0, 0, lz));
int v4 = addVert(Vect4( 0, ly, lz));
int v5 = addVert(Vect4(lx, 0, lz));
int v6 = addVert(Vect4(lx, ly, 0));
int v7 = addVert(Vect4(lx, ly, lz));
//0 xy face
addFace(Face(v0, v2, v1));
addFace(Face(v6, v1, v2));
//far xy face
addFace(Face(v4, v3, v5));
addFace(Face(v5, v7, v4));
//0 xz face
addFace(Face(v0, v1, v3));
addFace(Face(v5, v3, v1));
//far xz face
addFace(Face(v6, v2, v4));
addFace(Face(v4, v7, v6));
//0 yz face
addFace(Face(v0, v3, v2));
addFace(Face(v4, v2, v3));
//far yz face
addFace(Face(v5, v1, v6));
addFace(Face(v6, v7, v5));
}
void Mesh::genPrimSphere(double r) {
const int slices = 20;
const int pps = 10;//points per slice
double lastx, lasty, currx, curry;
int top = addVert(Vect4(0, r, 0));
int bot = addVert(Vect4(0, -r, 0));
//Contains all the vert indices except the top and bottom
int points[slices][pps - 1];
for(int i = 0; i < slices; i++) {
double phi = (TWOPI / slices) * i;
for(int j = 1; j < pps; j++) {
double theta = (PI / pps) * j;
//theta 0 -> top, theta pi -> bottom
//phi 0 -> +x, goes counterclockwise from top
double vert = r * cos(theta);
double horiz = r * sin(theta);
Vect4 temp;
temp[0] = horiz * sin(phi);
temp[1] = vert;
temp[2] = horiz * cos(phi);
points[i][j - 1] = addVert(temp);
}
}
//make caps
for(int i = 0; i < slices; i++) {
addFace(Face(
top,
points[i][0],
points[(i + 1) % slices][0]));
addFace(Face(
bot,
points[(i + 1) % slices][pps - 2],
points[i][pps - 2]));
}
//make body
for(int i = 0; i < slices; i++) {
for(int j = 1; j < pps - 1; j++) {
//bottomleft
addFace(Face(
points[i][j - 1],
points[i][j],
points[(i + 1) % slices][j]));
//topright
addFace(Face(
points[(i + 1) % slices][j - 1],
points[i][j - 1],
points[(i + 1) % slices][j]));
}
}
}
void Mesh::genPrimTorus(double R, double r) {
const int circles = 20;
const int ppc = 15;//points per circle
double lastx, lasty, currx, curry;
int points[circles][ppc];
for(int i = 0; i < circles; i++) {
double phi = (TWOPI / circles) * i;
Mat4 currRot = Mat4::mult(Mat4::RotateYMat(phi), Mat4::TranslateMat(Vect4(R, 0 , 0)));
for(int j = 0; j < ppc; j++) {
double theta = (TWOPI / ppc) * j;
//theta 0 -> top, theta pi -> bottom
//phi 0 -> +x, goes counterclockwise from top
double vert = r * cos(theta);
double horiz = r * sin(theta);
Vect4 temp;
temp[0] = horiz;
temp[1] = vert;
temp[2] = 0;
points[i][j] = addVert(currRot.mult(temp));
}
}
//make body
for(int i = 0; i < circles; i++) {
for(int j = 0; j < ppc; j++) {
//bottomleft
addFace(Face(
points[i][j],
points[i][(j + 1) % ppc],
points[(i + 1) % circles][(j + 1) % ppc]));
//topright
addFace(Face(
points[(i + 1) % circles][j],
points[i][j],
points[(i + 1) % circles][(j + 1) % ppc]));
}
}
}