Subversion Repositories gelsvn

Rev

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_Shape(XmlElement& elem)
46
    {
377 jrf 47
        cout << "Found shape" << endl;
48
        elem.process_elements();
49
        cout << "Shape ends" << endl;				
375 jrf 50
    }
51
 
52
    void handle_IndexedFaceSet(XmlElement& elem)
53
    {
377 jrf 54
        vector<int> coord_index;
55
        parse(elem.atts["coordIndex"].c_str(), coord_index);
56
        coord_index_to_face_vec(coord_index, faces, indices);
57
        elem.process_elements();
375 jrf 58
    }
59
 
60
    void handle_Coordinate(XmlElement& elem)
61
    {
377 jrf 62
        parse(elem.atts["point"].c_str(), vertices);
375 jrf 63
    }
64
 
377 jrf 65
    int find_last_of(const std::string& F, const std::string& C)
66
    {
67
        size_t pos = F.find_last_of(C);
68
        if (pos == string::npos) 
69
            return -1;
70
        return pos;
71
    }
149 jab 72
 
377 jrf 73
    bool x3d_load(const std::string& filename, Manifold& mani) 
74
    {
75
        faces.clear();
76
        indices.clear();
77
        vertices.clear();
149 jab 78
 
377 jrf 79
        Timer tim;
80
        tim.start();
149 jab 81
 
377 jrf 82
        std::string baseurl;
83
        int idx = s_max(find_last_of(filename, "\\"), 
84
                        find_last_of(filename, "/"));
149 jab 85
 
377 jrf 86
        if(idx != -1)
87
          baseurl = std::string(filename.substr(0, idx+1));
149 jab 88
 
375 jrf 89
        XmlDoc x3d_doc(filename.c_str());
90
        x3d_doc.add_handler("Shape", handle_Shape);
91
        x3d_doc.add_handler("IndexedFaceSet", handle_IndexedFaceSet);
92
        x3d_doc.add_handler("Coordinate", handle_Coordinate);
93
        x3d_doc.process_elements();
380 jrf 94
        x3d_doc.close();
149 jab 95
 
377 jrf 96
        build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
97
                       &faces[0], &indices[0]);
149 jab 98
 
377 jrf 99
        cout << " Loading took " << tim.get_secs() << endl;
392 jab 100
        return true;
377 jrf 101
    }
149 jab 102
}