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 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; }