Subversion Repositories gelsvn

Rev

Rev 24 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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