OSGOSGLoader.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #include <cstdlib>
00044 #include <cstdio>
00045 
00046 #include "OSGConfig.h"
00047 
00048 #include <iostream>
00049 
00050 #include "OSGOSGLoader.h"
00051 #include <OSGLog.h>
00052 
00053 
00054 #include <OSGSysSFields.h>
00055 #include <OSGSysMFields.h>
00056 
00057 #include <OSGBaseSFields.h>
00058 #include <OSGBaseMFields.h>
00059 
00060 #include <OSGMathSFields.h>
00061 #include <OSGMathMFields.h>
00062 
00063 #include <OSGVecSFields.h>
00064 #include <OSGVecMFields.h>
00065 
00066 #include <OSGFieldContainerSFields.h>
00067 #include <OSGFieldContainerMFields.h>
00068 
00069 #include <OSGFieldContainerFactory.h>
00070 #include <OSGFieldContainer.h>
00071 
00072 #include <OSGNode.h>
00073 #include <OSGGroup.h>
00074 
00075 #include <OSGAttachmentMapSFields.h>
00076 
00077 //#include <OSGSimpleAttachments.h>
00078 
00079 
00080 
00081 OSG_USING_NAMESPACE
00082 
00083 //---------------------------------------------------------------------------
00084 //  Class
00085 //---------------------------------------------------------------------------
00086 
00087 /***************************************************************************\
00088  *                               Types                                     *
00089 \***************************************************************************/
00090 
00091 /***************************************************************************\
00092  *                           Class variables                               *
00093 \***************************************************************************/
00094 
00095 /***************************************************************************\
00096  *                           Class methods                                 *
00097 \***************************************************************************/
00098 
00099 /*-------------------------------------------------------------------------*\
00100  -  private                                                                -
00101 \*-------------------------------------------------------------------------*/
00102 
00103 /*-------------------------------------------------------------------------*\
00104  -  protected                                                              -
00105 \*-------------------------------------------------------------------------*/
00106 
00107 /*-------------------------------------------------------------------------*\
00108  -  public                                                                 -
00109 \*-------------------------------------------------------------------------*/
00110 
00111 /***************************************************************************\
00112  *                           Instance methods                              *
00113 \***************************************************************************/
00114 
00115 /*-------------------------------------------------------------------------*\
00116  -  private                                                                -
00117 \*-------------------------------------------------------------------------*/
00118 
00119 /*-------------------------------------------------------------------------*\
00120  -  protected                                                              -
00121 \*-------------------------------------------------------------------------*/
00122 
00123 void OSGLoader::initFieldTypeMapper(void)
00124 {
00125     setIntExtMapping(SFBool::getClassType().getId(),
00126                      ScanParseSkel::OSGsfBool);
00127 
00128 /*
00129     setIntExtMapping(ScanParseSkel::OSGsfColor,
00130                ::getClassType().getId());
00131 */
00132 
00133     setIntExtMapping(SFReal32::getClassType().getId(),
00134                      ScanParseSkel::OSGsfFloat);
00135 /*
00136     setIntExtMapping(SFImagePtr::getClassType().getId(),
00137                      ScanParseSkel::OSGsfString);
00138 */
00139     setIntExtMapping(SFInt32::getClassType().getId(),
00140                      ScanParseSkel::OSGsfInt32);
00141 
00142 
00143     setIntExtMapping(SFQuaternion::getClassType().getId(),
00144                      ScanParseSkel::OSGsfRotation);
00145 
00146     setIntExtMapping(SFString::getClassType().getId(),
00147                      ScanParseSkel::OSGsfString);
00148 
00149     setIntExtMapping(SFTime::getClassType().getId(),
00150                      ScanParseSkel::OSGsfTime);
00151 
00152     setIntExtMapping(SFVec2f::getClassType().getId(),
00153                      ScanParseSkel::OSGsfVec2f);
00154 
00155     setIntExtMapping(SFVec3f::getClassType().getId(),
00156                      ScanParseSkel::OSGsfVec3f);
00157 
00158     setIntExtMapping(SFVec4f::getClassType().getId(),
00159                      ScanParseSkel::OSGsfVec4f);
00160 
00161     setIntExtMapping(SFColor4f::getClassType().getId(),
00162                      ScanParseSkel::OSGsfColor4f);
00163 
00164     setIntExtMapping(SFColor4ub::getClassType().getId(),
00165                      ScanParseSkel::OSGsfColor4i);
00166 
00167     setIntExtMapping(SFColor3f::getClassType().getId(),
00168                      ScanParseSkel::OSGsfColor3f);
00169 
00170 /*
00171     setIntExtMapping(ScanParseSkel::OSGmfColor,
00172                ::getClassType().getId());
00173 */
00174 
00175     setIntExtMapping(MFReal32::getClassType().getId(),
00176                      ScanParseSkel::OSGmfFloat);
00177 
00178     setIntExtMapping(MFInt32::getClassType().getId(),
00179                      ScanParseSkel::OSGmfInt32);
00180 
00181     setIntExtMapping(MFQuaternion::getClassType().getId(),
00182                      ScanParseSkel::OSGmfRotation);
00183 
00184     setIntExtMapping(MFString::getClassType().getId(),
00185                      ScanParseSkel::OSGmfString);
00186 
00187     setIntExtMapping(MFTime::getClassType().getId(),
00188                      ScanParseSkel::OSGmfTime);
00189 
00190 
00191     setIntExtMapping(MFVec2f::getClassType().getId(),
00192                      ScanParseSkel::OSGmfVec2f);
00193 
00194     setIntExtMapping(MFVec3f::getClassType().getId(),
00195                      ScanParseSkel::OSGmfVec3f);
00196 
00197     setIntExtMapping(MFVec4f::getClassType().getId(),
00198                      ScanParseSkel::OSGmfVec4f);
00199 
00200     setIntExtMapping(MFColor4f::getClassType().getId(),
00201                      ScanParseSkel::OSGmfColor4f);
00202 
00203     setIntExtMapping(MFColor4ub::getClassType().getId(),
00204                      ScanParseSkel::OSGmfColor4i);
00205 
00206     setIntExtMapping(MFColor3f::getClassType().getId(),
00207                      ScanParseSkel::OSGmfColor3f);
00208 
00209     setIntExtMapping(SFNodePtr::getClassType().getId(),
00210                      ScanParseSkel::OSGsfNode);
00211     setIntExtMapping(MFNodePtr::getClassType().getId(),
00212                      ScanParseSkel::OSGmfNode);
00213 
00214 
00215     /* To Base Type Mappings */
00216 
00217     setIntExtMapping(SFUInt8::getClassType().getId(),
00218                      ScanParseSkel::OSGsfInt32);
00219     setIntExtMapping(MFUInt8::getClassType().getId(),
00220                      ScanParseSkel::OSGmfInt32);
00221 
00222     setIntExtMapping(SFInt8::getClassType().getId(),
00223                      ScanParseSkel::OSGsfInt32);
00224     setIntExtMapping(MFInt8::getClassType().getId(),
00225                      ScanParseSkel::OSGmfInt32);
00226 
00227 
00228     setIntExtMapping(SFUInt16::getClassType().getId(),
00229                      ScanParseSkel::OSGsfInt32);
00230     setIntExtMapping(MFUInt16::getClassType().getId(),
00231                      ScanParseSkel::OSGmfInt32);
00232 
00233     setIntExtMapping(SFInt16::getClassType().getId(),
00234                      ScanParseSkel::OSGsfInt32);
00235     setIntExtMapping(MFInt16::getClassType().getId(),
00236                      ScanParseSkel::OSGmfInt32);
00237 
00238 
00239     setIntExtMapping(SFUInt32::getClassType().getId(),
00240                      ScanParseSkel::OSGsfInt32);
00241     setIntExtMapping(MFUInt32::getClassType().getId(),
00242                      ScanParseSkel::OSGmfInt32);
00243 
00244     setIntExtMapping(SFGLenum::getClassType().getId(),
00245                      ScanParseSkel::OSGsfInt32);
00246     setIntExtMapping(MFGLenum::getClassType().getId(),
00247                      ScanParseSkel::OSGmfInt32);
00248 
00249 
00250     /* To Node Mappings */
00251 
00252     setIntExtMapping(SFFieldContainerAttachmentPtrMap::getClassType().getId(),
00253                      ScanParseSkel::OSGmfNode);
00254 
00255     /* extended types */
00256 
00257     setIntExtMapping(SFMatrix::getClassType().getId(),
00258                      ScanParseSkel::OSGsfMatrix);
00259 
00260     setIntExtMapping(SFPnt3f::getClassType().getId(),
00261                      ScanParseSkel::OSGsfPnt3f);
00262 
00263     setIntExtMapping(MFPnt3f::getClassType().getId(),
00264                      ScanParseSkel::OSGmfPnt3f);
00265     setIntExtMapping(SFDynamicVolume::getClassType().getId(),
00266                      ScanParseSkel::OSGmfFloat);
00267 
00268     setIntExtMapping(SFPlane::getClassType().getId(),
00269                      ScanParseSkel::OSGsfPlane);
00270     setIntExtMapping(MFPlane::getClassType().getId(),
00271                      ScanParseSkel::OSGmfPlane);
00272 
00273     setIntExtMapping(SFDynamicVolume::getClassType().getId(),
00274                      ScanParseSkel::OSGsfVolume);
00275 }
00276 
00277 void OSGLoader::setFieldContainerValue(FieldContainerPtr pNewNode)
00278 {
00279     if(_pCurrentField != NullFC)
00280     {
00281         SFFieldContainerPtr::EditHandlePtr pSFHandle = 
00282             boost::dynamic_pointer_cast<SFFieldContainerPtr::EditHandle>(
00283                 _pCurrentField);
00284 
00285         MFFieldContainerPtr::EditHandlePtr pMFHandle = 
00286             boost::dynamic_pointer_cast<MFFieldContainerPtr::EditHandle>(
00287                 _pCurrentField);
00288 
00289         if(pSFHandle != NULL && pSFHandle->isValid())
00290         {
00291             pSFHandle->setValue(pNewNode);
00292         }
00293         else if(pMFHandle != NULL && pMFHandle->isValid())
00294         {
00295             pMFHandle->add(pNewNode);
00296         }
00297     }
00298 }
00299 
00300 /*-------------------------------------------------------------------------*\
00301  -  public                                                                 -
00302 \*-------------------------------------------------------------------------*/
00303 
00304 /*------------- constructors & destructors --------------------------------*/
00305 
00309 OSGLoader::OSGLoader(void) :
00310      Inherited        (      ),
00311     _pCurrentFC       (NullFC),
00312     _pRootNode        (NullFC),
00313     _pCurrentField    (      ),
00314     _pCurrentFieldDesc(NULL  ),
00315     _defMap           (      ),
00316     _fcStack          (      ),
00317     _fStack           (      ),
00318     _fdStack          (      ),
00319     _bvChanged        (     0),
00320     _sChangedStack    (      )
00321 {
00322     Self::setReferenceHeader("#OSG V1.0 ");
00323     initFieldTypeMapper();
00324 }
00325 
00329 OSGLoader::~OSGLoader(void)
00330 {
00331 }
00332 
00333 /*------------------------------ access -----------------------------------*/
00334 
00335 void OSGLoader::scanStream(std::istream &is)
00336 {
00337     if(is)
00338     {
00339         _pRootNode         = NullFC;
00340         _pCurrentFC        = NullFC;
00341 
00342         _pCurrentField.reset();
00343 
00344         _pCurrentFieldDesc = NULL;
00345 
00346         _defMap .clear();
00347 
00348         Inherited::scanStream(is);
00349     }
00350 }
00351 
00352 #if defined(OSG_WIN32_ICL)
00353 #pragma warning (disable : 383)
00354 #endif
00355 
00356 void OSGLoader::beginNode(const Char8 *szNodeTypename,
00357                           const Char8 *szNodename)
00358 {
00359     FieldContainerPtr pNewNode;
00360 
00361     if(szNodeTypename == NULL)
00362         return;
00363 
00364     PINFO << "Begin node : " << szNodeTypename << " | ";
00365 
00366     if(szNodename == NULL)
00367     {
00368         PINFO << "NULL";
00369     }
00370     else
00371     {
00372         PINFO << szNodename;
00373     }
00374 
00375     PINFO << std::endl;
00376 
00377     pNewNode =
00378         FieldContainerFactory::the()->createContainer(szNodeTypename);
00379 
00380     if(szNodename != NULL && pNewNode != NullFC)
00381     {
00382 #ifdef DO_CHECK
00383         AttachmentContainerPtr pAttCon =
00384             AttachmentContainerPtr::dcast(pNewNode);
00385 
00386         if(pAttCon != NullFC)
00387         {
00388             setName(pAttCon, szNodename);
00389         }
00390 #endif
00391 
00392         _defMap.insert(std::make_pair(std::string(szNodename), pNewNode));
00393     }
00394 
00395     // assign nodepointer to current sf|mf field
00396     setFieldContainerValue(pNewNode);
00397 
00398     _pCurrentFC = pNewNode;
00399 
00400     _fcStack.push(_pCurrentFC);
00401 
00402     if(_fcStack.size() == 1)
00403     {
00404         NodePtr pNode = NullFC;
00405 
00406         if(_pCurrentFC->getType().isNode())
00407         {
00408             pNode = dynamic_cast<NodePtr>(_pCurrentFC);
00409         }
00410         else if(_pCurrentFC->getType().isNodeCore())
00411         {
00412             pNode = Node::create();
00413 
00414             pNode->setCore(dynamic_cast<NodeCorePtr>(_pCurrentFC));
00415         }
00416         else
00417         {
00418             SLOG << "Fieldcontainer " << szNodeTypename
00419                  << "is neither Node nor NodeCore " << std::endl;
00420         }
00421 
00422         if(_pRootNode == NullFC)
00423         {
00424             GroupPtr pGroup = Group::create();
00425 
00426             _pRootNode = Node::create();
00427 
00428             _pRootNode->setCore(pGroup);
00429         }
00430 
00431         _pRootNode->addChild(pNode);
00432     }
00433 
00434     _sChangedStack.push(_bvChanged);
00435 
00436     _bvChanged = 0;
00437 }
00438 
00439 #if defined(OSG_WIN32_ICL)
00440 #pragma warning (default : 383)
00441 #endif
00442 
00443 void OSGLoader::endNode(void)
00444 {
00445     PINFO << "End Node" << std::endl;
00446 
00447     if(_pCurrentFC != NullFC)
00448     {
00449         if(_pCurrentFC->getType().isNode() == true)
00450         {
00451             NodePtr pNode = dynamic_cast<NodePtr>(_pCurrentFC);
00452 
00453             if(pNode->getCore() == NullFC)
00454             {
00455                 GroupPtr pGroup = Group::create();
00456 
00457                 pNode->setCore(pGroup);
00458             }
00459         }
00460     }
00461 
00462     _fcStack.pop();
00463 
00464     if(_fcStack.size() != 0)
00465     {
00466         _pCurrentFC = _fcStack.top();
00467     }
00468     else
00469     {
00470         _pCurrentFC = NullFC;
00471     }
00472 
00473     if(_sChangedStack.size() != 0)
00474     {
00475         _bvChanged = _sChangedStack.top();
00476     }
00477     else
00478     {
00479         _bvChanged = 0;
00480     }
00481 
00482     _sChangedStack.pop();
00483 
00484 }
00485 
00486 void OSGLoader::nullNode(void)
00487 {
00488     setFieldContainerValue(NullFC);
00489 }
00490 
00491 void OSGLoader::use(const Char8 *szName)
00492 {
00493     FieldContainerPtr pUseNode;
00494 
00495     // try to find a container with the given name attachment
00496 
00497     pUseNode = getReference(szName);
00498 
00499     if(pUseNode == NullFC)
00500     {
00501         SLOG << "No FieldContainer found with name " << szName << std::endl;
00502     }
00503     else
00504     {
00505         // assign nodepointer to current sf|mf field
00506         setFieldContainerValue(pUseNode);
00507     }
00508 }
00509 
00510 NodePtr OSGLoader::getRootNode(void)
00511 {
00512     return _pRootNode;
00513 }
00514 
00515 std::vector<FieldContainerPtr> OSGLoader::getRootNodes(void)
00516 {
00517     std::vector<FieldContainerPtr> fcVec;
00518 
00519     for( UInt32 i=0; i<_pRootNode->getNChildren(); ++i )
00520     {
00521         fcVec.push_back(_pRootNode->getChild(i));
00522     }
00523 
00524     return fcVec;
00525 }
00526 
00527 void OSGLoader::addFieldValue(const Char8 *szFieldVal)
00528 {
00529     PINFO << "\t\tFV : " << szFieldVal << std::endl;
00530 
00531     if(_pCurrentField != NULL)
00532     {
00533 //        _pCurrentFieldDesc->pushValueFromCString( szFieldVal,
00534 //                                                 _pCurrentField);
00535 
00536         _pCurrentField->pushValueFromCString(szFieldVal);
00537     }
00538 }
00539 
00540 UInt32 OSGLoader::getFieldType(const Char8 *szFieldname)
00541 {
00542     UInt32                returnValue = 0;
00543     FieldDescriptionBase *pFieldDesc  = NULL;
00544 
00545     if(szFieldname == NULL)
00546         return returnValue;
00547 
00548     if(_pCurrentFC != NullFC)
00549     {
00550         pFieldDesc = _pCurrentFC->getFieldDescription(szFieldname);
00551 
00552         PINFO << "GF : " << szFieldname << " " << pFieldDesc << std::endl;
00553 
00554         if(pFieldDesc != NULL)
00555         {
00556             returnValue = pFieldDesc->getFieldType().getScanTypeId();
00557         }
00558     }
00559 
00560     return returnValue;
00561 }
00562 
00563 Int32 OSGLoader::mapExtIntFieldType(const Char8 *szFieldname,
00564                                     const Int32  iFieldTypeId)
00565 {
00566     Int32 returnValue = Inherited::mapExtIntFieldType(szFieldname,
00567                                                       iFieldTypeId);
00568 
00569     if(returnValue < 0 && szFieldname != NULL && _pCurrentFC != NullFC)
00570     {
00571         FieldDescriptionBase *pFieldDesc = 
00572             _pCurrentFC->getFieldDescription(szFieldname);
00573 
00574         if(pFieldDesc != NULL)
00575         {
00576             const FieldType &oFieldType = pFieldDesc->getFieldType();
00577 
00578             PINFO << "FieldTypeId invalid, trying to fix. " << std::endl;
00579             PINFO << oFieldType.getContentType().getCName()
00580                   << " comparing with "
00581                   << FieldTraits<FieldContainerPtr>::getType().getCName()
00582                   << std::endl;
00583 
00584 
00585             if(oFieldType.getContentType().isDerivedFrom(
00586                    FieldTraits<FieldContainerPtr>::getType()) == true)
00587             {
00588                 PINFO << "FieldContainerPtr or derived class, "
00589                       << "parsing as Node"
00590                       << std::endl;
00591 
00592                 if(oFieldType.getCardinality() == FieldType::SINGLE_FIELD)
00593                 {
00594                     returnValue = ScanParseSkel::OSGsfNode;
00595                 }
00596                 else
00597                 {
00598                     returnValue = ScanParseSkel::OSGmfNode;
00599                 }
00600             }
00601         }
00602     }
00603 
00604     return returnValue;
00605 }
00606 
00607 void OSGLoader::beginField(const Char8 *szFieldname,
00608                            const UInt32 )
00609 {
00610     PINFO << "BeginField "
00611           << szFieldname
00612           << " "
00613           << _pCurrentField
00614           << std::endl;
00615 
00616     if(szFieldname == NULL)
00617         return;
00618 
00619     _pCurrentField.reset();
00620 
00621     if(_pCurrentFC != NullFC)
00622     {
00623         _pCurrentField     = _pCurrentFC->editField(szFieldname);
00624 
00625         _pCurrentFieldDesc =
00626             _pCurrentFC->getType().getFieldDesc(szFieldname);
00627 
00628         PINFO << "BF : "
00629               <<  szFieldname       << " "
00630               << _pCurrentField     << " "
00631               << _pCurrentFieldDesc << std::endl;
00632 
00633         if(_pCurrentFieldDesc != NULL)
00634         {
00635             _bvChanged |= _pCurrentFieldDesc->getFieldMask();
00636         }
00637     }
00638 
00639     _fStack.push (_pCurrentField);
00640     _fdStack.push(_pCurrentFieldDesc);
00641 }
00642 
00643 void OSGLoader::endField(void)
00644 {
00645     PINFO << "End Field" << std::endl;
00646 
00647     _fStack.pop();
00648 
00649     if(_fStack.size() != 0)
00650     {
00651         _pCurrentField = _fStack.top();
00652     }
00653     else
00654     {
00655         _pCurrentField.reset();
00656     }
00657 
00658     _fdStack.pop();
00659 
00660     if(_fdStack.size() != 0)
00661     {
00662         _pCurrentFieldDesc = _fdStack.top();
00663     }
00664     else
00665     {
00666         _pCurrentFieldDesc = NULL;
00667     }
00668 }
00669 
00670 FieldContainerPtr OSGLoader::getReference(const Char8 *szName)
00671 {
00672     // Find a previously DEF'ed FC by its name and return Ptr to it
00673 
00674     NamedFCMap::iterator entry = _defMap.find(std::string(szName));
00675 
00676     if(entry == _defMap.end())
00677         return NullFC;
00678 
00679     return entry->second; // return the stored FCPtr
00680 }