Files
EgtGeomKernel/GeomDB.cpp
T
2013-11-20 17:39:24 +00:00

651 lines
16 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 "GeoConst.h"
#include "GeomDB.h"
#include "GdbObjFactory.h"
#include "GeoObjTypeFun.h"
#include "Scan.h"
#include "StringUtils.h"
using namespace std ;
//----------------------------------------------------------------------------
// 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 sLine ;
GdbObj* pGObj ;
// recupero la prossima linea (con il tipo di oggetto)
if ( ! TheScanner.GetLine( sLine)) {
bEnd = true ;
return false ;
}
// se fine
if ( sLine == "END") {
bEnd = true ;
}
// altrimenti oggetto
else {
bEnd = false ;
// creo l'oggetto
pGObj = GDBOBJ_CREATE( sLine) ;
if ( pGObj == nullptr)
return false ;
// ne leggo i dati e lo inserisco nel DB
if ( ! pGObj->Load( TheScanner) ||
! AddToGeomDB( pGObj)) {
delete pGObj ;
return false ;
}
}
return true ;
}
//----------------------------------------------------------------------------
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::ExistsGdbObj( 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* pGObj)
{
// verifico validità oggetto puntato
if ( pGObj == nullptr)
return false ;
// verifica validità e unicità del nome
if ( pGObj->m_sName.length() == 0 || ExistsGdbObj( pGObj->m_sName))
return false ;
// inserisco in lista principale
m_GeomDB.push_back( pGObj) ;
// inserisco in mappa nomi
m_GdbNameMap[ pGObj->m_sName] = pGObj ;
return true ;
}
//----------------------------------------------------------------------------
bool
GeomDB::AddVector( const string& sName, const Vector3d& Vect)
{
GdbVector3d* pGVect ;
// verifico validità nome
if ( sName.length() == 0)
return false ;
// alloco oggetto
pGVect = new(nothrow) GdbVector3d ;
if ( pGVect == nullptr)
return false ;
// assegno nome
pGVect->m_sName = sName ;
// assegno dati
pGVect->m_gvV.Set( Vect) ;
// inserisco nel DB
if ( ! AddToGeomDB( pGVect)) {
delete pGVect ;
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
GeomDB::AddPoint( const string& sName, const Point3d& Pnt)
{
GdbPoint3d* pGPnt ;
// verifico validità nome
if ( sName.length() == 0)
return false ;
// alloco oggetto
pGPnt = new(nothrow) GdbPoint3d ;
if ( pGPnt == nullptr)
return false ;
// assegno nome
pGPnt->m_sName = sName ;
// assegno dati
pGPnt->m_gpP.Set( Pnt) ;
// inserisco nel DB
if ( ! AddToGeomDB( pGPnt)) {
delete pGPnt ;
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
GeomDB::AddCurveLine( const string& sName, const CurveLine& CrvLine)
{
GdbCurveLine* pGdbCL ;
// verifico validità nome
if ( sName.length() == 0)
return false ;
// verifico validità dati geometrici
if ( ! CrvLine.IsValid())
return false ;
// allocazione oggetto
pGdbCL = new(nothrow) GdbCurveLine ;
if ( pGdbCL == nullptr)
return false ;
// assegnazione nome
pGdbCL->m_sName = sName ;
// assegnazione dati
pGdbCL->m_ln = CrvLine ;
// inserimento nel DB
if ( ! AddToGeomDB( pGdbCL)) {
delete pGdbCL ;
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
GeomDB::AddCurveArc( const string& sName, const CurveArc& CrvArc)
{
GdbCurveArc* pGdbCA ;
// verifico validità nome
if ( sName.length() == 0)
return false ;
// verifico validità dati geometrici
if ( ! CrvArc.IsValid())
return false ;
// alloco oggetto
pGdbCA = new(nothrow) GdbCurveArc ;
if ( pGdbCA == nullptr)
return false ;
// assegno nome
pGdbCA->m_sName = sName ;
// assegno dati
pGdbCA->m_ar = CrvArc ;
// inserisco nel DB
if ( ! AddToGeomDB( pGdbCA)) {
delete pGdbCA ;
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
GeomDB::AddCurveBez( const string& sName, const CurveBezier& CrvBez)
{
GdbCurveBezier* pGdbCB ;
// verifico validità nome
if ( sName.length() == 0)
return false ;
// verifico validità dati geometrici
if ( ! CrvBez.IsValid())
return false ;
// allocazione oggetto
pGdbCB = new(nothrow) GdbCurveBezier ;
if ( pGdbCB == nullptr)
return false ;
// assegnazione nome
pGdbCB->m_sName = sName ;
// assegnamento dati
pGdbCB->m_bz = CrvBez ;
// inserimento nel DB
if ( ! AddToGeomDB( pGdbCB)) {
delete pGdbCB ;
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
GeomDB::AddCurveCompo( const string& sName, const CurveComposite& CrvCompo)
{
GdbCurveComposite* pGdbCC ;
// verifico validità nome
if ( sName.length() == 0)
return false ;
// verifico validità dati geometrici
if ( ! CrvCompo.IsValid())
return false ;
// allocazione oggetto
pGdbCC = new(nothrow) GdbCurveComposite ;
if ( pGdbCC == nullptr)
return false ;
// assegnazione nome
pGdbCC->m_sName = sName ;
// assegnamento dati
pGdbCC->m_cc = CrvCompo ;
// inserimento nel DB
if ( ! AddToGeomDB( pGdbCC)) {
delete pGdbCC ;
return false ;
}
return true ;
}
//----------------------------------------------------------------------------
GeoObjType
GeomDB::GetObjType( const std::string& sName)
{
const GdbObj* pGObj ;
// recupero l'oggetto
if ( ( pGObj = GetGdbObj( sName)) == nullptr)
return GEO_NONE ;
// ne identifico il tipo
return pGObj->GetType() ;
}
//----------------------------------------------------------------------------
const GeoVector3d*
GeomDB::GetGeoVector3d( const string& sName)
{
const GdbObj* pGObj ;
// recupero l'oggetto e ne verifico il tipo
if ( ( pGObj = GetGdbObj( sName)) == nullptr || pGObj->GetType() != GEO_VECT3D)
return nullptr ;
// assegno il vettore per riferimento
return &(static_cast<const GdbVector3d*>(pGObj))->m_gvV ;
}
//----------------------------------------------------------------------------
const Vector3d*
GeomDB::GetVector( const string& sName)
{
return &( GetGeoVector3d( sName)->m_vtV) ;
}
//----------------------------------------------------------------------------
const GeoPoint3d*
GeomDB::GetGeoPoint3d( const string& sName)
{
const GdbObj* pGObj ;
// recupero l'oggetto e ne verifico il tipo
if ( ( pGObj = GetGdbObj( sName)) == nullptr || pGObj->GetType() != GEO_PNT3D)
return nullptr ;
// assegno il punto per riferimento
return &(static_cast<const GdbPoint3d*>(pGObj))->m_gpP ;
}
//----------------------------------------------------------------------------
const Point3d*
GeomDB::GetPoint( const string& sName)
{
return &( GetGeoPoint3d( sName)->m_ptP) ;
}
//----------------------------------------------------------------------------
const CurveLine*
GeomDB::GetCurveLine( const string& sName)
{
const GdbObj* pGObj ;
// recupero l'oggetto e ne verifico il tipo
if ( ( pGObj = GetGdbObj( sName)) == nullptr || pGObj->GetType() != CRV_LINE)
return nullptr ;
// assegno la linea per riferimento
return &(static_cast<const GdbCurveLine*>(pGObj))->m_ln ;
}
//----------------------------------------------------------------------------
const CurveArc*
GeomDB::GetCurveArc( const string& sName)
{
const GdbObj* pGObj ;
// recupero l'oggetto e ne verifico il tipo
if ( ( pGObj = GetGdbObj( sName)) == nullptr || pGObj->GetType() != CRV_ARC)
return nullptr ;
// assegno l'arco per riferimento
return &(static_cast<const GdbCurveArc*>(pGObj))->m_ar ;
}
//----------------------------------------------------------------------------
const CurveBezier*
GeomDB::GetCurveBez( const string& sName)
{
const GdbObj* pGObj ;
// recupero l'oggetto e ne verifico il tipo
if ( ( pGObj = GetGdbObj( sName)) == nullptr || pGObj->GetType() != CRV_BEZ)
return nullptr ;
// assegno la curva di Bezier per riferimento
return &(static_cast<const GdbCurveBezier*>(pGObj))->m_bz ;
}
//----------------------------------------------------------------------------
const CurveComposite*
GeomDB::GetCurveCompo( const std::string& sName)
{
const GdbObj* pGObj ;
// recupero l'oggetto e ne verifico il tipo
if ( ( pGObj = GetGdbObj( sName)) == nullptr || pGObj->GetType() != CRV_COMPO)
return nullptr ;
// assegno la curva di Bezier per riferimento
return &(static_cast<const GdbCurveComposite*>(pGObj))->m_cc ;
}
//----------------------------------------------------------------------------
const Curve*
GeomDB::GetCurve( const string& sName)
{
const GdbObj* pGObj ;
// recupero l'oggetto
if ( ( pGObj = GetGdbObj( sName)) == nullptr)
return nullptr ;
// ritorno il puntatore alla curva (oppure nullptr se non è una curva)
return ::GetCurve( pGObj->GetGeoObj()) ;
}
//----------------------------------------------------------------------------
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* pGObj ;
// recupero l'oggetto
if ( ( pGObj = GetGdbObj( sName)) == nullptr)
return false ;
// eseguo l'operazione
return pGObj->Translate( vtMove) ;
}
//----------------------------------------------------------------------------
bool
GeomDB::Rotate( const string& sName, const Point3d& ptAx, const Vector3d& vtAx, double dAngDeg)
{
GdbObj* pGObj ;
// recupero l'oggetto
if ( ( pGObj = GetGdbObj( sName)) == nullptr)
return false ;
// eseguo l'operazione
return pGObj->Rotate( ptAx, vtAx, dAngDeg * DEGTORAD) ;
}
//----------------------------------------------------------------------------
bool
GeomDB::Scale( const string& sName, const Point3d& ptCen, double dCoeffX, double dCoeffY, double dCoeffZ)
{
GdbObj* pGObj ;
// recupero l'oggetto
if ( ( pGObj = GetGdbObj( sName)) == nullptr)
return false ;
// eseguo l'operazione
return pGObj->Scale( ptCen, dCoeffX, dCoeffY, dCoeffZ) ;
}
//----------------------------------------------------------------------------
bool
GeomDB::Mirror( const string& sName, const Point3d& ptOn, const Vector3d& vtNorm)
{
GdbObj* pGObj ;
// recupero l'oggetto
if ( ( pGObj = GetGdbObj( sName)) == nullptr)
return false ;
// eseguo l'operazione
return pGObj->Mirror( ptOn, vtNorm) ;
}