FileManagerT.cc 36.8 KB
Newer Older
Mike Kremer's avatar
Mike Kremer committed
1
2
3
4
/*===========================================================================*\
 *                                                                           *
 *                            OpenVolumeMesh                                 *
 *        Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen         *
5
 *                        www.openvolumemesh.org                             *
Mike Kremer's avatar
Mike Kremer committed
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
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenVolumeMesh.                                     *
 *                                                                           *
 *  OpenVolumeMesh is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenVolumeMesh is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenVolumeMesh.  If not,                              *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                          *
 *   $Date$                   *
 *   $LastChangedBy$                                                *
 *                                                                           *
\*===========================================================================*/

Mike Kremer's avatar
Mike Kremer committed
43
44
#define FILEMANAGERT_CC

45
46
47
48
49
50
51
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <typeinfo>

Mike Kremer's avatar
Mike Kremer committed
52
#include <OpenVolumeMesh/Geometry/VectorT.hh>
53
#include <OpenVolumeMesh/PolyhedralMesh/PolyhedralMesh.hh>
Mike Kremer's avatar
Mike Kremer committed
54

Mike Kremer's avatar
Mike Kremer committed
55
56
57
58
59
60
#include "FileManager.hh"

namespace OpenVolumeMesh {

namespace IO {

Mike Kremer's avatar
Mike Kremer committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//==================================================

FileManager::FileManager() {

}

//==================================================

FileManager::~FileManager() {

}

//==================================================

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
void FileManager::trimString(std::string& _string) const {

    // Trim Both leading and trailing spaces
    size_t start = _string.find_first_not_of(" \t\r\n");
    size_t end = _string.find_last_not_of(" \t\r\n");

    if((std::string::npos == start) || (std::string::npos == end)) {
        _string = "";
    } else {
        _string = _string.substr(start, end - start + 1);
    }
}

//==================================================

void FileManager::extractQuotedText(std::string& _string) const {

    // Trim Both leading and trailing quote marks
    size_t start = _string.find_first_of("\""); ++start;
    size_t end = _string.find_last_not_of("\"");

    if((std::string::npos == start) || (std::string::npos == end)) {
        _string = "";
    } else {
        _string = _string.substr(start, end - start + 1);
    }
}

//==================================================

bool FileManager::getCleanLine(std::istream& _ifs, std::string& _string, bool _skipEmptyLines) const {

    // While we are not at the end of the file
    while(true) {

        // Get the current line:
        std::getline(_ifs, _string);

        // Remove whitespace at beginning and end
        trimString(_string);

        // Check if string is not empty ( otherwise we continue
        if(_string.size() != 0) {

            // Check if string is a comment ( starting with # )
            if(_string[0] != '#') {
                return true;
            }

        } else {
            if(!_skipEmptyLines)
                return true;
        }

        if(_ifs.eof()) {
            std::cerr << "End of file reached while searching for input!" << std::endl;
            return false;
        }
    }

    return false;
}

//==================================================

Mike Kremer's avatar
Mike Kremer committed
140
template <class MeshT>
Mike Kremer's avatar
Mike Kremer committed
141
142
143
bool FileManager::readFile(const std::string& _filename, MeshT& _mesh,
    bool _topologyCheck, bool _computeBottomUpAdjacencies, bool _computeFaceNormals) const {

144
    std::ifstream iff(_filename.c_str(), std::ios::in);
Mike Kremer's avatar
Mike Kremer committed
145

146
147
148
149
150
    if(!iff.good()) {
        std::cerr << "Error: Could not open file " << _filename << " for reading!" << std::endl;
        iff.close();
        return false;
    }
Mike Kremer's avatar
Mike Kremer committed
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
    std::stringstream sstr;
    std::string line;
    std::string s_tmp;
    unsigned int c = 0u;
    typedef OpenVolumeMesh::Geometry::Vec3d Vec3d;
    Vec3d v = Vec3d(0.0, 0.0, 0.0);
    unsigned int v1 = 0; unsigned int v2 = 0;

    /*
     * Header
     */

    // Get first line
    getCleanLine(iff, line);
    sstr.str(line);

    // Check header
    sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    if(s_tmp != "OVM") {
        iff.close();
        std::cerr << "The specified file is not in OpenVolumeMesh format!" << std::endl;
        return false;
    }
Mike Kremer's avatar
Mike Kremer committed
176

177
178
179
180
181
182
183
184
    // Get ASCII/BINARY string
    sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    if(s_tmp == "BINARY") {
        iff.close();
        std::cerr << "Binary files are not supported at the moment!" << std::endl;
        return false;
    }
Mike Kremer's avatar
Mike Kremer committed
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
    /*
     * Vertices
     */
    getCleanLine(iff, line);
    sstr.clear();
    sstr.str(line);

    sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    if(s_tmp != "VERTICES") {
        iff.close();
        std::cerr << "No vertex section defined!" << std::endl;
        return false;
    } else {

        // Read in number of vertices
        getCleanLine(iff, line);
        sstr.clear();
        sstr.str(line);
        sstr >> c;

        // Read in vertices
        for(unsigned int i = 0u; i < c; ++i) {

            getCleanLine(iff, line);
            sstr.clear();
            sstr.str(line);
            sstr >> v[0];
            sstr >> v[1];
            sstr >> v[2];
            _mesh.add_vertex(v);
        }
    }

    getCleanLine(iff, line);
    sstr.clear();
    sstr.str(line);

Mike Kremer's avatar
Mike Kremer committed
224
225
    parseVertexProperties<MeshT, typename MeshT::VertexIter>
        (iff, sstr, "VERTEX_PROPERTY", _mesh, _mesh.vertices_begin(), _mesh.vertices_end());
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

    /*
     * Edges
     */
    sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    if(s_tmp != "EDGES") {
        iff.close();
        std::cerr << "No edge section defined!" << std::endl;
        return false;
    } else {

        // Read in number of edges
        getCleanLine(iff, line);
        sstr.clear();
        sstr.str(line);
        sstr >> c;

        // Read in edges
        for(unsigned int i = 0u; i < c; ++i) {

            getCleanLine(iff, line);
            sstr.clear();
            sstr.str(line);
            sstr >> v1;
            sstr >> v2;
            _mesh.add_edge(typename MeshT::VertexHandle(v1), typename MeshT::VertexHandle(v2));
        }
    }

    getCleanLine(iff, line);
    sstr.clear();
    sstr.str(line);

Mike Kremer's avatar
Mike Kremer committed
260
261
262
263
    parseEdgeProperties<MeshT, typename MeshT::EdgeIter>
        (iff, sstr, "EDGE_PROPERTY", _mesh, _mesh.edges_begin(), _mesh.edges_end());
    parseHalfEdgeProperties<MeshT, typename MeshT::HalfEdgeIter>
        (iff, sstr, "HALFEDGE_PROPERTY", _mesh, _mesh.halfedges_begin(), _mesh.halfedges_end());
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

    /*
     * Faces
     */
    sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    if(s_tmp != "FACES") {
        iff.close();
        std::cerr << "No face section defined!" << std::endl;
        return false;
    } else {

        // Read in number of faces
        getCleanLine(iff, line);
        sstr.clear();
        sstr.str(line);
        sstr >> c;

        // Read in faces
        for(unsigned int i = 0u; i < c; ++i) {

            getCleanLine(iff, line);
            sstr.clear();
            sstr.str(line);

            std::vector<typename MeshT::HalfEdgeHandle> hes;

            // Get face valence
            unsigned int val = 0u;
            sstr >> val;

            // Read half-edge indices
            for(unsigned int e = 0; e < val; ++e) {
                sstr >> v1;
                hes.push_back(typename MeshT::HalfEdgeHandle(v1));
            }

            _mesh.add_face(hes, _topologyCheck);
        }
    }

    getCleanLine(iff, line);
    sstr.clear();
    sstr.str(line);

Mike Kremer's avatar
Mike Kremer committed
309
310
311
312
    parseFaceProperties<MeshT, typename MeshT::FaceIter>
        (iff, sstr, "FACE_PROPERTY", _mesh, _mesh.faces_begin(), _mesh.faces_end());
    parseHalfFaceProperties<MeshT, typename MeshT::HalfFaceIter>
        (iff, sstr, "HALFFACE_PROPERTY", _mesh, _mesh.halffaces_begin(), _mesh.halffaces_end());
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

    /*
     * Cells
     */
    sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    if(s_tmp != "POLYHEDRA") {
        iff.close();
        std::cerr << "No polyhedra section defined!" << std::endl;
        return false;
    } else {

        // Read in number of cells
        getCleanLine(iff, line);
        sstr.clear();
        sstr.str(line);
        sstr >> c;

        // Read in cells
        for(unsigned int i = 0u; i < c; ++i) {

            getCleanLine(iff, line);
            sstr.clear();
            sstr.str(line);

            std::vector<typename MeshT::HalfFaceHandle> hfs;

            // Get cell valence
            unsigned int val = 0u;
            sstr >> val;

            // Read half-face indices
            for(unsigned int f = 0; f < val; ++f) {
                sstr >> v1;
                hfs.push_back(typename MeshT::HalfFaceHandle(v1));
            }

            _mesh.add_cell(hfs, _topologyCheck);
        }
    }

    if(!iff.eof()) {

        getCleanLine(iff, line);
        sstr.clear();
        sstr.str(line);

Mike Kremer's avatar
Mike Kremer committed
360
        parseCellProperties<MeshT, typename MeshT::CellIter>
Mike Kremer's avatar
Mike Kremer committed
361
            (iff, sstr, "POLYHEDRON_PROPERTY", _mesh, _mesh.cells_begin(), _mesh.cells_end());
362
    }
Mike Kremer's avatar
Mike Kremer committed
363
364
365

    iff.close();

366
367
368
369
    // Compute top-down-adjacencies
    if(_computeBottomUpAdjacencies) {
        _mesh.update_adjacencies();
    }
Mike Kremer's avatar
Mike Kremer committed
370

371
372
373
    // Compute face normals
    if(_computeFaceNormals) {
        _mesh.request_face_normals();
Mike Kremer's avatar
Mike Kremer committed
374
375
    }

376
377
378
379
380
381
382
383
384
385
386
387
    std::cerr << "######## openvolumemesh info #########" << std::endl;
    std::cerr << "#vertices: " << _mesh.n_vertices() << std::endl;
    std::cerr << "#edges:    " << _mesh.n_edges() << std::endl;
    std::cerr << "#faces:    " << _mesh.n_faces() << std::endl;
    std::cerr << "#cells:    " << _mesh.n_cells() << std::endl;
    std::cerr << "######################################" << std::endl;

    return true;
}

//==================================================

Mike Kremer's avatar
Mike Kremer committed
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
template<class MeshT, typename IterT>
void FileManager::parseVertexProperties(std::ifstream& _iff, std::stringstream& _sstr, const std::string& _identifier, MeshT& _mesh,
                                  const IterT& _begin, const IterT& _end) const {

    if(_iff.eof()) return;

    std::string line = _sstr.str();
    std::string s_tmp;

    // Check for properties
    _sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);

