-
Notifications
You must be signed in to change notification settings - Fork 5
/
mesh.cpp
1730 lines (1449 loc) · 42 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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* @file mesh.cpp
* @brief Functions for representing a mesh
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <limits>
#include <stdexcept>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cerrno>
#include <cstring>
#include "mesh.h"
namespace psalm
{
/*!
* Sets some default values.
*/
mesh::mesh()
{
id_offset = 0;
}
/*!
* Destructor for mesh. Calls destroy() function (if the user did not
* already delete data).
*/
mesh::~mesh()
{
destroy();
}
/*!
* Tries to load data (presumably mesh data) that from an input source the
* user specified. The type of the data is determined by the following
* process:
*
* 1) If the user did not specify a type:
*
* 1.1) If the user specified a filename, the function tries to
* identify the file by its extension.
*
* 1.1.1) If this identification fails, the function
* tries to load a PLY file.
*
* 1.2) If the user did not specify a filename, the function
* tries to load PLY data from standard input.
*
* 2) If the user specified a type:
*
* 2.1) If the user specified a filename, the function tries to
* load the file with the appropriate type set, regardless of its
* extension.
*
* 2.2) If the user did not specify a filename, the function
* tries to load data with the specified type from standard input.
*
* @param filename Filename of data file. An empty filename signals that
* the function tries to read data from standard input.
*
* @param type Type of mesh data to load. By default, the function tries
* to guess the data type using filename extensions (if the user specified
* a filename).
*
* @return true if the mesh could be loaded, else false
*/
bool mesh::load(const std::string& filename, file_type type)
{
status result = STATUS_UNDEFINED;
std::ifstream in;
if(filename.length() > 0)
{
errno = 0;
in.open(filename.c_str());
if(errno)
{
std::string error = strerror(errno);
std::cerr << "psalm: Could not load input file \""
<< filename << "\": "
<< error << "\n";
return(false);
}
}
this->destroy();
// Filename given, data type identification by extension
if(filename.length() >= 4 && type == TYPE_EXT)
{
std::string extension = filename.substr(filename.find_last_of('.')); // TODO: Handle errors
std::transform(extension.begin(), extension.end(), extension.begin(), (int(*)(int)) tolower);
if(extension == ".ply")
result = (load_ply(in) ? STATUS_OK : STATUS_ERROR);
else if(extension == ".obj")
result = (load_obj(in) ? STATUS_OK : STATUS_ERROR);
else if(extension == ".off")
result = (load_off(in) ? STATUS_OK : STATUS_ERROR);
// Unknown extension, so we fall back to PLY files (see below)
}
// Data type specified
else if(type != TYPE_EXT)
{
// Check whether file name has been specified. If no file name
// has been specified, use standard input to read data.
std::istream& input_stream = ((filename.length() > 0) ? in : std::cin);
switch(type)
{
case TYPE_PLY:
result = (load_ply(input_stream) ? STATUS_OK : STATUS_ERROR);
break;
case TYPE_OBJ:
result = (load_obj(input_stream) ? STATUS_OK : STATUS_ERROR);
break;
case TYPE_OFF:
result = (load_off(input_stream) ? STATUS_OK : STATUS_ERROR);
break;
case TYPE_EXT: // to shut up the compiler
break;
}
}
// Last resort: If a nonempty file name has been specified, try to
// parse a PLY file. Else, try to read a PLY file from standard input.
if(result == STATUS_UNDEFINED)
{
if(filename.length() > 0)
result = (load_ply(in) ? STATUS_OK : STATUS_ERROR);
else
result = (load_ply(std::cin) ? STATUS_OK : STATUS_ERROR);
}
in.close();
return(result);
}
/*!
* Tries to save the current mesh data to a user-specified output (a file
* or an output stream). The format of the mesh data is determined by the
* following process:
*
* 1) If the user did not specify a type:
*
* 1.1) If the user specified a filename, the function tries to
* identify the file by its extension.
*
* 1.1.1) If this identification fails, the function
* tries to save a PLY file.
*
* 1.2) If the user did not specify a filename, the function
* tries to save PLY data to standard output.
*
* 2) If the user specified a type:
*
* 2.1) If the user specified a filename, the function tries to
* save the file with the appropriate type set, regardless of its
* extension.
*
* 2.2) If the user did not specify a filename, the function
* tries to save data with the specified type to standard output.
*
* @param filename Filename for storing the current mesh. An empty
* filename signals that standard output is to be used.
*
* @param type Format in which to save the mesh data. By default, the
* function tries to guess the data type using filename extensions (if the
* user specified a filename).
*
* @warning The data file will be overwritten if it exists. The user will
* not be notified of this.
*
* @return true if the mesh could be stored, else false.
*/
bool mesh::save(const std::string& filename, file_type type)
{
status result = STATUS_UNDEFINED;
std::ofstream out;
if(filename.length() > 0)
{
errno = 0;
out.open(filename.c_str());
if(errno)
{
std::string error = strerror(errno);
std::cerr << "psalm: Could not save to file \""
<< filename << "\": "
<< error << "\n";
return(false);
}
}
// Filename given, data type identification by extension
if(filename.length() >= 4 && type == TYPE_EXT)
{
std::string extension = filename.substr(filename.find_last_of('.')); // TODO: Handle errors
std::transform(extension.begin(), extension.end(), extension.begin(), (int(*)(int)) tolower);
if(extension == ".ply")
result = (save_ply(out) ? STATUS_OK : STATUS_ERROR);
else if(extension == ".obj")
result = (save_obj(out) ? STATUS_OK : STATUS_ERROR);
else if(extension == ".off")
result = (save_off(out) ? STATUS_OK : STATUS_ERROR);
else if(extension == ".hole")
result = (save_hole(out) ? STATUS_OK : STATUS_ERROR);
// Unknown extension, so we fall back to PLY files (see below)
}
// Data type specified
else if(type != TYPE_EXT)
{
// Check whether file name has been specified. If no file name
// has been specified, use standard output to write data.
std::ostream& output_stream = ((filename.length() > 0) ? out : std::cout);
switch(type)
{
case TYPE_PLY:
result = (save_ply(output_stream) ? STATUS_OK : STATUS_ERROR);
break;
case TYPE_OBJ:
result = (save_obj(output_stream) ? STATUS_OK : STATUS_ERROR);
break;
case TYPE_OFF:
result = (save_off(output_stream) ? STATUS_OK : STATUS_ERROR);
break;
case TYPE_EXT: // to shut up the compiler
break;
}
}
// Last resort: If a nonempty filename has been specified, try to
// write a PLY file. Else, try to write a PLY file to standard output.
if(result == STATUS_UNDEFINED)
{
if(filename.length() > 0)
result = (save_ply(out) ? STATUS_OK : STATUS_ERROR);
else
result = (save_ply(std::cout) ? STATUS_OK : STATUS_ERROR);
}
out.close();
return(result);
}
/*!
* Tries to load mesh data in PLY format from an input stream.
*
* @param in Input stream (file, standard input)
* @return true if the mesh could be loaded, else false
*/
bool mesh::load_ply(std::istream& in)
{
if(!in.good())
return(false);
std::string data;
// Read the headers: Only ASCII format is accepted, but the version is
// ignored
std::getline(in, data);
if(data != "ply")
{
std::cerr << "psalm: I am missing a \"ply\" header for the input data.\n";
return(false);
}
std::getline(in, data);
if(data.find("format ascii") == std::string::npos)
{
std::cerr << "psalm: Expected \"format ascii\", got \"" << data << "\" instead.\n";
return(false);
}
/*
Parsing further element properties is quick and dirty: It is
assumed that face data is declared _after_ the vertex data.
Properties are assumed to come in the natural order, i.e.:
x
y
z
for vertex data.
*/
size_t num_vertices = 0;
size_t num_faces = 0;
enum modes
{
PARSE_HEADER,
PARSE_VERTEX_PROPERTIES,
PARSE_FACE_PROPERTIES
};
modes mode = PARSE_HEADER;
while(!in.eof())
{
getline(in, data);
/*
Lines contaning "comment" or "obj_info" are skipped.
Not sure whether obj_info is allowed to appear at all.
*/
if( data.find("comment") != std::string::npos ||
data.find("obj_info") != std::string::npos)
continue;
else if(data.find("end_header") != std::string::npos)
break;
switch(mode)
{
case PARSE_VERTEX_PROPERTIES:
if(data.find("property") != std::string::npos)
{
/*
Ignore. Some special handlings
for more properties could be
added here.
*/
continue;
}
else if(data.find("element face") != std::string::npos)
{
mode = PARSE_FACE_PROPERTIES;
std::string dummy; // not necessary, but more readable
std::istringstream converter(data);
converter >> dummy >> dummy >> num_faces;
if(num_faces == 0)
{
std::cerr << "psalm: Can't parse number of faces from \""
<< data
<< "\".\n";
return(false);
}
mode = PARSE_FACE_PROPERTIES;
}
else
{
std::cerr << "psalm: Expected \"property\", but got \"" << data << "\" instead.\n";
return(false);
}
break;
case PARSE_FACE_PROPERTIES:
if(data.find("property list") == std::string::npos)
{
std::cerr << "Warning: Got \"" << data << "\". "
<< "This property is unknown and might lead "
<< "to problems when parsing the file.\n";
}
break;
// Expect "element vertex" line
case PARSE_HEADER:
if(data.find("element vertex") != std::string::npos)
{
mode = PARSE_VERTEX_PROPERTIES;
std::string dummy; // not necessary, but more readable
std::istringstream converter(data);
converter >> dummy >> dummy >> num_vertices;
if(num_vertices == 0)
{
std::cerr << "psalm: Can't parse number of vertices from \""
<< data
<< "\".\n";
return(false);
}
mode = PARSE_VERTEX_PROPERTIES;
}
else
{
std::cerr << "psalm: Got \""
<< data
<< "\", but expected \"element vertex\" "
<< "or \"element face\" instead. I cannot continue.\n";
return(false);
}
break;
default:
break;
}
}
size_t cur_line = 0;
size_t k = 0; // number of vertices for face
double x, y, z;
std::string line;
while(std::getline(in, line))
{
std::istringstream parser(line);
if(cur_line < num_vertices)
{
parser >> x >> y >> z;
add_vertex(x, y, z);
}
else
{
k = 0;
parser >> k;
if(k == 0)
break;
// Store vertices of face in proper order and add a new
// face.
std::vector<vertex*> vertices;
size_t v = 0;
for(size_t i = 0; i < k; i++)
{
parser >> v;
vertices.push_back(get_vertex(v));
}
add_face(vertices);
}
cur_line++;
}
/*
FIXME
// Mark boundary vertices if the user has chosen to preserve them.
// Else, we do not need the additional information.
if(preserve_boundaries)
mark_boundaries();
*/
return(true);
}
/*!
* Saves the currently loaded mesh in PLY format.
*
* @param out Stream for data output
* @return true if the mesh could be stored, else false.
*/
bool mesh::save_ply(std::ostream& out)
{
if(!out.good())
return(false);
// header information
out << "ply\n"
<< "format ascii 1.0\n"
<< "element vertex " << V.size() << "\n"
<< "property float x\n"
<< "property float y\n"
<< "property float z\n"
<< "property uchar red\n"
<< "property uchar green\n"
<< "property uchar blue\n"
<< "element face " << F.size() << "\n"
<< "property list uchar int vertex_indices\n"
<< "end_header\n";
// write vertex list (separated by spaces)
for(size_t i = 0; i < V.size(); i++)
{
out << std::fixed << std::setprecision(8) << V[i]->get_position()[0] << " "
<< V[i]->get_position()[1] << " "
<< V[i]->get_position()[2];
// XXX
if(V[i]->is_on_boundary())
out << " 255 0 0\n";
else
out << " 0 255 0\n";
}
// write face list (separated by spaces)
for(size_t i = 0; i < F.size(); i++)
{
out << F[i]->num_vertices() << " ";
for(size_t j = 0; j < F[i]->num_vertices(); j++)
{
out << F[i]->get_vertex(j)->get_id();
if(j < F[i]->num_vertices()-1)
out << " ";
}
out << "\n";
}
return(true);
}
/*!
* Loads a mesh in Wavefront OBJ format from an input stream. Almost all
* possible information from the input stream will be ignored gracefully
* because the program is only interested in the raw geometrical data.
*
* @param in Input stream (file, standard input)
* @return true if the mesh could be loaded, else false
*/
bool mesh::load_obj(std::istream &in)
{
if(!in.good())
return(false);
std::string line;
std::string keyword;
std::istringstream converter;
// These are specify the only keywords of the .OBJ file that the parse
// is going to understand
const std::string OBJ_KEY_VERTEX = "v";
const std::string OBJ_KEY_FACE = "f";
while(!std::getline(in, line).eof())
{
converter.str(line);
converter >> keyword;
if(keyword == OBJ_KEY_VERTEX)
{
double x, y, z;
converter >> x >> y >> z;
if(converter.fail())
{
std::cerr << "psalm: I tried to parse vertex coordinates from line \""
<< line
<<" \" and failed.\n";
return(false);
}
add_vertex(x,y,z);
}
else if(keyword == OBJ_KEY_FACE)
{
std::vector<vertex*> vertices;
// Check whether it is a triplet data string
if(line.find_first_of('/') != std::string::npos)
{
while(!converter.eof())
{
std::string index_str;
converter >> index_str;
if(index_str.length() == 0)
continue;
size_t slash_pos = index_str.find_first_of('/'); // only interested in first occurrence
// Contains the index as a string (not
// including the '/'). The other
// attributes (normals, textures
// coordinates) are _removed_ here.
std::istringstream index_conv(index_str.substr(0, slash_pos));
long index = 0;
index_conv >> index;
if(index < 0)
{
std::cerr << "psalm: Handling of negative indices not yet implemented.\n";
return(false);
}
else
vertices.push_back(get_vertex(index-1));
}
add_face(vertices);
}
else
{
long index = 0;
while(!converter.eof())
{
index = 0;
converter >> index;
if(index == 0)
{
std::cerr << "psalm: I cannot parse face data from line \""
<< line
<< "\".\n";
return(false);
}
// Handle backwards references...
else if(index < 0)
{
// ...and check the range
if((V.size()+index) >= 0)
vertices.push_back(get_vertex(V.size()+index));
else
{
std::cerr << "psalm: Invalid backwards vertex reference "
<< "in line \""
<< line
<< "\".\n";
return(false);
}
}
else
vertices.push_back(get_vertex(index-1)); // Real men 0-index their variables.
}
add_face(vertices);
}
}
keyword.clear();
line.clear();
converter.clear();
}
return(true);
}
/*!
* Saves the currently loaded mesh in Wavefront OBJ format. Only raw
* geometrical data will be output.
*
* @param out Stream for data output
* @return true if the mesh could be stored, else false.
*/
bool mesh::save_obj(std::ostream& out)
{
if(!out.good())
return(false);
for(std::vector<vertex*>::const_iterator it = V.begin(); it != V.end(); it++)
{
v3ctor position = (*it)->get_position();
out << "v " << position[0] << " "
<< position[1] << " "
<< position[2] << "\n";
}
for(std::vector<face*>::const_iterator it = F.begin(); it != F.end(); it++)
{
out << "f ";
for(size_t i = 0; i < (*it)->num_vertices(); i++)
{
out << ((*it)->get_vertex(i)->get_id()+1); // OBJ is 1-indexed, not 0-indexed
if(i < (*it)->num_vertices()-1)
out << " ";
}
out << "\n";
}
return(true);
}
/*!
* Loads a mesh in ASCII Geomview format from an input stream.
*
* @param in Input stream (file, standard input)
* @return true if the mesh could be loaded, else false
*/
bool mesh::load_off(std::istream& in)
{
if(!in.good())
return(false);
std::string line;
std::istringstream converter;
/*
Read "header", i.e.,
OFF
num_vertices num_faces num_edges
where num_edges is ignored.
*/
std::getline(in, line);
if(line != "OFF")
{
std::cerr << "psalm: I am missing a \"OFF\" header for the input data.\n";
return(false);
}
size_t num_vertices, num_faces, num_edges;
size_t cur_line_num = 0; // count line numbers (after header)
std::getline(in, line);
converter.str(line);
converter >> num_vertices >> num_faces >> num_edges;
if(converter.fail())
{
std::cerr << "psalm: I cannot parse vertex, face, and edge numbers from \"" << line << "\"\n";
return(false);
}
converter.clear();
line.clear();
// These are specify the only keywords of the .OBJ file that the parse
// is going to understand
while(!std::getline(in, line).eof())
{
converter.str(line);
if(cur_line_num < num_vertices)
{
double x, y, z;
converter >> x >> y >> z;
if(converter.fail())
{
std::cerr << "psalm: I tried to parse vertex coordinates from line \""
<< line
<<" \" and failed.\n";
return(false);
}
add_vertex(x,y,z);
}
else if((cur_line_num-num_vertices) < num_faces)
{
size_t k = 0;
size_t index = 0;
converter >> k;
std::vector<vertex*> vertices;
for(size_t i = 0; i < k; i++)
{
converter >> index;
if(converter.fail())
{
std::cerr << "psalm: Tried to parse face data in line \""
<< line
<< "\", but failed.\n";
return(false);
}
if(index >= V.size())
{
std::cerr << "psalm: Index " << index << "in line \""
<< line
<< "\" is out of bounds.\n";
return(false);
}
vertices.push_back(get_vertex(index));
}
add_face(vertices);
}
else
{
std::cerr << "psalm: Got an unexpected data line \"" << line << "\".\n";
return(false);
}
cur_line_num++;
converter.clear();
line.clear();
}
return(true);
}
/*!
* Saves the currently loaded mesh in ASCII Geomview object file format
* (OFF).
*
* @param out Stream for data output
* @return true if the mesh could be stored, else false.
*/
bool mesh::save_off(std::ostream& out)
{
if(!out.good())
return(false);
out << "OFF\n"
<< V.size() << " " << F.size() << " " << "0\n"; // For programs that actually interpret edge data,
// the last parameter should be changed
for(std::vector<vertex*>::const_iterator it = V.begin(); it != V.end(); it++)
{
v3ctor position = (*it)->get_position();
out << position[0] << " "
<< position[1] << " "
<< position[2] << "\n";
}
for(std::vector<face*>::const_iterator it = F.begin(); it != F.end(); it++)
{
out << (*it)->num_vertices()
<< " ";
for(size_t i = 0; i < (*it)->num_vertices(); i++)
{
out << (*it)->get_vertex(i)->get_id();
if(i < (*it)->num_vertices()-1)
out << " ";
}
out << "\n";
}
return(true);
}
/*!
* Saves the mesh in a special format for holes. The file format is
* reminiscent of Wavefront OBJ: First, a list of non-boundary vertices is
* written in the form "v x y z". Afterwards, faces are written out as "f
* i j k", where i,j,k refers to the vertices that form the face. A
* negative vertex index indicates a boundary vertex that has been left
* out of the vertex list. A positive vertex index, starting at 0, refers
* to the vertices that were written to the file.
*
* @param out Stream for data output
* @return true if the mesh could be stored, else false.
*/
bool mesh::save_hole(std::ostream& out)
{
size_t num_boundary_vertices = 0;
for(std::vector<vertex*>::const_iterator v_it = V.begin(); v_it < V.end(); v_it++)
{
vertex* v = *v_it;
if(v->is_on_boundary())
num_boundary_vertices++;
else
out << "v " << v->get_position();
}
// num_boundary_vertices is used to adjust the offsets of vertices when
// writing the indexed faces
for(std::vector<face*>::const_iterator f_it = F.begin(); f_it < F.end(); f_it++)
{
out << "f ";
for(size_t i = 0; i < (*f_it)->num_vertices(); i++)
{
vertex* v = (*f_it)->get_vertex(i);
if(v->is_on_boundary())
out << "-" << v->get_id();
else
// If the offset is subtracted, all new vertices are in
// the range of [num_boundary_vertices, ...]. Hence, the
// range is adjusted by subtracting num_boundary_vertices
// again.
out << (v->get_id() - id_offset - num_boundary_vertices);
// No trailing spaces for the last entry
if(i < (*f_it)->num_vertices()-1)
out << " ";
}
out << "\n";
}
return(true);
}
/*!
* Destroys the mesh and all related data structures and frees up used
* memory.
*/
void mesh::destroy()
{
for(std::vector<vertex*>::iterator it = V.begin(); it != V.end(); it++)
{
if(*it != NULL)
delete(*it);
}
for(std::vector<edge*>::iterator it = E.begin(); it != E.end(); it++)
{
if(*it != NULL)
delete(*it);
}
for(std::vector<face*>::iterator it = F.begin(); it != F.end(); it++)
{
if(*it != NULL)
delete(*it);
}
V.clear();
E.clear();
F.clear();
E_M.clear();
}
/*!
* Replaces the current mesh with another one. The other mesh will
* be deleted/cleared by this operation.
*
* @param M Mesh to replace current mesh with
*/
void mesh::replace_with(mesh& M)
{
this->destroy();
this->V = M.V;
this->F = M.F;
this->E = M.E;
this->E_M = M.E_M;
// Options will _not_ be overwritten by this operation; previously this
// was the case.
// Clear old mesh
M.V.clear();
M.F.clear();
M.E.clear();
M.E_M.clear();
}
/*!
* Calculates the density of a triangular mesh by dividing the number of
* vertices by the area of the mesh.
*
* @return density = num_vertices / area or 0.0 if the area of the mesh is
* zero
*/
double mesh::get_density() // XXX: Should be a `const` function
{
double area = 0.0;
for(size_t i = 0; i < num_faces(); i++)
{
const face* f = get_face(i);
v3ctor a = f->get_vertex(1)->get_position() - f->get_vertex(0)->get_position();
v3ctor b = f->get_vertex(2)->get_position() - f->get_vertex(0)->get_position();
area += 0.5*(a|b).length();
}
if(area != 0.0)
return(num_vertices()/area);
else
return(0.0);
}
/*!
* Given a vector of pointers to vertices, where the vertices are assumed
* to be in counterclockwise order, construct a face and add it to the