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