    // No property found
    if(s_tmp != _identifier) {
        // Reset stream
        _sstr.clear();
        _sstr.str(line);
        return;
    }

    while(s_tmp == _identifier && !_iff.eof()) {

        s_tmp = _sstr.str();
        extractQuotedText(s_tmp);

        std::string type;
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        _sstr >> type;

        // Add and initialize property
        if(type == "int") {
            initializeProperty<MeshT, VPropHandleT<int>, int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned int") {
            initializeProperty<MeshT, VPropHandleT<unsigned int>, unsigned int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "float") {
            initializeProperty<MeshT, VPropHandleT<float>, float, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "double") {
            initializeProperty<MeshT, VPropHandleT<double>, double, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "char") {
            initializeProperty<MeshT, VPropHandleT<char>, char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned char") {
            initializeProperty<MeshT, VPropHandleT<unsigned char>, unsigned char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "bool") {
            initializeProperty<MeshT, VPropHandleT<bool>, bool, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "string") {
            initializeProperty<MeshT, VPropHandleT<std::string>, std::string, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else {
            std::cerr << "Data type '" << type << "' not recognized!" << std::endl;

            // Skip number of entities lines
            for(IterT it = _begin; it != _end; ++it) {
                getCleanLine(_iff, line);
            }
        }

        // Get next line
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        // Check for properties
        _sstr >> s_tmp;
        std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    }

    _sstr.clear();
    _sstr.str(line);
}

//==================================================

template<class MeshT, typename IterT>
void FileManager::parseEdgeProperties(std::ifstream& _iff, std::stringstream& _sstr, const std::string& _identifier, MeshT& _mesh,
                                  const IterT& _begin, const IterT& _end) const {

    if(_iff.eof()) return;

    std::string line = _sstr.str();
    std::string s_tmp;

    // Check for properties
    _sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);

