Subversion Repositories gelsvn

Rev

Rev 375 | Rev 380 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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