obj.h
14.4 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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#ifndef STIM_OBJ_H
#define STIM_OBJ_H
#include <vector>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <stim/parser/parser.h>
#include <stim/math/mathvec.h>
namespace stim{
/** This class provides an interface for loading, saving, and working with Wavefront OBJ files.
* The class structure uses similar terminology to the OBJ file structure, so one familiar with it
* should be able to work well with this class.
*
* This class currently provides the ability to load explicitly defined geometric objects, including point lists,
* lines, and faces with positions, texture coordinates, and normals. This data is stored in a protected series
* of lists that are accessible to classes that extend this one.
*
* Multiple helper classes are used to facilitate loading, storing, and saving geometry. These are protected
* and therefore only designed to be used within the confines of the class. These helper classes include:
*
* vertex class - contains methods for loading, saving, and storing 1, 2, 3, and 4 component vectors
* triplet class - contains methods for loading, saving, and storing indexed vertices used for geometric objects
* geometry class - contains a list of triplets used to define a geometric structure, such as a face or line
*/
enum obj_type { OBJ_NONE, OBJ_LINE, OBJ_FACE, OBJ_POINTS };
template <typename T>
class obj{
protected:
enum token_type { OBJ_INVALID, OBJ_V, OBJ_VT, OBJ_VN, OBJ_P, OBJ_L, OBJ_F };
struct vertex : public stim::vec<T>{
using vec<T>::push_back;
using vec<T>::size;
using vec<T>::at;
using vec<T>::resize;
//basic constructors (call the stim::vector constructors)
vertex(){}
vertex(T x){
resize(1);
at(0) = x;
}
vertex(T x, T y) : stim::vec<T>(x, y){}
vertex(T x, T y, T z) : stim::vec<T>(x, y, z){}
vertex(T x, T y, T z, T w) : stim::vec<T>(x, y, z, w){}
//constructor creates a vertex from a line string
vertex(std::string line){
//create a temporary vector used to store string tokens
std::vector<std::string> tokens = stim::parser::split(line, ' ');
//create temporary storage for casting the token values
T val;
//for each token (skipping the first)
for(unsigned int i = 1; i < tokens.size(); i++){
std::stringstream ss(tokens[i]); //create a stringstream object for casting
ss>>val; //cast the token to the correct numerical value
push_back(val); //push the coordinate into the vertex
}
}
//output the vertex as a string
std::string str(){
std::stringstream ss;
for(int i = 0; i < size(); i++){
if(i > 0)
ss<<' ';
ss<<at(i);
}
return ss.str(); //return the vertex string
}
}; //end vertex
//triplet used to specify geometric vertices consisting of a position vertex, texture vertex, and normal
struct triplet : public std::vector<unsigned int>{
//create a triplet given a parameter list (OBJ indices start at 1, so 0 can be used to indicate no value)
triplet(unsigned int v, unsigned int vt = 0, unsigned int vn = 0){
push_back(v);
if(vn != 0){
push_back(vt);
push_back(vn);
}
else if(vt != 0)
push_back(vt);
}
//build a triplet from a string
triplet(std::string s){
//create a temporary vector used to store string tokens
std::vector<std::string> tokens = stim::parser::split(s, '\\');
//std::cout<<"processing triplet: "<<s<<std::endl;
if(tokens.size() >= 1)
push_back( atoi( tokens[0].c_str() ) );
if(tokens.size() >= 2){
if(tokens[1].length())
push_back( atoi( tokens[1].c_str() ) );
else
push_back(0);
}
if(tokens.size() == 3)
push_back( atoi( tokens[2].c_str() ) );
}
//output the geometry as a string
std::string str(){
std::stringstream ss;
ss<<at(0); //the vertex is always the first value
if(size() == 3){
if(at(1) == 0)
ss<<"\\\\"<<at(2);
else
ss<<'\\'<<at(1)<<'\\'<<at(2);
}
else if(size() == 2)
ss<<"\\"<<at(1);
return ss.str();
}
}; //end triplet
//geometry used to specify a geometric structure consisting of several triplets
struct geometry : public std::vector<triplet>{
using std::vector<triplet>::size;
using std::vector<triplet>::at;
using std::vector<triplet>::push_back;
geometry(){}
//constructs a geometry object from a line read from an OBJ file
geometry(std::string line){
//create a temporary vector used to store string tokens
std::vector<std::string> tokens = stim::parser::split(line, ' ');
//for each triplet in the line
for(unsigned int i = 1; i < tokens.size(); i++){
//construct the triplet and push it to the geometry
push_back( triplet(tokens[i]) );
}
}
std::string str(){
std::stringstream ss;
for(unsigned int i = 0; i < size(); i++){
if(i != 0)
ss<<' ';
ss<<at(i).str();
}
return ss.str();
}
};
std::vector<vertex> V; //vertex spatial position
std::vector<vertex> VN;
std::vector<vertex> VT;
//structure lists
std::vector<geometry> L; //list of lines
std::vector<geometry> P; //list of points structures
std::vector<geometry> F; //list of faces
//information for the current geometric object
geometry current_geo;
vertex current_vt;
vertex current_vn;
//flags for the current geometric object
obj_type current_type;
bool geo_flag_vt;
bool geo_flag_vn;
bool vert_flag_vt;
bool vert_flag_vn;
void update_vt(vertex vt){
current_vt = vt;
//the geometry vertex texture flag can only be set for the first vertex
if(current_geo.size() == 0)
geo_flag_vt = true;
vert_flag_vt = true;
}
void update_vn(vertex vn){
current_vn = vn;
//the geometry normal flag can only be set for the first vertex
if(current_geo.size() == 0)
geo_flag_vn = true;
vert_flag_vn = true;
}
//create a triple and add it to the current geometry
void update_v(vertex vv){
unsigned int v;
unsigned int vt = 0;
unsigned int vn = 0;
//if the current geometry is using a texture coordinate, add the current texture coordinate to the geometry
if(geo_flag_vt){
if(vert_flag_vt) //don't add it more than once
VT.push_back(current_vt);
vt = VT.size();
}
//if the current geometry is using a normal, add the current texture coordinate to the geometry
if(geo_flag_vn){
if(vert_flag_vn) //don't add it more than once
VN.push_back(current_vn);
vn = VN.size();
}
//add the current vertex position to the geometry
V.push_back(vv);
v = V.size();
//create a triplet and add it to the current geometry
current_geo.push_back(triplet(v, vt, vn));
//clear the vertex flags
vert_flag_vt = false;
vert_flag_vn = false;
}
void init(){
//clear all lists
V.clear();
VT.clear();
VN.clear();
P.clear();
L.clear();
F.clear();
//initialize all of the flags
current_type = OBJ_NONE;
geo_flag_vt = false;
geo_flag_vn = false;
vert_flag_vt = false;
vert_flag_vn = false;
}
//gets the type of token representing the entry in the OBJ file
token_type get_token(std::string s){
//if the line contains a vertex
if(s[0] == 'v'){
if(s[1] == ' ') return OBJ_V;
if(s[1] == 't') return OBJ_VT;
if(s[1] == 'n') return OBJ_VN;
}
if(s[0] == 'l' && s[1] == ' ') return OBJ_L;
if(s[0] == 'p' && s[1] == ' ') return OBJ_P;
if(s[0] == 'f' && s[1] == ' ') return OBJ_F;
return OBJ_INVALID;
}
public:
/// Constructor loads a Wavefront OBJ file
obj(std::string filename){
load(filename);
}
//functions for setting the texture coordinate for the next vertex
void TexCoord(T x){ update_vt(vertex(x));}
void TexCoord(T x, T y){ update_vt(vertex(x, y));}
void TexCoord(T x, T y, T z){ update_vt(vertex(x, y, z));}
void TexCoord(T x, T y, T z, T w){ update_vt(vertex(x, y, z, w));}
//functions for setting the normal for the next vertex
void Normal(T x){ update_vn(vertex(x));}
void Normal(T x, T y){ update_vn(vertex(x, y));}
void Normal(T x, T y, T z){ update_vn(vertex(x, y, z));}
void Normal(T x, T y, T z, T w){ update_vn(vertex(x, y, z, w));}
//functions for setting the next vertex position (note that this updates the current geometry)
void Vertex(T x){ update_v(vertex(x));}
void Vertex(T x, T y){ update_v(vertex(x, y));}
void Vertex(T x, T y, T z){ update_v(vertex(x, y, z));}
void Vertex(T x, T y, T z, T w){ update_v(vertex(x, y, z, w));}
///This function starts drawing of a primitive object, such as a line, face, or point set
/// @param t is the type of object to be drawn: OBJ_POINTS, OBJ_LINE, OBJ_FACE
void Begin(obj_type t){
current_type = t;
}
/// This function terminates drawing of a primitive object, such as a line, face, or point set
void End(){
//copy the current object to the appropriate list
switch(current_type){
case OBJ_NONE:
std::cout<<"STIM::OBJ error, objEnd() called before objBegin()."<<std::endl;
break;
case OBJ_POINTS:
P.push_back(current_geo);
break;
case OBJ_LINE:
L.push_back(current_geo);
break;
case OBJ_FACE:
F.push_back(current_geo);
}
//clear everything
current_type = OBJ_NONE;
current_geo.clear();
vert_flag_vt = false;
vert_flag_vn = false;
geo_flag_vt = false;
geo_flag_vn = false;
}
//output the OBJ structure as a string
std::string str(){
std::stringstream ss;
unsigned int i;
//output all of the vertices
if(V.size()){
ss<<"#vertex positions"<<std::endl;
for(i = 0; i < V.size(); i++){
ss<<"v "<<V[i].str()<<std::endl;
}
}
//output all of the texture coordinates
if(VT.size()){
ss<<std::endl<<"#vertex texture coordinates"<<std::endl;
for(i = 0; i < VT.size(); i++){
ss<<"vt "<<VT[i].str()<<std::endl;
}
}
//output all of the normals
if(VN.size()){
ss<<std::endl<<"#vertex normals"<<std::endl;
for(i = 0; i < VN.size(); i++){
ss<<"vn "<<VN[i].str()<<std::endl;
}
}
//output all of the points
if(P.size()){
ss<<std::endl<<"#point structures"<<std::endl;
for(i = 0; i < P.size(); i++){
ss<<"p "<<P[i].str()<<std::endl;
}
}
//output all of the lines
if(L.size()){
ss<<std::endl<<"#line structures"<<std::endl;
for(i = 0; i < L.size(); i++){
ss<<"l "<<L[i].str()<<std::endl;
}
}
//output all of the lines
if(F.size()){
ss<<std::endl<<"#face structures"<<std::endl;
for(i = 0; i < F.size(); i++){
ss<<"f "<<F[i].str()<<std::endl;
}
}
return ss.str(); //return the constructed string
}
obj(){
init(); //private function that initializes everything
}
void clear(){
init();
}
/// This function saves the current structure as a Wavefront OBJ file
/// @param filename is the name of the file to be saved
bool save(std::string filename){
std::ofstream outfile(filename.c_str());
if(!outfile){
std::cout<<"STIM::OBJ error opening file for writing"<<std::endl;
return false;
}
//output the OBJ data to the file
outfile<<str();
//close the file
outfile.close();
return true;
}
/// Load a Wavefront OBJ file (support for faces, lines, and point lists)
/// @param filename is the name of the OBJ file to load
bool load(std::string filename){
//load the file
std::ifstream infile(filename.c_str());
//if the file is invalid, throw an error
if(!infile){
std::cout<<"STIM::OBJ Error loading file "<<filename<<std::endl;
return false;
}
std::string line;
getline(infile, line); //get the first line
while(infile){
//get the token representing the first parameter
token_type token = get_token(line);
switch(token){
case OBJ_INVALID:
break;
case OBJ_V:
V.push_back(vertex(line));
break;
case OBJ_VT:
VT.push_back(vertex(line));
break;
case OBJ_VN:
VN.push_back(vertex(line));
break;
case OBJ_F:
F.push_back(geometry(line));
break;
case OBJ_L:
L.push_back(geometry(line));
break;
};
//get the next line
getline(infile, line);
}
//close the file
infile.close();
return true;
}
/// Return the number of position vertices in the OBJ model
unsigned int numV(){
return V.size();
}
/// Retrieve the vertex stored in index i
/// @param i is the desired vertex index
stim::vec<T> getV(unsigned int i){
stim::vec<T> v = V[i];
return v;
}
stim::vec<T> centroid(){
//get the number of coordinates
unsigned int N = V[0].size();
//allocate space for the minimum and maximum coordinate points (bounding box corners)
stim::vec<float> vmin, vmax;
vmin.resize(N);
vmax.resize(N);
//find the minimum and maximum value for each coordinate
unsigned int NV = V.size();
for(unsigned int v = 0; v < NV; v++)
for(unsigned int i = 0; i < N; i++){
if(V[v][i] < vmin[i])
vmin[i] = V[v][i];
if(V[v][i] > vmax[i])
vmax[i] = V[v][i];
}
//find the centroid using the min and max points
stim::vec<T> c = vmin * 0.5 + vmax * 0.5;
return c;
}
/// Returns the number of lines in the OBJ structure
unsigned int numL(){
return L.size();
}
/// Returns all points in the line corresponding to a given index
/// @param i is the index of the desired line
std::vector< stim::vec<T> > getL_V(unsigned int i){
//get the number of points in the specified line
unsigned int nP = L[i].size();
//create a vector of points
std::vector< stim::vec<T> > l;
//set the size of the vector
l.resize(nP);
//copy the points from the point list to the stim vector
unsigned int pi;
for(unsigned int p = 0; p < nP; p++){
//get the index of the geometry point
pi = L[i][p][0] - 1;
//get the coordinates of the current point
stim::vec<T> newP = V[pi];
//copy the point into the vector
l[p] = newP;
}
return l;
}
/// Returns the vertex indices for the specified line
/// @param i is the index of the line
std::vector< unsigned int > getL_Vi(unsigned int i){
unsigned int nP = L[i].size();
std::vector< unsigned int > l;
//set the size of the vector
l.resize(nP);
//copy the indices from the geometry structure to the array
for(unsigned int p = 0; p < nP; p++){
l[p] = L[i][p][0];
}
return l;
}
};
};
#endif