FileManagerT.cc 34.2 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
//==================================================

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

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

69
70
71
72
73
    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
74

75
76
77
78
    std::stringstream sstr;
    std::string line;
    std::string s_tmp;
    unsigned int c = 0u;
Mike Kremer's avatar
Mike Kremer committed
79
80
    typedef typename MeshT::PointT Point;
    Point v = Point(0.0, 0.0, 0.0);
81
82
83
84
85
86
    unsigned int v1 = 0; unsigned int v2 = 0;

    /*
     * Header
     */

87
88
    bool header_found = true;

89
90
91
92
93
94
95
96
    // 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") {
97
98
        //iff.close();
        header_found = false;
Mike Kremer's avatar
Mike Kremer committed
99
        std::cerr << "The specified file might not be in OpenVolumeMesh format!" << std::endl;
100
        //return false;
101
    }
Mike Kremer's avatar
Mike Kremer committed
102

103
104
105
106
107
108
109
110
    // 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
111

112
113
114
    /*
     * Vertices
     */
115
116
117
118
119
120
121
122
    if(!header_found) {
        sstr.clear();
        sstr.str(line);
    } else {
        getCleanLine(iff, line);
        sstr.clear();
        sstr.str(line);
    }
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

    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
155
156
    parseVertexProperties<MeshT, typename MeshT::VertexIter>
        (iff, sstr, "VERTEX_PROPERTY", _mesh, _mesh.vertices_begin(), _mesh.vertices_end());
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

    /*
     * 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
191
192
193
194
    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());
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

    /*
     * 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
240
241
242
243
    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());
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

    /*
     * 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
291
        parseCellProperties<MeshT, typename MeshT::CellIter>
Mike Kremer's avatar
Mike Kremer committed
292
            (iff, sstr, "POLYHEDRON_PROPERTY", _mesh, _mesh.cells_begin(), _mesh.cells_end());
293
    }
Mike Kremer's avatar
Mike Kremer committed
294
295
296

    iff.close();

297
298
299
300
    // Compute top-down-adjacencies
    if(_computeBottomUpAdjacencies) {
        _mesh.update_adjacencies();
    }
Mike Kremer's avatar
Mike Kremer committed
301

302
303
304
    // Compute face normals
    if(_computeFaceNormals) {
        _mesh.request_face_normals();
Mike Kremer's avatar
Mike Kremer committed
305
306
    }

307
308
309
310
311
312
313
314
315
316
317
318
    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
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
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>
819
820
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
821

822
823
    std::string line;
    std::stringstream sstr;
Mike Kremer's avatar
Mike Kremer committed
824

825
826
    PropHandleT handle;
    _mesh.add_property(handle, _s_tmp);
Mike Kremer's avatar
Mike Kremer committed
827

828
    PropT c;
Mike Kremer's avatar
Mike Kremer committed
829

830
831
    IterT it = _begin;
    for(; it != _end; ++it) {
Mike Kremer's avatar
Mike Kremer committed
832

833
834
835
836
837
838
839
        getCleanLine(_iff, line);
        sstr.clear();
        sstr.str(line);
        sstr >> c;

        _mesh.property(handle, *it) = c;
    }
Mike Kremer's avatar
Mike Kremer committed
840
841
842
843
844
}

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

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

Mike Kremer's avatar
Mike Kremer committed
847
848
849
850
851
852
853
854
855
  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;
  }

856
857
858
859
  // Write header
  off << "OVM ASCII" << std::endl;

  unsigned int n_vertices(_mesh.n_vertices());
Mike Kremer's avatar
Mike Kremer committed
860
861
862
  off << "Vertices" << std::endl;
  off << n_vertices << std::endl;

Mike Kremer's avatar
Mike Kremer committed
863
864
  typedef typename MeshT::PointT Point;

Mike Kremer's avatar
Mike Kremer committed
865
866
867
  // write vertices
  for (typename MeshT::VertexIter v_it = _mesh.v_iter(); v_it; ++v_it) {

Mike Kremer's avatar
Mike Kremer committed
868
    Point v = _mesh.vertex(*v_it).position();
Mike Kremer's avatar
Mike Kremer committed
869
870
871
    off << v[0] << " " << v[1] << " " << v[2] << std::endl;
  }

872
873
874
875
  // Write vertex props
  writePropertySection<typename MeshT::const_prop_iterator>
                      (off, "Vertex_Property", _mesh.vprops_begin(), _mesh.vprops_end());

876
  unsigned int n_edges(_mesh.n_edges());
Mike Kremer's avatar
Mike Kremer committed
877
878
879
880
881
882
883
884
885
886
887
  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;
  }

888
889
890
891
892
893
894
  // 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());

895
  unsigned int n_faces(_mesh.n_faces());
Mike Kremer's avatar
Mike Kremer committed
896
897
898
899
900
901
902
903
904
905
906
907
908
  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) {

909
      off << it->idx();
Mike Kremer's avatar
Mike Kremer committed
910
911
912
913
914
915
916
917

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

    off << std::endl;
  }

918
919
920
921
922
923
924
  // 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());

925
  unsigned int n_cells(_mesh.n_cells());
Mike Kremer's avatar
Mike Kremer committed
926
927
928
929
930
931
932
933
934
935
936
937
  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) {

938
      off << it->idx();
Mike Kremer's avatar
Mike Kremer committed
939
940
941
942
943
944
945
946

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

    off << std::endl;
  }

947
948
949
950
  // 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
951
952
953
954
955
956
957
  off.close();

  return true;
}

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

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
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
1032
1033
1034
} // Namespace IO

} // Namespace FileManager