Blame view

stim/visualization/obj.h 27.7 KB
ba72c5b0   David Mayerich   added OBJ class
1
2
3
4
5
6
7
8
  #ifndef STIM_OBJ_H

  #define STIM_OBJ_H

  

  #include <vector>

  #include <sstream>

  #include <fstream>

  #include <stdlib.h>

  #include <stim/parser/parser.h>

c61fd046   David Mayerich   fixed vessel mode...
9
  #include <stim/math/vector.h>

5db14a38   David Mayerich   added materials t...
10
  #include <stim/visualization/obj/obj_material.h>

eb303393   David Mayerich   updates to the st...
11
  #include <algorithm>

ba72c5b0   David Mayerich   added OBJ class
12
  

5bccf89d   David Mayerich   fixed some errors...
13
14
  #include <time.h>

  

ba72c5b0   David Mayerich   added OBJ class
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  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

   */

  

1a31ffa0   David Mayerich   implemented OBJ_T...
33
  enum obj_type { OBJ_NONE, OBJ_LINE, OBJ_FACE, OBJ_POINTS, OBJ_TRIANGLE_STRIP };

ba72c5b0   David Mayerich   added OBJ class
34
35
36
37
38
39
40
41
  

  template <typename T>

  class obj{

  

  protected:

  

  	enum token_type { OBJ_INVALID, OBJ_V, OBJ_VT, OBJ_VN, OBJ_P, OBJ_L, OBJ_F };

  

487a9b49   David Mayerich   added the ability...
42
  	struct vertex : public stim::vec<T>{

ba72c5b0   David Mayerich   added OBJ class
43
  

487a9b49   David Mayerich   added the ability...
44
45
46
  		using vec<T>::push_back;

  		using vec<T>::size;

  		using vec<T>::at;

ac788020   David Mayerich   fixed 1D vertex s...
47
  		using vec<T>::resize;

ba72c5b0   David Mayerich   added OBJ class
48
  

271708c4   David Mayerich   updated OBJ objec...
49
  		//basic constructors (call the stim::vector constructors)

ba72c5b0   David Mayerich   added OBJ class
50
  		vertex(){}

ac788020   David Mayerich   fixed 1D vertex s...
51
52
53
54
  		vertex(T x){

  			resize(1);

  			at(0) = x;

  		}

271708c4   David Mayerich   updated OBJ objec...
55
56
57
  		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){}

ba72c5b0   David Mayerich   added OBJ class
58
  

e376212f   David Mayerich   added support for...
59
60
  		vertex(stim::vec<T> rhs) : stim::vec<T>(rhs){}

  

ba72c5b0   David Mayerich   added OBJ class
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
  		//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

5db14a38   David Mayerich   added materials t...
97
  	struct triplet : public std::vector<size_t>{

5bccf89d   David Mayerich   fixed some errors...
98
99
100
  

  		//default constructor, empty triplet

  		triplet(){}

ba72c5b0   David Mayerich   added OBJ class
101
102
  		

  		//create a triplet given a parameter list (OBJ indices start at 1, so 0 can be used to indicate no value)

5db14a38   David Mayerich   added materials t...
103
  		triplet(size_t v, size_t vt = 0, size_t vn = 0){

ba72c5b0   David Mayerich   added OBJ class
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
  			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)

5db14a38   David Mayerich   added materials t...
144
  					ss<<"//"<<at(2);

ba72c5b0   David Mayerich   added OBJ class
145
  				else

5db14a38   David Mayerich   added materials t...
146
  					ss<<'/'<<at(1)<<'/'<<at(2);

ba72c5b0   David Mayerich   added OBJ class
147
148
  			}

  			else if(size() == 2)

5db14a38   David Mayerich   added materials t...
149
  				ss<<"/"<<at(1);

ba72c5b0   David Mayerich   added OBJ class
150
151
152
153
154
155
156
157
158
159
160
161
  

  			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;

9b766f1f   Pavel Govyadinov   completed merge f...
162
  		using std::vector<triplet>::resize;

ba72c5b0   David Mayerich   added OBJ class
163
164
165
166
167
168
169
170
171
  

  		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, ' ');

  

