Subversion Repositories gelsvn

Rev

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

Rev 102 Rev 136
Line 27... Line 27...
27
using namespace HMesh;
27
using namespace HMesh;
28
 
28
 
29
namespace HMeshUtil
29
namespace HMeshUtil
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
    {
-
 
42
      int tri=0;
-
 
43
      Face face;
-
 
44
			int k=0;
-
 
45
      for(int i=0;i<coord_index.size();i++) 
-
 
46
				{
41
				{
47
					int idx = coord_index[i];
42
						Face face;
48
					if (idx==-1) 
43
						size_t k=0;
-
 
44
						for(size_t i=0;i<coord_index.size();i++) 
49
						{
45
						{
-
 
46
								int idx = coord_index[i];
-
 
47
								if (idx==-1) 
-
 
48
								{
50
							faces.push_back(k);
49
										faces.push_back(k);
51
							k=0;
50
										k=0;
52
						}
51
								}
53
					else
52
								else
54
						{
53
								{
55
							indices.push_back(idx);
54
										indices.push_back(idx);
56
							++k;
55
										++k;
-
 
56
								}
57
						}
57
						}
58
				}
58
				}
59
    }
-
 
60
 
59
 
61
  }
60
		}
62
	
61
	
63
  void startElement(void *userData, const char *name, const char **atts) 
62
		void startElement(void *userData, const char *name, const char **atts) 
64
  {
63
		{
65
    if (strcmp(name,"IndexedFaceSet")==0)
64
				if (strcmp(name,"IndexedFaceSet")==0)
66
      {
65
				{
67
				int j=0;
66
						int j=0;
68
				while (atts[j]!=0) {
67
						while (atts[j]!=0) {
69
					if (strcmp(atts[j],"coordIndex")==0) 
68
								if (strcmp(atts[j],"coordIndex")==0) 
70
						{ 
69
								{ 
71
							vector<int> coord_index;
70
										vector<int> coord_index;
72
							parse(atts[++j],coord_index);
71
										parse(atts[++j],coord_index);
73
							coord_index_to_face_vec(coord_index, faces, indices);
72
										coord_index_to_face_vec(coord_index, faces, indices);
-
 
73
								} 
-
 
74
								j++;
74
						} 
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
								} 
75
					j++;
89
								j++;
-
 
90
						}
76
				}
91
				}
77
      } 
92
		}
-
 
93
 
-
 
94
		void endElement(void *userData, const char *name) 
-
 
95
		{
78
    else if (strcmp(name,"Shape")==0) 
96
				if (strcmp(name,"Shape")==0) 
79
      {
97
				{
80
				cout << "Found shape" << endl;
98
						cout << "Shape ends" << endl;				
81
      }
99
				} 
-
 
100
		}
-
 
101
 
82
    else if (strcmp(name,"Coordinate")==0) 
102
		int find_last_of(const std::string& F, const std::string& C)
83
      { 
103
		{
-
 
104
				size_t pos = F.find_last_of(C);
-
 
105
				if (pos == string::npos) 
84
				int j=0;
106
						return -1;
85
				while (atts[j]!=0) 
107
				return pos;
-
 
108
		}
-
 
109
 
-
 
110
		bool x3d_load(const std::string& filename, Manifold& mani) 
86
					{
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, "\\"), 
87
						if (strcmp(atts[j],"point")==0) { 
121
												find_last_of(filename, "/"));
-
 
122
 
-
 
123
 
-
 
124
				if(idx != -1)
-
 
125
						baseurl = std::string(filename.substr(0, idx+1));
-
 
126
 
-
 
127
 
88
							parse(atts[++j],vertices);
128
				XML_Parser parser = XML_ParserCreate(NULL);
-
 
129
				int done=0;
-
 
130
				XML_SetElementHandler(parser, startElement, endElement);
-
 
131
 
89
						} 
132
				int in;
-
 
133
				if ((in=open(filename.data(),O_RDONLY)) == -1) {
-
 
134
						cout << "Error. "<<  filename << " not found" << endl;
90
					j++;
135
						assert(0);
-
 
136
						return false;
91
				}
137
				}
92
			}
-
 
93
	}
-
 
94
 
138
 
95
  void endElement(void *userData, const char *name) 
-
 
96
  {
-
 
97
    if (strcmp(name,"Shape")==0) 
-
 
98
      {
-
 
99
				cout << "Shape ends" << endl;				
-
 
100
      } 
-
 
101
  }
-
 
102
 
-
 
103
  bool x3d_load(const std::string& filename, Manifold& mani) 
-
 
104
  {
-
 
105
		faces.clear();
-
 
106
		indices.clear();
-
 
107
		vertices.clear();
-
 
108
 
-
 
109
    Timer tim;
-
 
110
    tim.start();
-
 
111
 
-
 
112
    std::string baseurl;
-
 
113
#define FIND_LAST_OF(F,C) (int)(F.find_last_of(C) == string::npos ? -1 : F.find_last_of(C))	
-
 
114
    int idx = max(FIND_LAST_OF(filename, "\\"), FIND_LAST_OF(filename, "/"));
-
 
115
#undef FIND_LAST_OF
-
 
116
 
-
 
117
    if(idx != -1)
-
 
118
      baseurl = std::string(filename.substr(0, idx+1));
-
 
119
 
-
 
120
 
-
 
121
    XML_Parser parser = XML_ParserCreate(NULL);
-
 
122
    int done=0;
-
 
123
    XML_SetElementHandler(parser, startElement, endElement);
-
 
124
 
-
 
125
    int in;
-
 
126
    if ((in=open(filename.data(),O_RDONLY)) == -1) {
-
 
127
      cout << "Error. "<<  filename << " not found" << endl;
-
 
128
      assert(0);
-
 
129
      return false;
-
 
130
    }
-
 
131
 
-
 
132
    struct stat stat_buf;
139
				struct stat stat_buf;
133
    fstat(in, &stat_buf);
140
				fstat(in, &stat_buf);
134
 
141
 
135
    const int 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));
136
    char* buf2 = new char[BUF_SIZE];
143
				char* buf2 = new char[BUF_SIZE];
137
    size_t len;
144
				size_t len;
138
    do {
145
				do {
139
      len = read(in, buf2, BUF_SIZE);
146
						len = read(in, buf2, BUF_SIZE);
140
      if (len!=BUF_SIZE)
147
						if (len!=BUF_SIZE)
141
				done=1;
148
								done=1;
142
      if (!XML_Parse(parser, buf2, len, done)) {
149
						if (!XML_Parse(parser, buf2, len, done)) {
143
				cerr << "%s at line %d\n" 
150
								cerr << "%s at line %d\n" 
144
						 << XML_ErrorString(XML_GetErrorCode(parser))
151
										 << XML_ErrorString(XML_GetErrorCode(parser))
145
						 << XML_GetCurrentLineNumber(parser);
152
										 << XML_GetCurrentLineNumber(parser);
146
				assert(0);
153
								assert(0);
147
				return false;
154
								return false;
148
      }
155
						}
149
    } while (!done);
156
				} while (!done);
150
    close(in);
157
				close(in);
151
    XML_ParserFree(parser);
158
				XML_ParserFree(parser);
152
    delete buf2;
159
				delete buf2;
153
		
160
		
154
		build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
161
				build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
155
									 &faces[0], &indices[0]);
162
											 &faces[0], &indices[0]);
156
 
163
 
157
    cout << " Loading took " << tim.get_secs() << endl;
164
				cout << " Loading took " << tim.get_secs() << endl;
158
    return false;
165
				return false;
159
  }
166
		}
160
}
167
}