    // No property found
    if(s_tmp != _identifier) {
        // Reset stream
        _sstr.clear();
        _sstr.str(line);
        return;
    }

    while(s_tmp == _identifier && !_iff.eof()) {

        s_tmp = _sstr.str();
        extractQuotedText(s_tmp);

        std::string type;
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        _sstr >> type;

        // Add and initialize property
        if(type == "int") {
            initializeProperty<MeshT, EPropHandleT<int>, int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned int") {
            initializeProperty<MeshT, EPropHandleT<unsigned int>, unsigned int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "float") {
            initializeProperty<MeshT, EPropHandleT<float>, float, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "double") {
            initializeProperty<MeshT, EPropHandleT<double>, double, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "char") {
            initializeProperty<MeshT, EPropHandleT<char>, char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned char") {
            initializeProperty<MeshT, EPropHandleT<unsigned char>, unsigned char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "bool") {
            initializeProperty<MeshT, EPropHandleT<bool>, bool, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "string") {
            initializeProperty<MeshT, EPropHandleT<std::string>, std::string, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else {
            std::cerr << "Data type '" << type << "' not recognized!" << std::endl;

            // Skip number of entities lines
            for(IterT it = _begin; it != _end; ++it) {
                getCleanLine(_iff, line);
            }
        }

        // Get next line
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        // Check for properties
        _sstr >> s_tmp;
        std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    }

    _sstr.clear();
    _sstr.str(line);
}

//==================================================

template<class MeshT, typename IterT>
void FileManager::parseHalfEdgeProperties(std::ifstream& _iff, std::stringstream& _sstr, const std::string& _identifier, MeshT& _mesh,
                                  const IterT& _begin, const IterT& _end) const {

    if(_iff.eof()) return;

    std::string line = _sstr.str();
    std::string s_tmp;

    // Check for properties
    _sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);

