osg::BINWriter Class Reference
[File Input/Output]

#include <OSGBINWriter.h>

List of all members.

Public Member Functions

Constructors/Destructor


 BINWriter (std::ostream &os)
virtual ~BINWriter (void)
write


bool write (NodePtr node)
bool write (std::vector< NodePtr > nodes)

Protected Types

Type information


typedef std::map< UInt32, FCInfoFCInfoMap
typedef std::map< std::string,
std::vector< UInt32 > > 
FCTypeIdMap

Protected Member Functions

Helper


void addToIdMap (FieldContainerPtr fcPtr)
void writeFileHeader (void)
void doIndexFC (FieldContainerPtr fieldConPtr)
void doWriteIndexedFC (void)

Private Member Functions

 BINWriter (const BINWriter &source)
void operator= (const BINWriter &source)

Private Attributes

provate members


FCInfoMap _fcMap
FCTypeIdMap _fcIdMap
BinaryFileHandler _outFileHandler
std::vector< NodePtr_vec_pRootNodes
bool _valid_stream

Classes

class  BinaryFileHandler
struct  FCInfo


Detailed Description

OpenSG propritrary file writer

Definition at line 55 of file OSGBINWriter.h.


Member Typedef Documentation

typedef std::map< UInt32, FCInfo > osg::BINWriter::FCInfoMap [protected]

Definition at line 92 of file OSGBINWriter.h.

typedef std::map<std::string, std::vector<UInt32> > osg::BINWriter::FCTypeIdMap [protected]

Definition at line 93 of file OSGBINWriter.h.


Constructor & Destructor Documentation

BINWriter::BINWriter ( std::ostream &  os  ) 

Definition at line 70 of file OSGBINWriter.cpp.

00070                                    :
00071     _fcMap(),
00072     _fcIdMap(),
00073     _outFileHandler(os),
00074     _vec_pRootNodes(),
00075     _valid_stream(os != false)
00076 {
00077 }

BINWriter::~BINWriter ( void   )  [virtual]

Destructor documentation

Definition at line 81 of file OSGBINWriter.cpp.

00082 {
00083 }

osg::BINWriter::BINWriter ( const BINWriter source  )  [private]


Member Function Documentation

bool BINWriter::write ( NodePtr  node  ) 

write contents of a single root node

Definition at line 90 of file OSGBINWriter.cpp.

References _fcIdMap, _fcMap, _outFileHandler, _valid_stream, _vec_pRootNodes, doIndexFC(), doWriteIndexedFC(), osg::BinaryDataHandler::flush(), SWARNING, and writeFileHeader().

Referenced by osg::BINSceneFileType::write().

00091 {
00092     if(!_valid_stream)
00093     {
00094         SWARNING << "BINLoader::write : Stream is invalid!" << std::endl;
00095         return false;
00096     }
00097 
00098     _fcMap.clear();
00099     _fcIdMap.clear();
00100     _vec_pRootNodes.clear();
00101     _vec_pRootNodes.push_back(node);
00102 
00103     // traverse and index containers to write
00104     doIndexFC(node);
00105 
00106     // write the header with number and type of containers
00107     writeFileHeader();
00108 
00109     // write the containers and flush the filehandler
00110     doWriteIndexedFC();
00111     _outFileHandler.flush();
00112 
00113     return true;
00114 }

bool BINWriter::write ( std::vector< NodePtr nodes  ) 

write contents of a vector of nodes

Definition at line 118 of file OSGBINWriter.cpp.

References _fcIdMap, _fcMap, _outFileHandler, _valid_stream, _vec_pRootNodes, doIndexFC(), doWriteIndexedFC(), osg::BinaryDataHandler::flush(), SWARNING, and writeFileHeader().

00119 {
00120     if(!_valid_stream)
00121     {
00122         SWARNING << "BINLoader::write : Stream is invalid!" << std::endl;
00123         return false;
00124     }
00125 
00126     //does the same as write(NodePtr) for every Node in the vector
00127     _fcMap.clear();
00128     _fcIdMap.clear();
00129     _vec_pRootNodes.clear();
00130     _vec_pRootNodes = nodes;
00131 
00132     std::vector < NodePtr >::const_iterator iter = nodes.begin();
00133     for(; iter != nodes.end(); ++iter)
00134     {
00135         doIndexFC(*iter);
00136     }
00137 
00138     writeFileHeader();
00139     doWriteIndexedFC();
00140     _outFileHandler.flush();
00141 
00142     return true;
00143 }

void BINWriter::addToIdMap ( FieldContainerPtr  fcPtr  )  [protected]

Add new container to the mapping table

Definition at line 150 of file OSGBINWriter.cpp.

References _fcIdMap, osg::FieldContainerPtrBase::getFieldContainerId(), osg::NullFC, and SWARNING.

Referenced by doIndexFC().

