Subversion Repositories gelsvn

Rev

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

Rev Author Line No. Line
39 bj 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
 
29
namespace HMeshUtil
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
      int tri=0;
43
      Face face;
44
			int k=0;
45
      for(int i=0;i<coord_index.size();i++) 
46
				{
47
					int idx = coord_index[i];
48
					if (idx==-1) 
49
						{
50
							faces.push_back(k);
51
							k=0;
52
						}
53
					else
54
						{
55
							indices.push_back(idx);
56
							++k;
57
						}
58
				}
59
    }
60
 
61
  }
62
 
63
  void startElement(void *userData, const char *name, const char **atts) 
64
  {
65
    if (strcmp(name,"IndexedFaceSet")==0)
66
      {
67
				int j=0,i;
68
				while (atts[j]!=0) {
69
					if (strcmp(atts[j],"coordIndex")==0) 
70
						{ 
71
							vector<int> coord_index;
72
							parse(atts[++j],coord_index);
73
							coord_index_to_face_vec(coord_index, faces, indices);
74
						} 
75
					j++;
76
				}
77
      } 
78
    else if (strcmp(name,"Shape")==0) 
79
      {
80
				cout << "Found shape" << endl;
81
      }
82
    else if (strcmp(name,"Coordinate")==0) 
83
      { 
84
				int j=0;
85
				while (atts[j]!=0) 
86
					{
87
						if (strcmp(atts[j],"point")==0) { 
88
							parse(atts[++j],vertices);
89
						} 
90
					j++;
91
				}
92
			}
93
	}
94
 
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;
133
    fstat(in, &stat_buf);
134
 
135
    const int BUF_SIZE = s_min(5000000, static_cast<int>(stat_buf.st_size));
136
    char* buf2 = new char[BUF_SIZE];
137
    size_t len;
138
    do {
139
      len = read(in, buf2, BUF_SIZE);
140
      if (len!=BUF_SIZE)
141
				done=1;
142
      if (!XML_Parse(parser, buf2, len, done)) {
143
				cerr << "%s at line %d\n" 
144
						 << XML_ErrorString(XML_GetErrorCode(parser))
145
						 << XML_GetCurrentLineNumber(parser);
146
				assert(0);
147
				return false;
148
      }
149
    } while (!done);
150
    close(in);
151
    XML_ParserFree(parser);
152
    delete buf2;
153
 
154
		build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
155
									 &faces[0], &indices[0]);
156
 
157
    cout << " Loading took " << tim.get_secs() << endl;
158
    return true;
159
  }
160
}