Subversion Repositories gelsvn

Rev

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

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