    // No property found
    if(s_tmp != _identifier) {
        // Reset stream
        _sstr.clear();
        _sstr.str(line);

        return;
    }

    while(s_tmp == _identifier && !_iff.eof()) {

        s_tmp = _sstr.str();
        extractQuotedText(s_tmp);

        std::string type;
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        _sstr >> type;

        // Add and initialize property
        if(type == "int") {
            initializeProperty<MeshT, HEPropHandleT<int>, int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned int") {
            initializeProperty<MeshT, HEPropHandleT<unsigned int>, unsigned int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "float") {
            initializeProperty<MeshT, HEPropHandleT<float>, float, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "double") {
            initializeProperty<MeshT, HEPropHandleT<double>, double, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "char") {
            initializeProperty<MeshT, HEPropHandleT<char>, char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned char") {
            initializeProperty<MeshT, HEPropHandleT<unsigned char>, unsigned char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "bool") {
            initializeProperty<MeshT, HEPropHandleT<bool>, bool, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "string") {
            initializeProperty<MeshT, HEPropHandleT<std::string>, std::string, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else {
            std::cerr << "Data type '" << type << "' not recognized!" << std::endl;

            // Skip number of entities lines
            for(IterT it = _begin; it != _end; ++it) {
                getCleanLine(_iff, line);
            }
        }

        // Get next line
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        // Check for properties
        _sstr >> s_tmp;
        std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    }

    _sstr.clear();
    _sstr.str(line);
}

//==================================================

template<class MeshT, typename IterT>
void FileManager::parseFaceProperties(std::ifstream& _iff, std::stringstream& _sstr, const std::string& _identifier, MeshT& _mesh,
                                  const IterT& _begin, const IterT& _end) const {

    if(_iff.eof()) return;

    std::string line = _sstr.str();
    std::string s_tmp;

    // Check for properties
    _sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);

