Subversion Repositories gelsvn

Rev

Rev 595 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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