5bccf89d   David Mayerich   fixed some errors...
172
  			resize(tokens.size() - 1);											//set the geometry size based on the number of tokens

ba72c5b0   David Mayerich   added OBJ class
173
174
175
  			//for each triplet in the line

  			for(unsigned int i = 1; i < tokens.size(); i++){

  				//construct the triplet and push it to the geometry

5bccf89d   David Mayerich   fixed some errors...
176
177
  				//push_back( triplet(tokens[i]) );

  				at(i-1) = triplet(tokens[i]);

ba72c5b0   David Mayerich   added OBJ class
178
179
180
181
  			}

  

  		}

  

cbce396e   David Mayerich   added support for...
182
183
184
  		//returns a vector containing the vertex indices for the geometry

  		void get_v(std::vector<unsigned>& v){

  			v.resize(size());							//resize the vector to match the number of vertices

5bccf89d   David Mayerich   fixed some errors...
185
  			for(unsigned int i = 0; i<size(); i++){		//for each vertex

b8b15cb8   David Mayerich   further simplific...
186
  				v[i] = (unsigned)at(i)[0];						//copy the vertex index (1st element of triplet)

5bccf89d   David Mayerich   fixed some errors...
187
  			}

cbce396e   David Mayerich   added support for...
188
189
190
191
192
  		}

  

  		void get_vt(std::vector<unsigned>& vt){

  			vt.resize(size());							//resize the vector to match the number of vertices

  			for(unsigned int i = 0; i<size(); i++)		//for each vertex

5bccf89d   David Mayerich   fixed some errors...
193
  				vt[i] = at(i)[1];						//copy the vertex texture index (2nd element of triplet)

cbce396e   David Mayerich   added support for...
194
195
196
197
198
199
200
201
  		}

  

  		void get_vn(std::vector<unsigned>& vn){

  			vn.resize(size());							//resize the vector to match the number of vertices

  			for(unsigned int i = 0; i<size(); i++)		//for each vertex

  				vn[i] = at(i)[2];						//copy the vertex index

  		}

  

ba72c5b0   David Mayerich   added OBJ class
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
  		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

  

5db14a38   David Mayerich   added materials t...
227
228
229
230
  	//material lists

  	std::vector< obj_material<T> > M;	//list of material descriptors

  	std::vector<size_t> Mf;			//face index where each material begins

  

ba72c5b0   David Mayerich   added OBJ class
231
232
233
234
  	//information for the current geometric object

  	geometry current_geo;

  	vertex current_vt;

  	vertex current_vn;

5db14a38   David Mayerich   added materials t...
235
236
  	obj_material<T> current_material;					//stores the current material

  	bool new_material;								//flags if a material property has been changed since the last material was pushed

ba72c5b0   David Mayerich   added OBJ class
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
  

  		//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){

  

5db14a38   David Mayerich   added materials t...
268
269
270
  		size_t v;

  		size_t vt = 0;

  		size_t vn = 0;

ba72c5b0   David Mayerich   added OBJ class
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
  

  		//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;

5db14a38   David Mayerich   added materials t...
313
314
  

  		new_material = false;						//initialize a new material to false (start with no material)

ba72c5b0   David Mayerich   added OBJ class
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  	}

  

  	//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;

  	}

  

ba72c5b0   David Mayerich   added OBJ class
334
  public:

487a9b49   David Mayerich   added the ability...
335
336
337
338
339
  	/// Constructor loads a Wavefront OBJ file

  	obj(std::string filename){

  		load(filename);

  	}

  

ba72c5b0   David Mayerich   added OBJ class
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  	//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));}

  

