Subversion Repositories gelsvn

Rev

Rev 349 | Rev 377 | 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
{
16
		namespace
17
		{
18
				vector<int> faces;
19
				vector<int> indices;
20
				vector<Vec3f> vertices;
21
 
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
 
44
		}
45
 
375 jrf 46
    void handle_X3D(XmlElement& elem)
47
    {
48
      elem.process_elements();
49
    }
149 jab 50
 
375 jrf 51
    void handle_Scene(XmlElement& elem)
52
    {
53
      elem.process_elements();
54
    }
149 jab 55
 
375 jrf 56
    void handle_Transform(XmlElement& elem)
57
    {
58
      elem.process_elements();
59
    }
60
 
61
    void handle_Shape(XmlElement& elem)
62
    {
63
      cout << "Found shape" << endl;
64
      elem.process_elements();
65
			cout << "Shape ends" << endl;				
66
    }
67
 
68
    void handle_IndexedFaceSet(XmlElement& elem)
69
    {
70
			vector<int> coord_index;
71
			parse(elem.atts["coordIndex"].c_str(), coord_index);
72
			coord_index_to_face_vec(coord_index, faces, indices);
73
    }
74
 
75
    void handle_Coordinate(XmlElement& elem)
76
    {
77
		  parse(elem.atts["point"].c_str(), vertices);
78
    }
79
 
149 jab 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
		}
87
 
88
		bool x3d_load(const std::string& filename, Manifold& mani) 
89
		{
90
				faces.clear();
91
				indices.clear();
92
				vertices.clear();
93
 
94
				Timer tim;
95
				tim.start();
96
 
97
				std::string baseurl;
98
				int idx = s_max(find_last_of(filename, "\\"), 
99
												find_last_of(filename, "/"));
100
 
101
				if(idx != -1)
102
						baseurl = std::string(filename.substr(0, idx+1));
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
 
113
				build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
114
											 &faces[0], &indices[0]);
115
 
116
				cout << " Loading took " << tim.get_secs() << endl;
117
				return false;
118
		}
119
}