    // No property found
    if(s_tmp != _identifier) {
        // Reset stream
        _sstr.clear();
        _sstr.str(line);
        return;
    }

    while(s_tmp == _identifier && !_iff.eof()) {

        s_tmp = _sstr.str();
        extractQuotedText(s_tmp);

        std::string type;
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        _sstr >> type;

        // Add and initialize property
        if(type == "int") {
            initializeProperty<MeshT, FPropHandleT<int>, int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned int") {
            initializeProperty<MeshT, FPropHandleT<unsigned int>, unsigned int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "float") {
            initializeProperty<MeshT, FPropHandleT<float>, float, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "double") {
            initializeProperty<MeshT, FPropHandleT<double>, double, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "char") {
            initializeProperty<MeshT, FPropHandleT<char>, char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned char") {
            initializeProperty<MeshT, FPropHandleT<unsigned char>, unsigned char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "bool") {
            initializeProperty<MeshT, FPropHandleT<bool>, bool, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "string") {
            initializeProperty<MeshT, FPropHandleT<std::string>, std::string, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else {
            std::cerr << "Data type '" << type << "' not recognized!" << std::endl;

            // Skip number of entities lines
            for(IterT it = _begin; it != _end; ++it) {
                getCleanLine(_iff, line);
            }
        }

        // Get next line
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        // Check for properties
        _sstr >> s_tmp;
        std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    }

    _sstr.clear();
    _sstr.str(line);
}

//==================================================

template<class MeshT, typename IterT>
void FileManager::parseHalfFaceProperties(std::ifstream& _iff, std::stringstream& _sstr, const std::string& _identifier, MeshT& _mesh,
                                  const IterT& _begin, const IterT& _end) const {

    if(_iff.eof()) return;

    std::string line = _sstr.str();
    std::string s_tmp;

    // Check for properties
    _sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);

    // No property found
    if(s_tmp != _identifier) {
        // Reset stream
        _sstr.clear();
        _sstr.str(line);
        return;
    }

    while(s_tmp == _identifier && !_iff.eof()) {

        s_tmp = _sstr.str();
        extractQuotedText(s_tmp);

        std::string type;
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        _sstr >> type;

        // Add and initialize property
        if(type == "int") {
            initializeProperty<MeshT, HFPropHandleT<int>, int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned int") {
            initializeProperty<MeshT, HFPropHandleT<unsigned int>, unsigned int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "float") {
            initializeProperty<MeshT, HFPropHandleT<float>, float, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "double") {
            initializeProperty<MeshT, HFPropHandleT<double>, double, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "char") {
            initializeProperty<MeshT, HFPropHandleT<char>, char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned char") {
            initializeProperty<MeshT, HFPropHandleT<unsigned char>, unsigned char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "bool") {
            initializeProperty<MeshT, HFPropHandleT<bool>, bool, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "string") {
            initializeProperty<MeshT, HFPropHandleT<std::string>, std::string, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else {
            std::cerr << "Data type '" << type << "' not recognized!" << std::endl;

            // Skip number of entities lines
            for(IterT it = _begin; it != _end; ++it) {
                getCleanLine(_iff, line);
            }
        }

        // Get next line
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        // Check for properties
        _sstr >> s_tmp;
        std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    }

    _sstr.clear();
    _sstr.str(line);
}

//==================================================

template<class MeshT, typename IterT>
void FileManager::parseCellProperties(std::ifstream& _iff, std::stringstream& _sstr, const std::string& _identifier, MeshT& _mesh,
                                  const IterT& _begin, const IterT& _end) const {

    if(_iff.eof()) return;

    std::string line = _sstr.str();
    std::string s_tmp;

    // Check for properties
    _sstr >> s_tmp;
    std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);

    // No property found
    if(s_tmp != _identifier) {
        // Reset stream
        _sstr.clear();
        _sstr.str(line);
        return;
    }

