Subversion Repositories gelsvn

Rev

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

Rev 349 Rev 375
1
#include <sys/types.h>
-
 
2
#include <sys/stat.h>
-
 
3
#include <fcntl.h>
-
 
4
 
-
 
5
#if defined(_MSC_VER)
-
 
6
#include <io.h>
1
#include <iostream>
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"
2
#include "Util/Timer.h"
17
#include "Util/Parse.h"
3
#include "Util/Parse.h"
-
 
4
#include "Util/XmlParser.h"
18
#include "HMesh/Manifold.h"
5
#include "HMesh/Manifold.h"
19
#include "build_manifold.h"
6
#include "build_manifold.h"
20
#include <iostream>
-
 
21
 
-
 
22
#include "x3d_load.h"
7
#include "x3d_load.h"
23
 
8
 
24
using namespace CGLA;
9
using namespace CGLA;
25
using namespace Util;
10
using namespace Util;
26
using namespace std;
11
using namespace std;
27
using namespace HMesh;
12
using namespace HMesh;
28
 
13
 
29
namespace HMesh
14
namespace HMesh
30
{
15
{
31
 
-
 
32
		namespace
16
		namespace
33
		{
17
		{
34
				vector<int> faces;
18
				vector<int> faces;
35
				vector<int> indices;
19
				vector<int> indices;
36
				vector<Vec3f> vertices;
20
				vector<Vec3f> vertices;
37
 
21
 
38
				void coord_index_to_face_vec(const vector<int>& coord_index, 
22
				void coord_index_to_face_vec(const vector<int>& coord_index, 
39
																		 vector<int>& faces,
23
																		 vector<int>& faces,
40
																		 vector<int>& indices)
24
																		 vector<int>& indices)
41
				{
25
				{
42
						Face face;
26
						Face face;
43
						size_t k=0;
27
						size_t k=0;
44
						for(size_t i=0;i<coord_index.size();i++) 
28
						for(size_t i=0;i<coord_index.size();i++) 
45
						{
29
						{
46
								int idx = coord_index[i];
30
								int idx = coord_index[i];
47
								if (idx==-1) 
31
								if (idx==-1) 
48
								{
32
								{
49
										faces.push_back(k);
33
										faces.push_back(k);
50
										k=0;
34
										k=0;
51
								}
35
								}
52
								else
36
								else
53
								{
37
								{
54
										indices.push_back(idx);
38
										indices.push_back(idx);
55
										++k;
39
										++k;
56
								}
40
								}
57
						}
41
						}
58
				}
42
				}
59
 
43
 
60
		}
44
		}
61
	
45
	
62
		void startElement(void *userData, const char *name, const char **atts) 
46
    void handle_X3D(XmlElement& elem)
63
		{
47
    {
64
				if (strcmp(name,"IndexedFaceSet")==0)
48
      elem.process_elements();
65
				{
49
    }
66
						int j=0;
50
 
67
						while (atts[j]!=0) {
-
 
68
								if (strcmp(atts[j],"coordIndex")==0) 
51
    void handle_Scene(XmlElement& elem)
69
								{ 
52
    {
70
										vector<int> coord_index;
53
      elem.process_elements();
-
 
54
    }
-
 
55
 
71
										parse(atts[++j],coord_index);
56
    void handle_Transform(XmlElement& elem)
72
										coord_index_to_face_vec(coord_index, faces, indices);
-
 
73
								} 
57
    {
74
								j++;
58
      elem.process_elements();
75
						}
59
    }
76
				} 
60
 
77
				else if (strcmp(name,"Shape")==0) 
61
    void handle_Shape(XmlElement& elem)
78
				{
62
    {
79
						cout << "Found shape" << endl;
63
      cout << "Found shape" << endl;
-
 
64
      elem.process_elements();
-
 
65
			cout << "Shape ends" << endl;				
80
				}
66
    }
-
 
67
 
81
				else if (strcmp(name,"Coordinate")==0) 
68
    void handle_IndexedFaceSet(XmlElement& elem)
82
				{ 
69
    {
83
						int j=0;
-
 
84
						while (atts[j]!=0) 
70
			vector<int> coord_index;
85
						{
-
 
86
								if (strcmp(atts[j],"point")==0) { 
71
			parse(elem.atts["coordIndex"].c_str(), coord_index);
87
										parse(atts[++j],vertices);
72
			coord_index_to_face_vec(coord_index, faces, indices);
88
								} 
-
 
89
								j++;
-
 
90
						}
-
 
91
				}
73
    }
92
		}
-
 
93
 
74
 
94
		void endElement(void *userData, const char *name) 
75
    void handle_Coordinate(XmlElement& elem)
95
		{
-
 
96
				if (strcmp(name,"Shape")==0) 
-
 
97
				{
76
    {
98
						cout << "Shape ends" << endl;				
77
		  parse(elem.atts["point"].c_str(), vertices);
99
				} 
-
 
100
		}
78
    }
101
 
79
 
102
		int find_last_of(const std::string& F, const std::string& C)
80
		int find_last_of(const std::string& F, const std::string& C)
103
		{
81
		{
104
				size_t pos = F.find_last_of(C);
82
				size_t pos = F.find_last_of(C);
105
				if (pos == string::npos) 
83
				if (pos == string::npos) 
106
						return -1;
84
						return -1;
107
				return pos;
85
				return pos;
108
		}
86
		}
109
 
87
 
110
		bool x3d_load(const std::string& filename, Manifold& mani) 
88
		bool x3d_load(const std::string& filename, Manifold& mani) 
111
		{
89
		{
112
				faces.clear();
90
				faces.clear();
113
				indices.clear();
91
				indices.clear();
114
				vertices.clear();
92
				vertices.clear();
115
 
93
 
116
				Timer tim;
94
				Timer tim;
117
				tim.start();
95
				tim.start();
118
 
96
 
119
				std::string baseurl;
97
				std::string baseurl;
120
				int idx = s_max(find_last_of(filename, "\\"), 
98
				int idx = s_max(find_last_of(filename, "\\"), 
121
												find_last_of(filename, "/"));
99
												find_last_of(filename, "/"));
122
 
100
 
123
 
-
 
124
				if(idx != -1)
101
				if(idx != -1)
125
						baseurl = std::string(filename.substr(0, idx+1));
102
						baseurl = std::string(filename.substr(0, idx+1));
126
 
103
 
-
 
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();
127
 
112
 
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(),
113
				build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
162
											 &faces[0], &indices[0]);
114
											 &faces[0], &indices[0]);
163
 
115
 
164
				cout << " Loading took " << tim.get_secs() << endl;
116
				cout << " Loading took " << tim.get_secs() << endl;
165
				return false;
117
				return false;
166
		}
118
		}
167
}
119
}
168
 
120