Subversion Repositories gelsvn

Rev

Rev 392 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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