Subversion Repositories gelsvn

Rev

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