Subversion Repositories gelsvn

Rev

Rev 380 | Rev 595 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 380 Rev 392
1
#include <iostream>
1
#include <iostream>
2
#include "Util/Timer.h"
2
#include "Util/Timer.h"
3
#include "Util/Parse.h"
3
#include "Util/Parse.h"
4
#include "Util/XmlParser.h"
4
#include "Util/XmlParser.h"
5
#include "HMesh/Manifold.h"
5
#include "HMesh/Manifold.h"
6
#include "build_manifold.h"
6
#include "build_manifold.h"
7
#include "x3d_load.h"
7
#include "x3d_load.h"
8
 
8
 
9
using namespace CGLA;
9
using namespace CGLA;
10
using namespace Util;
10
using namespace Util;
11
using namespace std;
11
using namespace std;
12
using namespace HMesh;
12
using namespace HMesh;
13
 
13
 
14
namespace HMesh
14
namespace HMesh
15
{
15
{
16
    namespace
16
    namespace
17
    {
17
    {
18
        vector<int> faces;
18
        vector<int> faces;
19
        vector<int> indices;
19
        vector<int> indices;
20
        vector<Vec3f> vertices;
20
        vector<Vec3f> vertices;
21
 
21
 
22
        void coord_index_to_face_vec(const vector<int>& coord_index, 
22
        void coord_index_to_face_vec(const vector<int>& coord_index, 
23
                                     vector<int>& faces,
23
                                     vector<int>& faces,
24
                                     vector<int>& indices)
24
                                     vector<int>& indices)
25
        {
25
        {
26
            Face face;
26
            Face face;
27
            size_t k=0;
27
            size_t k=0;
28
            for(size_t i=0;i<coord_index.size();i++) 
28
            for(size_t i=0;i<coord_index.size();i++) 
29
            {
29
            {
30
                int idx = coord_index[i];
30
                int idx = coord_index[i];
31
                if (idx==-1) 
31
                if (idx==-1) 
32
                {
32
                {
33
                    faces.push_back(k);
33
                    faces.push_back(k);
34
                    k=0;
34
                    k=0;
35
                }
35
                }
36
                else
36
                else
37
                {
37
                {
38
                    indices.push_back(idx);
38
                    indices.push_back(idx);
39
                    ++k;
39
                    ++k;
40
                }
40
                }
41
            }
41
            }
42
        }
42
        }
43
    }
43
    }
44
 
44
 
45
    void handle_Shape(XmlElement& elem)
45
    void handle_Shape(XmlElement& elem)
46
    {
46
    {
47
        cout << "Found shape" << endl;
47
        cout << "Found shape" << endl;
48
        elem.process_elements();
48
        elem.process_elements();
49
        cout << "Shape ends" << endl;				
49
        cout << "Shape ends" << endl;				
50
    }
50
    }
51
 
51
 
52
    void handle_IndexedFaceSet(XmlElement& elem)
52
    void handle_IndexedFaceSet(XmlElement& elem)
53
    {
53
    {
54
        vector<int> coord_index;
54
        vector<int> coord_index;
55
        parse(elem.atts["coordIndex"].c_str(), coord_index);
55
        parse(elem.atts["coordIndex"].c_str(), coord_index);
56
        coord_index_to_face_vec(coord_index, faces, indices);
56
        coord_index_to_face_vec(coord_index, faces, indices);
57
        elem.process_elements();
57
        elem.process_elements();
58
    }
58
    }
59
 
59
 
60
    void handle_Coordinate(XmlElement& elem)
60
    void handle_Coordinate(XmlElement& elem)
61
    {
61
    {
62
        parse(elem.atts["point"].c_str(), vertices);
62
        parse(elem.atts["point"].c_str(), vertices);
63
    }
63
    }
64
 
64
 
65
    int find_last_of(const std::string& F, const std::string& C)
65
    int find_last_of(const std::string& F, const std::string& C)
66
    {
66
    {
67
        size_t pos = F.find_last_of(C);
67
        size_t pos = F.find_last_of(C);
68
        if (pos == string::npos) 
68
        if (pos == string::npos) 
69
            return -1;
69
            return -1;
70
        return pos;
70
        return pos;
71
    }
71
    }
72
 
72
 
73
    bool x3d_load(const std::string& filename, Manifold& mani) 
73
    bool x3d_load(const std::string& filename, Manifold& mani) 
74
    {
74
    {
75
        faces.clear();
75
        faces.clear();
76
        indices.clear();
76
        indices.clear();
77
        vertices.clear();
77
        vertices.clear();
78
 
78
 
79
        Timer tim;
79
        Timer tim;
80
        tim.start();
80
        tim.start();
81
 
81
 
82
        std::string baseurl;
82
        std::string baseurl;
83
        int idx = s_max(find_last_of(filename, "\\"), 
83
        int idx = s_max(find_last_of(filename, "\\"), 
84
                        find_last_of(filename, "/"));
84
                        find_last_of(filename, "/"));
85
 
85
 
86
        if(idx != -1)
86
        if(idx != -1)
87
          baseurl = std::string(filename.substr(0, idx+1));
87
          baseurl = std::string(filename.substr(0, idx+1));
88
 
88
 
89
        XmlDoc x3d_doc(filename.c_str());
89
        XmlDoc x3d_doc(filename.c_str());
90
        x3d_doc.add_handler("Shape", handle_Shape);
90
        x3d_doc.add_handler("Shape", handle_Shape);
91
        x3d_doc.add_handler("IndexedFaceSet", handle_IndexedFaceSet);
91
        x3d_doc.add_handler("IndexedFaceSet", handle_IndexedFaceSet);
92
        x3d_doc.add_handler("Coordinate", handle_Coordinate);
92
        x3d_doc.add_handler("Coordinate", handle_Coordinate);
93
        x3d_doc.process_elements();
93
        x3d_doc.process_elements();
94
        x3d_doc.close();
94
        x3d_doc.close();
95
 
95
 
96
        build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
96
        build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
97
                       &faces[0], &indices[0]);
97
                       &faces[0], &indices[0]);
98
 
98
 
99
        cout << " Loading took " << tim.get_secs() << endl;
99
        cout << " Loading took " << tim.get_secs() << endl;
100
        return false;
100
        return true;
101
    }
101
    }
102
}
102
}
103
 
103