00151 {
00152     if(fcPtr == NullFC)
00153     {
00154         SWARNING << "ERROR in BINWriter::addToIdMap : NullFC" << std::endl;
00155         return;
00156     }
00157 
00158     FCTypeIdMap::iterator   iIdMap;
00159 
00160     // add new ContainerType
00161     if(_fcIdMap.find(fcPtr->getType().getCName()) == _fcIdMap.end())
00162     {
00163         std::vector < UInt32 > idVec;
00164         idVec.push_back(0);
00165         if(!(_fcIdMap.insert(std::pair < std::string,
00166                              std::vector < UInt32 > >
00167                              (fcPtr->getType().getCName(), idVec)).second))
00168         {
00169             SWARNING <<
00170                 "BINWriter::addToIdMap(FieldContainerPtr):" <<
00171                 std::endl <<
00172                 "ERROR while inserting into _fcIdMap" <<
00173                 std::endl;
00174         };
00175     }
00176 
00177     // add container to the vector of appropriate type
00178     iIdMap = _fcIdMap.find(fcPtr->getType().getCName());
00179     iIdMap->second.push_back(fcPtr.getFieldContainerId());
00180 
00181     //increase the 0th entry of the vector (number of entries)
00182     iIdMap->second[0]++;
00183 }

void BINWriter::writeFileHeader ( void   )  [protected]

Write a file header containing the following:

    Number (k) of roots (osg::UInt32)
    {
        root IDs (osg::UInt32)
    } (k times)

    Number(i) of different containertypes (osg::UInt32)
    {
        containertype (std::string)
        number(j) of appropriate containers (osg::UInt32)
        {
            container Ids (osg::UInt32)
        } (j times)
    } (i times)
    

Definition at line 202 of file OSGBINWriter.cpp.

References _fcIdMap, _outFileHandler, _vec_pRootNodes, and osg::BinaryDataHandler::putValue().

Referenced by write().

00203 {
00204     FCTypeIdMap::iterator   iIdMap = _fcIdMap.begin();
00205     std::vector < NodePtr >::iterator iRoots = _vec_pRootNodes.begin();
00206 
00207     UInt32  mapSize = _fcIdMap.size();
00208     UInt32  numOfRoots = _vec_pRootNodes.size();
00209     UInt32  i = 0;
00210 
00211     _outFileHandler.putValue(numOfRoots);
00212     for(i = 0; i != numOfRoots; ++i, ++iRoots)
00213     {
00214         _outFileHandler.putValue(iRoots->getFieldContainerId());
00215     }
00216 
00217     _outFileHandler.putValue(mapSize);
00218     for(i = 0; i < mapSize; ++i, ++iIdMap)
00219     {
00220         _outFileHandler.putValue(iIdMap->first);
00221 
00222         std::vector < UInt32 >::iterator intIt = iIdMap->second.begin();
00223         std::vector < UInt32 >::iterator intEnd = iIdMap->second.end();
00224 
00225         while(intIt != intEnd)
00226         {
00227             _outFileHandler.putValue(*intIt);
00228 
00229             ++intIt;
00230         }
00231     }
00232 }

void BINWriter::doIndexFC ( FieldContainerPtr  fieldConPtr  )  [protected]

collect fc and contained fcs. Ignore internal field.

Definition at line 236 of file OSGBINWriter.cpp.

References _fcMap, addToIdMap(), osg::Field::getCardinality(), osg::Node::getClassType(), osg::FieldDescription::getCName(), osg::TypeBase::getCName(), osg::FieldContainerPtrBase::getFieldContainerId(), osg::FieldContainerType::getFieldDescription(), osg::FieldDescription::getFieldMask(), osg::FieldContainerType::getNumFieldDescs(), osg::Field::getType(), osg::Field::isEmpty(), osg::FieldDescription::isInternal(), osg::BINWriter::FCInfo::mask, osg::FieldType::MULTI_FIELD, osg::NullFC, osg::BINWriter::FCInfo::ptr, osg::FieldType::SINGLE_FIELD, osg::BINWriter::FCInfo::type, and osg::Node::VolumeFieldMask.

Referenced by write().

