Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
149 jab 1
#include <sys/types.h>
2
#include <sys/stat.h>
3
#include <fcntl.h>
4
 
5
#if defined(_MSC_VER)
6
#include <io.h>
7
 
8
#pragma message("Note: including lib: expat.lib\n")
9
#pragma comment(lib, "expat.lib")
10
 
11
#else
12
#include <unistd.h>
13
#endif
14
#include <expat.h>
15
 
16
#include "Util/Timer.h"
17
#include "Util/Parse.h"
18
#include "HMesh/Manifold.h"
19
#include "build_manifold.h"
20
#include <iostream>
21
 
22
#include "x3d_load.h"
23
 
24
using namespace CGLA;
25
using namespace Util;
26
using namespace std;
27
using namespace HMesh;
28
 
150 jab 29
namespace HMesh
149 jab 30
{
31
 
32
		namespace
33
		{
34
				vector<int> faces;
35
				vector<int> indices;
36
				vector<Vec3f> vertices;
37
 
38
				void coord_index_to_face_vec(const vector<int>& coord_index, 
39
																		 vector<int>& faces,
40
																		 vector<int>& indices)
41
				{
42
						Face face;
43
						size_t k=0;
44
						for(size_t i=0;i<coord_index.size();i++) 
45
						{
46
								int idx = coord_index[i];
47
								if (idx==-1) 
48
								{
49
										faces.push_back(k);
50
										k=0;
51
								}
52
								else
53
								{
54
										indices.push_back(idx);
55
										++k;
56
								}
57
						}
58
				}
59
 
60
		}
61
 
62
		void startElement(void *userData, const char *name, const char **atts) 
63
		{
64
				if (strcmp(name,"IndexedFaceSet")==0)
65
				{
66
						int j=0;
67
						while (atts[j]!=0) {
68
								if (strcmp(atts[j],"coordIndex")==0) 
69
								{ 
70
										vector<int> coord_index;
71
										parse(atts[++j],coord_index);
72
										coord_index_to_face_vec(coord_index, faces, indices);
73
								} 
74
								j++;
75
						}
76
				} 
77
				else if (strcmp(name,"Shape")==0) 
78
				{
79
						cout << "Found shape" << endl;
80
				}
81
				else if (strcmp(name,"Coordinate")==0) 
82
				{ 
83
						int j=0;
84
						while (atts[j]!=0) 
85
						{
86
								if (strcmp(atts[j],"point")==0) { 
87
										parse(atts[++j],vertices);
88
								} 
89
								j++;
90
						}
91
				}
92
		}
93
 
94
		void endElement(void *userData, const char *name) 
95
		{
96
				if (strcmp(name,"Shape")==0) 
97
				{
98
						cout << "Shape ends" << endl;				
99
				} 
100
		}
101
 
102
		int find_last_of(const std::string& F, const std::string& C)
103
		{
104
				size_t pos = F.find_last_of(C);
105
				if (pos == string::npos) 
106
						return -1;
107
				return pos;
108
		}
109
 
110
		bool x3d_load(const std::string& filename, Manifold& mani) 
111
		{
112
				faces.clear();
113
				indices.clear();
114
				vertices.clear();
115
 
116
				Timer tim;
117
				tim.start();
118
 
119
				std::string baseurl;
120
				int idx = s_max(find_last_of(filename, "\\"), 
121
												find_last_of(filename, "/"));
122
 
123
 
124
				if(idx != -1)
125
						baseurl = std::string(filename.substr(0, idx+1));
126
 
127
 
128
				XML_Parser parser = XML_ParserCreate(NULL);
129
				int done=0;
130
				XML_SetElementHandler(parser, startElement, endElement);
131
 
132
				int in;
133
				if ((in=open(filename.data(),O_RDONLY)) == -1) {
134
						cout << "Error. "<<  filename << " not found" << endl;
135
						assert(0);
136
						return false;
137
				}
138
 
139
				struct stat stat_buf;
140
				fstat(in, &stat_buf);
141
 
142
				const size_t BUF_SIZE = s_min(5000000, static_cast<int>(stat_buf.st_size));
143
				char* buf2 = new char[BUF_SIZE];
144
				size_t len;
145
				do {
146
						len = read(in, buf2, BUF_SIZE);
147
						if (len!=BUF_SIZE)
148
								done=1;
149
						if (!XML_Parse(parser, buf2, len, done)) {
150
								cerr << "%s at line %d\n" 
151
										 << XML_ErrorString(XML_GetErrorCode(parser))
152
										 << XML_GetCurrentLineNumber(parser);
153
								assert(0);
154
								return false;
155
						}
156
				} while (!done);
157
				close(in);
158
				XML_ParserFree(parser);
159
				delete buf2;
160
 
161
				build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
162
											 &faces[0], &indices[0]);
163
 
164
				cout << " Loading took " << tim.get_secs() << endl;
165
				return false;
166
		}
167
}