    while(s_tmp == _identifier && !_iff.eof()) {

        s_tmp = _sstr.str();
        extractQuotedText(s_tmp);

        std::string type;
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        _sstr >> type;

        // Add and initialize property
        if(type == "int") {
            initializeProperty<MeshT, CPropHandleT<int>, int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned int") {
            initializeProperty<MeshT, CPropHandleT<unsigned int>, unsigned int, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "float") {
            initializeProperty<MeshT, CPropHandleT<float>, float, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "double") {
            initializeProperty<MeshT, CPropHandleT<double>, double, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "char") {
            initializeProperty<MeshT, CPropHandleT<char>, char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "unsigned char") {
            initializeProperty<MeshT, CPropHandleT<unsigned char>, unsigned char, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "bool") {
            initializeProperty<MeshT, CPropHandleT<bool>, bool, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else if(type == "string") {
            initializeProperty<MeshT, CPropHandleT<std::string>, std::string, IterT>
            (_iff, _mesh, s_tmp, _begin, _end);
        } else {
            std::cerr << "Data type '" << type << "' not recognized!" << std::endl;

            // Skip number of entities lines
            for(IterT it = _begin; it != _end; ++it) {
                getCleanLine(_iff, line);
            }
        }

        // Get next line
        getCleanLine(_iff, line);
        _sstr.clear();
        _sstr.str(line);

        // Check for properties
        _sstr >> s_tmp;
        std::transform(s_tmp.begin(), s_tmp.end(), s_tmp.begin(), ::toupper);
    }

    _sstr.clear();
    _sstr.str(line);
}

//==================================================

template<class MeshT, typename PropHandleT, typename PropT, typename IterT>
888
889
void FileManager::initializeProperty(std::ifstream& _iff, MeshT& _mesh, const std::string& _s_tmp,
                                     const IterT& _begin, const IterT& _end) const {
Mike Kremer's avatar
Mike Kremer committed
890

891
892
    std::string line;
    std::stringstream sstr;
Mike Kremer's avatar
Mike Kremer committed
893

894
895
    PropHandleT handle;
    _mesh.add_property(handle, _s_tmp);
Mike Kremer's avatar
Mike Kremer committed
896

897
    PropT c;
Mike Kremer's avatar
Mike Kremer committed
898

899
900
    IterT it = _begin;
    for(; it != _end; ++it) {
Mike Kremer's avatar
Mike Kremer committed
901

902
903
904
905
906
907
908
        getCleanLine(_iff, line);
        sstr.clear();
        sstr.str(line);
        sstr >> c;

        _mesh.property(handle, *it) = c;
    }
Mike Kremer's avatar
Mike Kremer committed
909
910
911
912
913
}

//==================================================

template <class MeshT>
Mike Kremer's avatar
Mike Kremer committed
914
bool FileManager::writeFile(const std::string& _filename, const MeshT& _mesh) const {
Mike Kremer's avatar
Mike Kremer committed
915

Mike Kremer's avatar
Mike Kremer committed
916
917
918
919
920
921
922
923
924
  typedef typename MeshT::Face Face;
  std::ofstream off(_filename.c_str(), std::ios::out);

  if(!off.good()) {
    std::cerr << "Error: Could not open file " << _filename << " for writing!" << std::endl;
    off.close();
    return false;
  }

925
926
927
928
  // Write header
  off << "OVM ASCII" << std::endl;

  unsigned int n_vertices(_mesh.n_vertices());
Mike Kremer's avatar
Mike Kremer committed
929
930
931
932
933
934
935
936
937
938
  off << "Vertices" << std::endl;
  off << n_vertices << std::endl;

  // write vertices
  for (typename MeshT::VertexIter v_it = _mesh.v_iter(); v_it; ++v_it) {

    Vec3d v = _mesh.vertex(*v_it).position();
    off << v[0] << " " << v[1] << " " << v[2] << std::endl;
  }

939
940
941
942
  // Write vertex props
  writePropertySection<typename MeshT::const_prop_iterator>
                      (off, "Vertex_Property", _mesh.vprops_begin(), _mesh.vprops_end());

943
  unsigned int n_edges(_mesh.n_edges());
Mike Kremer's avatar
Mike Kremer committed
944
945
946
947
948
949
950
951
952
953
954
  off << "Edges" << std::endl;
  off << n_edges << std::endl;

  // write edges
  for (typename MeshT::EdgeIter e_it = _mesh.e_iter(); e_it; ++e_it) {

    typename MeshT::VertexHandle from_vertex = _mesh.edge(*e_it).from_vertex();
    typename MeshT::VertexHandle to_vertex = _mesh.edge(*e_it).to_vertex();
    off << from_vertex << " " << to_vertex << std::endl;
  }

955
956
957
958
959
960
961
  // Write edge props
  writePropertySection<typename MeshT::const_prop_iterator>
                      (off, "Edge_Property", _mesh.eprops_begin(), _mesh.eprops_end());
  // Write half-edge props
  writePropertySection<typename MeshT::const_prop_iterator>
                      (off, "HalfEdge_Property", _mesh.heprops_begin(), _mesh.heprops_end());

962
  unsigned int n_faces(_mesh.n_faces());
Mike Kremer's avatar
Mike Kremer committed
963
964
965
966
967
968
969
970
971
972
973
974
975
  off << "Faces" << std::endl;
  off << n_faces << std::endl;

  // write faces
  for (typename MeshT::FaceIter f_it = _mesh.f_iter(); f_it; ++f_it) {

    off << _mesh.face(*f_it).halfedges().size() << " ";

    std::vector<typename MeshT::HalfEdgeHandle> halfedges = _mesh.face(*f_it).halfedges();

    for (typename std::vector<typename MeshT::HalfEdgeHandle>::const_iterator it = halfedges.begin(); it
        != halfedges.end(); ++it) {

976
      off << it->idx();
Mike Kremer's avatar
Mike Kremer committed
977
978
979
980
981
982
983
984

      if ((it + 1) != halfedges.end())
        off << " ";
    }

    off << std::endl;
  }

985
986
987
988
989
990
991
  // Write face props
  writePropertySection<typename MeshT::const_prop_iterator>
                      (off, "Face_Property", _mesh.fprops_begin(), _mesh.fprops_end());
  // Write half-face props
  writePropertySection<typename MeshT::const_prop_iterator>
                      (off, "HalfFace_Property", _mesh.hfprops_begin(), _mesh.hfprops_end());

992
  unsigned int n_cells(_mesh.n_cells());
Mike Kremer's avatar
Mike Kremer committed
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  off << "Polyhedra" << std::endl;
  off << n_cells << std::endl;

  for (typename MeshT::CellIter c_it = _mesh.c_iter(); c_it; ++c_it) {

    off << _mesh.cell(*c_it).halffaces().size() << " ";

    std::vector<typename MeshT::HalfFaceHandle> halffaces = _mesh.cell(*c_it).halffaces();

    for (typename std::vector<typename MeshT::HalfFaceHandle>::const_iterator it = halffaces.begin(); it
        != halffaces.end(); ++it) {

1005
      off << it->idx();
Mike Kremer's avatar
Mike Kremer committed
1006
1007
1008
1009
1010
1011
1012
1013

      if ((it + 1) != halffaces.end())
        off << " ";
    }

    off << std::endl;
  }

1014
1015
1016
1017
  // Write polyhedron props
  writePropertySection<typename MeshT::const_prop_iterator>
                      (off, "Polyhedron_Property", _mesh.cprops_begin(), _mesh.cprops_end());

Mike Kremer's avatar
Mike Kremer committed
1018
1019
1020
1021
1022
1023
1024
  off.close();

  return true;
}

//==================================================

1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
template<typename PropIterT>
void FileManager::writePropertySection(std::ofstream& _ofs, const std::string& _identifier,
                            const PropIterT& _begin, const PropIterT& _end) const {

    for(PropIterT it = _begin; it != _end; ++it) {

        // Write property header
        _ofs << _identifier << " \"" << (*it)->name() << "\"" << std::endl;

        typedef OpenVolumeMeshPropertyT<int> P_I;
        typedef OpenVolumeMeshPropertyT<unsigned int> P_UI;
        typedef OpenVolumeMeshPropertyT<float> P_F;
        typedef OpenVolumeMeshPropertyT<double> P_D;
        typedef OpenVolumeMeshPropertyT<char> P_C;
        typedef OpenVolumeMeshPropertyT<unsigned char> P_UC;
        typedef OpenVolumeMeshPropertyT<bool> P_B;
        typedef OpenVolumeMeshPropertyT<std::string> P_S;

        P_I*  prop_i =  dynamic_cast<P_I*> (*it);
        P_UI* prop_ui = dynamic_cast<P_UI*>(*it);
        P_F*  prop_f =  dynamic_cast<P_F*> (*it);
        P_D*  prop_d =  dynamic_cast<P_D*> (*it);
        P_C*  prop_c =  dynamic_cast<P_C*> (*it);
        P_UC* prop_uc = dynamic_cast<P_UC*>(*it);
        P_B*  prop_b =  dynamic_cast<P_B*> (*it);
        P_S*  prop_s =  dynamic_cast<P_S*> (*it);

        if(prop_i != NULL) {
            _ofs << "int" << std::endl;
            for(typename P_I::vector_type::const_iterator it = prop_i->data_vector().begin();
                    it != prop_i->data_vector().end(); ++it)
                _ofs << *it << std::endl;
        } else if(prop_ui != NULL) {
            _ofs << "unsigned int" << std::endl;
            for(typename P_UI::vector_type::const_iterator it = prop_ui->data_vector().begin();
                    it != prop_ui->data_vector().end(); ++it)
                _ofs << *it << std::endl;
        } else if(prop_f != NULL) {
            _ofs << "float" << std::endl;
            for(typename P_F::vector_type::const_iterator it = prop_f->data_vector().begin();
                    it != prop_f->data_vector().end(); ++it)
                _ofs << *it << std::endl;
        } else if(prop_d != NULL) {
            _ofs << "double" << std::endl;
            for(typename P_D::vector_type::const_iterator it = prop_d->data_vector().begin();
                    it != prop_d->data_vector().end(); ++it)
                _ofs << *it << std::endl;
        } else if(prop_c != NULL) {
            _ofs << "char" << std::endl;
            for(typename P_C::vector_type::const_iterator it = prop_c->data_vector().begin();
                    it != prop_c->data_vector().end(); ++it)
                _ofs << *it << std::endl;
        } else if(prop_uc != NULL) {
            _ofs << "unsigned char" << std::endl;
            for(typename P_UC::vector_type::const_iterator it = prop_uc->data_vector().begin();
                    it != prop_uc->data_vector().end(); ++it)
                _ofs << *it << std::endl;
        } else if(prop_b != NULL) {
            _ofs << "bool" << std::endl;
            for(unsigned int i = 0; i < prop_b->n_elements(); ++i)
                _ofs << (*prop_b)[i] << std::endl;
        } else if(prop_s != NULL) {
            _ofs << "string" << std::endl;
            for(unsigned int i = 0; i < prop_s->n_elements(); ++i)
                _ofs << (*prop_b)[i] << std::endl;
        } else {
            _ofs << "unknown" << std::endl << "0" << std::endl;
            continue;
        }
    }
}

//==================================================

Mike Kremer's avatar
Mike Kremer committed
1099
1100
1101
1102
bool FileManager::isHexahedralMesh(const std::string& _filename) const {

  std::ifstream iff(_filename.c_str(), std::ios::in);

1103
1104
1105
1106
1107
1108
  if(!iff.good()) {
    std::cerr << "Could not open file " << _filename << " for reading!" << std::endl;
    iff.close();
    return false;
  }

Mike Kremer's avatar
Mike Kremer committed
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
  std::string s;
  unsigned int n = 0u;

  // Skip until we find polyhedra section
  while (true || !iff.eof()) {
    iff >> s;
    if (s == "Polyhedra") {
      break;
    }
  }

  if (iff.eof()) {
    // Polyhedra section not found in file. Defaulting to polyhedral type.
    iff.close();
    return false;
  }

  // Read in number of cells
  iff >> n;
  unsigned int v = 0;
  char tmp[256];
  for (unsigned int i = 0; i < n; ++i) {
    iff >> v;
    iff.getline(tmp, 256);
    if (v != 6u) {
      iff.close();
      return false;
    }
  }
  iff.close();
Mike Kremer's avatar
Mike Kremer committed
1139
  return true;
Mike Kremer's avatar
Mike Kremer committed
1140
}
Mike Kremer's avatar
Mike Kremer committed
1141

Mike Kremer's avatar
Mike Kremer committed
1142
1143
//==================================================

Mike Kremer's avatar
Mike Kremer committed
1144
1145
1146
} // Namespace IO

} // Namespace FileManager