5db14a38   David Mayerich   added materials t...
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
  	///Material functions

  	void matKa(T r, T g, T b) {

  		new_material = true;

  		current_material.ka[0] = r;

  		current_material.ka[1] = g;

  		current_material.ka[2] = b;

  	}

  	void matKa(std::string tex = std::string()) {

  		new_material = true;

  		current_material.tex_ka = tex;

  	}

  	void matKd(T r, T g, T b) {

  		new_material = true;

  		current_material.kd[0] = r;

  		current_material.kd[1] = g;

  		current_material.kd[2] = b;

  	}

  	void matKd(std::string tex = std::string()) {

  		new_material = true;

  		current_material.tex_kd = tex;

  	}

  	void matKs(T r, T g, T b) {

  		new_material = true;

  		current_material.ks[0] = r;

  		current_material.ks[1] = g;

  		current_material.ks[2] = b;

  	}

  	void matKs(std::string tex = std::string()) {

  		new_material = true;

  		current_material.tex_ks = tex;

  	}

  	void matNs(T n) {

  		new_material = true;

  		current_material.ns = n;

  	}

  	void matNs(std::string tex = std::string()) {

  		new_material = true;

  		current_material.tex_ns = tex;

  	}

  	void matIllum(int i) {

  		new_material = true;

  		current_material.illum = i;

  	}

  	void matD(std::string tex = std::string()) {

  		new_material = true;

  		current_material.tex_alpha = tex;

  	}

  	void matBump(std::string tex = std::string()) {

  		new_material = true;

  		current_material.tex_bump = tex;

  	}

  	void matDisp(std::string tex = std::string()) {

  		new_material = true;

  		current_material.tex_disp = tex;

  	}

  	void matDecal(std::string tex = std::string()) {

  		new_material = true;

  		current_material.tex_decal = tex;

  	}

  

ba72c5b0   David Mayerich   added OBJ class
418
419
420
421
  	///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){

5db14a38   David Mayerich   added materials t...
422
423
424
425
426
427
428
429
430
  		if (new_material) {							//if a new material has been specified

  			if (current_material.name == "") {		//if a name wasn't given, create a new one

  				std::stringstream ss;				//create a name for it

  				ss << "material" << M.size();		//base it on the material number

  				current_material.name = ss.str();

  			}

  			Mf.push_back(F.size());					//start the material at the current face index

  			M.push_back(current_material);			//push the current material

  			current_material.name = "";			//reset the name of the current material

4b9199d3   Pavel Govyadinov   added the voxel s...
431
  			new_material = false;

5db14a38   David Mayerich   added materials t...
432
  		}

ba72c5b0   David Mayerich   added OBJ class
433
434
435
  		current_type = t;

  	}

  

1a31ffa0   David Mayerich   implemented OBJ_T...
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  	//generates a list of faces from a list of points, assuming the input list forms a triangle strip

  	std::vector<geometry> genTriangleStrip(geometry s) {

  		if (s.size() < 3) return std::vector<geometry>();	//return an empty list if there aren't enough points to form a triangle

  		size_t nt = s.size() - 2;							//calculate the number of triangles in the strip

  		std::vector<geometry> r(nt);								//create a list of geometry objects, where the number of faces = the number of triangles in the strip

  

  		r[0].push_back(s[0]);

  		r[0].push_back(s[1]);

  		r[0].push_back(s[2]);

  		for (size_t i = 1; i < nt; i++) {

  			if (i % 2) {

  				r[i].push_back(s[i + 1]);

  				r[i].push_back(s[i + 0]);

  				r[i].push_back(s[i + 2]);

  			}

  			else {

  				r[i].push_back(s[i + 0]);

  				r[i].push_back(s[i + 1]);

  				r[i].push_back(s[i + 2]);

  			}

  		}

  		return r;		

  	}

  

