421 lines
9.8 KiB
C++
421 lines
9.8 KiB
C++
//----------------------------------------------------------------------------
|
|
// 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 <iostream>
|
|
#include <new>
|
|
#include "/EgtDev/Include/EgnStringUtils.h"
|
|
#include "/EgtDev/Include/EgtReleasePointer.h"
|
|
#include "GdbObj.h"
|
|
#include "GeomDB.h"
|
|
|
|
using namespace std ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
IGeomDB*
|
|
CreateGeomDB( void)
|
|
{
|
|
return static_cast<IGeomDB*> ( new GeomDB) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
// GeomDB
|
|
//----------------------------------------------------------------------------
|
|
GeomDB::GeomDB( void)
|
|
{
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
GeomDB::~GeomDB( void)
|
|
{
|
|
Clear() ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
GeomDB::Init( void)
|
|
{
|
|
// imposto numero minimo buckets di m_GdbIdMap
|
|
m_GdbIdMap.rehash( 1024) ;
|
|
|
|
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_GdbIdMap.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( int nId)
|
|
{
|
|
INTPGDBO_UMAP::const_iterator Iter ;
|
|
|
|
|
|
Iter = m_GdbIdMap.find( nId) ;
|
|
return ( Iter != m_GdbIdMap.end()) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
GdbObj*
|
|
GeomDB::GetGdbObj( int nId)
|
|
{
|
|
INTPGDBO_UMAP::const_iterator Iter ;
|
|
|
|
|
|
Iter = m_GdbIdMap.find( nId) ;
|
|
if ( Iter != m_GdbIdMap.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_nId <= 0 || ExistsObj( pGdbObj->m_nId))
|
|
return false ;
|
|
|
|
// inserisco in lista principale
|
|
m_GeomDB.push_back( pGdbObj) ;
|
|
|
|
// inserisco in mappa nomi
|
|
m_GdbIdMap[ pGdbObj->m_nId] = pGdbObj ;
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
GeomDB::AddGeoObj( int nId, IGeoObj* pGeoObj)
|
|
{
|
|
GdbObj* pGdbObj ;
|
|
|
|
|
|
// assegno GeoObj a gestore puntatore con rilascio automatico
|
|
ReleasePtr<IGeoObj> pRPGeoObj( pGeoObj) ;
|
|
// verifico validità nome
|
|
if ( nId <= 0)
|
|
return false ;
|
|
// verifico validità oggetto Geo
|
|
if ( ! IsValid( pRPGeoObj) || ! pRPGeoObj->IsValid())
|
|
return false ;
|
|
// alloco oggetto Gdb
|
|
pGdbObj = new(nothrow) GdbObj ;
|
|
if ( pGdbObj == nullptr)
|
|
return false ;
|
|
// assegno nome
|
|
pGdbObj->m_nId = nId ;
|
|
// assegno dati ( ma non ne trasferisco il possesso)
|
|
pGdbObj->m_pGeoObj = Get( pRPGeoObj) ;
|
|
// inserisco nel DB
|
|
if ( ! AddToGeomDB( pGdbObj)) {
|
|
delete pGdbObj ;
|
|
return false ;
|
|
}
|
|
// rilascio il possesso di GeoObj
|
|
Release( pRPGeoObj) ;
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
GeoObjType
|
|
GeomDB::GetObjType( int nId)
|
|
{
|
|
const GdbObj* pGdbObj ;
|
|
|
|
|
|
// recupero l'oggetto
|
|
if ( ( pGdbObj = GetGdbObj( nId)) == nullptr)
|
|
return GEO_NONE ;
|
|
// ne identifico il tipo
|
|
return pGdbObj->GetType() ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
IGeoObj*
|
|
GeomDB::GetGeoObj( int nId)
|
|
{
|
|
const GdbObj* pGdbObj ;
|
|
|
|
|
|
// recupero l'oggetto Gdb
|
|
if ( ( pGdbObj = GetGdbObj( nId)) == nullptr)
|
|
return nullptr ;
|
|
|
|
// restituisco il suo contenuto geometrico
|
|
return pGdbObj->m_pGeoObj ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
GeomDB::Copy( int nIdSou, int nIdDest)
|
|
{
|
|
GdbObj* pGdoSou ;
|
|
GdbObj* pGdoDest ;
|
|
|
|
|
|
// verifico validità Id destinazione
|
|
if ( nIdDest <= 0)
|
|
return false ;
|
|
|
|
// verifico esistenza del sorgente
|
|
if ( ( pGdoSou = GetGdbObj( nIdSou)) == nullptr)
|
|
return false ;
|
|
|
|
// eseguo la copia
|
|
if ( ( pGdoDest = pGdoSou->Clone()) == nullptr)
|
|
return false ;
|
|
|
|
// assegno il nuovo nome
|
|
pGdoDest->m_nId = nIdDest ;
|
|
|
|
// inserisco nel DB
|
|
if ( ! AddToGeomDB( pGdoDest)) {
|
|
delete pGdoDest ;
|
|
return false ;
|
|
}
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
GeomDB::Erase( int nId)
|
|
{
|
|
PGDBO_LIST::iterator Iter ;
|
|
|
|
|
|
// elimino da mappa dei nomi
|
|
m_GdbIdMap.erase( nId) ;
|
|
|
|
// elimino da lista principale
|
|
for ( Iter = m_GeomDB.begin() ; Iter != m_GeomDB.end() ; Iter++) {
|
|
// se oggetto cercato
|
|
if ( (*Iter)->m_nId == nId) {
|
|
// lo disalloco (distruttore virtuale)
|
|
delete( (*Iter)) ;
|
|
// lo tolgo dalla lista
|
|
m_GeomDB.erase( Iter) ;
|
|
// esco
|
|
return true ;
|
|
}
|
|
}
|
|
|
|
return false ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
GeomDB::Translate( int nId, const Vector3d& vtMove)
|
|
{
|
|
GdbObj* pGdbObj ;
|
|
|
|
|
|
// recupero l'oggetto
|
|
if ( ( pGdbObj = GetGdbObj( nId)) == nullptr)
|
|
return false ;
|
|
|
|
// eseguo l'operazione
|
|
return pGdbObj->Translate( vtMove) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
GeomDB::Rotate( int nId, const Point3d& ptAx, const Vector3d& vtAx, double dCosAng, double dSinAng)
|
|
{
|
|
GdbObj* pGdbObj ;
|
|
|
|
|
|
// recupero l'oggetto
|
|
if ( ( pGdbObj = GetGdbObj( nId)) == nullptr)
|
|
return false ;
|
|
|
|
// eseguo l'operazione
|
|
return pGdbObj->Rotate( ptAx, vtAx, dCosAng, dSinAng) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
GeomDB::Scale( int nId, const Point3d& ptCen, double dCoeffX, double dCoeffY, double dCoeffZ)
|
|
{
|
|
GdbObj* pGdbObj ;
|
|
|
|
|
|
// recupero l'oggetto
|
|
if ( ( pGdbObj = GetGdbObj( nId)) == nullptr)
|
|
return false ;
|
|
|
|
// eseguo l'operazione
|
|
return pGdbObj->Scale( ptCen, dCoeffX, dCoeffY, dCoeffZ) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
GeomDB::Mirror( int nId, const Point3d& ptOn, const Vector3d& vtNorm)
|
|
{
|
|
GdbObj* pGdbObj ;
|
|
|
|
|
|
// recupero l'oggetto
|
|
if ( ( pGdbObj = GetGdbObj( nId)) == nullptr)
|
|
return false ;
|
|
|
|
// eseguo l'operazione
|
|
return pGdbObj->Mirror( ptOn, vtNorm) ;
|
|
}
|