Subversion Repositories gelsvn

Rev

Rev 182 | Rev 349 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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