ba72c5b0   David Mayerich   added OBJ class
460
461
  	/// This function terminates drawing of a primitive object, such as a line, face, or point set

  	void End(){

ba72c5b0   David Mayerich   added OBJ class
462
  		//copy the current object to the appropriate list

14b500f9   Pavel Govyadinov   minor bug fixes
463
464
465
  		if(current_geo.size() != 0)

  		{

  			switch(current_type){

ba72c5b0   David Mayerich   added OBJ class
466
  

14b500f9   Pavel Govyadinov   minor bug fixes
467
468
469
  			case OBJ_NONE:

  				std::cout<<"STIM::OBJ error, objEnd() called before objBegin()."<<std::endl;

  				break;

ba72c5b0   David Mayerich   added OBJ class
470
  

14b500f9   Pavel Govyadinov   minor bug fixes
471
472
473
  			case OBJ_POINTS:

  				P.push_back(current_geo);

  				break;

ba72c5b0   David Mayerich   added OBJ class
474
  

14b500f9   Pavel Govyadinov   minor bug fixes
475
476
477
  			case OBJ_LINE:

  				L.push_back(current_geo);

  				break;

ba72c5b0   David Mayerich   added OBJ class
478
  

14b500f9   Pavel Govyadinov   minor bug fixes
479
480
  			case OBJ_FACE:

  				F.push_back(current_geo);

1a31ffa0   David Mayerich   implemented OBJ_T...
481
482
483
484
  				break;

  

  			case OBJ_TRIANGLE_STRIP:

  				std::vector<geometry> tstrip = genTriangleStrip(current_geo);		//generate a list of faces from the current geometry

85777f53   David Mayerich   fixed triangle st...
485
  				F.insert(F.end(), tstrip.begin(), tstrip.end());					//insert all of the triangles into the face list

1a31ffa0   David Mayerich   implemented OBJ_T...
486
  				break;

14b500f9   Pavel Govyadinov   minor bug fixes
487
  			}

ba72c5b0   David Mayerich   added OBJ class
488
  		}

ba72c5b0   David Mayerich   added OBJ class
489
490
491
492
493
494
495
496
  		//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;

  	}

14b500f9   Pavel Govyadinov   minor bug fixes
497
498
499
500
501
502
  //temporary convenience method

  	void rev(){

  		if(current_geo.size() != 0)

  		//	current_geo.reverse(current_geo.begin(), current_geo.end());

  			std::reverse(current_geo.begin(), current_geo.end());

  	}

ba72c5b0   David Mayerich   added OBJ class
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
  

  	//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;

  			}

  		}

  

5db14a38   David Mayerich   added materials t...
551
  		//output all of the faces

ba72c5b0   David Mayerich   added OBJ class
552
553
  		if(F.size()){

  			ss<<std::endl<<"#face structures"<<std::endl;

5db14a38   David Mayerich   added materials t...
554
  			size_t mi = 0;											//start the current material index at 0

ba72c5b0   David Mayerich   added OBJ class
555
  			for(i = 0; i < F.size(); i++){

5db14a38   David Mayerich   added materials t...
556
557
558
559
  				if (mi < M.size() && Mf[mi] == i) {

  					ss << "usemtl " << M[mi].name << std::endl;

  					mi++;

  				}

ba72c5b0   David Mayerich   added OBJ class
560
561
562
563
564
565
566
  				ss<<"f "<<F[i].str()<<std::endl;

  			}

  		}

  

  		return ss.str();	//return the constructed string

  	}

  

5db14a38   David Mayerich   added materials t...
567
568
569
570
571
572
  	///Output the material file as a string

  	std::string matstr() {

  		std::stringstream ss;

  		for (size_t i = 0; i < M.size(); i++) {

  			ss << M[i].str() << std::endl;

  		}

7f1ab3c2   Pavel Govyadinov   fixed problems wi...
573
  		return ss.str();

5db14a38   David Mayerich   added materials t...
574
575
  	}

  

