00001
00002
00003
00004
00005
00006
00007
00008
00009
00015 #include "VrmlParser.h"
00016
00017 #include <cmath>
00018 #include <vector>
00019 #include <list>
00020 #include <iostream>
00021 #include <hrpUtil/EasyScanner.h>
00022 #include <hrpUtil/UrlUtil.h>
00023
00024 #include <boost/version.hpp>
00025 #if (BOOST_VERSION <= 103301)
00026 #include <boost/filesystem/path.hpp>
00027 #include <boost/filesystem/operations.hpp>
00028 #else
00029 #include <boost/filesystem.hpp>
00030 #endif
00031
00032 using namespace std;
00033 using namespace boost;
00034 using namespace hrp;
00035
00036 namespace {
00037
00041 enum {
00042
00043 NO_SYMBOL = 0,
00044
00045
00046 V_TRUE,
00047 V_FALSE,
00048 V_NULL,
00049
00050
00051 T_SFINT32,
00052 T_MFINT32,
00053 T_SFFLOAT,
00054 T_MFFLOAT,
00055 T_SFVEC2F,
00056 T_MFVEC2F,
00057 T_SFVEC3F,
00058 T_MFVEC3F,
00059 T_SFSTRING,
00060 T_MFSTRING,
00061 T_SFROTATION,
00062 T_MFROTATION,
00063 T_SFTIME,
00064 T_MFTIME,
00065 T_SFCOLOR,
00066 T_MFCOLOR,
00067 T_SFNODE,
00068 T_MFNODE,
00069 T_SFBOOL,
00070 T_SFIMAGE,
00071
00072
00073 N_PROTO,
00074 N_INLINE,
00075 N_BACKGROUND,
00076 N_NAVIGATION_INFO,
00077 N_VIEWPOINT,
00078 N_GROUP,
00079 N_TRANSFORM,
00080 N_APPEARANCE,
00081 N_MATERIAL,
00082 N_IMAGE_TEXTURE,
00083 N_TEXTURE_TRANSFORM,
00084 N_SHAPE,
00085 N_BOX,
00086 N_CONE,
00087 N_CYLINDER,
00088 N_SPHERE,
00089 N_TEXT,
00090 N_FONT_STYLE,
00091 N_INDEXED_LINE_SET,
00092 N_INDEXED_FACE_SET,
00093 N_COLOR,
00094 N_COORDINATE,
00095 N_TEXTURE_COORDINATE,
00096 N_NORMAL,
00097 N_CYLINDER_SENSOR,
00098
00099 N_POINTSET,
00100 N_PIXEL_TEXTURE,
00101 N_MOVIE_TEXTURE,
00102 N_ELEVATION_GRID,
00103 N_EXTRUSION,
00104 N_SWITCH,
00105 N_LOD,
00106 N_COLLISION,
00107 N_ANCHOR,
00108 N_FOG,
00109 N_BILLBOARD,
00110 N_WORLD_INFO,
00111 N_POINT_LIGHT,
00112 N_DIRECTIONAL_LIGHT,
00113 N_SPOT_LIGHT,
00114
00115 N_AUDIO_CLIP,
00116 N_SOUND,
00117 N_COLOR_INTERPOLATOR,
00118 N_COORDINATE_INTERPOLATOR,
00119 N_ORIENTATION_INTERPOLATOR,
00120 N_NORMAL_INTERPOLATOR,
00121 N_POSITION_INTERPOLATOR,
00122 N_SCALAR_INTERPOLATOR,
00123 N_PLANE_SENSOR,
00124 N_PROXIMITY_SENSOR,
00125 N_SPHERE_SENSOR,
00126 N_TIME_SENSOR,
00127 N_TOUCH_SENSOR,
00128 N_VISIBILITY_SENSOR,
00129
00130
00131 F_IS,
00132
00133 F_URL,
00134
00135 F_GROUND_ANGLE,
00136 F_GROUND_COLOR,
00137 F_SKY_ANGLE,
00138 F_SKY_COLOR,
00139 F_BACK_URL,
00140 F_BOTTOM_URL,
00141 F_FRONT_URL,
00142 F_LEFT_URL,
00143 F_RIGHT_URL,
00144 F_TOP_URL,
00145
00146 F_AVATAR_SIZE,
00147 F_HEADLIGHT,
00148 F_SPEED,
00149 F_TYPE,
00150 F_VISIBILITY_LIMIT,
00151
00152 F_FIELD_OF_VIEW,
00153 F_JUMP,
00154 F_ORIENTATION,
00155 F_POSITION,
00156 F_DESCRIPTION,
00157
00158 F_CHILDREN,
00159 F_ADD_CHILDREN,
00160 F_REMOVE_CHILDREN,
00161 F_BBOX_CENTER,
00162 F_BBOX_SIZE,
00163
00164 F_CENTER,
00165 F_ROTATION,
00166 F_SCALE,
00167 F_SCALE_ORIENTATION,
00168 F_TRANSLATION,
00169
00170 F_APPEARANCE,
00171 F_GEOMETRY,
00172
00173 F_MATERIAL,
00174 F_TEXTURE,
00175 F_TEXTURE_TRANSFORM,
00176
00177 F_AMBIENT_INTENSITY,
00178 F_DIFFUSE_COLOR,
00179 F_EMISSIVE_COLOR,
00180 F_SHININESS,
00181 F_SPECULAR_COLOR,
00182 F_TRANSPARANCY,
00183
00184 F_REPEAT_S,
00185 F_REPEAT_T,
00186
00187 F_SIZE,
00188
00189 F_BOTTOM,
00190 F_BOTTOM_RADIUS,
00191 F_HEIGHT,
00192 F_SIDE,
00193
00194 F_RADIUS,
00195 F_TOP,
00196
00197 F_STRING,
00198 F_FONT_STYLE,
00199 F_LENGTH,
00200 F_MAX_EXTENT,
00201
00202 F_FAMILY,
00203 F_HORIZONTAL,
00204 F_JUSTIFY,
00205 F_LANGUAGE,
00206 F_LEFT_TO_RIGHT,
00207 F_SPACING,
00208 F_STYLE,
00209 F_TOP_TO_BOTTOM,
00210
00211 F_COLOR,
00212 F_COORD,
00213 F_COLOR_INDEX,
00214 F_COLOR_PER_VERTEX,
00215 F_COORD_INDEX,
00216
00217 F_CCW,
00218 F_CONVEX,
00219 F_SOLID,
00220 F_CREASE_ANGLE,
00221 F_NORMAL,
00222 F_NORMAL_INDEX,
00223 F_NORMAL_PER_VERTEX,
00224 F_TEX_COORD_INDEX,
00225 F_TEX_COORD,
00226
00227 F_POINT,
00228 F_VECTOR,
00229
00230 F_BBOXCENTER,
00231 F_BBOXSIZE,
00232
00233 F_AUTO_OFFSET,
00234 F_DISK_ANGLE,
00235 F_ENABLED,
00236 F_MAX_ANGLE,
00237 F_MIN_ANGLE,
00238 F_OFFSET,
00239
00240 F_IMAGE,
00241
00242 F_X_DIMENSION,
00243 F_Z_DIMENSION,
00244 F_X_SPACING,
00245 F_Z_SPACING,
00246
00247 F_LOOP,
00248 F_START_TIME,
00249 F_STOP_TIME,
00250
00251 F_CROSS_SECTION,
00252 F_SPINE,
00253 F_BEGIN_CAP,
00254 F_END_CAP,
00255
00256 F_CHOICE,
00257 F_WHICH_CHOICE,
00258
00259 F_RANGE,
00260 F_LEVEL,
00261
00262 F_COLLIDE,
00263 F_PROXY,
00264
00265 F_PARAMETER,
00266
00267 F_VISIBILITY_RANGE,
00268 F_FOG_TYPE,
00269
00270 F_AXIS_OF_ROTATION,
00271
00272 F_TITLE,
00273 F_INFO,
00274
00275 F_LOCATION,
00276 F_ON,
00277 F_INTENSITY,
00278 F_ATTENUATION,
00279 F_DIRECTION,
00280 F_BEAM_WIDTH,
00281 F_CUT_OFF_RANGE,
00282
00283
00284 E_FIELD,
00285 E_EXPOSED_FIELD,
00286 E_EVENTIN,
00287 E_EVENTOUT,
00288
00289
00290 D_DEF,
00291 D_USE,
00292 D_ROUTE,
00293
00294
00295 U_SCRIPT,
00296 U_EXTERNPROTO
00297
00298 };
00299 }
00300
00301
00302 namespace hrp {
00303
00304 class VrmlParserImpl
00305 {
00306 public:
00307 VrmlParserImpl(VrmlParser* self);
00308 VrmlParser* self;
00309
00310 EasyScannerPtr scanner;
00311 VrmlProtoInstancePtr currentProtoInstance;
00312
00313 bool protoInstanceActualNodeExtractionMode;
00314
00315 typedef map<VrmlProto*, EasyScannerPtr> ProtoToEntityScannerMap;
00316 ProtoToEntityScannerMap protoToEntityScannerMap;
00317
00318 typedef map<string, VrmlNodePtr> TDefNodeMap;
00319 typedef pair<string, VrmlNodePtr> TDefNodePair;
00320 typedef map<string, VrmlProtoPtr> TProtoMap;
00321 typedef pair<string, VrmlProtoPtr> TProtoPair;
00322
00323 TProtoMap protoMap;
00324 TDefNodeMap defNodeMap;
00325
00326 void load(const string& filename);
00327 VrmlNodePtr readSpecificNode(VrmlNodeCategory nodeCategory, int symbol, const std::string& symbolString);
00328 VrmlNodePtr readInlineNode(VrmlNodeCategory nodeCategory);
00329 VrmlNodePtr newInlineSource(std::string& io_filename);
00330 VrmlProtoPtr defineProto();
00331
00332 VrmlNodePtr readNode(VrmlNodeCategory nodeCategory);
00333 VrmlProtoInstancePtr readProtoInstanceNode(const std::string& proto_name, VrmlNodeCategory nodeCategory);
00334 VrmlNodePtr evalProtoInstance(VrmlProtoInstancePtr proto, VrmlNodeCategory nodeCategory);
00335 VrmlUnsupportedNodePtr skipUnsupportedNode(const std::string& nodeTypeName);
00336 VrmlUnsupportedNodePtr skipScriptNode();
00337 VrmlUnsupportedNodePtr skipExternProto();
00338
00339 VrmlViewpointPtr readViewpointNode();
00340 VrmlNavigationInfoPtr readNavigationInfoNode();
00341 VrmlBackgroundPtr readBackgroundNode();
00342 VrmlGroupPtr readGroupNode();
00343 VrmlTransformPtr readTransformNode();
00344 VrmlShapePtr readShapeNode();
00345 VrmlCylinderSensorPtr readCylinderSensorNode();
00346 VrmlBoxPtr readBoxNode();
00347 VrmlConePtr readConeNode();
00348 VrmlCylinderPtr readCylinderNode();
00349
00350 VrmlPointSetPtr readPointSetNode();
00351 VrmlPixelTexturePtr readPixelTextureNode();
00352 VrmlMovieTexturePtr readMovieTextureNode();
00353 VrmlElevationGridPtr readElevationGridNode();
00354 VrmlExtrusionPtr readExtrusionNode();
00355 VrmlSwitchPtr readSwitchNode();
00356 VrmlLODPtr readLODNode();
00357 VrmlCollisionPtr readCollisionNode();
00358 VrmlAnchorPtr readAnchorNode();
00359 VrmlFogPtr readFogNode();
00360 VrmlBillboardPtr readBillboardNode();
00361 VrmlWorldInfoPtr readWorldInfoNode();
00362 VrmlPointLightPtr readPointLightNode();
00363 VrmlDirectionalLightPtr readDirectionalLightNode();
00364 VrmlSpotLightPtr readSpotLightNode();
00365
00366 VrmlSpherePtr readSphereNode();
00367 VrmlTextPtr readTextNode();
00368 VrmlFontStylePtr readFontStyleNode();
00369 VrmlIndexedLineSetPtr readIndexedLineSetNode();
00370 VrmlIndexedFaceSetPtr readIndexedFaceSetNode();
00371 void checkIndexedFaceSet(VrmlIndexedFaceSetPtr node);
00372 VrmlCoordinatePtr readCoordNode();
00373 VrmlTextureCoordinatePtr readTextureCoordinateNode();
00374 VrmlColorPtr readColorNode();
00375 VrmlAppearancePtr readAppearanceNode();
00376 VrmlMaterialPtr readMaterialNode();
00377 VrmlImageTexturePtr readImageTextureNode();
00378 VrmlTextureTransformPtr readTextureTransformNode();
00379 VrmlNormalPtr readNormalNode();
00380
00381 VrmlVariantField& readProtoField(VrmlFieldTypeId fieldTypeId);
00382
00383 void readSFInt32(SFInt32& out_value);
00384 void readSFFloat(SFFloat& out_value);
00385 void readSFString(SFString& out_value);
00386 void readMFInt32(MFInt32& out_value);
00387 void readMFFloat(MFFloat& out_value);
00388 void readSFColor(SFColor& out_value);
00389 void readMFColor(MFColor& out_value);
00390 void readMFString(MFString& out_value);
00391 void readSFVec2f(SFVec2f& out_value);
00392 void readMFVec2f(MFVec2f& out_value);
00393 void readSFVec3f(SFVec3f& out_value);
00394 void readMFVec3f(MFVec3f& out_value);
00395 void readSFRotation(SFRotation& out_value);
00396 void readMFRotation(MFRotation& out_value);
00397 void readSFBool(SFBool& out_value);
00398 void readSFTime(SFTime& out_value);
00399 void readMFTime(MFTime& out_value);
00400 void readSFNode(SFNode& out_node, VrmlNodeCategory nodeCategory);
00401 SFNode readSFNode(VrmlNodeCategory nodeCategory);
00402 void readMFNode(MFNode& out_nodes, VrmlNodeCategory nodeCategory);
00403 void readSFImage( SFImage& out_image );
00404 private:
00405 VrmlParserImpl(const VrmlParserImpl& self, const list< string >& ref);
00406 const list< string >* getAncestorPathsList() const {return &ancestorPathsList;}
00407 void setSymbols();
00408 void init();
00409 list< string > ancestorPathsList;
00410 };
00411 }
00412
00413
00414 VrmlParser::VrmlParser()
00415 {
00416 init();
00417 }
00418
00419
00420 VrmlParser::VrmlParser(const string& filename)
00421 {
00422 init();
00423 load(filename);
00424 }
00425
00426
00427
00428 void VrmlParser::init()
00429 {
00430
00431 impl = new VrmlParserImpl(this);
00432 }
00433
00434
00435 VrmlParserImpl::VrmlParserImpl(VrmlParser* self) : self(self)
00436 {
00437 init();
00438 }
00439
00440 VrmlParserImpl::VrmlParserImpl(const VrmlParserImpl& refThis, const list< string >& refSet)
00441 : self(refThis.self), ancestorPathsList(refSet)
00442 {
00443 init();
00444 }
00445
00446
00447 VrmlParser::~VrmlParser()
00448 {
00449 delete impl;
00450 }
00451
00452
00453 void VrmlParser::setProtoInstanceActualNodeExtractionMode(bool isOn)
00454 {
00455 impl->protoInstanceActualNodeExtractionMode = isOn;
00456 }
00457
00458
00459
00463 void VrmlParser::load(const string& filename)
00464 {
00465 impl->load(filename);
00466 }
00467
00468
00469 void VrmlParserImpl::load(const string& filename)
00470 {
00471 filesystem::path localPath(filename);
00472 localPath.normalize();
00473
00474 #if (BOOST_VERSION < 104600)
00475 ancestorPathsList.push_back(localPath.file_string());
00476 scanner->loadFile(localPath.file_string());
00477 #else
00478 ancestorPathsList.push_back(localPath.string());
00479 scanner->loadFile(localPath.string());
00480 #endif
00481
00482
00483 scanner->setCommentChar(0);
00484 bool ok = scanner->readString("#VRML V2.0");
00485 if(ok){
00486 scanner->skipLine();
00487 }
00488
00489 scanner->setCommentChar('#');
00490 scanner->setQuoteChar('"');
00491 scanner->setWhiteSpaceChar(',');
00492 scanner->setLineOriented(false);
00493 }
00494
00495
00496 VrmlNodePtr VrmlParser::readNode()
00497 {
00498 return impl->readNode(TOP_NODE);
00499 }
00500
00501
00502 VrmlNodePtr VrmlParserImpl::readNode(VrmlNodeCategory nodeCategory)
00503 {
00504 VrmlNodePtr node;
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 if(!scanner->readWord()){
00515 return 0;
00516 }
00517
00518 string def_name;
00519 string nodeTypeName(scanner->stringValue);
00520 int symbol = scanner->getSymbolID(scanner->stringValue);
00521
00522 if(symbol){
00523
00524 if(symbol==N_INLINE)
00525 return readInlineNode(nodeCategory);
00526
00527 if(symbol==N_PROTO){
00528 if(nodeCategory == TOP_NODE){
00529 return defineProto();
00530 } else {
00531 scanner->throwException("PROTO node cannot be defined here");
00532 }
00533 }
00534
00535 if(symbol==D_USE){
00536 scanner->readString();
00537 const string& label = scanner->stringValue;
00538 TDefNodeMap::iterator p = defNodeMap.find(label);
00539 if(p != defNodeMap.end()){
00540 return p->second;
00541 } else {
00542 scanner->throwException
00543 (string("A node \"") + label + "\" specified by the USE directive does not exist");
00544 }
00545 }
00546
00547
00548
00549 if(symbol == D_ROUTE){
00550 scanner->readString();
00551 if(!scanner->readString("TO"))
00552 scanner->throwException("Illegal ROUTE (without TO)");
00553 scanner->readString();
00554
00555 return readNode( nodeCategory );
00556 }
00557
00558
00559 if(symbol == U_SCRIPT){
00560 cerr << "Script is not supported. " << endl;
00561 skipScriptNode();
00562 return readNode( nodeCategory );
00563 }
00564 if(symbol == U_EXTERNPROTO){
00565 cerr << "ExternProto is not supported." << endl;
00566 skipExternProto();
00567 return readNode( nodeCategory );
00568 }
00569
00570 if(symbol == D_DEF){
00571 def_name = scanner->readStringEx("Illegal DEF name");
00572 scanner->readWord();
00573 symbol = scanner->getSymbolID(scanner->stringValue);
00574 nodeTypeName = scanner->stringValue;
00575 }
00576
00577 }
00578
00579 if(!scanner->readChar('{')){
00580 scanner->throwException
00581 (string("The entity of a ") + nodeTypeName + " node does not correctly begin with '{'");
00582 }
00583
00584 if(symbol){
00585 node = readSpecificNode(nodeCategory, symbol, nodeTypeName);
00586 } else {
00587 node = readProtoInstanceNode(scanner->stringValue, nodeCategory);
00588 }
00589
00590 if(!scanner->readChar('}')){
00591 scanner->throwException
00592 (string("A ") + nodeTypeName + " node is not correctly closed with '}'");
00593 }
00594
00595 if(def_name.size() > 0) {
00596 defNodeMap.insert(TDefNodePair(def_name, node));
00597 node->defName = def_name;
00598 }
00599
00600 return node;
00601 }
00602
00603
00604 VrmlNodePtr VrmlParserImpl::readSpecificNode(VrmlNodeCategory nodeCategory, int symbol, const string& nodeTypeName)
00605 {
00606 VrmlNodePtr node;
00607
00608 switch(symbol){
00609
00610 case N_BACKGROUND: node = readBackgroundNode(); break;
00611 case N_NAVIGATION_INFO: node = readNavigationInfoNode(); break;
00612 case N_VIEWPOINT: node = readViewpointNode(); break;
00613
00614 case N_GROUP: node = readGroupNode(); break;
00615 case N_TRANSFORM: node = readTransformNode(); break;
00616 case N_SHAPE: node = readShapeNode(); break;
00617 case N_CYLINDER_SENSOR: node = readCylinderSensorNode(); break;
00618
00619 case N_POINTSET: node = readPointSetNode(); break;
00620 case N_PIXEL_TEXTURE: node = readPixelTextureNode(); break;
00621 case N_MOVIE_TEXTURE: node = readMovieTextureNode(); break;
00622 case N_ELEVATION_GRID: node = readElevationGridNode(); break;
00623 case N_EXTRUSION: node = readExtrusionNode(); break;
00624 case N_SWITCH: node = readSwitchNode(); break;
00625 case N_LOD: node = readLODNode(); break;
00626 case N_COLLISION: node = readCollisionNode(); break;
00627 case N_ANCHOR: node = readAnchorNode(); break;
00628 case N_FOG: node = readFogNode(); break;
00629 case N_BILLBOARD: node = readBillboardNode(); break;
00630 case N_WORLD_INFO: node = readWorldInfoNode(); break;
00631 case N_POINT_LIGHT: node = readPointLightNode(); break;
00632 case N_DIRECTIONAL_LIGHT: node = readDirectionalLightNode(); break;
00633 case N_SPOT_LIGHT: node = readSpotLightNode(); break;
00634
00635 case N_MATERIAL: node = readMaterialNode(); break;
00636 case N_APPEARANCE: node = readAppearanceNode(); break;
00637 case N_IMAGE_TEXTURE: node = readImageTextureNode(); break;
00638 case N_TEXTURE_TRANSFORM: node = readTextureTransformNode(); break;
00639
00640 case N_BOX: node = readBoxNode(); break;
00641 case N_CONE: node = readConeNode(); break;
00642 case N_CYLINDER: node = readCylinderNode(); break;
00643 case N_SPHERE: node = readSphereNode(); break;
00644 case N_TEXT: node = readTextNode(); break;
00645 case N_INDEXED_FACE_SET: node = readIndexedFaceSetNode(); break;
00646 case N_INDEXED_LINE_SET: node = readIndexedLineSetNode(); break;
00647
00648 case N_COORDINATE: node = readCoordNode(); break;
00649 case N_TEXTURE_COORDINATE: node = readTextureCoordinateNode(); break;
00650 case N_COLOR: node = readColorNode(); break;
00651 case N_NORMAL: node = readNormalNode(); break;
00652 case N_FONT_STYLE: node = readFontStyleNode(); break;
00653
00654
00655 case N_AUDIO_CLIP: node = skipUnsupportedNode("AudioClip"); break;
00656 case N_SOUND: node = skipUnsupportedNode("Sound"); break;
00657 case N_COLOR_INTERPOLATOR: node = skipUnsupportedNode("ColorInterpolator"); break;
00658 case N_COORDINATE_INTERPOLATOR: node = skipUnsupportedNode("CoordinateInterpolator"); break;
00659 case N_ORIENTATION_INTERPOLATOR: node = skipUnsupportedNode("OrientationInterpolator"); break;
00660 case N_NORMAL_INTERPOLATOR: node = skipUnsupportedNode("NormalInterpolator"); break;
00661 case N_POSITION_INTERPOLATOR: node = skipUnsupportedNode("PositionInterpolator"); break;
00662 case N_SCALAR_INTERPOLATOR: node = skipUnsupportedNode("ScalarInterpolator"); break;
00663 case N_PLANE_SENSOR: node = skipUnsupportedNode("PlaneSensor"); break;
00664 case N_PROXIMITY_SENSOR: node = skipUnsupportedNode("ProximitySensor"); break;
00665 case N_SPHERE_SENSOR: node = skipUnsupportedNode("SphereSensor"); break;
00666 case N_TIME_SENSOR: node = skipUnsupportedNode("TimeSensor"); break;
00667 case N_TOUCH_SENSOR: node = skipUnsupportedNode("TouchSensor"); break;
00668 case N_VISIBILITY_SENSOR: node = skipUnsupportedNode("VisibilitySensor"); break;
00669
00670 default: scanner->throwException
00671 (string("Node type \"") + nodeTypeName + "\" is not supported");
00672
00673 }
00674
00675 if(!node->isCategoryOf(nodeCategory)){
00676 scanner->throwException
00677 (string("A ") + nodeTypeName + " node is put in a illegal place");
00678 }
00679
00680 return node;
00681 }
00682
00683
00684 VrmlUnsupportedNodePtr VrmlParserImpl::skipUnsupportedNode(const std::string& nodeTypeName)
00685 {
00686 while(true){
00687 if(!scanner->readQuotedString()){
00688 if(scanner->peekChar() == '}'){
00689 break;
00690 } if(!scanner->readChar()){
00691 scanner->throwException( "Node is not closed." );
00692 }
00693 }
00694 }
00695 return new VrmlUnsupportedNode( nodeTypeName );
00696 }
00697
00698
00699 VrmlUnsupportedNodePtr VrmlParserImpl::skipScriptNode()
00700 {
00701
00702 for(int i=0; i<1; i++){
00703 while(true){
00704 if(!scanner->readQuotedString()){
00705 if(scanner->peekChar() == '}'){
00706 scanner->readChar();
00707 break;
00708 }
00709 if(!scanner->readChar()){
00710 scanner->throwException( "Script is not closed." );
00711 }
00712 }
00713 }
00714 }
00715
00716
00717 return NULL;
00718 }
00719
00720
00721 VrmlUnsupportedNodePtr VrmlParserImpl::skipExternProto()
00722 {
00723
00724 while(true){
00725 if(!scanner->readQuotedString()){
00726 if( scanner->peekChar() == ']' ){
00727
00728 scanner->readChar();
00729 break;
00730 }
00731 if(!scanner->readChar()){
00732 scanner->throwException( "EXTERNPROTO is not closed." );
00733 }
00734 }
00735 }
00736
00737 SFString url;
00738 readSFString( url );
00739
00740
00741 return NULL;
00742 }
00743
00744
00745 VrmlNodePtr VrmlParserImpl::readInlineNode(VrmlNodeCategory nodeCategory)
00746 {
00747 scanner->readChar('{');
00748
00749 if(scanner->readSymbol() && scanner->symbolValue == F_URL){
00750 MFString inlineUrls;
00751 readMFString(inlineUrls);
00752 scanner->readCharEx('}', "syntax error 2");
00753
00754 VrmlInlinePtr inlineNode = new VrmlInline();
00755 for( MFString::iterator ite = inlineUrls.begin(); ite != inlineUrls.end(); ++ite ){
00756 inlineNode->children.push_back( newInlineSource( *ite ) );
00757 inlineNode->urls.push_back(*ite);
00758 }
00759 return inlineNode;
00760 }
00761 return 0;
00762 }
00763
00764
00765 VrmlNodePtr VrmlParserImpl::newInlineSource(string& io_filename)
00766 {
00767 filesystem::path localPath;
00768 string chkFile("");
00769 if( isFileProtocol( io_filename ) )
00770 {
00771 localPath = filesystem::path( deleteURLScheme(io_filename) );
00772
00773 localPath.normalize();
00774
00775 if ( ! exists(localPath) ){
00776
00777 filesystem::path parentPath( scanner->filename );
00778 #if BOOST_VERSION < 103600
00779 localPath = parentPath.branch_path() / localPath;
00780 #else
00781 localPath = parentPath.parent_path() / localPath;
00782 #endif
00783 localPath.normalize();
00784 }
00785 chkFile = complete( localPath ).string();
00786 } else {
00787
00788 chkFile = io_filename;
00789 }
00790 for( list<string>::const_iterator cIte = ancestorPathsList.begin(); cIte != ancestorPathsList.end(); ++cIte){
00791 if( chkFile == *cIte )
00792 {
00793 scanner->throwException("Infinity loop ! " + chkFile + " is included ancestor list");
00794 }
00795 }
00796
00797 VrmlParserImpl inlineParser( *this, ancestorPathsList );
00798
00799 inlineParser.load( chkFile );
00800 io_filename = chkFile;
00801
00802 VrmlGroupPtr group = new VrmlGroup();
00803 while(VrmlNodePtr node = inlineParser.readNode(TOP_NODE)){
00804 if(node->isCategoryOf(CHILD_NODE)){
00805 group->children.push_back(node);
00806 }
00807 }
00808
00809 if(group->children.size() == 1){
00810 return group->children.front();
00811 } else {
00812 return group;
00813 }
00814 }
00815
00816
00817 VrmlProtoPtr VrmlParserImpl::defineProto()
00818 {
00819 string proto_name = scanner->readWordEx("illegal PROTO name");
00820 scanner->readCharEx('[', "syntax error 3");
00821
00822 VrmlProtoPtr proto = new VrmlProto(proto_name);
00823
00824 while(!scanner->readChar(']')){
00825 int event_type = scanner->readSymbolEx("illegal field event type");
00826 int field_symbol = scanner->readSymbolEx("illegal field type");
00827 string field_name = scanner->readWordEx("syntax error 4");
00828
00829
00830 VrmlVariantField& field = proto->fields.insert(TProtoFieldPair(field_name, VrmlVariantField())).first->second;
00831
00832 switch(event_type){
00833
00834 case E_FIELD:
00835 case E_EXPOSED_FIELD:
00836
00837 switch(field_symbol){
00838
00839 case T_SFINT32: field.setType(SFINT32); readSFInt32(field.sfInt32()); break;
00840 case T_MFINT32: field.setType(MFINT32); readMFInt32(field.mfInt32()); break;
00841 case T_SFFLOAT: field.setType(SFFLOAT); readSFFloat(field.sfFloat()); break;
00842 case T_MFFLOAT: field.setType(MFFLOAT); readMFFloat(field.mfFloat()); break;
00843 case T_SFVEC3F: field.setType(SFVEC3F); readSFVec3f(field.sfVec3f()); break;
00844 case T_MFVEC3F: field.setType(MFVEC3F); readMFVec3f(field.mfVec3f()); break;
00845 case T_SFCOLOR: field.setType(SFCOLOR); readSFColor(field.sfColor()); break;
00846 case T_MFCOLOR: field.setType(MFCOLOR); readMFColor(field.mfColor()); break;
00847 case T_SFSTRING: field.setType(SFSTRING); readSFString(field.sfString()); break;
00848 case T_MFSTRING: field.setType(MFSTRING); readMFString(field.mfString()); break;
00849 case T_SFROTATION: field.setType(SFROTATION); readSFRotation(field.sfRotation()); break;
00850 case T_MFROTATION: field.setType(MFROTATION); readMFRotation(field.mfRotation()); break;
00851 case T_SFBOOL: field.setType(SFBOOL); readSFBool(field.sfBool()); break;
00852 case T_SFNODE: field.setType(SFNODE); readSFNode(field.sfNode(), ANY_NODE); break;
00853 case T_MFNODE: field.setType(MFNODE); readMFNode(field.mfNode(), ANY_NODE); break;
00854 case T_SFIMAGE: field.setType(SFIMAGE); readSFImage(field.sfImage()); break;
00855
00856
00857 default: scanner->throwException("illegal field type");
00858 }
00859 break;
00860
00861 case E_EVENTIN:
00862 case E_EVENTOUT:
00863
00864 switch(field_symbol){
00865
00866 case T_SFINT32: field.setType(SFINT32); break;
00867 case T_MFINT32: field.setType(MFINT32); break;
00868 case T_SFFLOAT: field.setType(SFFLOAT); break;
00869 case T_MFFLOAT: field.setType(MFFLOAT); break;
00870 case T_SFVEC3F: field.setType(SFVEC3F); break;
00871 case T_MFVEC3F: field.setType(MFVEC3F); break;
00872 case T_SFCOLOR: field.setType(SFCOLOR); break;
00873 case T_MFCOLOR: field.setType(MFCOLOR); break;
00874 case T_SFSTRING: field.setType(SFSTRING); break;
00875 case T_MFSTRING: field.setType(MFSTRING); break;
00876 case T_SFROTATION: field.setType(SFROTATION); break;
00877 case T_MFROTATION: field.setType(MFROTATION); break;
00878 case T_SFBOOL: field.setType(SFBOOL); break;
00879 case T_SFNODE: field.setType(SFNODE); break;
00880 case T_MFNODE: field.setType(MFNODE); break;
00881 case T_SFIMAGE: field.setType(SFIMAGE); break;
00882 }
00883 break;
00884
00885 default: scanner->throwException("illegal field event type");
00886
00887 }
00888 }
00889
00890 scanner->readCharEx('{', "A PROTO definition has no entity");
00891 char* begin = scanner->text;
00892 int brace_level = 1;
00893 while(true){
00894 int token = scanner->readToken();
00895 if(token == EasyScanner::T_NONE){
00896 scanner->throwException("syntax error 5");
00897 } else if(token == EasyScanner::T_SIGLUM){
00898 if(scanner->charValue == '{') {
00899 brace_level++;
00900 } else if(scanner->charValue == '}') {
00901 brace_level--;
00902 if(brace_level==0)
00903 break;
00904 }
00905 }
00906 }
00907
00908 EasyScannerPtr entityScanner(new EasyScanner(*scanner, false));
00909 entityScanner->setText(begin, scanner->text - begin - 1);
00910 entityScanner->setLineNumberOffset(scanner->lineNumber);
00911
00912 protoToEntityScannerMap[proto.get()] = entityScanner;
00913
00914 protoMap.insert(TProtoPair(proto_name, proto));
00915
00916 return proto;
00917 }
00918
00919
00920 VrmlProtoInstancePtr VrmlParserImpl::readProtoInstanceNode(const string& proto_name, VrmlNodeCategory nodeCategory)
00921 {
00922 TProtoMap::iterator p = protoMap.find(proto_name);
00923 if(p == protoMap.end()){
00924 scanner->throwException("undefined node");
00925 }
00926
00927 VrmlProtoPtr proto = p->second;
00928 VrmlProtoInstancePtr protoInstance(new VrmlProtoInstance(proto));
00929
00930 while(scanner->readWord()){
00931 TProtoFieldMap::iterator p = protoInstance->fields.find(scanner->stringValue);
00932 if(p == protoInstance->fields.end())
00933 scanner->throwException("undefined field");
00934
00935 VrmlVariantField& field = p->second;
00936
00937 switch(field.typeId()){
00938
00939 case SFINT32: readSFInt32(field.sfInt32()); break;
00940 case MFINT32: readMFInt32(field.mfInt32()); break;
00941 case SFFLOAT: readSFFloat(field.sfFloat()); break;
00942 case MFFLOAT: readMFFloat(field.mfFloat()); break;
00943 case SFVEC2F: readSFVec2f(field.sfVec2f()); break;
00944 case MFVEC2F: readMFVec2f(field.mfVec2f()); break;
00945 case SFVEC3F: readSFVec3f(field.sfVec3f()); break;
00946 case MFVEC3F: readMFVec3f(field.mfVec3f()); break;
00947 case SFCOLOR: readSFVec3f(field.sfColor()); break;
00948 case MFCOLOR: readMFVec3f(field.mfColor()); break;
00949 case SFSTRING: readSFString(field.sfString()); break;
00950 case MFSTRING: readMFString(field.mfString()); break;
00951 case SFROTATION: readSFRotation(field.sfRotation()); break;
00952 case MFROTATION: readMFRotation(field.mfRotation()); break;
00953 case SFBOOL: readSFBool(field.sfBool()); break;
00954 case SFNODE: readSFNode(field.sfNode(), ANY_NODE); break;
00955 case MFNODE: readMFNode(field.mfNode(), ANY_NODE); break;
00956 case SFIMAGE: readSFImage(field.sfImage()); break;
00957 default:
00958 break;
00959 }
00960 }
00961
00962 if(protoInstanceActualNodeExtractionMode){
00963 protoInstance->actualNode = evalProtoInstance(protoInstance, nodeCategory);
00964 }
00965
00966 return protoInstance;
00967 }
00968
00969
00970 VrmlNodePtr VrmlParserImpl::evalProtoInstance(VrmlProtoInstancePtr protoInstance, VrmlNodeCategory nodeCategory)
00971 {
00972 EasyScannerPtr orgScanner = scanner;
00973 ProtoToEntityScannerMap::iterator p;
00974 p = protoToEntityScannerMap.find(protoInstance->proto.get());
00975 if(p == protoToEntityScannerMap.end()){
00976 scanner->throwException("Undefined proto node instance");
00977 }
00978 scanner = p->second;
00979 scanner->moveToHead();
00980
00981 VrmlProtoInstancePtr orgProtoInstance = currentProtoInstance;
00982 currentProtoInstance = protoInstance;
00983
00984 VrmlNodePtr node = readNode(nodeCategory);
00985 if(node){
00986 node->defName = protoInstance->defName;
00987 }
00988
00989 scanner = orgScanner;
00990 currentProtoInstance = orgProtoInstance;
00991
00992 return node;
00993 }
00994
00995
00996 VrmlViewpointPtr VrmlParserImpl::readViewpointNode()
00997 {
00998 VrmlViewpointPtr node(new VrmlViewpoint);
00999
01000 while(scanner->readSymbol()){
01001
01002 switch(scanner->symbolValue){
01003
01004 case F_FIELD_OF_VIEW: readSFFloat(node->fieldOfView); break;
01005 case F_JUMP: readSFBool(node->jump); break;
01006 case F_ORIENTATION: readSFRotation(node->orientation); break;
01007 case F_POSITION: readSFVec3f(node->position); break;
01008 case F_DESCRIPTION: readSFString(node->description); break;
01009
01010 default: scanner->throwException("Undefined field");
01011 }
01012 }
01013
01014 return node;
01015 }
01016
01017
01018 VrmlNavigationInfoPtr VrmlParserImpl::readNavigationInfoNode()
01019 {
01020 VrmlNavigationInfoPtr node(new VrmlNavigationInfo);
01021
01022 while(scanner->readSymbol()){
01023
01024 switch(scanner->symbolValue){
01025
01026 case F_AVATAR_SIZE: readMFFloat(node->avatarSize); break;
01027 case F_HEADLIGHT: readSFBool(node->headlight); break;
01028 case F_SPEED: readSFFloat(node->speed); break;
01029 case F_TYPE: readMFString(node->type); break;
01030 case F_VISIBILITY_LIMIT: readSFFloat(node->visibilityLimit); break;
01031
01032 default: scanner->throwException("Undefined field");
01033 }
01034 }
01035
01036 return node;
01037 }
01038
01039
01040 VrmlBackgroundPtr VrmlParserImpl::readBackgroundNode()
01041 {
01042 VrmlBackgroundPtr node(new VrmlBackground);
01043
01044 while(scanner->readSymbol()){
01045
01046 switch(scanner->symbolValue){
01047
01048 case F_GROUND_ANGLE: readMFFloat(node->groundAngle); break;
01049 case F_GROUND_COLOR: readMFColor(node->groundColor); break;
01050 case F_SKY_ANGLE: readMFFloat(node->skyAngle); break;
01051 case F_SKY_COLOR: readMFColor(node->skyColor); break;
01052 case F_BACK_URL: readMFString(node->backUrl); break;
01053 case F_BOTTOM_URL: readMFString(node->bottomUrl); break;
01054 case F_FRONT_URL: readMFString(node->frontUrl); break;
01055 case F_LEFT_URL: readMFString(node->leftUrl); break;
01056 case F_RIGHT_URL: readMFString(node->rightUrl); break;
01057 case F_TOP_URL: readMFString(node->topUrl); break;
01058
01059 default: scanner->throwException("Undefined field");
01060 }
01061 }
01062
01063 return node;
01064 }
01065
01066
01067 VrmlGroupPtr VrmlParserImpl::readGroupNode()
01068 {
01069 VrmlGroupPtr node(new VrmlGroup);
01070
01071 while(scanner->readSymbol()){
01072
01073 switch(scanner->symbolValue){
01074
01075 case F_BBOX_CENTER: readSFVec3f(node->bboxCenter); break;
01076 case F_BBOX_SIZE: readSFVec3f(node->bboxSize); break;
01077 case F_CHILDREN: readMFNode(node->children, CHILD_NODE); break;
01078
01079 case F_ADD_CHILDREN:
01080 case F_REMOVE_CHILDREN:
01081 {
01082 MFNode dummy;
01083 readMFNode(dummy, CHILD_NODE);
01084 }
01085 break;
01086
01087 default: scanner->throwException("Undefined field");
01088 }
01089 }
01090
01091 return node;
01092 }
01093
01094
01095 VrmlTransformPtr VrmlParserImpl::readTransformNode()
01096 {
01097 VrmlTransformPtr node(new VrmlTransform);
01098
01099 while(scanner->readSymbol()){
01100
01101 switch(scanner->symbolValue){
01102
01103 case F_CENTER: readSFVec3f(node->center); break;
01104 case F_ROTATION: readSFRotation(node->rotation); break;
01105 case F_SCALE: readSFVec3f(node->scale); break;
01106 case F_SCALE_ORIENTATION: readSFRotation(node->scaleOrientation); break;
01107 case F_TRANSLATION: readSFVec3f(node->translation); break;
01108 case F_BBOX_CENTER: readSFVec3f(node->bboxCenter); break;
01109 case F_BBOX_SIZE: readSFVec3f(node->bboxSize); break;
01110
01111 case F_CHILDREN: readMFNode(node->children, CHILD_NODE); break;
01112
01113 default: scanner->throwException("Undefined field");
01114 }
01115 }
01116
01117 return node;
01118 }
01119
01120
01121 VrmlShapePtr VrmlParserImpl::readShapeNode()
01122 {
01123 VrmlShapePtr node(new VrmlShape);
01124
01125 while(scanner->readSymbol()){
01126
01127 switch(scanner->symbolValue){
01128
01129 case F_APPEARANCE: node->appearance = dynamic_pointer_cast<VrmlAppearance>(readSFNode(APPEARANCE_NODE)); break;
01130 case F_GEOMETRY: node->geometry = readSFNode(GEOMETRY_NODE); break;
01131
01132 default: scanner->throwException("Undefined field");
01133 }
01134 }
01135
01136 return node;
01137 }
01138
01139
01140 VrmlCylinderSensorPtr VrmlParserImpl::readCylinderSensorNode()
01141 {
01142 VrmlCylinderSensorPtr node(new VrmlCylinderSensor);
01143
01144 while(scanner->readSymbol()){
01145
01146 switch(scanner->symbolValue){
01147
01148 case F_AUTO_OFFSET: readSFBool(node->autoOffset); break;
01149 case F_DISK_ANGLE: readSFFloat(node->diskAngle); break;
01150 case F_ENABLED: readSFBool(node->enabled); break;
01151 case F_MAX_ANGLE: readSFFloat(node->maxAngle); break;
01152 case F_MIN_ANGLE: readSFFloat(node->minAngle); break;
01153 case F_OFFSET: readSFFloat(node->offset); break;
01154
01155 default: scanner->throwException("Undefined field");
01156 }
01157 }
01158
01159 return node;
01160 }
01161
01162
01163 VrmlPointSetPtr VrmlParserImpl::readPointSetNode()
01164 {
01165 VrmlPointSetPtr node(new VrmlPointSet);
01166
01167 while(scanner->readSymbol()){
01168 switch(scanner->symbolValue){
01169 case F_COORD:
01170 node->coord = dynamic_pointer_cast<VrmlCoordinate>( readSFNode( COORDINATE_NODE ) );
01171 break;
01172
01173 case F_COLOR:
01174 node->color = dynamic_pointer_cast<VrmlColor>( readSFNode( COLOR_NODE ) );
01175 break;
01176
01177 default:
01178 scanner->throwException( "Undefined field" );
01179 }
01180 }
01181
01182 return node;
01183 }
01184
01185
01186 VrmlPixelTexturePtr VrmlParserImpl::readPixelTextureNode()
01187 {
01188 VrmlPixelTexturePtr node(new VrmlPixelTexture);
01189
01190 while(scanner->readSymbol()){
01191 switch(scanner->symbolValue){
01192 case F_IMAGE:
01193 readSFImage( node->image );
01194 break;
01195
01196 case F_REPEAT_S:
01197 readSFBool( node->repeatS );
01198 break;
01199
01200 case F_REPEAT_T:
01201 readSFBool( node->repeatT );
01202 break;
01203
01204 default:
01205 scanner->throwException( "Undefined field" );
01206 }
01207 }
01208
01209 return node;
01210 }
01211
01212
01213 VrmlMovieTexturePtr VrmlParserImpl::readMovieTextureNode()
01214 {
01215 VrmlMovieTexturePtr node(new VrmlMovieTexture);
01216
01217 while(scanner->readSymbol()){
01218 switch(scanner->symbolValue){
01219 case F_URL:
01220 readMFString( node->url );
01221 break;
01222
01223 case F_LOOP:
01224 readSFBool( node->loop );
01225 break;
01226
01227 case F_SPEED:
01228 readSFFloat( node->speed );
01229 break;
01230
01231 case F_START_TIME:
01232 readSFTime( node->startTime );
01233 break;
01234
01235 case F_STOP_TIME:
01236 readSFTime( node->stopTime );
01237 break;
01238
01239 case F_REPEAT_S:
01240 readSFBool( node->repeatS );
01241 break;
01242
01243 case F_REPEAT_T:
01244 readSFBool( node->repeatT );
01245 break;
01246
01247 default:
01248 scanner->throwException( "Undefined field" );
01249 }
01250 }
01251
01252 return node;
01253 }
01254
01255
01256 VrmlElevationGridPtr VrmlParserImpl::readElevationGridNode()
01257 {
01258 VrmlElevationGridPtr node(new VrmlElevationGrid);
01259
01260 while(scanner->readSymbol()){
01261 switch(scanner->symbolValue){
01262 case F_X_DIMENSION:
01263 readSFInt32( node->xDimension );
01264 break;
01265
01266 case F_Z_DIMENSION:
01267 readSFInt32( node->zDimension );
01268 break;
01269
01270 case F_X_SPACING:
01271 readSFFloat( node->xSpacing );
01272 break;
01273
01274 case F_Z_SPACING:
01275 readSFFloat( node->zSpacing );
01276 break;
01277
01278 case F_HEIGHT:
01279 readMFFloat( node->height );
01280 break;
01281
01282 case F_CCW:
01283 readSFBool( node->ccw );
01284 break;
01285
01286 case F_COLOR_PER_VERTEX:
01287 readSFBool( node->colorPerVertex );
01288 break;
01289
01290 case F_CREASE_ANGLE:
01291 readSFFloat( node->creaseAngle );
01292 break;
01293
01294 case F_NORMAL_PER_VERTEX:
01295 readSFBool( node->normalPerVertex );
01296 break;
01297
01298 case F_SOLID:
01299 readSFBool( node->solid );
01300 break;
01301
01302 case F_COLOR:
01303 node->color = dynamic_pointer_cast<VrmlColor>( readSFNode( COLOR_NODE ) );
01304 break;
01305
01306 case F_NORMAL:
01307 node->normal = dynamic_pointer_cast<VrmlNormal>( readSFNode( NORMAL_NODE ) );
01308 break;
01309
01310 case F_TEX_COORD:
01311 node->texCoord = dynamic_pointer_cast<VrmlTextureCoordinate>( readSFNode( TEXTURE_COORDINATE_NODE ) );
01312 break;
01313
01314 default:
01315 scanner->throwException( "Undefined field" );
01316 }
01317 }
01318
01319 return node;
01320 }
01321
01322
01323 VrmlExtrusionPtr VrmlParserImpl::readExtrusionNode()
01324 {
01325 VrmlExtrusionPtr node( new VrmlExtrusion );
01326
01327 while(scanner->readSymbol()){
01328 switch(scanner->symbolValue){
01329 case F_CROSS_SECTION:
01330 readMFVec2f( node->crossSection );
01331 break;
01332
01333 case F_SPINE:
01334 readMFVec3f( node->spine );
01335 break;
01336
01337 case F_SCALE:
01338 readMFVec2f( node->scale );
01339 break;
01340
01341 case F_ORIENTATION:
01342 readMFRotation( node->orientation );
01343 break;
01344
01345 case F_BEGIN_CAP:
01346 readSFBool( node->beginCap );
01347 break;
01348
01349 case F_END_CAP:
01350 readSFBool( node->endCap );
01351 break;
01352
01353 case F_SOLID:
01354 readSFBool( node->solid );
01355 break;
01356
01357 case F_CCW:
01358 readSFBool( node->ccw );
01359 break;
01360
01361 case F_CONVEX:
01362 readSFBool( node->convex );
01363 break;
01364
01365 case F_CREASE_ANGLE:
01366 readSFFloat( node->creaseAngle );
01367 break;
01368
01369 default:
01370 scanner->throwException( "Undefined field" );
01371 }
01372 }
01373
01374 return node;
01375 }
01376
01377
01378 VrmlSwitchPtr VrmlParserImpl::readSwitchNode()
01379 {
01380 VrmlSwitchPtr node( new VrmlSwitch );
01381
01382 while(scanner->readSymbol()){
01383 switch(scanner->symbolValue){
01384 case F_CHOICE:
01385 readMFNode(node->choice, CHILD_NODE);
01386 break;
01387
01388 case F_WHICH_CHOICE:
01389 readSFInt32(node->whichChoice);
01390 break;
01391
01392 default:
01393 scanner->throwException( "Undefined field" );
01394 }
01395 }
01396
01397 return node;
01398 }
01399
01400
01401 VrmlLODPtr VrmlParserImpl::readLODNode()
01402 {
01403 VrmlLODPtr node( new VrmlLOD );
01404
01405 while(scanner->readSymbol()){
01406 switch(scanner->symbolValue){
01407 case F_RANGE:
01408 readMFFloat(node->range);
01409 break;
01410
01411 case F_CENTER:
01412 readSFVec3f(node->center);
01413 break;
01414
01415 case F_LEVEL:
01416 readMFNode(node->level, ANY_NODE);
01417 break;
01418
01419 default:
01420 scanner->throwException("Undefined field");
01421 }
01422 }
01423
01424 return node;
01425 }
01426
01427
01428 VrmlCollisionPtr VrmlParserImpl::readCollisionNode()
01429 {
01430 VrmlCollisionPtr node(new VrmlCollision);
01431
01432 while(scanner->readSymbol()){
01433 switch(scanner->symbolValue){
01434 case F_COLLIDE:
01435 readSFBool(node->collide);
01436 break;
01437
01438 case F_CHILDREN:
01439 readMFNode(node->children, CHILD_NODE);
01440 break;
01441
01442 case F_PROXY:
01443 readSFNode(node->proxy, SHAPE_NODE);
01444 break;
01445
01446 case F_BBOX_CENTER:
01447 readSFVec3f(node->bboxCenter);
01448 break;
01449
01450 case F_BBOX_SIZE:
01451 readSFVec3f(node->bboxSize);
01452 break;
01453
01454 default:
01455 scanner->throwException( "Undefined field" );
01456 }
01457 }
01458
01459 return node;
01460 }
01461
01462
01463 VrmlAnchorPtr VrmlParserImpl::readAnchorNode()
01464 {
01465 VrmlAnchorPtr node(new VrmlAnchor);
01466
01467 while(scanner->readSymbol()){
01468 switch( scanner->symbolValue){
01469 case F_CHILDREN:
01470 readMFNode(node->children, CHILD_NODE);
01471 break;
01472
01473 case F_DESCRIPTION:
01474 readSFString(node->description);
01475 break;
01476
01477 case F_PARAMETER:
01478 readMFString(node->parameter);
01479 break;
01480
01481 case F_URL:
01482 readMFString(node->url);
01483 break;
01484
01485 case F_BBOX_CENTER:
01486 readSFVec3f(node->bboxCenter);
01487 break;
01488
01489 case F_BBOX_SIZE:
01490 readSFVec3f(node->bboxSize);
01491 break;
01492
01493 default:
01494 scanner->throwException( "Undefined field" );
01495 }
01496 }
01497
01498 return node;
01499 }
01500
01501
01502 VrmlFogPtr VrmlParserImpl::readFogNode()
01503 {
01504 VrmlFogPtr node(new VrmlFog);
01505
01506 while(scanner->readSymbol()){
01507 switch(scanner->symbolValue){
01508 case F_COLOR:
01509 readSFColor(node->color);
01510 break;
01511
01512 case F_VISIBILITY_RANGE:
01513 readSFFloat(node->visibilityRange);
01514 break;
01515
01516 case F_FOG_TYPE:
01517 readSFString(node->fogType);
01518 break;
01519
01520 default:
01521 scanner->throwException( "Undefined field" );
01522 }
01523 }
01524
01525 return node;
01526 }
01527
01528
01529 VrmlBillboardPtr VrmlParserImpl::readBillboardNode()
01530 {
01531 VrmlBillboardPtr node( new VrmlBillboard );
01532
01533 while(scanner->readSymbol()){
01534 switch(scanner->symbolValue){
01535 case F_AXIS_OF_ROTATION:
01536 readSFVec3f(node->axisOfRotation);
01537 break;
01538
01539 case F_CHILDREN:
01540 readMFNode(node->children, CHILD_NODE);
01541 break;
01542
01543 case F_BBOX_CENTER:
01544 readSFVec3f(node->bboxCenter);
01545 break;
01546
01547 case F_BBOX_SIZE:
01548 readSFVec3f(node->bboxSize);
01549 break;
01550
01551 default:
01552 scanner->throwException( "Undefined field" );
01553 }
01554 }
01555
01556 return node;
01557 }
01558
01559
01560 VrmlWorldInfoPtr VrmlParserImpl::readWorldInfoNode()
01561 {
01562 VrmlWorldInfoPtr node(new VrmlWorldInfo);
01563
01564 while(scanner->readSymbol()){
01565 switch(scanner->symbolValue){
01566 case F_TITLE:
01567 readSFString(node->title);
01568 break;
01569
01570 case F_INFO:
01571 readMFString(node->info);
01572 break;
01573
01574 default:
01575 scanner->throwException("Undefined field");
01576 }
01577 }
01578
01579 return node;
01580 }
01581
01582
01583 VrmlPointLightPtr VrmlParserImpl::readPointLightNode()
01584 {
01585 VrmlPointLightPtr node( new VrmlPointLight );
01586
01587 while(scanner->readSymbol()){
01588 switch(scanner->symbolValue){
01589 case F_LOCATION:
01590 readSFVec3f(node->location);
01591 break;
01592
01593 case F_ON:
01594 readSFBool(node->on);
01595 break;
01596
01597 case F_INTENSITY:
01598 readSFFloat(node->intensity);
01599 break;
01600
01601 case F_COLOR:
01602 readSFColor(node->color);
01603 break;
01604
01605 case F_RADIUS:
01606 readSFFloat(node->radius);
01607 break;
01608
01609 case F_AMBIENT_INTENSITY:
01610 readSFFloat(node->ambientIntensity);
01611 break;
01612
01613 case F_ATTENUATION:
01614 readSFVec3f(node->attenuation);
01615 break;
01616
01617 default:
01618 scanner->throwException( "Undefined field" );
01619 }
01620 }
01621
01622 return node;
01623 }
01624
01625
01626
01627 VrmlDirectionalLightPtr VrmlParserImpl::readDirectionalLightNode()
01628 {
01629 VrmlDirectionalLightPtr node(new VrmlDirectionalLight);
01630
01631 while(scanner->readSymbol()){
01632
01633 switch(scanner->symbolValue){
01634
01635 case F_DIRECTION: readSFVec3f(node->direction); break;
01636 case F_ON: readSFBool(node->on); break;
01637 case F_INTENSITY: readSFFloat(node->intensity); break;
01638 case F_COLOR: readSFColor(node->color); break;
01639 case F_AMBIENT_INTENSITY: readSFFloat(node->ambientIntensity); break;
01640
01641 default: scanner->throwException("Undefined field");
01642 }
01643 }
01644
01645 return node;
01646 }
01647
01648
01649 VrmlSpotLightPtr VrmlParserImpl::readSpotLightNode()
01650 {
01651 VrmlSpotLightPtr node(new VrmlSpotLight);
01652
01653 while(scanner->readSymbol()){
01654 switch(scanner->symbolValue){
01655 case F_LOCATION:
01656 readSFVec3f(node->location);
01657 break;
01658
01659 case F_DIRECTION:
01660 readSFVec3f(node->direction);
01661 break;
01662
01663 case F_ON:
01664 readSFBool(node->on);
01665 break;
01666
01667 case F_COLOR:
01668 readSFColor(node->color);
01669 break;
01670
01671 case F_INTENSITY:
01672 readSFFloat(node->intensity);
01673 break;
01674
01675 case F_RADIUS:
01676 readSFFloat(node->radius);
01677 break;
01678
01679 case F_AMBIENT_INTENSITY:
01680 readSFFloat(node->ambientIntensity);
01681 break;
01682
01683 case F_ATTENUATION:
01684 readSFVec3f(node->attenuation);
01685 break;
01686
01687 case F_BEAM_WIDTH:
01688 readSFFloat(node->beamWidth);
01689 break;
01690
01691 case F_CUT_OFF_RANGE:
01692 readSFFloat(node->cutOffAngle);
01693 break;
01694
01695 default:
01696 scanner->throwException( "Undefined field" );
01697 }
01698 }
01699
01700 return node;
01701 }
01702
01703
01704 VrmlBoxPtr VrmlParserImpl::readBoxNode()
01705 {
01706 VrmlBoxPtr node(new VrmlBox);
01707
01708 if(scanner->readSymbol()){
01709 if(scanner->symbolValue != F_SIZE)
01710 scanner->throwException("Undefined field");
01711 readSFVec3f(node->size);
01712 }
01713
01714 return node;
01715 }
01716
01717
01718 VrmlConePtr VrmlParserImpl::readConeNode()
01719 {
01720 VrmlConePtr node(new VrmlCone);
01721
01722 while(scanner->readSymbol()){
01723
01724 switch(scanner->symbolValue){
01725
01726 case F_BOTTOM: readSFBool(node->bottom); break;
01727 case F_BOTTOM_RADIUS: readSFFloat(node->bottomRadius); break;
01728 case F_HEIGHT: readSFFloat(node->height); break;
01729 case F_SIDE: readSFBool(node->side); break;
01730
01731 default: scanner->throwException("Undefined field");
01732 }
01733 }
01734
01735 return node;
01736 }
01737
01738
01739 VrmlCylinderPtr VrmlParserImpl::readCylinderNode()
01740 {
01741 VrmlCylinderPtr node(new VrmlCylinder);
01742
01743 while(scanner->readSymbol()){
01744
01745 switch(scanner->symbolValue){
01746
01747 case F_BOTTOM: readSFBool(node->bottom); break;
01748 case F_HEIGHT: readSFFloat(node->height); break;
01749 case F_RADIUS: readSFFloat(node->radius); break;
01750 case F_SIDE: readSFBool(node->side); break;
01751 case F_TOP: readSFBool(node->top); break;
01752
01753 default: scanner->throwException("Undefined field");
01754 }
01755 }
01756
01757 return node;
01758 }
01759
01760
01761 VrmlSpherePtr VrmlParserImpl::readSphereNode()
01762 {
01763 VrmlSpherePtr node(new VrmlSphere);
01764
01765 if(scanner->readSymbol()){
01766 if(scanner->symbolValue != F_RADIUS)
01767 scanner->throwException("Undefined field");
01768 readSFFloat(node->radius);
01769 }
01770
01771 return node;
01772 }
01773
01774
01775 VrmlTextPtr VrmlParserImpl::readTextNode()
01776 {
01777 VrmlTextPtr node(new VrmlText);
01778
01779 while(scanner->readSymbol()){
01780
01781 switch(scanner->symbolValue){
01782
01783 case F_STRING: readMFString(node->fstring); break;
01784 case F_LENGTH: readMFFloat(node->length); break;
01785 case F_MAX_EXTENT: readSFFloat(node->maxExtent); break;
01786 case F_FONT_STYLE: node->fontStyle = dynamic_pointer_cast<VrmlFontStyle>(readSFNode(FONT_STYLE_NODE)); break;
01787
01788 default: scanner->throwException("Undefined field");
01789 }
01790 }
01791
01792 return node;
01793 }
01794
01795
01796 VrmlFontStylePtr VrmlParserImpl::readFontStyleNode()
01797 {
01798 VrmlFontStylePtr node(new VrmlFontStyle);
01799
01800 while(scanner->readSymbol()){
01801
01802 switch(scanner->symbolValue){
01803
01804 case F_FAMILY: readMFString(node->family); break;
01805 case F_HORIZONTAL: readSFBool(node->horizontal); break;
01806 case F_JUSTIFY: readMFString(node->justify); break;
01807 case F_LANGUAGE: readSFString(node->language); break;
01808 case F_LEFT_TO_RIGHT: readSFBool(node->leftToRight); break;
01809 case F_SIZE: readSFFloat(node->size); break;
01810 case F_SPACING: readSFFloat(node->spacing); break;
01811 case F_STYLE: readSFString(node->style); break;
01812 case F_TOP_TO_BOTTOM: readSFBool(node->topToBottom); break;
01813
01814 default: scanner->throwException("Undefined field");
01815 }
01816 }
01817
01818 return node;
01819 }
01820
01821
01822 VrmlIndexedLineSetPtr VrmlParserImpl::readIndexedLineSetNode()
01823 {
01824 VrmlIndexedLineSetPtr node(new VrmlIndexedLineSet);
01825
01826 while(scanner->readSymbol()){
01827
01828 switch(scanner->symbolValue){
01829
01830 case F_COLOR: node->color = dynamic_pointer_cast<VrmlColor>(readSFNode(COLOR_NODE)); break;
01831 case F_COORD: node->coord = dynamic_pointer_cast<VrmlCoordinate>(readSFNode(COORDINATE_NODE)); break;
01832 case F_COLOR_INDEX: readMFInt32(node->colorIndex); break;
01833 case F_COLOR_PER_VERTEX: readSFBool(node->colorPerVertex); break;
01834 case F_COORD_INDEX: readMFInt32(node->coordIndex); break;
01835
01836 default: scanner->throwException("Undefined field");
01837 }
01838 }
01839
01840 return node;
01841 }
01842
01843
01844 VrmlIndexedFaceSetPtr VrmlParserImpl::readIndexedFaceSetNode()
01845 {
01846 VrmlIndexedFaceSetPtr node(new VrmlIndexedFaceSet);
01847
01848 while(scanner->readSymbol()){
01849
01850 switch(scanner->symbolValue){
01851
01852 case F_COLOR: node->color = dynamic_pointer_cast<VrmlColor>(readSFNode(COLOR_NODE)); break;
01853 case F_COORD: node->coord = dynamic_pointer_cast<VrmlCoordinate>(readSFNode(COORDINATE_NODE)); break;
01854
01855 case F_COLOR_INDEX: readMFInt32(node->colorIndex); break;
01856 case F_COLOR_PER_VERTEX: readSFBool(node->colorPerVertex); break;
01857 case F_COORD_INDEX: readMFInt32(node->coordIndex); break;
01858
01859 case F_CCW: readSFBool(node->ccw); break;
01860 case F_CONVEX: readSFBool(node->convex); break;
01861 case F_SOLID: readSFBool(node->solid); break;
01862 case F_CREASE_ANGLE: readSFFloat(node->creaseAngle); break;
01863 case F_NORMAL_INDEX: readMFInt32(node->normalIndex); break;
01864 case F_NORMAL_PER_VERTEX: readSFBool(node->normalPerVertex); break;
01865 case F_TEX_COORD_INDEX: readMFInt32(node->texCoordIndex); break;
01866
01867 case F_TEX_COORD:
01868 node->texCoord = dynamic_pointer_cast<VrmlTextureCoordinate>(readSFNode(TEXTURE_COORDINATE_NODE));
01869 break;
01870 case F_NORMAL:
01871 node->normal = dynamic_pointer_cast<VrmlNormal>(readSFNode(NORMAL_NODE));
01872 break;
01873
01874 default: scanner->throwException("Undefined field");
01875 }
01876 }
01877
01878
01879
01880 return node;
01881 }
01882
01883
01884 void VrmlParserImpl::checkIndexedFaceSet(VrmlIndexedFaceSetPtr node)
01885 {
01886 MFInt32& index = node->coordIndex;
01887 MFVec3f& coord = node->coord->point;
01888
01889 int numUsedVertices = 0;
01890 vector<bool> usedVertices(coord.size(), false);
01891
01892 int n = index.size();
01893
01894 int i = 0;
01895 vector<int> polygon;
01896 while(i < n){
01897 polygon.resize(0);
01898 bool isSeparated = false;
01899 while(i < n){
01900 if(index[i] < 0){
01901 isSeparated = true;
01902 i++;
01903 break;
01904 }
01905 polygon.push_back(index[i]);
01906 if(!usedVertices[index[i]]){
01907 usedVertices[index[i]] = true;
01908 numUsedVertices++;
01909 }
01910 i++;
01911 }
01912
01913 const int numVertices = polygon.size();
01914
01915 if(numVertices < 3){
01916 cerr << "Number of vertices is less than 3 !" << endl;
01917 }
01918 if(numVertices > 3){
01919 cerr << "Polygon is not a triangle in ";
01920 cerr << scanner->filename << endl;
01921 for(int j=0; j < numVertices; j++){
01922 cerr << polygon[j] << ",";
01923 }
01924 cerr << endl;
01925 }
01926 if(!isSeparated){
01927 cerr << "Vertex index is not correctly separated by '-1'" << endl;
01928 }
01929
01930 int n = coord.size();
01931 for(int j=0; j < numVertices; j++){
01932 if(polygon[j] >= n){
01933 cerr << "index " << polygon[j] << " is over the number of vertices" << endl;
01934 }
01935 }
01936
01937 bool isIndexOverlapped = false;
01938 bool isVertexOverlapped = false;
01939 for(int j = 0; j < numVertices - 1; j++){
01940 for(int k = j+1; k < numVertices; k++){
01941 if(polygon[j] == polygon[k]){
01942 isIndexOverlapped = true;
01943 }
01944 SFVec3f& v1 = coord[polygon[j]];
01945 SFVec3f& v2 = coord[polygon[k]];
01946 if(v1[0] == v2[0] && v1[1] == v2[1] && v1[2] == v2[2]){
01947 isVertexOverlapped = true;
01948 }
01949 }
01950 }
01951 if(isIndexOverlapped){
01952 cerr << "overlapped vertex index in one polygon: ";
01953 for(int l = 0; l < numVertices; l++){
01954 cerr << polygon[l] << ",";
01955 }
01956 cerr << endl;
01957 }
01958
01959 if(isVertexOverlapped){
01960 cerr << "In " << scanner->filename << ":";
01961 cerr << "two vertices in one polygon have the same position\n";
01962
01963 for(int l = 0; l < numVertices; l++){
01964 SFVec3f& v = coord[polygon[l]];
01965 cerr << polygon[l] << " = (" << v[0] << "," << v[1] << "," << v[2] << ") ";
01966 }
01967 cerr << endl;
01968 }
01969 }
01970
01971 if(numUsedVertices < static_cast<int>(coord.size())){
01972 cerr << "There are vertices which are not used in" << scanner->filename << ".\n";
01973 cerr << "Number of vertices is " << coord.size();
01974 cerr << ", Number of used ones is " << numUsedVertices << endl;
01975 }
01976 }
01977
01978
01979 VrmlCoordinatePtr VrmlParserImpl::readCoordNode()
01980 {
01981 VrmlCoordinatePtr node(new VrmlCoordinate);
01982
01983 if(scanner->readSymbol()){
01984 if(scanner->symbolValue != F_POINT)
01985 scanner->throwException("Undefined field");
01986 readMFVec3f(node->point);
01987 }
01988
01989 return node;
01990 }
01991
01992
01993 VrmlTextureCoordinatePtr VrmlParserImpl::readTextureCoordinateNode()
01994 {
01995 VrmlTextureCoordinatePtr node(new VrmlTextureCoordinate);
01996
01997 if(scanner->readSymbol()){
01998 if(scanner->symbolValue != F_POINT)
01999 scanner->throwException("Undefined field");
02000 readMFVec2f(node->point);
02001 }
02002
02003 return node;
02004 }
02005
02006
02007 VrmlColorPtr VrmlParserImpl::readColorNode()
02008 {
02009 VrmlColorPtr node(new VrmlColor);
02010
02011 if(scanner->readSymbol()){
02012 if(scanner->symbolValue != F_COLOR)
02013 scanner->throwException("Undefined field");
02014 readMFColor(node->color);
02015 }
02016
02017 return node;
02018 }
02019
02020
02021 VrmlNormalPtr VrmlParserImpl::readNormalNode()
02022 {
02023 VrmlNormalPtr node(new VrmlNormal);
02024
02025 if(scanner->readSymbol()){
02026 if(scanner->symbolValue != F_VECTOR)
02027 scanner->throwException("Undefined field");
02028 readMFVec3f(node->vector);
02029 }
02030
02031 return node;
02032 }
02033
02034
02035 VrmlAppearancePtr VrmlParserImpl::readAppearanceNode()
02036 {
02037 VrmlAppearancePtr node(new VrmlAppearance);
02038
02039 while(scanner->readSymbol()){
02040
02041 switch(scanner->symbolValue){
02042
02043 case F_MATERIAL: node->material = dynamic_pointer_cast<VrmlMaterial>(readSFNode(MATERIAL_NODE)); break;
02044 case F_TEXTURE: node->texture = dynamic_pointer_cast<VrmlTexture>(readSFNode(TEXTURE_NODE)); break;
02045 case F_TEXTURE_TRANSFORM:
02046 node->textureTransform = dynamic_pointer_cast<VrmlTextureTransform>(readSFNode(TEXTURE_TRANSFORM_NODE)); break;
02047
02048 default: scanner->throwException("Undefined field");
02049 }
02050 }
02051
02052 return node;
02053 }
02054
02055
02056 VrmlMaterialPtr VrmlParserImpl::readMaterialNode()
02057 {
02058 VrmlMaterialPtr node(new VrmlMaterial);
02059
02060 while(scanner->readSymbol()){
02061
02062 switch(scanner->symbolValue){
02063
02064 case F_AMBIENT_INTENSITY: readSFFloat(node->ambientIntensity); break;
02065 case F_DIFFUSE_COLOR: readSFColor(node->diffuseColor); break;
02066 case F_EMISSIVE_COLOR: readSFColor(node->emissiveColor); break;
02067 case F_SHININESS: readSFFloat(node->shininess); break;
02068 case F_SPECULAR_COLOR: readSFColor(node->specularColor); break;
02069 case F_TRANSPARANCY: readSFFloat(node->transparency); break;
02070
02071 default: scanner->throwException("Undefined field");
02072 }
02073 }
02074
02075 return node;
02076 }
02077
02078
02079 VrmlImageTexturePtr VrmlParserImpl::readImageTextureNode()
02080 {
02081 VrmlImageTexturePtr node = new VrmlImageTexture;
02082
02083 while(scanner->readSymbol()){
02084
02085 switch(scanner->symbolValue){
02086 case F_URL: readMFString(node->url); break;
02087 case F_REPEAT_S: readSFBool(node->repeatS); break;
02088 case F_REPEAT_T: readSFBool(node->repeatT); break;
02089
02090 default: scanner->throwException("Undefined field");
02091 }
02092 }
02093
02094 return node;
02095 }
02096
02097
02098 VrmlTextureTransformPtr VrmlParserImpl::readTextureTransformNode()
02099 {
02100 VrmlTextureTransformPtr node(new VrmlTextureTransform);
02101
02102 while(scanner->readSymbol()){
02103
02104 switch(scanner->symbolValue){
02105 case F_CENTER: readSFVec2f(node->center); break;
02106 case F_ROTATION: readSFFloat(node->rotation); break;
02107 case F_SCALE: readSFVec2f(node->scale); break;
02108 case F_TRANSLATION: readSFVec2f(node->translation); break;
02109
02110 default: scanner->throwException("Undefined field");
02111 }
02112 }
02113
02114 return node;
02115 }
02116
02117
02118 VrmlVariantField& VrmlParserImpl::readProtoField(VrmlFieldTypeId fieldTypeId)
02119 {
02120 if(!currentProtoInstance){
02121 scanner->throwException("cannot use proto field value here");
02122 }
02123 scanner->readWordEx("illegal field");
02124 TProtoFieldMap::iterator p = currentProtoInstance->fields.find(scanner->stringValue);
02125
02126 if(p == currentProtoInstance->fields.end()){
02127 string msg = "This field(";
02128 msg += scanner->stringValue +") does not exist in proto node";
02129 scanner->throwException(msg);
02130 }
02131 if(p->second.typeId() != fieldTypeId){
02132 scanner->throwException("Unmatched field type");
02133 }
02134
02135 return p->second;
02136 }
02137
02138
02139 void VrmlParserImpl::readSFInt32(SFInt32& out_value)
02140 {
02141 if(scanner->readSymbol(F_IS)){
02142 VrmlVariantField& field = readProtoField(SFINT32);
02143 out_value = field.sfInt32();
02144 } else {
02145 out_value = scanner->readIntEx("illegal int value");
02146 }
02147 }
02148
02149
02150 void VrmlParserImpl::readMFInt32(MFInt32& out_value)
02151 {
02152 if(scanner->readSymbol(F_IS)){
02153 VrmlVariantField& field = readProtoField(MFINT32);
02154 out_value = field.mfInt32();
02155 } else {
02156 int v;
02157 out_value.clear();
02158 if(!scanner->readChar('[')){
02159 readSFInt32(v);
02160 out_value.push_back(v);
02161 } else {
02162 while(!scanner->readChar(']')){
02163 readSFInt32(v);
02164 out_value.push_back(v);
02165 }
02166 }
02167 }
02168 }
02169
02170
02171 void VrmlParserImpl::readSFFloat(SFFloat& out_value)
02172 {
02173 if(scanner->readSymbol(F_IS)){
02174 VrmlVariantField& field = readProtoField(SFFLOAT);
02175 out_value = field.sfFloat();
02176 } else {
02177 out_value = scanner->readDoubleEx("illegal float value");
02178 }
02179 }
02180
02181
02182 void VrmlParserImpl::readMFFloat(MFFloat& out_value)
02183 {
02184 if(scanner->readSymbol(F_IS)){
02185 VrmlVariantField& field = readProtoField(MFFLOAT);
02186 out_value = field.mfFloat();
02187 } else {
02188 SFFloat v;
02189 out_value.clear();
02190 if(!scanner->readChar('[')){
02191 readSFFloat(v);
02192 out_value.push_back(v);
02193 } else {
02194 while(!scanner->readChar(']')){
02195 readSFFloat(v);
02196 out_value.push_back(v);
02197 }
02198 }
02199 }
02200 }
02201
02202
02203 void VrmlParserImpl::readSFString(SFString& out_value)
02204 {
02205 if(scanner->readSymbol(F_IS)){
02206 VrmlVariantField& field = readProtoField(SFSTRING);
02207 out_value = field.sfString();
02208 } else {
02209 out_value = scanner->readQuotedStringEx("illegal string");
02210 }
02211 }
02212
02213
02214 void VrmlParserImpl::readMFString(MFString& out_value)
02215 {
02216 if(scanner->readSymbol(F_IS)){
02217 VrmlVariantField& field = readProtoField(MFSTRING);
02218 out_value = field.mfString();
02219 } else {
02220 string s;
02221 out_value.clear();
02222 if(!scanner->readChar('[')){
02223 readSFString(s);
02224 out_value.push_back(s);
02225 } else {
02226 while(!scanner->readChar(']')){
02227 readSFString(s);
02228 out_value.push_back(s);
02229 }
02230 }
02231 }
02232 }
02233
02234
02235 void VrmlParserImpl::readSFVec2f(SFVec2f& out_value)
02236 {
02237 if(scanner->readSymbol(F_IS)){
02238 VrmlVariantField& field = readProtoField(SFVEC2F);
02239 out_value = field.sfVec2f();
02240 } else {
02241 readSFFloat(out_value[0]);
02242 readSFFloat(out_value[1]);
02243 }
02244 }
02245
02246
02247 void VrmlParserImpl::readMFVec2f(MFVec2f& out_value)
02248 {
02249 if(scanner->readSymbol(F_IS)){
02250 VrmlVariantField& field = readProtoField(MFVEC2F);
02251 out_value = field.mfVec2f();
02252 } else {
02253 SFVec2f v;
02254 out_value.clear();
02255 if(!scanner->readChar('[')){
02256 readSFVec2f(v);
02257 out_value.push_back(v);
02258 } else {
02259 while(!scanner->readChar(']')){
02260 readSFVec2f(v);
02261 out_value.push_back(v);
02262 }
02263 }
02264 }
02265 }
02266
02267
02268 void VrmlParserImpl::readSFVec3f(SFVec3f& out_value)
02269 {
02270 if(scanner->readSymbol(F_IS)){
02271 VrmlVariantField& field = readProtoField(SFVEC3F);
02272 out_value = field.sfVec3f();
02273 } else {
02274 readSFFloat(out_value[0]);
02275 readSFFloat(out_value[1]);
02276 readSFFloat(out_value[2]);
02277 }
02278 }
02279
02280
02281 void VrmlParserImpl::readMFVec3f(MFVec3f& out_value)
02282 {
02283 if(scanner->readSymbol(F_IS)){
02284 VrmlVariantField& field = readProtoField(MFVEC3F);
02285 out_value = field.mfVec3f();
02286 } else {
02287 SFVec3f v;
02288 out_value.clear();
02289 if(!scanner->readChar('[')){
02290 readSFVec3f(v);
02291 out_value.push_back(v);
02292 } else {
02293 while(!scanner->readChar(']')){
02294 readSFVec3f(v);
02295 out_value.push_back(v);
02296 }
02297 }
02298 }
02299 }
02300
02301
02302 void VrmlParserImpl::readSFColor(SFColor& out_value)
02303 {
02304 if(scanner->readSymbol(F_IS)){
02305 VrmlVariantField& field = readProtoField(SFCOLOR);
02306 out_value = field.sfColor();
02307 } else {
02308 readSFVec3f(out_value);
02309 }
02310 }
02311
02312
02313
02314 void VrmlParserImpl::readMFColor(MFColor& out_value)
02315 {
02316 if(scanner->readSymbol(F_IS)){
02317 VrmlVariantField& field = readProtoField(MFCOLOR);
02318 out_value = field.mfColor();
02319 } else {
02320 readMFVec3f(out_value);
02321 }
02322 }
02323
02324
02325 void VrmlParserImpl::readSFRotation(SFRotation& out_value)
02326 {
02327 if(scanner->readSymbol(F_IS)){
02328 VrmlVariantField& field = readProtoField(SFROTATION);
02329 out_value = field.sfRotation();
02330 } else {
02331 double len2 = 0.0;
02332
02333 for(int i=0; i < 3; i++){
02334 readSFFloat(out_value[i]);
02335 len2 += out_value[i] * out_value[i];
02336 }
02337 readSFFloat(out_value[3]);
02338
02339 const double len = sqrt(len2);
02340
02341 if(fabs(len) < 1.0e-6){
02342 scanner->throwException("Rotation axis is zero vector");
02343 }
02344
02345
02346 for(int i=0; i < 3; i++){
02347 out_value[i] /= len;
02348 }
02349 }
02350 }
02351
02352
02353 void VrmlParserImpl::readMFRotation(MFRotation& out_value)
02354 {
02355 if(scanner->readSymbol(F_IS)){
02356 VrmlVariantField& field = readProtoField(MFROTATION);
02357 out_value = field.mfRotation();
02358 } else {
02359 SFRotation r;
02360 out_value.clear();
02361 if(!scanner->readChar('[')){
02362 readSFRotation(r);
02363 out_value.push_back(r);
02364 } else {
02365 while(!scanner->readChar(']')){
02366 readSFRotation(r);
02367 out_value.push_back(r);
02368 }
02369 }
02370 }
02371 }
02372
02373
02374 void VrmlParserImpl::readSFBool(SFBool& out_value)
02375 {
02376 if(scanner->readSymbol(F_IS)){
02377 VrmlVariantField& field = readProtoField(SFBOOL);
02378 out_value = field.sfBool();
02379 } else {
02380 switch(scanner->readSymbolEx("no bool value")){
02381 case V_TRUE: out_value = true; break;
02382 case V_FALSE: out_value = false; break;
02383
02384 default: scanner->throwException("no bool value");
02385 }
02386 }
02387 }
02388
02389
02390
02391
02392
02402
02403 void VrmlParserImpl::readSFImage(
02404 SFImage& out_image )
02405 {
02406 if( scanner->readSymbol( F_IS ) )
02407 {
02408 VrmlVariantField& field = readProtoField( SFIMAGE );
02409 out_image = field.sfImage();
02410 }
02411 else
02412 {
02413 SFInt32 pixelValue;
02414 unsigned char componentValue;
02415
02416 readSFInt32( out_image.width );
02417 readSFInt32( out_image.height );
02418 readSFInt32( out_image.numComponents );
02419
02424 int w, h, i;
02425 int shift;
02426 const SFInt32 comps = out_image.numComponents;
02427
02428 for( h=0; h<out_image.height; h++ )
02429 {
02430 for( w=0; w<out_image.width; w++ )
02431 {
02432 readSFInt32( pixelValue );
02433 for( i=0, shift=8*( comps - 1 ); i<comps; i++, shift-=8 )
02434 {
02435
02436 componentValue = (unsigned char)( (pixelValue>>shift) & 0x000000FF );
02437 out_image.pixels.push_back( componentValue );
02438 }
02439 }
02440 }
02441 }
02442 }
02443
02444
02445 void VrmlParserImpl::readSFTime(SFTime& out_value)
02446 {
02447 if(scanner->readSymbol( F_IS )){
02448 VrmlVariantField& field = readProtoField( SFTIME );
02449 out_value = field.sfFloat();
02450 } else {
02451 out_value = scanner->readDoubleEx( "illegal time value" );
02452 }
02453 }
02454
02455
02456 void VrmlParserImpl::readMFTime(MFTime& out_value)
02457 {
02458 if(scanner->readSymbol(F_IS)){
02459 VrmlVariantField& field = readProtoField( MFTIME );
02460 out_value = field.mfFloat();
02461 } else {
02462 SFFloat v;
02463 out_value.clear();
02464 if(!scanner->readChar('[' )){
02465 readSFTime( v );
02466 out_value.push_back( v );
02467 } else {
02468 while(!scanner->readChar(']')){
02469 readSFTime(v);
02470 out_value.push_back(v);
02471 }
02472 }
02473 }
02474 }
02475
02476
02477
02478 void VrmlParserImpl::readSFNode(SFNode& out_node, VrmlNodeCategory nodeCategory)
02479 {
02480 if(scanner->readSymbol(F_IS)){
02481 VrmlVariantField& field = readProtoField(SFNODE);
02482 out_node = field.sfNode();
02483 } else if(scanner->readSymbol(V_NULL)){
02484 out_node = 0;
02485 } else {
02486 out_node = readNode(nodeCategory);
02487 }
02488 }
02489
02490
02491 SFNode VrmlParserImpl::readSFNode(VrmlNodeCategory nodeCategory)
02492 {
02493 if(scanner->readSymbol(F_IS)){
02494 VrmlVariantField& field = readProtoField(SFNODE);
02495 return field.sfNode();
02496 } else if(scanner->readSymbol(V_NULL)){
02497 return 0;
02498 } else {
02499 return readNode(nodeCategory);
02500 }
02501 }
02502
02503
02504 void VrmlParserImpl::readMFNode(MFNode& out_nodes, VrmlNodeCategory nodeCategory)
02505 {
02506 if(scanner->readSymbol(F_IS)){
02507 VrmlVariantField& field = readProtoField(MFNODE);
02508 out_nodes = field.mfNode();
02509 } else {
02510 SFNode sfnode;
02511 out_nodes.clear();
02512 if(!scanner->readChar('[')){
02513 readSFNode(sfnode, nodeCategory);
02514 out_nodes.push_back(sfnode);
02515 } else {
02516 while(true) {
02517 readSFNode(sfnode, nodeCategory);
02518 if(sfnode){
02519 out_nodes.push_back(sfnode);
02520 }
02521 bool closed = scanner->readChar(']');
02522 if(closed){
02523 break;
02524 }
02525 if(!sfnode && !closed){
02526 scanner->throwException("syntax error");
02527 }
02528 }
02529 }
02530 }
02531 }
02532
02533 void VrmlParserImpl::init()
02534 {
02535 currentProtoInstance = 0;
02536 protoInstanceActualNodeExtractionMode = true;
02537
02538 scanner = boost::shared_ptr<EasyScanner>( new EasyScanner() );
02539 setSymbols();
02540 }
02541
02542 void VrmlParserImpl::setSymbols()
02543 {
02544 struct TSymbol {
02545 int id;
02546 const char* symbol;
02547 };
02548
02549 static TSymbol symbols[] = {
02550
02551
02552 { V_TRUE, "TRUE" },
02553 { V_FALSE, "FALSE" },
02554 { V_NULL, "NULL" },
02555
02556
02557 { T_SFINT32, "SFInt32" },
02558 { T_MFINT32, "MFInt32" },
02559 { T_SFFLOAT, "SFFloat" },
02560 { T_MFFLOAT, "MFFloat" },
02561 { T_SFVEC2F, "SFVec2f" },
02562 { T_MFVEC2F, "MFVec2f" },
02563 { T_SFVEC3F, "SFVec3f" },
02564 { T_MFVEC3F, "MFVec3f" },
02565 { T_SFROTATION, "SFRotation" },
02566 { T_MFROTATION, "MFRotation" },
02567 { T_SFTIME, "SFTime" },
02568 { T_MFTIME, "MFTime" },
02569 { T_SFCOLOR, "SFColor" },
02570 { T_MFCOLOR, "MFColor" },
02571 { T_SFSTRING, "SFString" },
02572 { T_MFSTRING, "MFString" },
02573 { T_SFNODE, "SFNode" },
02574 { T_MFNODE, "MFNode" },
02575 { T_SFBOOL, "SFBool" },
02576 { T_SFIMAGE, "SFImage" },
02577
02578
02579 { N_PROTO, "PROTO" },
02580 { N_INLINE, "Inline" },
02581 { N_BACKGROUND, "Background" },
02582 { N_NAVIGATION_INFO, "NavigationInfo" },
02583 { N_VIEWPOINT, "Viewpoint" },
02584 { N_GROUP, "Group" },
02585 { N_TRANSFORM, "Transform" },
02586 { N_SHAPE, "Shape" },
02587 { N_APPEARANCE, "Appearance" },
02588 { N_MATERIAL, "Material" },
02589 { N_IMAGE_TEXTURE, "ImageTexture" },
02590 { N_TEXTURE_TRANSFORM, "TextureTransform" },
02591 { N_BOX, "Box" },
02592 { N_CONE, "Cone" },
02593 { N_CYLINDER, "Cylinder" },
02594 { N_SPHERE, "Sphere" },
02595 { N_TEXT, "Text" },
02596 { N_FONT_STYLE, "FontStyle" },
02597 { N_INDEXED_LINE_SET, "IndexedLineSet" },
02598 { N_INDEXED_FACE_SET, "IndexedFaceSet" },
02599 { N_COLOR, "Color" },
02600 { N_COORDINATE, "Coordinate" },
02601 { N_TEXTURE_COORDINATE, "TextureCoordinate" },
02602 { N_NORMAL, "Normal" },
02603 { N_CYLINDER_SENSOR, "CylinderSensor" },
02604
02605 { N_POINTSET, "PointSet" },
02606 { N_PIXEL_TEXTURE,"PixelTexture" },
02607 { N_MOVIE_TEXTURE, "MovieTexture" },
02608 { N_ELEVATION_GRID, "ElevationGrid" },
02609 { N_EXTRUSION, "Extrusion" },
02610 { N_SWITCH, "Switch" },
02611 { N_LOD,"LOD" },
02612 { N_COLLISION, "Collision" },
02613 { N_ANCHOR, "Anchor" },
02614 { N_FOG, "Fog" },
02615 { N_BILLBOARD, "Billboard" },
02616 { N_WORLD_INFO, "WorldInfo" },
02617 { N_POINT_LIGHT, "PointLight" },
02618 { N_DIRECTIONAL_LIGHT, "DirectionalLight" },
02619 { N_SPOT_LIGHT, "SpotLight" },
02620
02621
02622 { N_AUDIO_CLIP, "AudioClip" },
02623 { N_SOUND, "Sound" },
02624 { N_COLOR_INTERPOLATOR, "ColorInterpolator" },
02625 { N_COORDINATE_INTERPOLATOR, "CoordinateInterpolator" },
02626 { N_ORIENTATION_INTERPOLATOR, "OrientationInterpolator" },
02627 { N_NORMAL_INTERPOLATOR, "NormalInterpolator" },
02628 { N_POSITION_INTERPOLATOR, "PositionInterpolator" },
02629 { N_SCALAR_INTERPOLATOR, "ScalarInterpolator" },
02630 { N_PLANE_SENSOR, "PlaneSensor" },
02631 { N_PROXIMITY_SENSOR, "ProximitySensor" },
02632 { N_SPHERE_SENSOR, "SphereSensor" },
02633 { N_TIME_SENSOR, "TimeSensor" },
02634 { N_TOUCH_SENSOR, "TouchSensor" },
02635 { N_VISIBILITY_SENSOR, "VisibilitySensor" },
02636
02637
02638 { F_IS, "IS" },
02639
02640 { F_URL, "url" },
02641
02642 { F_GROUND_ANGLE, "groundAngle" },
02643 { F_GROUND_COLOR, "groundColor" },
02644 { F_SKY_ANGLE, "skyAngle" },
02645 { F_SKY_COLOR, "skyColor" },
02646 { F_BACK_URL, "backUrl" },
02647 { F_BOTTOM_URL, "bottomUrl" },
02648 { F_FRONT_URL, "frontUrl" },
02649 { F_LEFT_URL, "leftUrl" },
02650 { F_RIGHT_URL, "rightUrl" },
02651 { F_TOP_URL, "topUrl" },
02652
02653 { F_AVATAR_SIZE, "avatarSize" },
02654 { F_HEADLIGHT, "headlight" },
02655 { F_SPEED, "speed" },
02656 { F_TYPE, "type" },
02657 { F_VISIBILITY_LIMIT, "visibilityLimit" },
02658
02659 { F_FIELD_OF_VIEW, "fieldOfView" },
02660 { F_JUMP, "jump" },
02661 { F_ORIENTATION, "orientation" },
02662 { F_POSITION, "position" },
02663 { F_DESCRIPTION, "description" },
02664
02665 { F_CHILDREN, "children" },
02666 { F_ADD_CHILDREN, "addChildren" },
02667 { F_REMOVE_CHILDREN, "removeChildren" },
02668 { F_BBOX_CENTER, "bboxCenter" },
02669 { F_BBOX_SIZE, "bboxSize" },
02670
02671 { F_CENTER, "center" },
02672 { F_ROTATION, "rotation" },
02673 { F_SCALE, "scale" },
02674 { F_SCALE_ORIENTATION, "scaleOrientation" },
02675 { F_TRANSLATION, "translation" },
02676
02677 { F_APPEARANCE, "appearance" },
02678 { F_GEOMETRY, "geometry" },
02679
02680 { F_MATERIAL, "material" },
02681 { F_TEXTURE, "texture" },
02682 { F_TEXTURE_TRANSFORM, "textureTransform" },
02683
02684 { F_AMBIENT_INTENSITY, "ambientIntensity" },
02685 { F_DIFFUSE_COLOR, "diffuseColor" },
02686 { F_EMISSIVE_COLOR, "emissiveColor" },
02687 { F_SHININESS, "shininess" },
02688 { F_SPECULAR_COLOR, "specularColor" },
02689 { F_TRANSPARANCY, "transparency" },
02690 { F_DIRECTION, "direction" },
02691
02692 { F_REPEAT_S, "repeatS" },
02693 { F_REPEAT_T, "repeatT" },
02694
02695 { F_SIZE, "size" },
02696
02697 { F_BOTTOM, "bottom" },
02698 { F_BOTTOM_RADIUS, "bottomRadius" },
02699 { F_HEIGHT, "height" },
02700 { F_SIDE, "side" },
02701
02702 { F_RADIUS, "radius" },
02703 { F_TOP, "top" },
02704
02705 { F_STRING, "string" },
02706 { F_FONT_STYLE, "fontStyle" },
02707 { F_LENGTH, "length" },
02708 { F_MAX_EXTENT, "maxExtent" },
02709
02710 { F_FAMILY, "family" },
02711 { F_HORIZONTAL, "horizontal" },
02712 { F_JUSTIFY, "justify" },
02713 { F_LANGUAGE, "language" },
02714 { F_LEFT_TO_RIGHT, "leftToRight" },
02715 { F_SPACING, "spacing" },
02716 { F_STYLE, "style" },
02717 { F_TOP_TO_BOTTOM, "topToBottom" },
02718
02719 { F_COLOR, "color" },
02720 { F_COORD, "coord" },
02721 { F_COLOR_INDEX, "colorIndex" },
02722 { F_COLOR_PER_VERTEX, "colorPerVertex" },
02723 { F_COORD_INDEX, "coordIndex" },
02724
02725 { F_CCW, "ccw" },
02726 { F_CONVEX, "convex" },
02727 { F_SOLID, "solid" },
02728 { F_CREASE_ANGLE, "creaseAngle" },
02729 { F_NORMAL_INDEX, "normalIndex" },
02730 { F_NORMAL, "normal" },
02731 { F_NORMAL_PER_VERTEX, "normalPerVertex" },
02732 { F_TEX_COORD_INDEX, "texCoordIndex" },
02733 { F_TEX_COORD, "texCoord" },
02734
02735 { F_POINT, "point" },
02736 { F_VECTOR, "vector" },
02737
02738 { F_AUTO_OFFSET, "autoOffset" },
02739 { F_DISK_ANGLE, "diskAngle" },
02740 { F_ENABLED, "enabled" },
02741 { F_MAX_ANGLE, "maxAngle" },
02742 { F_MIN_ANGLE, "minAngle" },
02743 { F_OFFSET, "offset" },
02744
02745 { F_IMAGE, "image" },
02746
02747 { F_X_DIMENSION, "xDimension" },
02748 { F_Z_DIMENSION, "zDimension" },
02749 { F_X_SPACING, "xSpacing" },
02750 { F_Z_SPACING, "zSpacing" },
02751
02752 { F_CROSS_SECTION, "crossSection" },
02753 { F_SPINE, "spine" },
02754 { F_BEGIN_CAP, "beginCap" },
02755 { F_END_CAP, "endCap" },
02756
02757 { F_CHOICE, "choice" },
02758 { F_WHICH_CHOICE, "whichChoice" },
02759
02760 { F_COLLIDE, "collide" },
02761 { F_PROXY, "proxy" },
02762
02763 { F_PARAMETER, "parameter" },
02764
02765 { F_VISIBILITY_RANGE, "visibilityRange" },
02766 { F_FOG_TYPE, "fogType" },
02767
02768 { F_AXIS_OF_ROTATION, "axisOfRotation" },
02769
02770 { F_TITLE, "title" },
02771 { F_INFO, "info" },
02772
02773 { F_LOCATION, "location" },
02774 { F_ON, "on" },
02775 { F_INTENSITY, "intensity" },
02776 { F_ATTENUATION, "attenuation" },
02777 { F_DIRECTION, "direction" },
02778 { F_BEAM_WIDTH, "beamWidth" },
02779 { F_CUT_OFF_RANGE, "cutOffAngle" },
02780
02781
02782 { E_FIELD, "field" },
02783 { E_EXPOSED_FIELD, "exposedField" },
02784 { E_EVENTIN, "eventIn" },
02785 { E_EVENTOUT, "eventOut" },
02786
02787
02788 { D_DEF, "DEF" },
02789 { D_USE, "USE" },
02790 { D_ROUTE, "ROUTE" },
02791
02792
02793 { U_SCRIPT, "Script" },
02794 { U_EXTERNPROTO, "EXTERNPROTO" },
02795
02796 { 0, "" }
02797 };
02798
02799 for(int i=0; symbols[i].id != 0; i++){
02800 scanner->registerSymbol(symbols[i].id, symbols[i].symbol);
02801 }
02802 }