00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "OSGConfig.h"
00047
00048 #include <iostream>
00049
00050 #include "OSGLoader.h"
00051 #include <OSGLog.h>
00052
00053
00054 #include <OSGSFSysTypes.h>
00055 #include <OSGMFSysTypes.h>
00056
00057 #include <OSGSFBaseTypes.h>
00058 #include <OSGMFBaseTypes.h>
00059
00060 #include <OSGSFMathTypes.h>
00061 #include <OSGMFMathTypes.h>
00062
00063 #include <OSGSFVecTypes.h>
00064 #include <OSGMFVecTypes.h>
00065
00066 #include <OSGMFFieldContainerTypes.h>
00067 #include <OSGSFFieldContainerTypes.h>
00068
00069 #include <OSGFieldContainerFactory.h>
00070 #include <OSGFieldContainer.h>
00071
00072 #include <OSGGeoProperty.h>
00073 #include <OSGMaterialFields.h>
00074 #include <OSGNode.h>
00075 #include <OSGGroup.h>
00076 #include <OSGImageFields.h>
00077 #include <OSGImageFileHandler.h>
00078
00079 #include <OSGSimpleAttachments.h>
00080
00081 #include "OSGViewport.h"
00082 #include "OSGCamera.h"
00083 #include "OSGWindow.h"
00084 #include "OSGPerspectiveCamera.h"
00085 #include "OSGBackground.h"
00086
00087 OSG_USING_NAMESPACE
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 void OSGLoader::initFieldTypeMapper(void)
00130 {
00131 setIntExtMapping(SFBool::getClassType().getId(),
00132 ScanParseSkel::OSGsfBool);
00133
00134
00135
00136
00137
00138
00139 setIntExtMapping(SFReal32::getClassType().getId(),
00140 ScanParseSkel::OSGsfFloat);
00141
00142
00143
00144
00145 setIntExtMapping(SFInt32::getClassType().getId(),
00146 ScanParseSkel::OSGsfInt32);
00147
00148
00149 setIntExtMapping(SFQuaternion::getClassType().getId(),
00150 ScanParseSkel::OSGsfRotation);
00151
00152 setIntExtMapping(SFString::getClassType().getId(),
00153 ScanParseSkel::OSGsfString);
00154
00155 setIntExtMapping(SFTime::getClassType().getId(),
00156 ScanParseSkel::OSGsfTime);
00157
00158 setIntExtMapping(SFVec2f::getClassType().getId(),
00159 ScanParseSkel::OSGsfVec2f);
00160
00161 setIntExtMapping(SFVec3f::getClassType().getId(),
00162 ScanParseSkel::OSGsfVec3f);
00163
00164 setIntExtMapping(SFVec4f::getClassType().getId(),
00165 ScanParseSkel::OSGsfVec4f);
00166
00167 setIntExtMapping(SFColor4f::getClassType().getId(),
00168 ScanParseSkel::OSGsfColor4f);
00169
00170 setIntExtMapping(SFColor4ub::getClassType().getId(),
00171 ScanParseSkel::OSGsfColor4i);
00172
00173 setIntExtMapping(SFColor3f::getClassType().getId(),
00174 ScanParseSkel::OSGsfColor3f);
00175
00176
00177
00178
00179
00180
00181 setIntExtMapping(MFReal32::getClassType().getId(),
00182 ScanParseSkel::OSGmfFloat);
00183
00184 setIntExtMapping(MFInt32::getClassType().getId(),
00185 ScanParseSkel::OSGmfInt32);
00186
00187 setIntExtMapping(MFQuaternion::getClassType().getId(),
00188 ScanParseSkel::OSGmfRotation);
00189
00190 setIntExtMapping(MFString::getClassType().getId(),
00191 ScanParseSkel::OSGmfString);
00192
00193 setIntExtMapping(MFTime::getClassType().getId(),
00194 ScanParseSkel::OSGmfTime);
00195
00196
00197 setIntExtMapping(MFVec2f::getClassType().getId(),
00198 ScanParseSkel::OSGmfVec2f);
00199
00200 setIntExtMapping(MFVec3f::getClassType().getId(),
00201 ScanParseSkel::OSGmfVec3f);
00202
00203 setIntExtMapping(MFVec4f::getClassType().getId(),
00204 ScanParseSkel::OSGmfVec4f);
00205
00206 setIntExtMapping(MFColor4f::getClassType().getId(),
00207 ScanParseSkel::OSGmfColor4f);
00208
00209 setIntExtMapping(MFColor4ub::getClassType().getId(),
00210 ScanParseSkel::OSGmfColor4i);
00211
00212 setIntExtMapping(MFColor3f::getClassType().getId(),
00213 ScanParseSkel::OSGmfColor3f);
00214
00215 setIntExtMapping(SFNodePtr::getClassType().getId(),
00216 ScanParseSkel::OSGsfNode);
00217 setIntExtMapping(MFNodePtr::getClassType().getId(),
00218 ScanParseSkel::OSGmfNode);
00219
00220
00221
00222
00223 setIntExtMapping(SFUInt8::getClassType().getId(),
00224 ScanParseSkel::OSGsfInt32);
00225 setIntExtMapping(MFUInt8::getClassType().getId(),
00226 ScanParseSkel::OSGmfInt32);
00227
00228 setIntExtMapping(SFInt8::getClassType().getId(),
00229 ScanParseSkel::OSGsfInt32);
00230 setIntExtMapping(MFInt8::getClassType().getId(),
00231 ScanParseSkel::OSGmfInt32);
00232
00233
00234 setIntExtMapping(SFUInt16::getClassType().getId(),
00235 ScanParseSkel::OSGsfInt32);
00236 setIntExtMapping(MFUInt16::getClassType().getId(),
00237 ScanParseSkel::OSGmfInt32);
00238
00239 setIntExtMapping(SFInt16::getClassType().getId(),
00240 ScanParseSkel::OSGsfInt32);
00241 setIntExtMapping(MFInt16::getClassType().getId(),
00242 ScanParseSkel::OSGmfInt32);
00243
00244
00245 setIntExtMapping(SFUInt32::getClassType().getId(),
00246 ScanParseSkel::OSGsfInt32);
00247 setIntExtMapping(MFUInt32::getClassType().getId(),
00248 ScanParseSkel::OSGmfInt32);
00249
00250 setIntExtMapping(SFGLenum::getClassType().getId(),
00251 ScanParseSkel::OSGsfInt32);
00252 setIntExtMapping(SFGLenum::getClassType().getId(),
00253 ScanParseSkel::OSGmfInt32);
00254
00255
00256
00257
00258 setIntExtMapping(SFAttachmentMap::getClassType().getId(),
00259 ScanParseSkel::OSGmfNode);
00260
00261
00262
00263 setIntExtMapping(SFMatrix::getClassType().getId(),
00264 ScanParseSkel::OSGsfMatrix);
00265
00266 setIntExtMapping(SFPnt3f::getClassType().getId(),
00267 ScanParseSkel::OSGsfPnt3f);
00268
00269 setIntExtMapping(MFPnt3f::getClassType().getId(),
00270 ScanParseSkel::OSGmfPnt3f);
00271 setIntExtMapping(SFDynamicVolume::getClassType().getId(),
00272 ScanParseSkel::OSGmfFloat);
00273
00274 setIntExtMapping(SFPlane::getClassType().getId(),
00275 ScanParseSkel::OSGsfPlane);
00276 setIntExtMapping(MFPlane::getClassType().getId(),
00277 ScanParseSkel::OSGmfPlane);
00278
00279 setIntExtMapping(SFDynamicVolume::getClassType().getId(),
00280 ScanParseSkel::OSGsfVolume);
00281 }
00282
00283 void OSGLoader::setFieldContainerValue(FieldContainerPtr pNewNode)
00284 {
00285
00286
00287
00288 if(_pCurrentField != NULL && _pCurrentFC != NullFC && pNewNode != NullFC)
00289 {
00290 if(_pCurrentField == _pCurrentFC->getField("core"))
00291 {
00292 NodePtr pNode = NodePtr ::dcast(_pCurrentFC);
00293 NodeCorePtr pCore = NodeCorePtr::dcast( pNewNode);
00294
00295 pNode->setCore(pCore);
00296 }
00297 else if(_pCurrentField == _pCurrentFC->getField("children"))
00298 {
00299 NodePtr pNode = NodePtr::dcast(_pCurrentFC);
00300 NodePtr pChildNode;
00301 if(pNewNode->getType().isNode())
00302 {
00303 pChildNode=NodePtr::dcast(pNewNode);
00304 }
00305 else if(pNewNode->getType().isNodeCore())
00306 {
00307 pChildNode=Node::create();
00308 pChildNode->setCore(NodeCorePtr::dcast(pNewNode));
00309 }
00310 else
00311 {
00312 SLOG << "Fieldcontainer " << pNewNode->getType().getName()
00313 << "is neither Node nor NodeCore " << std::endl;
00314 }
00315 pNode->addChild(pChildNode);
00316 }
00317 else if(_pCurrentField == _pCurrentFC->getField("attachments"))
00318 {
00319 if(pNewNode->getType().isAttachment() == true)
00320 {
00321 AttachmentContainerPtr pAttContainer =
00322 AttachmentContainerPtr::dcast(_pCurrentFC);
00323
00324 if(pAttContainer != NullFC)
00325 {
00326 AttachmentPtr pAtt = AttachmentPtr::dcast(pNewNode);
00327
00328 pAttContainer->addAttachment(pAtt);
00329 }
00330 else
00331 {
00332 SLOG << "FieldContainer "
00333 << _pCurrentFC->getType().getName()
00334 << "is no attachment container"
00335 << std::endl;
00336 }
00337 }
00338 else
00339 {
00340 SLOG << "FieldContainer : " << pNewNode->getType().getName()
00341 << " is no attachment" << std::endl;
00342 }
00343 }
00344 else if(_pCurrentField->getCardinality() == FieldType::SINGLE_FIELD)
00345 {
00346 ((SFFieldContainerPtr *) _pCurrentField)->setValue(pNewNode);
00347 }
00348 else if(_pCurrentField->getCardinality() == FieldType::MULTI_FIELD)
00349 {
00350 ((MFFieldContainerPtr *) _pCurrentField)->push_back(pNewNode);
00351 }
00352 }
00353 }
00354
00355
00356
00357
00358
00359
00360
00364 OSGLoader::OSGLoader(void) :
00365 Inherited(),
00366 _pCurrentFC (NullFC),
00367 _pRootNode (NullFC),
00368 _pCurrentField(NULL),
00369 _fcStack (),
00370 _fStack (),
00371 _fdStack ()
00372 {
00373 Self::setReferenceHeader("#OSG V1.0 ");
00374 initFieldTypeMapper();
00375 }
00376
00380 OSGLoader::~OSGLoader(void)
00381 {
00382 }
00383
00384
00385
00386 void OSGLoader::scanStream(std::istream &is)
00387 {
00388 if(is)
00389 {
00390 _pRootNode = NullFC;
00391 _pCurrentFC = NullFC;
00392 _pCurrentField = NULL;
00393 _pCurrentFieldDesc = NULL;
00394 _defMap.clear();
00395
00396 Inherited::scanStream(is);
00397 }
00398 }
00399
00400 #if defined(OSG_WIN32_ICL)
00401 #pragma warning (disable : 383)
00402 #endif
00403
00404 void OSGLoader::beginNode(const Char8 *szNodeTypename,
00405 const Char8 *szNodename)
00406 {
00407 FieldContainerPtr pNewNode;
00408
00409 if(szNodeTypename == NULL)
00410 return;
00411
00412 PINFO << "Begin node : " << szNodeTypename << " | ";
00413
00414 if(szNodename == NULL)
00415 {
00416 PINFO << "NULL";
00417 }
00418 else
00419 {
00420 PINFO << szNodename;
00421 }
00422
00423 PINFO << std::endl;
00424
00425 pNewNode =
00426 FieldContainerFactory::the()->createFieldContainer(szNodeTypename);
00427
00428 if(szNodename != NULL && pNewNode != NullFC)
00429 {
00430 AttachmentContainerPtr pAttCon =
00431 AttachmentContainerPtr::dcast(pNewNode);
00432
00433 if(pAttCon != NullFC)
00434 {
00435 setName(pAttCon, szNodename);
00436 }
00437
00438 _defMap.insert(std::make_pair(std::string(szNodename), pNewNode));
00439 }
00440
00441
00442 setFieldContainerValue(pNewNode);
00443
00444 _pCurrentFC = pNewNode;
00445
00446 _fcStack.push(_pCurrentFC);
00447
00448 if(_fcStack.size() == 1)
00449 {
00450 NodePtr pNode;
00451 if(_pCurrentFC->getType().isNode())
00452 {
00453 pNode = NodePtr::dcast(_pCurrentFC);
00454 }
00455 else if(_pCurrentFC->getType().isNodeCore())
00456 {
00457 pNode = Node::create();
00458 pNode->setCore(NodeCorePtr::dcast(_pCurrentFC));
00459 }
00460 else
00461 {
00462 SLOG << "Fieldcontainer " << szNodeTypename
00463 << "is neither Node nor NodeCore " << std::endl;
00464 }
00465
00466 if(_pRootNode == NullFC)
00467 {
00468 GroupPtr pGroup = Group::create();
00469
00470 _pRootNode = Node::create();
00471
00472 beginEditCP(_pRootNode, Node::CoreFieldMask);
00473 {
00474 _pRootNode->setCore(pGroup);
00475 }
00476 endEditCP (_pRootNode, Node::CoreFieldMask);
00477 }
00478
00479 beginEditCP(_pRootNode, Node::ChildrenFieldMask);
00480 {
00481 _pRootNode->addChild(pNode);
00482 }
00483 endEditCP (_pRootNode, Node::ChildrenFieldMask);
00484 }
00485 }
00486
00487 #if defined(OSG_WIN32_ICL)
00488 #pragma warning (default : 383)
00489 #endif
00490
00491 void OSGLoader::endNode(void)
00492 {
00493 PINFO << "End Node" << std::endl;
00494
00495 if(_pCurrentFC != NullFC)
00496 {
00497 if(_pCurrentFC->getType().isNode() == true)
00498 {
00499 NodePtr pNode = NodePtr::dcast(_pCurrentFC);
00500
00501 if(pNode->getCore() == NullFC)
00502 {
00503 GroupPtr pGroup = Group::create();
00504
00505 pNode->setCore(pGroup);
00506 }
00507 }
00508 }
00509
00510 _fcStack.pop();
00511
00512 if(_fcStack.size() != 0)
00513 {
00514 _pCurrentFC = _fcStack.top();
00515 }
00516 else
00517 {
00518 _pCurrentFC = NullFC;
00519 }
00520 }
00521
00522 void OSGLoader::use(const Char8 *szName)
00523 {
00524 FieldContainerPtr pUseNode;
00525
00526
00527
00528 pUseNode = getReference(szName);
00529
00530 if(pUseNode == NullFC)
00531 {
00532 SLOG << "No FieldContainer found with name " << szName << std::endl;
00533 }
00534 else
00535 {
00536
00537 setFieldContainerValue(pUseNode);
00538 }
00539 }
00540
00541 NodePtr OSGLoader::getRootNode(void)
00542 {
00543 return _pRootNode;
00544 }
00545
00546 std::vector<FieldContainerPtr> OSGLoader::getRootNodes(void)
00547 {
00548 std::vector<FieldContainerPtr> fcVec;
00549
00550 for( UInt32 i=0; i<_pRootNode->getNChildren(); ++i )
00551 {
00552 fcVec.push_back( _pRootNode->getChild(i) );
00553 }
00554 return fcVec;
00555 }
00556
00557 void OSGLoader::addFieldValue(const Char8 *szFieldVal)
00558 {
00559 PINFO << "\t\tFV : " << szFieldVal << std::endl;
00560
00561 if(_pCurrentField != NULL)
00562 {
00563 _pCurrentField->pushValueByStr(szFieldVal);
00564 }
00565 }
00566
00567 UInt32 OSGLoader::getFieldType(const Char8 *szFieldname)
00568 {
00569 UInt32 returnValue = 0;
00570 Field *pField = NULL;
00571
00572 if(szFieldname == NULL)
00573 return returnValue;
00574
00575 if(_pCurrentFC != NullFC)
00576 {
00577 pField = _pCurrentFC->getField(szFieldname);
00578
00579 PINFO << "GF : " << szFieldname << " " << pField << std::endl;
00580
00581 if(pField != NULL)
00582 {
00583 returnValue = pField->getType().getScanTypeId();
00584 }
00585 }
00586
00587 return returnValue;
00588 }
00589
00590 Int32 OSGLoader::mapExtIntFieldType(const Char8 *szFieldname,
00591 const Int32 iFieldTypeId)
00592 {
00593 Int32 returnValue = Inherited::mapExtIntFieldType(szFieldname,
00594 iFieldTypeId);
00595
00596 if(returnValue < 0 && szFieldname != NULL && _pCurrentFC != NullFC)
00597 {
00598 Field *pField = _pCurrentFC->getField(szFieldname);
00599
00600 if(pField != NULL)
00601 {
00602 const FieldType &oFieldType = pField->getType();
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612 PINFO << "FieldTypeId invalid, trying to fix. " << std::endl;
00613 PINFO << oFieldType.getContentType().getCName()
00614 << " comparing with "
00615 << FieldDataTraits<FieldContainerPtr>::getType().getCName()
00616 << std::endl;
00617
00618
00619 if(strstr(oFieldType.getContentType().getCName(),
00620 "Ptr" ) != NULL)
00621 {
00622 PINFO << "FieldContainerPtr or derived class, "
00623 << "parsing as Node"
00624 << std::endl;
00625
00626 if(oFieldType.getCardinality() == FieldType::SINGLE_FIELD)
00627 {
00628 returnValue = ScanParseSkel::OSGsfNode;
00629 }
00630 else
00631 {
00632 returnValue = ScanParseSkel::OSGmfNode;
00633 }
00634 }
00635 }
00636 }
00637
00638 return returnValue;
00639 }
00640
00641 void OSGLoader::beginField(const Char8 *szFieldname,
00642 const UInt32 )
00643 {
00644 PINFO << "BeginField "
00645 << szFieldname
00646 << " "
00647 << _pCurrentField
00648 << std::endl;
00649
00650 if(szFieldname == NULL)
00651 return;
00652
00653 _pCurrentField = NULL;
00654
00655 if(_pCurrentFC != NullFC)
00656 {
00657 _pCurrentField = _pCurrentFC->getField(szFieldname);
00658
00659 _pCurrentFieldDesc =
00660 _pCurrentFC->getType().findFieldDescription(szFieldname);
00661
00662 PINFO << "BF : "
00663 << szFieldname << " "
00664 << _pCurrentField << " "
00665 << _pCurrentFieldDesc << std::endl;
00666
00667 if(_pCurrentFieldDesc != NULL)
00668 {
00669 beginEditCP(_pCurrentFC,
00670 _pCurrentFieldDesc->getFieldMask(),
00671 ChangedOrigin::Abstract |
00672 ChangedOrigin::AbstrIgnoreCore |
00673 ChangedOrigin::AbstrIgnoreChild );
00674 }
00675 }
00676
00677 _fStack.push (_pCurrentField);
00678 _fdStack.push(_pCurrentFieldDesc);
00679 }
00680
00681 void OSGLoader::endField(void)
00682 {
00683 PINFO << "End Field" << std::endl;
00684
00685 _fStack.pop();
00686
00687 if(_fStack.size() != 0)
00688 {
00689 _pCurrentField = _fStack.top();
00690 }
00691 else
00692 {
00693 _pCurrentField = NULL;
00694 }
00695
00696
00697 if(_pCurrentFC != NullFC && _pCurrentFieldDesc != NULL)
00698 {
00699 endEditCP(_pCurrentFC,
00700 _pCurrentFieldDesc->getFieldMask(),
00701 ChangedOrigin::Abstract |
00702 ChangedOrigin::AbstrIgnoreCore |
00703 ChangedOrigin::AbstrIgnoreChild );
00704 }
00705
00706 _fdStack.pop();
00707
00708 if(_fdStack.size() != 0)
00709 {
00710 _pCurrentFieldDesc = _fdStack.top();
00711 }
00712 else
00713 {
00714 _pCurrentFieldDesc = NULL;
00715 }
00716 }
00717
00718 FieldContainerPtr OSGLoader::getReference(const Char8 *szName)
00719 {
00720
00721
00722 NamedFCMap::iterator entry = _defMap.find(std::string(szName));
00723
00724 if(entry == _defMap.end())
00725 return NullFC;
00726
00727 return entry->second;
00728 }
00729
00730
00731
00732
00733
00734 #ifdef __sgi
00735 #pragma set woff 1174
00736 #endif
00737
00738 #ifdef OSG_LINUX_ICC
00739 #pragma warning( disable : 177 )
00740 #endif
00741
00742 namespace
00743 {
00744 static Char8 cvsid_cpp[] = "@(#)$Id: $";
00745 static Char8 cvsid_hpp[] = OSGLOADER_HEADER_CVSID;
00746 }