ba72c5b0   David Mayerich   added OBJ class
576
577
578
579
580
581
582
583
584
585
586
587
588
  	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){

  

5db14a38   David Mayerich   added materials t...
589
  		

ba72c5b0   David Mayerich   added OBJ class
590
  

5db14a38   David Mayerich   added materials t...
591
592
593
594
595
596
597
598
599
600
601
602
603
  		std::string obj_ext = ".obj";

  		size_t ext_found = filename.find(obj_ext);

  		if (ext_found != std::string::npos)									//if the extension was found

  			filename = filename.substr(0, ext_found);

  		std::string obj_filename;

  		std::string mtl_filename;

  		obj_filename = filename + ".obj";

  		mtl_filename = filename + ".mtl";

  

  

  		std::ofstream outfile(obj_filename.c_str());

  		if (!outfile) {

  			std::cout << "STIM::OBJ error opening file for writing" << std::endl;

ba72c5b0   David Mayerich   added OBJ class
604
605
606
  			return false;

  		}

  

5db14a38   David Mayerich   added materials t...
607
608
609
  		if (M.size())															//if there are any materials, there will be a corresponding material file

  			outfile << "mtllib " << mtl_filename << std::endl;				//output the material library name

  

ba72c5b0   David Mayerich   added OBJ class
610
611
612
613
614
615
  		//output the OBJ data to the file

  		outfile<<str();

  

  		//close the file

  		outfile.close();

  

5db14a38   David Mayerich   added materials t...
616
617
  		if (M.size()) {															//if materials are used

  			

5a58238a   David Mayerich   fixed linux compa...
618
  			outfile.open(mtl_filename.c_str());										//open the material file

5db14a38   David Mayerich   added materials t...
619
620
621
622
623
624
  			for (size_t i = 0; i < M.size(); i++) {								//for each material

  				outfile << M[i].str() << std::endl;								//output the material name and properties

  			}

  			outfile.close();

  		}

  

ba72c5b0   David Mayerich   added OBJ class
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
  		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;

271708c4   David Mayerich   updated OBJ objec...
643
  		getline(infile, line);		//get the first line

5bccf89d   David Mayerich   fixed some errors...
644
  		unsigned int l = 0;

ba72c5b0   David Mayerich   added OBJ class
645
  		while(infile){

5bccf89d   David Mayerich   fixed some errors...
646
647
  			l++;

  			//unsigned int t = time(NULL);

ba72c5b0   David Mayerich   added OBJ class
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  

  			//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;

  

271708c4   David Mayerich   updated OBJ objec...
673
  			case OBJ_L:

5bccf89d   David Mayerich   fixed some errors...
674
  				

271708c4   David Mayerich   updated OBJ objec...
675
676
677
  				L.push_back(geometry(line));

  				break;

  

ba72c5b0   David Mayerich   added OBJ class
678
  			};

271708c4   David Mayerich   updated OBJ objec...
679
  

5bccf89d   David Mayerich   fixed some errors...
680
  			

271708c4   David Mayerich   updated OBJ objec...
681
682
  			//get the next line

  			getline(infile, line);

ba72c5b0   David Mayerich   added OBJ class
683
  

5bccf89d   David Mayerich   fixed some errors...
684
  		}

ba72c5b0   David Mayerich   added OBJ class
685
686
687
688
689
690
691
692
  

  		//close the file

  		infile.close();

  

  		return true;

  

  	}

  

487a9b49   David Mayerich   added the ability...
693
694
695
696
697
  	/// Return the number of position vertices in the OBJ model

  	unsigned int numV(){

  		return V.size();

  	}

  

4ea8daa8   Jiaming Guo   add necessary fun...
698
699
700
701
  	unsigned int numVT() {

  		return VT.size();

  	}

  

487a9b49   David Mayerich   added the ability...
702
  	/// Retrieve the vertex stored in index i

cbce396e   David Mayerich   added support for...
703
704
705
706
707
  	/// @param vi is the desired vertex index

  	stim::vec<T> getV(unsigned int vi){

  		stim::vec<T> v = V[vi];

  		return v;

  	}

b8b15cb8   David Mayerich   further simplific...
708
709
710
711
712
713
714
  	std::vector< stim::vec<T> > getAllV() {

  		std::vector< stim::vec<T> > v(V.size());

  		for (unsigned i = 0; i < V.size(); i++)

  			v[i] = V[i];

  		return v;

  	}

  