00237 {
00238     // traverse
00239     if(fieldConPtr == NullFC)
00240     {
00241         // nothing to do
00242         return;
00243     }
00244 
00245     FieldContainerType  &fcType = fieldConPtr->getType();
00246     UInt32 i;
00247 
00248     // insert into map
00249     std::pair < std::map<UInt32, FCInfo>::iterator, bool > element = _fcMap.
00250         insert(std::pair < UInt32,
00251                    FCInfo > (fieldConPtr.getFieldContainerId(), FCInfo()));
00252 
00253     // avoid loop. Element was already in the map
00254     if(element.second == false)
00255     {
00256         return;
00257     }
00258 
00259     // get inserted element
00260     FCInfo  &fcInfo = element.first->second;
00261 
00262     //go through all fields
00263     for(i = 1; i <= fcType.getNumFieldDescs(); ++i)
00264     {
00265         FieldDescription    *fDesc = fcType.getFieldDescription(i);
00266         Field               *fieldPtr = fieldConPtr->getField(i);
00267         const FieldType     &fType = fieldPtr->getType();
00268 
00269         // process all containers
00270         if(!fDesc->isInternal())
00271         {
00272             //detect referenced containers #DIRTY-HACK#
00273             if(strstr(fType.getCName(), "Ptr") != NULL)
00274             {
00275                 //traverse the referenced SF/MF-containers
00276                 if(fieldPtr->getCardinality() == FieldType::SINGLE_FIELD)
00277                 {
00278                     doIndexFC(((SFFieldContainerPtr *) fieldPtr)->getValue());
00279                 }
00280                 else if(fieldPtr->getCardinality() == FieldType::MULTI_FIELD)
00281                 {
00282                     UInt32 j;
00283                     for(j = 0; j < ((MFFieldContainerPtr *) fieldPtr)->size();
00284                                             ++j)
00285                     {
00286                         doIndexFC((*(((MFFieldContainerPtr *) fieldPtr)))[j]);
00287                     }
00288                 }
00289             }
00290 
00291             //attachments
00292             if(strcmp(fDesc->getCName(), "attachments") == 0)
00293             {
00294                 AttachmentMap::const_iterator   mapIt =
00295                     ((SFAttachmentMap *) fieldPtr)->getValue().begin();
00296                 AttachmentMap::const_iterator   mapEnd =
00297                     ((SFAttachmentMap *) fieldPtr)->getValue().end();
00298 
00299                 for(; mapIt != mapEnd; ++mapIt)
00300                 {
00301                     /*
00302                     fprintf(stderr, "Write %s %s\n",
00303                                                 mapIt->second->getType().getCName(),
00304                                                 GenericAtt::getClassType().getCName());
00305                     */
00306 
00307                     doIndexFC(mapIt->second);
00308                 }
00309             }
00310         }
00311 
00312         // create field mask
00313         // ignore internal fields except parent fields
00314         if(fDesc->isInternal() &&
00315                    strcmp(fDesc->getCName(), "parent") != 0 &&
00316                    strcmp(fDesc->getCName(), "parents") != 0)
00317         {
00318             continue;
00319         }
00320 
00321         // ignore node volume
00322         if(fcType == Node::getClassType() &&
00323                    fcType.getFieldDescription(i)->getFieldMask()
00324                        == Node::VolumeFieldMask)
00325         {
00326             continue;
00327         }
00328 
00329         // ignore empty mfields
00330         if(fieldPtr->getCardinality() == FieldType::MULTI_FIELD &&
00331                    fieldPtr->isEmpty() == true)
00332         {
00333             continue;
00334         }
00335 
00336         // add field to fieldmask
00337         fcInfo.mask =
00338             (
00339                 fcInfo.mask |
00340                 fcType.getFieldDescription(i)->getFieldMask()
00341             );
00342     }
00343 
00344     // add container to the Map of containers to write
00345     fcInfo.ptr = fieldConPtr;
00346     fcInfo.type = fieldConPtr->getType().getName();
00347 
00348     // add container id to index
00349     addToIdMap(fcInfo.ptr);
00350 }

void BINWriter::doWriteIndexedFC ( void   )  [protected]

write field containers

Definition at line 354 of file OSGBINWriter.cpp.

References _fcMap, _outFileHandler, osg::BinaryDataHandler::putValue(), and SINFO.

Referenced by write().

00355 {
00356     FCInfoMap::const_iterator   i = _fcMap.begin();
00357     UInt32 mapSize = _fcMap.size(), count = 1;
00358 
00359     for(; i != _fcMap.end(); ++i, ++count)
00360     {
00361         SINFO <<
00362             "writing container " <<
00363             std::setw(4) <<
00364             count <<
00365             "/" <<
00366             mapSize <<
00367             "..." <<
00368             std::endl;
00369 
00370         //for each entry in _fcMap
00371         //write ID
00372         _outFileHandler.putValue(i->first);
00373 
00374         //write FieldMask
00375         _outFileHandler.putValue(i->second.mask);
00376 
00377         //write data
00378         i->second.ptr->copyToBin(_outFileHandler, i->second.mask);
00379     }
00380 }

void osg::BINWriter::operator= ( const BINWriter source  )  [private]


Member Data Documentation

Definition at line 130 of file OSGBINWriter.h.

Referenced by doIndexFC(), doWriteIndexedFC(), and write().

Definition at line 131 of file OSGBINWriter.h.

Referenced by addToIdMap(), write(), and writeFileHeader().

Definition at line 132 of file OSGBINWriter.h.

Referenced by doWriteIndexedFC(), write(), and writeFileHeader().

std::vector<NodePtr> osg::BINWriter::_vec_pRootNodes [private]

Definition at line 133 of file OSGBINWriter.h.

Referenced by write(), and writeFileHeader().

Definition at line 134 of file OSGBINWriter.h.

Referenced by write().


The documentation for this class was generated from the following files:

Generated on Mon Mar 17 11:12:06 2008 for OpenSG by  doxygen 1.5.5