//---------------------------------------------------------------------------- // EgalTech 2013-2013 //---------------------------------------------------------------------------- // File : GeomDB.cpp Data : 08.04.13 Versione : 1.1c1 // Contenuto : Implementazione della classe GeomDB. // // // // Modifiche : 22.01.13 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include #include #include "GdbObj.h" #include "GeomDB.h" using namespace std ; //---------------------------------------------------------------------------- IGeomDB* CreateGeomDB( void) { return static_cast ( new GeomDB) ; } //---------------------------------------------------------------------------- // GeomDB //---------------------------------------------------------------------------- GeomDB::GeomDB( void) { } //---------------------------------------------------------------------------- GeomDB::~GeomDB( void) { Clear() ; } //---------------------------------------------------------------------------- bool GeomDB::Init( void) { // imposto numero minimo buckets di m_GdbNameMap m_GdbNameMap.rehash( 128) ; return true ; } //---------------------------------------------------------------------------- bool GeomDB::Clear( void) { PGDBO_LIST::iterator Iter ; // ciclo di pulizia for ( Iter = m_GeomDB.begin() ; Iter != m_GeomDB.end() ; Iter++) delete (*Iter) ; m_GeomDB.clear() ; m_GdbNameMap.clear() ; return true ; } //---------------------------------------------------------------------------- bool GeomDB::Load( std::ifstream& osIn) { bool bOk ; bool bEnd ; CScan TheScanner ; // inizializzo lo scanner if ( ! TheScanner.Initialize( osIn)) { cout << "GeomDbLoad : Error on Init " << endl ; return false ; } // leggo l'intestazione if ( ! LoadStart( TheScanner)) { cout << "GeomDbLoad : Error on line " << TheScanner.GetCurrLineNbr() << endl ; return false ; } // ciclo di lettura degli oggetti bOk = true ; do { if ( ! LoadOneObject( TheScanner, bEnd)) { bOk = false ; cout << "GeomDbLoad : Error on line " << TheScanner.GetCurrLineNbr() << endl ; } } while ( ! bEnd) ; return bOk ; } //---------------------------------------------------------------------------- bool GeomDB::LoadStart( CScan& TheScanner) { string sLine ; // recupero la prima linea if ( ! TheScanner.GetLine( sLine)) return false ; // deve essere l'intestazione if ( sLine != "START") return false ; // recupero la riga successiva if ( ! TheScanner.GetLine( sLine)) return false ; // leggo i parametri // TODO return true ; } //---------------------------------------------------------------------------- bool GeomDB::LoadOneObject( CScan& TheScanner, bool& bEnd) { string sErrLine ; GdbObj* pGdbObj ; // in generale non è fine file bEnd = false ; // creo l'oggetto GDB pGdbObj = new GdbObj ; if ( pGdbObj == nullptr) return false ; // se lettura dati va bene if ( pGdbObj->Load( TheScanner, sErrLine)) { // se inserimento nel DB va bene if ( AddToGeomDB( pGdbObj)) return true ; // altrimenti errore else { delete pGdbObj ; return false ; } } else { // in ogni caso non serve oggetto GDB delete pGdbObj ; // se fine dati if ( sErrLine == "END") { bEnd = true ; return true ; } // altrimenti errore else return false ; } } //---------------------------------------------------------------------------- bool GeomDB::Save( std::ofstream& osOut) { bool bOk ; PGDBO_LIST::iterator Iter ; bOk = true ; // intestazione osOut << "START" << endl ; osOut << "GeomDB,1.2a3" << endl ; // ciclo di scrittura degli oggetti for ( Iter = m_GeomDB.begin() ; Iter != m_GeomDB.end() ; ++Iter) { // oggetto geometrico if ( ! (*Iter)->Save( osOut)) bOk = false ; } // terminazione osOut << "END" << endl ; return bOk ; } //---------------------------------------------------------------------------- bool GeomDB::ExistsObj( const std::string& sName) { STRPGDBO_UMAP::const_iterator Iter ; Iter = m_GdbNameMap.find( sName) ; return ( Iter != m_GdbNameMap.end()) ; } //---------------------------------------------------------------------------- GdbObj* GeomDB::GetGdbObj( const std::string& sName) { STRPGDBO_UMAP::const_iterator Iter ; Iter = m_GdbNameMap.find( sName) ; if ( Iter != m_GdbNameMap.end()) return Iter->second ; else return nullptr ; } //---------------------------------------------------------------------------- bool GeomDB::AddToGeomDB( GdbObj* pGdbObj) { // verifico validità oggetto puntato if ( pGdbObj == nullptr) return false ; // verifica validità e unicità del nome if ( pGdbObj->m_sName.length() == 0 || ExistsObj( pGdbObj->m_sName)) return false ; // inserisco in lista principale m_GeomDB.push_back( pGdbObj) ; // inserisco in mappa nomi m_GdbNameMap[ pGdbObj->m_sName] = pGdbObj ; return true ; } //---------------------------------------------------------------------------- bool GeomDB::AddGeoObj( const std::string& sName, IGeoObj* pGeoObj) { GdbObj* pGdbObj ; // verifico validità nome if ( sName.length() == 0) return false ; // verifico validità oggetto Geo if ( pGeoObj == nullptr || ! pGeoObj->IsValid()) return false ; // alloco oggetto Gdb pGdbObj = new(nothrow) GdbObj ; if ( pGdbObj == nullptr) return false ; // assegno nome pGdbObj->m_sName = sName ; // assegno dati pGdbObj->m_pGeoObj = pGeoObj ; // inserisco nel DB if ( ! AddToGeomDB( pGdbObj)) { delete pGdbObj ; return false ; } return true ; } //---------------------------------------------------------------------------- GeoObjType GeomDB::GetObjType( const std::string& sName) { const GdbObj* pGdbObj ; // recupero l'oggetto if ( ( pGdbObj = GetGdbObj( sName)) == nullptr) return GEO_NONE ; // ne identifico il tipo return pGdbObj->GetType() ; } //---------------------------------------------------------------------------- IGeoObj* GeomDB::GetGeoObj( const string& sName) { const GdbObj* pGdbObj ; // recupero l'oggetto Gdb if ( ( pGdbObj = GetGdbObj( sName)) == nullptr) return nullptr ; // restituisco il suo contenuto geometrico return pGdbObj->m_pGeoObj ; } //---------------------------------------------------------------------------- bool GeomDB::Copy( const string& sNameSou, const string& sNameDest) { GdbObj* pGdoSou ; GdbObj* pGdoDest ; // verifico esistenza del sorgente if ( ( pGdoSou = GetGdbObj( sNameSou)) == nullptr) return false ; // eseguo la copia if ( ( pGdoDest = pGdoSou->Clone()) == nullptr) return false ; // assegno il nuovo nome pGdoDest->m_sName = sNameDest ; // inserisco nel DB if ( ! AddToGeomDB( pGdoDest)) { delete pGdoDest ; return false ; } return true ; } //---------------------------------------------------------------------------- bool GeomDB::Erase( const string& sName) { PGDBO_LIST::iterator Iter ; // elimino da mappa dei nomi m_GdbNameMap.erase( sName) ; // elimino da lista principale for ( Iter = m_GeomDB.begin() ; Iter != m_GeomDB.end() ; Iter++) { // se oggetto cercato if ( (*Iter)->m_sName == sName) { // lo disalloco (distruttore virtuale) delete( (*Iter)) ; // lo tolgo dalla lista m_GeomDB.erase( Iter) ; // esco return true ; } } return false ; } //---------------------------------------------------------------------------- bool GeomDB::Translate( const string& sName, const Vector3d& vtMove) { GdbObj* pGdbObj ; // recupero l'oggetto if ( ( pGdbObj = GetGdbObj( sName)) == nullptr) return false ; // eseguo l'operazione return pGdbObj->Translate( vtMove) ; } //---------------------------------------------------------------------------- bool GeomDB::Rotate( const string& sName, const Point3d& ptAx, const Vector3d& vtAx, double dCosAng, double dSinAng) { GdbObj* pGdbObj ; // recupero l'oggetto if ( ( pGdbObj = GetGdbObj( sName)) == nullptr) return false ; // eseguo l'operazione return pGdbObj->Rotate( ptAx, vtAx, dCosAng, dSinAng) ; } //---------------------------------------------------------------------------- bool GeomDB::Scale( const string& sName, const Point3d& ptCen, double dCoeffX, double dCoeffY, double dCoeffZ) { GdbObj* pGdbObj ; // recupero l'oggetto if ( ( pGdbObj = GetGdbObj( sName)) == nullptr) return false ; // eseguo l'operazione return pGdbObj->Scale( ptCen, dCoeffX, dCoeffY, dCoeffZ) ; } //---------------------------------------------------------------------------- bool GeomDB::Mirror( const string& sName, const Point3d& ptOn, const Vector3d& vtNorm) { GdbObj* pGdbObj ; // recupero l'oggetto if ( ( pGdbObj = GetGdbObj( sName)) == nullptr) return false ; // eseguo l'operazione return pGdbObj->Mirror( ptOn, vtNorm) ; }