018bda33   pranathivemuri   added functions t...
715
716
  	std::vector< stim::vec<T> > getAllV(std::vector<unsigned> vertexIndices){

  		 std::vector< stim::vec<T> > allVerticesList;

1ab6c061   pranathivemuri   int to unsigned int
717
  		 for (unsigned int i=0; i<numV(); i++)

018bda33   pranathivemuri   added functions t...
718
719
720
721
722
723
724
725
726
727
  		 {

  			 vertexIndices[i] = i + 1;

  		 }

  		 allVerticesList = getV(vertexIndices);

  		 return allVerticesList;

  	}

  	int 

  	getIndex(std::vector< stim::vec<T> >allVerticesList, stim::vec<T> v0) 

  	{

  		int result = 0;

1ab6c061   pranathivemuri   int to unsigned int
728
  		for (unsigned int i = 0; i < allVerticesList.size() ; i++)

018bda33   pranathivemuri   added functions t...
729
730
731
732
733
734
735
736
  		{

  			if (allVerticesList[i] == v0) 

  			{

  				result = i;

  			}

  		}

  		return result;

      }

487a9b49   David Mayerich   added the ability...
737
  

cbce396e   David Mayerich   added support for...
738
739
740
741
742
743
  	/// Retrieve the vertex texture coordinate at index i

  	/// @param vti is the desired index

  	stim::vec<T> getVT(unsigned int vti){

  		stim::vec<T> vt = VT[vti];

  		return vt;

  	}

487a9b49   David Mayerich   added the ability...
744
  

