Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
667 khor 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
#include "x3d_load.h"
8
 
9
#include <iostream>
10
#include <vector>
11
#include <string>
12
 
13
#include "../CGLA/Vec3f.h"
14
#include "../Util/Timer.h"
15
#include "../Util/Parse.h"
16
#include "../Util/XmlParser.h"
17
 
18
#include "Manifold.h"
19
 
20
namespace HMesh
21
{
22
    using namespace std;
23
    using namespace CGLA;
24
    using namespace Util;
25
 
26
    namespace
27
    {
28
        vector<int> faces;
29
        vector<int> indices;
30
        vector<float> vertices;
31
 
32
        void coord_index_to_face_vec(const vector<int>& coord_index, vector<int>& faces, vector<int>& indices)
33
        {
34
            int k = 0;
35
            for(size_t i = 0; i < coord_index.size(); ++i){
36
                int idx = coord_index[i];
37
                if (idx == -1) {
38
                    faces.push_back(k);
39
                    k=0;
40
                }
41
                else{
42
                    indices.push_back(idx);
43
                    ++k;
44
                }
45
            }
46
        }
47
    }
48
 
49
    void handle_Shape(XmlElement& elem)
50
    {
51
        cout << "Found Shape" << endl;
52
        elem.process_elements();
53
        cout << "Shape ends" << endl;				
54
    }
55
 
56
    void handle_IndexedFaceSet(XmlElement& elem)
57
    {
58
        cout << "Found IndexedFaceSet" << endl;
59
        vector<int> coord_index;
60
        parse(elem.atts["coordIndex"].c_str(), coord_index);
61
        coord_index_to_face_vec(coord_index, faces, indices);
62
        elem.process_elements();
63
        cout << "IndexedFaceSet ends" << endl;
64
    }
65
 
66
    void handle_Coordinate(XmlElement& elem)
67
    {
68
        cout << "Found Coordinate" << endl;
69
        parse(elem.atts["point"].c_str(), vertices);
70
        cout << "Coordinate ends" << endl;
71
    }
72
 
73
    int find_last_of(const string& F, const string& C)
74
    {
75
        int pos = F.find_last_of(C);
76
        if (pos == string::npos) 
77
            return -1;
78
        return pos;
79
    }
80
 
81
    bool x3d_load(const string& filename, Manifold& m) 
82
    {
83
        faces.clear();
84
        indices.clear();
85
        vertices.clear();
86
 
87
        Timer tim;
88
        tim.start();
89
 
90
        string baseurl;
91
        int idx = max(find_last_of(filename, "\\"), 
92
            find_last_of(filename, "/"));
93
 
94
        if(idx != -1)
95
            baseurl = string(filename.substr(0, idx+1));
96
 
97
        XmlDoc x3d_doc(filename.c_str());
98
 
99
        if(!x3d_doc.is_valid())
100
            return false;
101
 
102
        x3d_doc.add_handler("Shape", handle_Shape);    
103
        x3d_doc.add_handler("IndexedFaceSet", handle_IndexedFaceSet);
104
        x3d_doc.add_handler("Coordinate", handle_Coordinate);
105
        x3d_doc.process_elements();
106
        x3d_doc.close();
107
 
108
        cout << "vertices " << vertices.size() << endl;
109
 
110
        m.build(vertices.size()/3, 
111
            reinterpret_cast<float*>(&vertices[0]), 
112
            faces.size(), 
113
            &faces[0], 
114
            &indices[0]);
115
 
116
        cout << " Loading took " << tim.get_secs() << endl;
117
        return true;
118
    }
119
}