Subversion Repositories gelsvn

Rev

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

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