cbce396e   David Mayerich   added support for...
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
  	/// Retrieve the vertex normal at index i

  	/// @param vni is the desired index

  	stim::vec<T> getVN(unsigned int vni){

  		stim::vec<T> vn = VN[vni];

  		return vn;

  	}

  

  

  	/// Retrieve a vertex stored at a list of given indices

  	/// @param vi is an array containing a series of indices

  	std::vector< stim::vec<T> > getV( std::vector<unsigned> vi ){

  

  		std::vector< stim::vec<T> > v;

  		v.resize(vi.size());							//pre-allocate an array of vertices

  

cbce396e   David Mayerich   added support for...
760
761
762
763
764
765
766
767
768
769
770
771
772
  		for(unsigned i = 0; i < vi.size(); i++)

  			v[i] = V[vi[i] - 1];

  

  		return v;										//return the array of vertices

  	}

  

  	/// Retrieve a vertex stored at a list of given indices

  	/// @param vi is an array containing a series of indices

  	std::vector< stim::vec<T> > getVT( std::vector<unsigned> vti ){

  

  		std::vector< stim::vec<T> > vt;

  		vt.resize(vti.size());							//pre-allocate an array of vertices

  

cbce396e   David Mayerich   added support for...
773
774
775
776
777
778
779
780
781
782
783
784
785
  		for(unsigned i = 0; i < vti.size(); i++)

  			vt[i] = VT[vti[i] - 1];

  

  		return vt;										//return the array of vertices

  	}

  

  	/// Retrieve a vertex stored at a list of given indices

  	/// @param vi is an array containing a series of indices

  	std::vector< stim::vec<T> > getVN( std::vector<unsigned> vni ){

  

  		std::vector< stim::vec<T> > vn;

  		vn.resize(vni.size());							//pre-allocate an array of vertices

  

cbce396e   David Mayerich   added support for...
786
787
788
789
  		for(unsigned i = 0; i < vni.size(); i++)

  			vn[i] = VN[vni[i] - 1];

  

  		return vn;										//return the array of vertices

487a9b49   David Mayerich   added the ability...
790
791
792
793
794
  	}

  

  	stim::vec<T> centroid(){

  

  		//get the number of coordinates

b8b15cb8   David Mayerich   further simplific...
795
  		size_t N = V[0].size();

487a9b49   David Mayerich   added the ability...
796
797
798
799
800
801
802
  

  		//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

b8b15cb8   David Mayerich   further simplific...
803
804
805
  		size_t NV = V.size();

  		for(size_t v = 0; v < NV; v++)

  			for(size_t i = 0; i < N; i++){

487a9b49   David Mayerich   added the ability...
806
807
808
809
810
811
812
813
814
815
816
817
818
  

  				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;

  	}

  

b8b15cb8   David Mayerich   further simplific...
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
  	stim::vec<T> dimensions() {

  		//get the number of coordinates

  		size_t 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

  		size_t NV = V.size();

  		for (size_t v = 0; v < NV; v++)

  			for (size_t 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 soze using the min and max points

  		stim::vec<T> d = vmax - vmin;

  

  		return d;

  	}

  

271708c4   David Mayerich   updated OBJ objec...
845
846
  	/// Returns the number of lines in the OBJ structure

  	unsigned int numL(){

b8b15cb8   David Mayerich   further simplific...
847
  		return (unsigned)L.size();

271708c4   David Mayerich   updated OBJ objec...
848
849
850
851
852
853
854
855
856
  	}

  

  	/// 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();

271708c4   David Mayerich   updated OBJ objec...
857
858
859
860
861
862
863
  		//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

84eff8b1   Pavel Govyadinov   Merged only the n...
864
  		unsigned int pie;

271708c4   David Mayerich   updated OBJ objec...
865
866
867
  		for(unsigned int p = 0; p < nP; p++){

  

  			//get the index of the geometry point

84eff8b1   Pavel Govyadinov   Merged only the n...
868
  			pie = L[i][p][0] - 1;

271708c4   David Mayerich   updated OBJ objec...
869
870
  

  			//get the coordinates of the current point

84eff8b1   Pavel Govyadinov   Merged only the n...
871
  			stim::vec<T> newP = V[pie];

271708c4   David Mayerich   updated OBJ objec...
872
873
874
875
876
877
878
879
880
881
882
  

  			//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

4277a87d   cherub   modified flow.h &...
883
  	std::vector< unsigned int > getL_Vi(unsigned int i){

271708c4   David Mayerich   updated OBJ objec...
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  

  		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;

4277a87d   cherub   modified flow.h &...
900
  	}

df036f4c   David Mayerich   added the network...
901
  

14b500f9   Pavel Govyadinov   minor bug fixes
902
  

df036f4c   David Mayerich   added the network...
903
904
905
  	/// Returns a vector containing the list of texture coordinates associated with each point of a line.

  

  	/// @param i is the index of the desired line

4277a87d   cherub   modified flow.h &...
906
  	std::vector< stim::vec<T> > getL_VT(unsigned int i){

df036f4c   David Mayerich   added the network...
907
908
909
910
911
912
913
914
915
916
917
  

  		//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

84eff8b1   Pavel Govyadinov   Merged only the n...
918
  		unsigned int pie;

df036f4c   David Mayerich   added the network...
919
920
921
  		for(unsigned int p = 0; p < nP; p++){

  

  			//get the index of the geometry point

84eff8b1   Pavel Govyadinov   Merged only the n...
922
  			pie = L[i][p][1] - 1;

df036f4c   David Mayerich   added the network...
923
924
  

  			//get the coordinates of the current point

84eff8b1   Pavel Govyadinov   Merged only the n...
925
  			stim::vec<T> newP = VT[pie];

df036f4c   David Mayerich   added the network...
926
927
928
929
930
931
  

  			//copy the point into the vector

  			l[p] = newP;

  		}

  

  		return l;

4277a87d   cherub   modified flow.h &...
932
  	}

e376212f   David Mayerich   added support for...
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
  

  	/// Add an array of vertices to the vertex list

  	unsigned int addV(std::vector< stim::vec<T> > vertices){

  

  		unsigned int NV = vertices.size();			//get the total number of new vertices

  

  		unsigned int currentV = V.size() + 1;			//store the index to the first submitted point

  		

  		//for each vertex

  		for(unsigned int vi = 0; vi < NV; vi++){

  			vertex v(vertices[vi]);

  			V.push_back(v);

  		}

  

  		//return the index to the first point

  		return currentV;

  

  	}

  

  	/// Add a line to the object

  	void addLine(std::vector<unsigned> v, std::vector<unsigned> vt = std::vector<unsigned>(), std::vector<unsigned> vn = std::vector<unsigned>()){

  

  		//create a new line geometry

  		geometry new_line;

  

  		unsigned int v_i, vt_i, vn_i;

  		for(unsigned int i = 0; i < v.size(); i++){

  			v_i = vt_i = vn_i = 0;

  

  			v_i = v[i];

  			if(vt.size() != 0)

  				vt_i = vt[i];

  			if(vn.size() != 0)

  				vn_i = vn[i];

  

  			new_line.push_back(triplet(v_i, vt_i, vn_i));

  		}

  

  		//push the new geometry to the line list

  		L.push_back(new_line);

cbce396e   David Mayerich   added support for...
973
974
975
976
977
978
979
980
  	}

  

  	/// Fills three std::vector structures with the indices representing a line

  	/// @param l is the line index

  	/// @param vi is a vertex index array that will be filled

  	void getLinei(unsigned l, std::vector<unsigned>& vi){

  		L[l-1].get_v(vi);

  	}

271708c4   David Mayerich   updated OBJ objec...
981
  

cbce396e   David Mayerich   added support for...
982
983
984
985
986
987
988
  	/// Fills three std::vector structures with the indices representing a line

  	/// @param l is the line index

  	/// @param vi is a vertex index array that will be filled

  	/// @param vti is a vertex texture coordinate index array that will be filled

  	void getLinei(unsigned l, std::vector<unsigned>& vi, std::vector<unsigned>& vti){

  		getLinei(l, vi);

  		L[l-1].get_vt(vti);

271708c4   David Mayerich   updated OBJ objec...
989
  	}

ba72c5b0   David Mayerich   added OBJ class
990
  

cbce396e   David Mayerich   added support for...
991
992
993
994
995
996
997
998
999
1000
1001
  	/// Fills three std::vector structures with the indices representing a line

  	/// @param l is the line index

  	/// @param vi is a vertex index array that will be filled

  	/// @param vti is a vertex texture coordinate index array that will be filled

  	/// @param vni is a vertex normal index array that will be filled

  	void getLinei(unsigned l, std::vector<unsigned>& vi, std::vector<unsigned>& vti, std::vector<unsigned>& vni){

  		getLinei(l, vi, vti);

  		L[l-1].get_vn(vni);

  	}

  

  	/// Returns a list of points corresponding to the vertices of a line

eb303393   David Mayerich   updates to the st...
1002
  	void getLine(unsigned l, std::vector< stim::vec<T> >& v){

cbce396e   David Mayerich   added support for...
1003
1004
1005
1006
1007
1008
  

  		std::vector<unsigned> vi;			//create a vector to store indices to vertices

  		getLinei(l, vi);					//get the indices for the line vertices

  		v = getV(vi);						//get the vertices corresponding to the indices

  	}

  

eb303393   David Mayerich   updates to the st...
1009
  	void getLine(unsigned l, std::vector< stim::vec<T> >& v, std::vector< stim::vec<T> >& vt){

cbce396e   David Mayerich   added support for...
1010
1011
1012
1013
1014
1015
1016
  

  		std::vector<unsigned> vi, vti;

  		getLinei(l, vi, vti);					//get the indices for the line vertices

  		v = getV(vi);						//get the vertices corresponding to the indices

  		vt = getVT(vti);

  	}

  

eb303393   David Mayerich   updates to the st...
1017
1018
1019
  	void getLine(unsigned l, std::vector< stim::vec<T> >& v,

  							 std::vector< stim::vec<T> >& vt,

  							 std::vector< stim::vec<T> >& vn){

cbce396e   David Mayerich   added support for...
1020
1021
1022
1023
1024
1025
1026
1027
1028
  

  		std::vector<unsigned> vi, vti, vni;

  		getLinei(l, vi, vti, vni);					//get the indices for the line vertices

  		v = getV(vi);						//get the vertices corresponding to the indices

  		vt = getVT(vti);

  		vn = getVN(vni);

  

  	}

  

b8b15cb8   David Mayerich   further simplific...
1029
1030
1031
1032
1033
1034
  	/// Return the number of vertices in a line

  	/// @param l is the line index

  	unsigned getLineN(unsigned l) {

  		return (unsigned)L[l].size();

  	}

  

cbce396e   David Mayerich   added support for...
1035
  

df036f4c   David Mayerich   added the network...
1036
1037
  

  

ba72c5b0   David Mayerich   added OBJ class
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
  };

  

  

  

  

  

  

  };

  

  

  #endif