-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseX3D.cpp
executable file
·410 lines (380 loc) · 11.3 KB
/
parseX3D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (c) 2007 University of Michigan, Ann Arbor.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of Michigan, Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Author: Igor Guskov
*
*/
#include <string.h>
#include <limits.h>
#include <iostream>
#include <string>
using namespace std;
#include "expat.h"
#include "parseX3D.h"
#include "scene.h"
#ifndef _WIN32
#include <libgen.h>
#else
extern const char *dirname(const char* path);
extern const char *basename(const char* path);
#endif
__gnu_cxx::hash_map<std::string, X3NodeType> X3Reader::name_hash;
struct str2int {
const char* s;
X3NodeType n;
};
str2int name2type[] =
{
{"X3D", X3NODE_X3D},
{"Scene", X3NODE_SCENE},
{"Transform", X3NODE_TRANSFORM},
{"Group", X3NODE_GROUP},
{"Shape", X3NODE_SHAPE},
{"Viewpoint", X3NODE_VIEWPOINT},
{"Box", X3NODE_BOX},
{"Cylinder", X3NODE_CYLINDER},
{"Cone", X3NODE_CONE},
{"IndexedFaceSet", X3NODE_INDEXEDFACESET},
{"Material", X3NODE_MATERIAL},
{"Appearance", X3NODE_APPEARANCE},
{"Coordinate", X3NODE_COORDINATE},
{"PointLight", X3NODE_POINTLIGHT},
{"ImageTexture", X3NODE_IMAGETEXTURE},
{"Link", X3NODE_LINK},
{"PositionInterpolator", X3NODE_POSITIONINTERPOLATOR},
{"OrientationInterpolator", X3NODE_ORIENTATIONINTERPOLATOR},
{"ScalarInterpolator", X3NODE_SCALARINTERPOLATOR},
{"Timer", X3NODE_TIMER},
{"TextureCoordinate", X3NODE_TEXTURECOORDINATE},
{"TextureTransform", X3NODE_TEXTURETRANSFORM},
{"Curve", X3NODE_CURVE},
};
#ifndef XMLCALL
#define XMLCALL
#endif
// This is just a callback wrapper for the appropriate X3Reader call.
static void XMLCALL
startElement(void *userData, const char *name, const char **atts)
{
X3Reader* reader = static_cast<X3Reader*>(userData);
reader->StartElement(name, atts);
}
// This is just a callback wrapper for the appropriate X3Reader call.
static void XMLCALL
endElement(void *userData, const char *name)
{
X3Reader* reader = static_cast<X3Reader*>(userData);
reader->EndElement(name);
}
// For X3D models, there is nothing to parse, as everything is
// passed in the attributes.
static void XMLCALL
char_cb(void *userData, const XML_Char *s, int len)
{
}
X3Reader::X3Reader()
: level_(-1),
skip_level_(INT_MAX),
scene_(NULL)
{
if(name_hash.empty()) {
for (int k = 0; k < (int)(sizeof(name2type)/sizeof(str2int)); ++k)
name_hash.insert(make_pair(string(name2type[k].s), name2type[k].n));
}
}
X3NodeType
X3Reader::GetType(const char* name)
{
__gnu_cxx::hash_map<string, X3NodeType>::const_iterator mi =
name_hash.find(string(name));
return mi==name_hash.end() ? X3NODE_UNKNOWN : (*mi).second;
}
bool
X3Reader::ProcessLinkAttributes(const char **atts,
X3InterpolatorNode** src_node,
X3Timer** timer_node,
X3Node** dest_node,
string* dest_field_name)
{
if(atts==0)
return false;
__gnu_cxx::hash_map<string, X3Node*>::iterator hi;
for (const char** ref = atts; *ref!=NULL; ++ref) {
const char* name = *ref;
++ref;
if(*ref==NULL) {
break;
}
if(strcmp(name, "INTERPOLATOR")==0) {
hi = def_nodes_.find(string(*ref));
if(hi!=def_nodes_.end()) {
// need to check if this is an interpolation node
if((*hi).second==NULL) {
return false;
}
if(!(*hi).second->IsInterpolator()) {
cerr << "WARNING: FROM_NODE " << *ref << " cannot interpolate" << endl;
return false;
}
*src_node = static_cast<X3InterpolatorNode*>((*hi).second);
} else {
cerr << "WARNING: FROM_NODE not found: " << *ref << endl;
return false;
}
} else if(strcmp(name, "TIMER")==0) {
hi = def_nodes_.find(string(*ref));
if(hi!=def_nodes_.end()) {
// need to check if this is a timer node
if((*hi).second==NULL) {
return false;
}
if(!(*hi).second->IsTimer()) {
cerr << "WARNING: node " << *ref
<< " is not a timer." << endl;
return false;
}
*timer_node = static_cast<X3Timer*>((*hi).second);
} else {
cerr << "WARNING: TIMER not found: " << *ref << endl;
return false;
}
} else if(strcmp(name, "TO_NODE")==0) {
hi = def_nodes_.find(string(*ref));
if(hi!=def_nodes_.end()) {
*dest_node = (*hi).second;
} else {
cerr << "WARNING: TO_NODE not found: " << *ref << endl;
return false;
}
} else if(strcmp(name, "TO_FIELD")==0) {
dest_field_name->assign(*ref);
}
}
return *src_node && *dest_node &&
*timer_node && !dest_field_name->empty();
}
void
X3Reader::StartElement(const char *name, const char **atts)
{
++level_;
if(level_>skip_level_)
return;
X3Node* node = NULL;
X3NodeType node_type = GetType(name);
string defname;
int def_or_use = ProcessNames(atts, &defname);
if(def_or_use==NAME_USE) {
// For the node with the USE attribute, find the corresponding DEF node
// in the table and use a pointer to the old node at the current position in
// the scene graph.
__gnu_cxx::hash_map<string, X3Node*>::iterator hi = def_nodes_.find(defname);
if(hi!=def_nodes_.end()) {
node = (*hi).second;
}
} else {
// Otherwise allocate a new node usually.
switch(node_type) {
case X3NODE_X3D:
// Can just skip this.
break;
case X3NODE_SCENE:
// The scene node is the root of our scene graph hierarchy. We shall
// only process the first scene in the file.
if(scene_) {
// If we already have the scene, we skip all the other ones.
skip_level_ = level_;
return;
}
//
scene_ = new X3Scene;
node = scene_;
break;
case X3NODE_LINK:
// this is a non-standard node -- it sets up a link between
// interpolator nodes
// and their consumer node-fields which are given in the attributes
if(scene_) {
// get the from and to nodes from the def_nodes_ table
// the destination node should provide linking by name method
// with type checking. This will not happen too often.
// the source should provide type id and destination should check that
// against the field
//scene_->AddLink(atts);
X3InterpolatorNode* src_node;
X3Timer* timer_node;
X3Node* dest_node;
string dest_field_name;
if(ProcessLinkAttributes(atts, &src_node, &timer_node,
&dest_node, &dest_field_name)) {
scene_->AddLink(src_node, timer_node, dest_node, dest_field_name);
}
}
case X3NODE_TRANSFORM:
node = new X3Transform(atts);
break;
case X3NODE_GROUP:
node = new X3Group;
break;
case X3NODE_VIEWPOINT:
node = new X3Viewpoint(atts);
break;
case X3NODE_SHAPE:
node = new X3Shape;
break;
case X3NODE_BOX:
node = new X3Box(atts);
break;
case X3NODE_CYLINDER:
node = new X3Cylinder(atts);
break;
case X3NODE_CONE:
node = new X3Cone(atts);
break;
case X3NODE_INDEXEDFACESET:
node = new X3IndexedFaceSet(atts);
break;
case X3NODE_APPEARANCE:
node = new X3Appearance;
break;
case X3NODE_MATERIAL:
node = new X3Material(atts);
break;
case X3NODE_COORDINATE:
node = new X3Coordinate(atts);
break;
case X3NODE_TEXTURECOORDINATE:
node = new X3TextureCoordinate(atts);
break;
case X3NODE_POINTLIGHT:
node = new X3PointLight(atts);
break;
case X3NODE_IMAGETEXTURE:
node = new X3ImageTexture(dirname_, atts);
break;
case X3NODE_POSITIONINTERPOLATOR:
node = new X3PositionInterpolator(atts);
break;
case X3NODE_ORIENTATIONINTERPOLATOR:
node = new X3OrientationInterpolator(atts);
break;
case X3NODE_SCALARINTERPOLATOR:
node = new X3ScalarInterpolator(atts);
break;
case X3NODE_TIMER:
node = new X3Timer(atts);
break;
case X3NODE_TEXTURETRANSFORM:
node = new X3TextureTransform(atts);
break;
case X3NODE_CURVE:
node = new X3Curve(atts);
break;
default:
// This is how we skip everything we do not process: set the skip level
// and wait for the end element at this level. Then start processing again.
skip_level_ = level_;
return;
}
if(def_or_use==NAME_DEF) {
// If there is a DEF attribute, we need to insert the node into
// the def node table to be used later.
def_nodes_.insert(make_pair(defname, node));
}
// remember the node to later delete it
if(scene_) {
if(node != scene_) {
scene_->Register(node);
}
}
}
if(node) {
// If a valid node is present we push it onto the stack (linking
// with its parent if that is present.
// Note that the Scene node should appear before any other meaningful
// node for this to work.
if(!node_stack_.empty())
node_stack_.top()->Add(node_type, node);
node_stack_.push(node);
}
//cout << level_ << name << endl;
}
// Searches for DEF/USE attributes.
int
X3Reader::ProcessNames(const char** atts, string* name)
{
for (const char** ref = atts; *ref!=NULL; ++ref) {
if(strcmp(*ref, "USE")==0) {
++ref;
if(ref)
name->assign(*ref);
return NAME_USE;
}
if(strcmp(*ref, "DEF")==0) {
++ref;
if(ref)
name->assign(*ref);
return NAME_DEF;
}
++ref;
if(*ref==NULL) {
//cout << "UNDEFINED";
break;
}
}
return NAME_NONE;
}
// This method handles skipping and pops elements off the stack as needed.
void
X3Reader::EndElement(const char *name)
{
if(level_ <= skip_level_) {
if(level_ == skip_level_) {
ResetSkip();
} else {
if(!node_stack_.empty()) {
// pop the stack
node_stack_.pop();
}
//cout << level_ << "/" << name << endl;
}
}
--level_;
}
X3Scene*
X3Reader::Read(std::istream& ist)
{
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetStartElementHandler(parser, startElement);
XML_SetEndElementHandler(parser, endElement);
XML_SetCharacterDataHandler(parser, char_cb);
XML_SetUserData(parser, this);
level_ = -1;
const int BUFSIZE = 32;
char buf[BUFSIZE];
bool done;
do {
ist.read(buf, BUFSIZE);
done = ist.gcount() < BUFSIZE;
if (XML_Parse(parser, buf, ist.gcount(), done) == XML_STATUS_ERROR) {
cerr << XML_ErrorString(XML_GetErrorCode(parser))
<< " at line " << XML_GetCurrentLineNumber(parser) << endl;
delete scene_;
return NULL;
}
} while (!done);
XML_ParserFree(parser);
return scene_;
}