bool SGNODE::SwapParent( SGNODE* aNewParent ) { if( aNewParent == m_Parent ) return true; if( NULL == aNewParent ) return false; if( NULL == m_Parent ) { if( aNewParent->AddChildNode( this ) ) return true; return false; } if( aNewParent->GetNodeType() != m_Parent->GetNodeType() ) return false; SGNODE* oldParent = m_Parent; m_Parent->unlinkChildNode( this ); m_Parent = NULL; aNewParent->unlinkRefNode( this ); aNewParent->AddChildNode( this ); oldParent->AddRefNode( this ); return true; }
SGNODE* S3D::ReadCache( const char* aFileName, void* aPluginMgr, bool (*aTagCheck)( const char*, void* ) ) { if( NULL == aFileName || aFileName[0] == 0 ) return NULL; wxString ofile = wxString::FromUTF8Unchecked( aFileName ); if( !wxFileName::FileExists( aFileName ) ) { std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; wxString errmsg = _( "no such file" ); ostr << " * [INFO] " << errmsg.ToUTF8() << " '"; ostr << aFileName << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); return NULL; } SGNODE* np = new SCENEGRAPH( NULL ); if( NULL == np ) { #ifdef DEBUG do { std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] failed to instantiate SCENEGRAPH"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); } while( 0 ); #endif return NULL; } std::ifstream file; file.open( aFileName, std::ios_base::in | std::ios_base::binary ); if( !file.is_open() ) { delete np; std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; wxString errmsg = _( "failed to open file" ); ostr << " * [INFO] " << errmsg.ToUTF8() << " '"; ostr << aFileName << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); return NULL; } // from SG_VERSION_TAG 1, read the version tag; if it's not the expected tag // then we fail to read the cache file do { std::string name; char schar; file.get( schar ); if( '(' != schar ) { #ifdef DEBUG do { std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; missing left parenthesis at position "; ostr << file.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); } while( 0 ); #endif file.close(); return NULL; } file.get( schar ); while( ')' != schar && file.good() ) { name.push_back( schar ); file.get( schar ); } if( name.compare( SG_VERSION_TAG ) ) { file.close(); return NULL; } } while( 0 ); // from SG_VERSION_TAG 2, read the PluginInfo string and check that it matches // version tag; if it's not the expected tag then we fail to read the file do { std::string name; char schar; file.get( schar ); if( '(' != schar ) { #ifdef DEBUG do { std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; missing left parenthesis at position "; ostr << file.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); } while( 0 ); #endif file.close(); return NULL; } file.get( schar ); while( ')' != schar && file.good() ) { name.push_back( schar ); file.get( schar ); } // check the plugin tag if( NULL != aTagCheck && NULL != aPluginMgr && !aTagCheck( name.c_str(), aPluginMgr ) ) { file.close(); return NULL; } } while( 0 ); bool rval = np->ReadCache( file, NULL ); file.close(); if( !rval ) { delete np; std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; wxString errmsg = "problems encountered reading cache file"; ostr << " * [INFO] " << errmsg.ToUTF8() << " '"; ostr << aFileName << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); return NULL; } return np; }
bool SGFACESET::ReadCache( std::ifstream& aFile, SGNODE* parentNode ) { if( m_Coords || m_RCoords || m_CoordIndices || m_Colors || m_RColors || m_Normals || m_RNormals ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [BUG] non-empty node"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } #define NITEMS 7 bool items[NITEMS]; for( int i = 0; i < NITEMS; ++i ) aFile.read( (char*)&items[i], sizeof(bool) ); if( ( items[0] && items[1] ) || ( items[3] && items[4] ) || ( items[5] && items[6] ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; multiple item definitions at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } std::string name; if( items[0] ) { if( S3D::SGTYPE_COORDS != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad child coords tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_Coords = new SGCOORDS( this ); m_Coords->SetName( name.c_str() ); if( !m_Coords->ReadCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data while reading coords '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } if( items[1] ) { if( S3D::SGTYPE_COORDS != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad ref coords tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGNODE* np = FindNode( name.c_str(), this ); if( !np ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: cannot find ref coords '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( S3D::SGTYPE_COORDS != np->GetNodeType() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: type is not SGCOORDS '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_RCoords = (SGCOORDS*)np; m_RCoords->addNodeRef( this ); } if( items[2] ) { if( S3D::SGTYPE_COORDINDEX != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad coord index tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_CoordIndices = new SGCOORDINDEX( this ); m_CoordIndices->SetName( name.c_str() ); if( !m_CoordIndices->ReadCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data while reading coord index '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } if( items[3] ) { if( S3D::SGTYPE_NORMALS != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad child normals tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_Normals = new SGNORMALS( this ); m_Normals->SetName( name.c_str() ); if( !m_Normals->ReadCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data while reading normals '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } if( items[4] ) { if( S3D::SGTYPE_NORMALS != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad ref normals tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGNODE* np = FindNode( name.c_str(), this ); if( !np ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: cannot find ref normals '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( S3D::SGTYPE_NORMALS != np->GetNodeType() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: type is not SGNORMALS '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_RNormals = (SGNORMALS*)np; m_RNormals->addNodeRef( this ); } if( items[5] ) { if( S3D::SGTYPE_COLORS != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad child colors tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_Colors = new SGCOLORS( this ); m_Colors->SetName( name.c_str() ); if( !m_Colors->ReadCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data while reading colors '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } if( items[6] ) { if( S3D::SGTYPE_COLORS != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad ref colors tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGNODE* np = FindNode( name.c_str(), this ); if( !np ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: cannot find ref colors '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( S3D::SGTYPE_COLORS != np->GetNodeType() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: type is not SGCOLORS '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_RColors = (SGCOLORS*)np; m_RColors->addNodeRef( this ); } if( aFile.fail() ) return false; return true; }
bool SGFACESET::WriteCache( std::ofstream& aFile, SGNODE* parentNode ) { if( NULL == parentNode ) { if( NULL == m_Parent ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [BUG] corrupt data; m_aParent is NULL"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGNODE* np = m_Parent; while( NULL != np->GetParent() ) np = np->GetParent(); if( np->WriteCache( aFile, NULL ) ) { m_written = true; return true; } return false; } if( parentNode != m_Parent ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [BUG] corrupt data; parentNode != m_aParent"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( !aFile.good() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] bad stream"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } // check if any references are unwritten and swap parents if so if( NULL != m_RCoords && !m_RCoords->isWritten() ) m_RCoords->SwapParent( this ); if( NULL != m_RNormals && !m_RNormals->isWritten() ) m_RNormals->SwapParent( this ); if( NULL != m_RColors && !m_RColors->isWritten() ) m_RColors->SwapParent( this ); aFile << "[" << GetName() << "]"; #define NITEMS 7 bool items[NITEMS]; int i; for( i = 0; i < NITEMS; ++i ) items[i] = 0; i = 0; if( NULL != m_Coords ) items[i] = true; ++i; if( NULL != m_RCoords ) items[i] = true; ++i; if( NULL != m_CoordIndices ) items[i] = true; ++i; if( NULL != m_Normals ) items[i] = true; ++i; if( NULL != m_RNormals ) items[i] = true; ++i; if( NULL != m_Colors ) items[i] = true; ++i; if( NULL != m_RColors ) items[i] = true; for( int i = 0; i < NITEMS; ++i ) aFile.write( (char*)&items[i], sizeof(bool) ); if( items[0] ) m_Coords->WriteCache( aFile, this ); if( items[1] ) aFile << "[" << m_RCoords->GetName() << "]"; if( items[2] ) m_CoordIndices->WriteCache( aFile, this ); if( items[3] ) m_Normals->WriteCache( aFile, this ); if( items[4] ) aFile << "[" << m_RNormals->GetName() << "]"; if( items[5] ) m_Colors->WriteCache( aFile, this ); if( items[6] ) aFile << "[" << m_RColors->GetName() << "]"; if( aFile.fail() ) return false; m_written = true; return true; }
bool SGSHAPE::ReadCache( std::ifstream& aFile, SGNODE* parentNode ) { if( m_Appearance || m_RAppearance || m_FaceSet || m_RFaceSet ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [BUG] non-empty node"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } #define NITEMS 4 bool items[NITEMS]; for( int i = 0; i < NITEMS; ++i ) aFile.read( (char*)&items[i], sizeof(bool) ); if( ( items[0] && items[1] ) || ( items[2] && items[3] ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; multiple item definitions at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } std::string name; if( items[0] ) { if( S3D::SGTYPE_APPEARANCE != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad child apperance tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_Appearance = new SGAPPEARANCE( this ); m_Appearance->SetName( name.c_str() ); if( !m_Appearance->ReadCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data while reading appearance '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } if( items[1] ) { if( S3D::SGTYPE_APPEARANCE != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad ref appearance tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGNODE* np = FindNode( name.c_str(), this ); if( !np ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: cannot find ref appearance '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( S3D::SGTYPE_APPEARANCE != np->GetNodeType() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: type is not SGAPPEARANCE '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_RAppearance = (SGAPPEARANCE*)np; m_RAppearance->addNodeRef( this ); } if( items[2] ) { if( S3D::SGTYPE_FACESET != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad child face set tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_FaceSet = new SGFACESET( this ); m_FaceSet->SetName( name.c_str() ); if( !m_FaceSet->ReadCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data while reading face set '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } if( items[3] ) { if( S3D::SGTYPE_FACESET != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad ref face set tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGNODE* np = FindNode( name.c_str(), this ); if( !np ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: cannot find ref face set '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( S3D::SGTYPE_FACESET != np->GetNodeType() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: type is not SGFACESET '"; ostr << name << "'"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_RFaceSet = (SGFACESET*)np; m_RFaceSet->addNodeRef( this ); } if( aFile.fail() ) return false; return true; }
bool SCENEGRAPH::ReadCache( std::istream& aFile, SGNODE* parentNode ) { if( !m_Transforms.empty() || !m_RTransforms.empty() || !m_Shape.empty() || !m_RShape.empty() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [BUG] non-empty node"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } std::string name; // name of the node if( NULL == parentNode ) { // we need to read the tag and verify its type if( S3D::SGTYPE_TRANSFORM != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; tag mismatch at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } m_Name = name; } // read fixed member data S3D::ReadPoint( aFile, center ); S3D::ReadPoint( aFile, translation ); S3D::ReadVector( aFile, rotation_axis ); aFile.read( (char*)&rotation_angle, sizeof( rotation_angle ) ); S3D::ReadPoint( aFile, scale ); S3D::ReadVector( aFile, scale_axis ); aFile.read( (char*)&scale_angle, sizeof( scale_angle ) ); size_t sizeCT = 0; // child transforms size_t sizeRT = 0; // referenced transforms size_t sizeCS = 0; // child shapes size_t sizeRS = 0; // referenced shapes aFile.read( (char*)&sizeCT, sizeof( size_t ) ); aFile.read( (char*)&sizeRT, sizeof( size_t ) ); aFile.read( (char*)&sizeCS, sizeof( size_t ) ); aFile.read( (char*)&sizeRS, sizeof( size_t ) ); size_t i; // read child transforms for( i = 0; i < sizeCT; ++i ) { if( S3D::SGTYPE_TRANSFORM != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad child transform tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SCENEGRAPH* sp = new SCENEGRAPH( this ); sp->SetName( name.c_str() ); if( !sp->ReadCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data while reading transform '"; ostr << name << "' pos " << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } // read referenced transforms for( i = 0; i < sizeRT; ++i ) { if( S3D::SGTYPE_TRANSFORM != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad ref transform tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGNODE* sp = FindNode( name.c_str(), this ); if( !sp ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: cannot find ref transform '"; ostr << name << "' pos " << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( S3D::SGTYPE_TRANSFORM != sp->GetNodeType() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: type is not TRANSFORM '"; ostr << name << "' pos " << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } AddRefNode( sp ); } // read child shapes for( i = 0; i < sizeCS; ++i ) { if( S3D::SGTYPE_SHAPE != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad child shape tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGSHAPE* sp = new SGSHAPE( this ); sp->SetName( name.c_str() ); if( !sp->ReadCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data while reading shape '"; ostr << name << "' pos " << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } // read referenced shapes for( i = 0; i < sizeRS; ++i ) { if( S3D::SGTYPE_SHAPE != S3D::ReadTag( aFile, name ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data; bad ref shape tag at position "; ostr << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } SGNODE* sp = FindNode( name.c_str(), this ); if( !sp ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: cannot find ref shape '"; ostr << name << "' pos " << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( S3D::SGTYPE_SHAPE != sp->GetNodeType() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] corrupt data: type is not SGSHAPE '"; ostr << name << "' pos " << aFile.tellg(); wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } AddRefNode( sp ); } if( aFile.fail() ) return false; return true; }
bool SCENEGRAPH::WriteCache( std::ostream& aFile, SGNODE* parentNode ) { if( NULL == parentNode && NULL != m_Parent ) { SGNODE* np = m_Parent; while( NULL != np->GetParent() ) np = np->GetParent(); if( np->WriteCache( aFile, NULL ) ) { m_written = true; return true; } return false; } if( parentNode != m_Parent ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [BUG] corrupt data; parentNode != m_aParent"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } if( NULL == m_Parent ) { // ensure unique node names ResetNodeIndex(); ReNameNodes(); } if( aFile.fail() ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] bad stream"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } aFile << "[" << GetName() << "]"; S3D::WritePoint( aFile, center ); S3D::WritePoint( aFile, translation ); S3D::WriteVector( aFile, rotation_axis ); aFile.write( (char*)&rotation_angle, sizeof( rotation_angle ) ); S3D::WritePoint( aFile, scale ); S3D::WriteVector( aFile, scale_axis ); aFile.write( (char*)&scale_angle, sizeof( scale_angle ) ); // Transfer ownership of any Transform references which hadn't been written size_t asize = m_RTransforms.size(); size_t i; for( i = 0; i < asize; ++i ) { if( !m_RTransforms[i]->isWritten() ) { m_RTransforms[i]->SwapParent( this ); --asize; --i; } } // Transfer ownership of any Shape references which hadn't been written asize = m_RShape.size(); for( i = 0; i < asize; ++i ) { if( !m_RShape[i]->isWritten() ) { m_RShape[i]->SwapParent( this ); --asize; --i; } } asize = m_Transforms.size(); aFile.write( (char*)&asize, sizeof( size_t ) ); asize = m_RTransforms.size(); aFile.write( (char*)&asize, sizeof( size_t ) ); asize = m_Shape.size(); aFile.write( (char*)&asize, sizeof( size_t ) ); asize = m_RShape.size(); aFile.write( (char*)&asize, sizeof( size_t ) ); asize = m_Transforms.size(); // write child transforms for( i = 0; i < asize; ++i ) { if( !m_Transforms[i]->WriteCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] bad stream while writing child transforms"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } // write referenced transform names asize = m_RTransforms.size(); for( i = 0; i < asize; ++i ) aFile << "[" << m_RTransforms[i]->GetName() << "]"; // write child shapes asize = m_Shape.size(); for( i = 0; i < asize; ++i ) { if( !m_Shape[i]->WriteCache( aFile, this ) ) { #ifdef DEBUG std::ostringstream ostr; ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n"; ostr << " * [INFO] bad stream while writing child shapes"; wxLogTrace( MASK_3D_SG, "%s\n", ostr.str().c_str() ); #endif return false; } } // write referenced transform names asize = m_RShape.size(); for( i = 0; i < asize; ++i ) aFile << "[" << m_RShape[i]->GetName() << "]"; if( aFile.fail() ) return false; m_written = true; return true; }