Files
EgtGeomKernel/SurfTriMesh.h
T
DarioS e4815830cd EgtGeomKernel 2.5g1 :
- aggiunta funzione GetSurfTmNearestVertex
- a SurfTriMesh aggiunti metodi MoveVertex e GetAllVertInFacet
- corretta DoCompacting di SurfTriMesh aggiungendo verifica del calcolo della normale dei triangoli.
2023-07-07 17:04:15 +02:00

424 lines
22 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2014-2023
//----------------------------------------------------------------------------
// File : SurfTriMesh.h Data : 07.07.23 Versione : 2.5g1
// Contenuto : Dichiarazione della classe Superficie TriMesh.
//
//
//
// Modifiche : 26.03.14 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
#pragma once
#include "ObjGraphicsMgr.h"
#include "DllMain.h"
#include "GeoObjRW.h"
#include "/EgtDev/Include/EGkSurfTriMesh.h"
#include "/EgtDev/Include/EGkHashGrids3d.h"
#include <deque>
#include <set>
class SurfFlatRegion ;
//----------------------------------------------------------------------------
// Classe Vertice
class StmVert
{
public :
StmVert( void) : ptP(), nIdTria( SVT_NULL), nFlag( 0), nTemp( 0) {}
StmVert( const Point3d& ptQ) : ptP( ptQ), nIdTria( SVT_NULL), nFlag( 0), nTemp( 0) {}
StmVert( const Point3d& ptQ, int nIdT, int nF) : ptP( ptQ), nIdTria( nIdT), nFlag( nF), nTemp( 0) {}
public :
Point3d ptP ;
int nIdTria ;
int nFlag ;
mutable int nTemp ;
} ;
//----------------------------------------------------------------------------
// Classe Triangolo
class StmTria
{
public :
StmTria( void) : vtN(), nIdFacet( SVT_NULL), nTFlag( 0), nEFlag( 0), nPart( SVT_NULL), nTemp( 0)
{ nIdVert[0] = SVT_NULL ; nIdVert[1] = SVT_NULL ; nIdVert[2] = SVT_NULL ;
nIdAdjac[0] = SVT_NULL ; nIdAdjac[1] = SVT_NULL ; nIdAdjac[2] = SVT_NULL ; }
StmTria( const int nIdV[3]) : vtN(), nIdFacet( SVT_NULL), nTFlag( 0), nEFlag( 0), nPart( SVT_NULL), nTemp( 0)
{ nIdVert[0] = nIdV[0] ; nIdVert[1] = nIdV[1] ; nIdVert[2] = nIdV[2] ;
nIdAdjac[0] = SVT_NULL ; nIdAdjac[1] = SVT_NULL ; nIdAdjac[2] = SVT_NULL ; }
StmTria( const int nIdV[3], int nTF) : vtN(), nIdFacet( SVT_NULL), nTFlag( nTF), nEFlag( 0), nPart( SVT_NULL), nTemp( 0)
{ nIdVert[0] = nIdV[0] ; nIdVert[1] = nIdV[1] ; nIdVert[2] = nIdV[2] ;
nIdAdjac[0] = SVT_NULL ; nIdAdjac[1] = SVT_NULL ; nIdAdjac[2] = SVT_NULL ; }
StmTria( const int nIdV[3], const int nIdA[3], const Vector3d& vtV, int nTF, int nEF)
: vtN( vtV), nIdFacet( SVT_NULL), nTFlag( nTF), nEFlag( nEF), nPart( SVT_NULL), nTemp( 0)
{ nIdVert[0] = nIdV[0] ; nIdVert[1] = nIdV[1] ; nIdVert[2] = nIdV[2] ;
nIdAdjac[0] = nIdA[0] ; nIdAdjac[1] = nIdA[1] ; nIdAdjac[2] = nIdA[2] ; }
public :
int nIdVert[3] ;
int nIdAdjac[3] ;
int nETempFlag[3] ;
Vector3d vtN ;
int nIdFacet ;
int nTFlag ;
int nEFlag ;
mutable int nPart ;
mutable int nTemp ;
mutable int nTempPart ;
} ;
//----------------------------------------------------------------------------
// Classe di tre interi per code di elaborazione sui triangoli (senza iterazioni)
struct Stm3Int {
Stm3Int( int nI, int nJ, int nK) : nI1( nI), nI2( nJ), nI3( nK) {}
int nI1 ; int nI2 ; int nI3 ;
} ;
//----------------------------------------------------------------------------
// Struttura segmento di intersezione
struct IntSegment {
Point3d ptSt ;
Point3d ptEn ;
Vector3d vtOuter ;
bool bDegenerate ;
} ;
// Tipo chain
typedef std::vector<IntSegment> Chain ;
// Tipo vettore di Chain
typedef std::vector<Chain> CHAINVECTOR ;
// Tipo unordered map di Chain
typedef std::unordered_map< int, Chain> CHAINMAP ;
// Tipo unordered map di TRIA3DVECTOR
typedef std::unordered_map< int, TRIA3DVECTOR> TRIA3DVECTORMAP ;
//----------------------------------------------------------------------------
// Struttura intersezione linea-faccia
struct LineFacetClass {
Point3d ptSt, ptEn ;
int nTypeA, nTypeB ;
LineFacetClass( void) {
nTypeA = 0 ;
nTypeB = 0 ;
}
LineFacetClass( const Point3d& ptS, const Point3d& ptE, int nTpA, int nTpB) {
ptSt = ptS ;
ptEn = ptE ;
nTypeA = nTpA ;
nTypeB = nTpB ;
}
} ;
typedef std::vector<LineFacetClass> LineFacetClassVector ;
//----------------------------------------------------------------------------
enum FacetPlaneIntersType { FPI_ERROR = 0, FPI_CUT = 1, FPI_IN = 2, FPI_OUT = 3, FPI_ON = 4 } ;
//----------------------------------------------------------------------------
// Strutture e contenitori contatti interno-interno
struct IntersInnSeg {
Point3d ptSt ;
Point3d ptEn ;
Vector3d vtOuter ;
IntersInnSeg( void) {
;
}
IntersInnSeg( const Point3d& ptS, const Point3d& ptE) {
ptSt = ptS ;
ptEn = ptE ;
}
IntersInnSeg( const Point3d& ptS, const Point3d& ptE, const Vector3d& vtO) {
ptSt = ptS ;
ptEn = ptE ;
vtOuter = vtO ;
}
} ;
typedef std::vector<IntersInnSeg> IntersInnChain ;
typedef std::vector<IntersInnChain> INNCHAINVECTOR ;
typedef std::unordered_map<int, IntersInnChain> INTERSCHAINMAP ;
// Contatti con spigolo
struct IntersEdge {
Point3d ptSt ;
Point3d ptEn ;
INTVECTOR vOthFacetIndex ;
IntersEdge( const Point3d& ptS, const Point3d& ptE, const INTVECTOR& vOFI) {
ptSt = ptS;
ptEn = ptE;
vOthFacetIndex = vOFI ;
}
} ;
typedef std::vector<IntersEdge> IntersEdgeVec ;
typedef std::unordered_map<int, IntersEdgeVec> INTERSEDGEMAP ;
// Parte di faccia
struct FacetPiece {
POLYLINEVECTOR vPieceLoop ;
int nPiecePart ;
} ;
typedef std::unordered_map<int, std::vector<FacetPiece>> PieceMap ;
//----------------------------------------------------------------------------
class SurfTriMesh : public ISurfTriMesh, public IGeoObjRW
{
public : // IGeoObj
~SurfTriMesh( void) override ;
SurfTriMesh* Clone( void) const override ;
GeoObjType GetType( void) const override ;
bool IsValid( void) const override
{ return ( m_nStatus == OK) ; }
const std::string& GetTitle( void) const override ;
bool Dump( std::string& sOut, bool bMM = true, const char* szNewLine = "\n") const override ;
bool GetLocalBBox( BBox3d& b3Loc, int nFlag = BBF_STANDARD) const override ;
bool GetBBox( const Frame3d& frRef, BBox3d& b3Ref, int nFlag = BBF_STANDARD) const override ;
bool Translate( const Vector3d& vtMove) override ;
bool Rotate( const Point3d& ptAx, const Vector3d& vtAx, double dAngDeg) override
{ double dAngRad = dAngDeg * DEGTORAD ;
return Rotate( ptAx, vtAx, cos( dAngRad), sin( dAngRad)) ; }
bool Rotate( const Point3d& ptAx, const Vector3d& vtAx, double dCosAng, double dSinAng) override ;
bool Scale( const Frame3d& frRef, double dCoeffX, double dCoeffY, double dCoeffZ) override ;
bool Mirror( const Point3d& ptOn, const Vector3d& vtNorm) override ;
bool Shear( const Point3d& ptOn, const Vector3d& vtNorm, const Vector3d& vtDir, double dCoeff) override ;
bool ToGlob( const Frame3d& frRef) override ;
bool ToLoc( const Frame3d& frRef) override ;
bool LocToLoc( const Frame3d& frOri, const Frame3d& frDest) override ;
void SetObjGraphics( IObjGraphics* pOGr) override
{ m_OGrMgr.SetObjGraphics( pOGr) ; }
IObjGraphics* GetObjGraphics( void) override
{ return m_OGrMgr.GetObjGraphics() ; }
const IObjGraphics* GetObjGraphics( void) const override
{ return m_OGrMgr.GetObjGraphics() ; }
void SetTempProp( int nProp, int nPropInd = 0) override
{ if ( nPropInd >= 0 && nPropInd < 2)
m_nTempProp[nPropInd] = nProp ; }
int GetTempProp( int nPropInd = 0) const override
{ return (( nPropInd >= 0 && nPropInd < 2) ? m_nTempProp[nPropInd] : 0) ; }
public : // ISurf
bool IsSimple( void) const override
{ return true ; }
bool IsClosed( void) const override
{ return m_bClosed ; }
bool GetArea( double& dArea) const override ;
bool GetVolume( double& dVolume) const override ;
bool GetCentroid( Point3d& ptCen) const override ;
bool Invert( void) override ;
public : // ISurfTriMesh
bool CopyFrom( const IGeoObj* pGObjSrc) override ;
bool Init( int nNumVert, int nNumTria, int nNumFacet = 0) override ;
void SetLinearTolerance( double dLinTol) override
{ m_dLinTol = std::max( dLinTol, EPS_SMALL) ; }
void SetBoundaryAngle( double dBoundaryAngDeg) override
{ m_dBoundaryAng = std::max( dBoundaryAngDeg, EPS_ANG_SMALL) ;
m_dCosBndAng = cos( m_dBoundaryAng * DEGTORAD) ;
m_OGrMgr.Reset() ; }
void SetSmoothAngle( double dSmoothAngDeg) override
{ m_dSmoothAng = std::max( dSmoothAngDeg, EPS_ANG_SMALL) ;
m_dCosSmAng = cos( m_dSmoothAng * DEGTORAD) ;
m_OGrMgr.Reset() ; }
int AddVertex( const Point3d& ptVert) override ;
bool MoveVertex( int nInd, const Point3d& ptNewVert) override ;
int AddTriangle( const int nIdVert[3], int nTFlag = 0) override ;
bool RemoveTriangle( int nId) override ;
bool AdjustTopology( void) override ;
bool CreateByFlatContour( const PolyLine& PL) override ;
bool CreateByRegion( const POLYLINEVECTOR& vPL) override ;
bool CreateByExtrusion( const PolyLine& PL, const Vector3d& vtExtr) override ;
bool CreateByPointCurve( const Point3d& ptP, const PolyLine& PL) override ;
bool CreateByTwoCurves( const PolyLine& PL1, const PolyLine& PL2, int nRuledType) override ;
bool CreateByRevolution( const PolyLine& PL, const Point3d& ptAx, const Vector3d& vtAx,
double dAngRot, double dStepRot) override ;
bool CreateByScrewing( const PolyLine& PL, const Point3d& ptAx, const Vector3d& vtAx,
double dAngRot, double dStepRot, double dMove) override ;
bool DoCompacting( double dTol = EPS_SMALL) override ;
bool DoSewing( const ISurfTriMesh& stmOther, const Frame3d& frOther = GLOB_FRM, double dTol = 2 * EPS_SMALL) override ;
bool IsEmpty( void) const override ;
int GetVertexCount( void) const override ;
int GetTriangleCount( void) const override ;
int GetTriangleCount( int nTFlag) const override ;
int GetVertexSize( void) const override
{ return int( m_vVert.size()) ; }
int GetTriangleSize( void) const override
{ return int( m_vTria.size()) ; }
double GetLinearTolerance( void) const override
{ return m_dLinTol ; }
double GetSmoothAngle( void) const override
{ return m_dSmoothAng ; }
bool GetVertex( int nId, Point3d& ptP) const override ;
int GetFirstVertex( Point3d& ptP) const override ;
int GetNextVertex( int nId, Point3d& ptP) const override ;
bool GetTriangle( int nId, int nIdVert[3]) const override ;
int GetFirstTriangle( int nIdVert[3]) const override ;
int GetNextTriangle( int nId, int nIdVert[3]) const override ;
bool GetTriangle( int nId, Triangle3d& Tria) const override ;
int GetFirstTriangle( Triangle3d& Tria) const override ;
int GetNextTriangle( int nId, Triangle3d& Tria) const override ;
bool GetTriangle( int nId, Triangle3dEx& Tria) const override ;
int GetFirstTriangle( Triangle3dEx& Tria) const override ;
int GetNextTriangle( int nId, Triangle3dEx& Tria) const override ;
int GetAllTriaAroundVertex( int nV, INTVECTOR& vT, bool& bCirc) const override ;
bool GetVertexSmoothNormal( int nV, int nT, Vector3d& vtN) const override ;
bool GetTriangleBoundaryEdges( int nId, TriFlags3d& TFlags) const override ;
bool GetTriangleSmoothNormals( int nId, TriNormals3d& TNrms) const override ;
SurfTriMesh* CloneTriangle( int nT) const override ;
bool GetLoops( POLYLINEVECTOR& vPL) const override ;
bool GetSilhouette( const Vector3d& vtDir, double dTol, POLYLINEVECTOR& vPL) const override ;
int GetFacetCount( void) const override ;
int GetFacetSize( void) const override
{ return int( m_vFacet.size()) ; }
int GetFacetFromTria( int nT) const override ;
bool GetAllTriaInFacet( int nF, INTVECTOR& vT) const override ;
bool GetAllVertInFacet( int nF, INTVECTOR& vVert) const override ;
bool GetFacetLoops( int nF, POLYLINEVECTOR& vPL) const override ;
bool GetFacetAdjacencies( int nF, INTMATRIX& vAdj) const override ;
bool GetFacetNearestEndPoint( int nF, const Point3d& ptNear, Point3d& ptEnd, Vector3d& vtN) const override ;
bool GetFacetNearestMidPoint( int nF, const Point3d& ptNear, Point3d& ptMid, Vector3d& vtN) const override ;
bool GetFacetCenter( int nF, Point3d& ptCen, Vector3d& vtN) const override ;
bool GetFacetNormal( int nF, Vector3d& vtN) const override ;
bool GetFacetArea( int nF, double& dArea) const override ;
bool GetFacetsContact( int nF1, int nF2, bool& bAdjac, Point3d& ptP1, Point3d& ptP2, double& dAng) const override ;
bool RemoveFacet( int nF) override ;
SurfTriMesh* CloneFacet( int nF) const override ;
bool SwapFacets( int nF1, int nF2) override ;
bool GetFacetLocalBBox( int nF, BBox3d& b3Loc, int nFlag = BBF_STANDARD) const override ;
bool GetFacetBBox( int nF, const Frame3d& frRef, BBox3d& b3Ref, int nFlag = BBF_STANDARD) const override ;
bool Cut( const Plane3d& plPlane, bool bSaveOnEq) override ;
bool GeneralizedCut( const ICurve& cvCurve, bool bSaveOnEq) override ;
bool Add( const ISurfTriMesh& Other) override ;
bool Intersect( const ISurfTriMesh& Other) override ;
bool Subtract( const ISurfTriMesh& Other) override ;
bool GetSurfClassification( const ISurfTriMesh& ClassifierSurf,
INTVECTOR& vTriaIn, INTVECTOR& vTriaOut, INTVECTOR& vTriaOnP, INTVECTOR& vTriaOnM, INTVECTOR& vTriaIndef) override ;
bool CutWithOtherSurf( const ISurfTriMesh& CutterSurf, bool bInVsOut, bool bSaveOnEq) override ;
bool Repair( double dMaxEdgeLen = MAX_EDGE_LEN_STD) override ;
bool GetAllTriaOverlapBox( const BBox3d& b3Box, INTVECTOR& vT) const override ;
const BBox3d& GetAllTriaBox( void) const override ;
int GetPartCount( void) const override ;
bool RemovePart( int nPart) override ;
SurfTriMesh* ClonePart( int nPart) const override ;
int GetMaxTFlag( void) const override
{ return m_nMaxTFlag ; }
bool ResetTFlags( void) override ;
public : // IGeoObjRW
int GetNgeId( void) const override ;
bool Save( NgeWriter& ngeOut) const override ;
bool Load( NgeReader& ngeIn) override ;
public :
SurfTriMesh( void) ;
SurfTriMesh( const SurfTriMesh& stSrc) : m_pHGrd3d( nullptr)
{ if ( ! CopyFrom( stSrc))
LOG_ERROR( GetEGkLogger(), "SurfTriMesh : copy constructor error") }
SurfTriMesh& operator =( const SurfTriMesh& stSrc)
{ if ( ! CopyFrom( stSrc))
LOG_ERROR( GetEGkLogger(), "SurfTriMesh : copy error")
return *this ; }
bool Clear( void) ;
bool ExistsTriangle( int nT) const
{ return ( nT >= 0 && nT < GetTriangleSize() && m_vTria[nT].nIdVert[0] != SVT_DEL) ; }
bool GetTriangleAdjacencies( int nId, int nIdAdjTriaId[3]) const ;
bool GetTFlag( int nId, int& nFlag) const ;
bool GetTempInt( int nId, int& nTempInt) const ;
bool ResetTempInts( void) const ;
bool SetTFlag( int nId, int nTFlag) ;
bool SetTempInt( int nId, int nTempInt) const ;
private :
typedef std::vector<StmVert> VERTVECTOR ;
typedef std::vector<StmTria> TRIAVECTOR ;
typedef std::deque<Stm3Int> TRINTDEQUE ;
private :
enum Status { ERR = 0, OK = 1, TO_VERIFY = 2} ;
private :
bool CopyFrom( const SurfTriMesh& clSrc) ;
bool Validate( bool bCorrect = false) ;
bool AdjustVertices( void) ;
bool AdjustAdjacencies( void) ;
bool AdjustOrientations( void) ;
bool AdjustTriaOrientation( TRINTDEQUE& S3iQ) ;
bool TestSealing( void) ;
bool PackVertices( void) ;
bool PackTriangles( void) ;
bool SetVertex( int nInd, const StmVert& vV) ;
bool SetTriangle( int nInd, const StmTria& tT) ;
bool CalcTriangleNormal( int nT) ;
bool InvertTriangle( int nT) ;
int Next( int i) const
{ return (( i + 1) % 3) ; }
int Prev( int i) const
{ return (( i + 2) % 3) ; }
bool FindVertexInTria( int nV, int nT, int& nK) const ;
int NextIndAroundVertex( int nInd, int nSize, bool bCirc) const ;
int PrevIndAroundVertex( int nInd, int nSize, bool bCirc) const ;
bool GetTriangleSmoothNormal( int nT, int nV, Vector3d& vtN) const ;
bool MarchAlongLoop( int nT, int nV, int nTimeStamp, PolyLine& PL) const ;
bool MarchOneTria( int& nT, int& nV, int nTimeStamp, PolyLine& PL, bool& bEnd) const ;
bool VerifyPolylinesForTwoCurves( const PolyLine& PL1, const PolyLine& PL2) const ;
bool AddBiTriangle( const int nIdVert[4]) ;
bool VerifyFaceting( void) const ;
bool UpdateFaceting( void) ;
bool UpdateOneFace( int nFacet, int nT) ;
bool UpdateTriaFaceting( int nRefT, int nFacet, const Plane3d& plPlane, int nT) ;
bool SetFacet( int nInd, int nT) ;
bool VerifyAdjacTriaFacet( INTVECTOR& vT) const ;
bool MarchAlongFacetLoop( int nF, int nT, int nV, int nTimeStamp, PolyLine& PL) const ;
bool MarchOneFacetTria( int nF, int& nT, int& nV, int nTimeStamp, PolyLine& PL, bool& bEnd) const ;
void ResetHashGrids3d( void) const ;
bool VerifyHashGrids3d( void) const ;
bool VerifyConnection( void) const ;
bool CutTriangleByPlane( int nTriaId, const Plane3d& plPlane, bool bSaveOnEq, bool& bModif) ;
bool CutByTriangles( const Plane3d& plPlane, bool bSaveOnEq, bool& bModif) ;
bool DecomposeLoop( CHAINVECTOR& cvOpenChain, INTVECTOR& vnDegVec, PNTMATRIX& cvBoundClosedLoopVec, BOOLVECTOR& vbInOut) ;
bool RetriangulationForBooleanOperation( CHAINMAP& LoopLines, TRIA3DVECTORMAP& Ambiguos, SurfTriMesh& Surf, bool& bModif) ;
bool AmbiguosTriangleManager( TRIA3DVECTORMAP& Ambiguos, SurfTriMesh& Surf) ;
bool IntersectTriMeshTriangle( SurfTriMesh& Other) ;
bool IdentifyParts( void) const ;
bool RemoveDoubleTriangles( bool& bModified) ;
bool RemoveTJunctions( bool& bModified) ;
bool FlipTriangles( int nTA, int nTB) ;
bool SimplifyFacets( double dMaxEdgeLen = MAX_EDGE_LEN_STD, bool bForced = true) ;
bool AddChainToChain( const Chain& ChainToAdd, PNTVECTOR& OrigChain) ;
bool DistPointFacet( const Point3d& ptP, const POLYLINEVECTOR& vPolyVec, double& dPointFacetDist) ;
bool ChangeStart( const Point3d& ptNewStart, PNTVECTOR& Loop) ;
bool SplitAtPoint( const Point3d& ptStop, const PNTVECTOR& Loop, PNTVECTOR& Loop1, PNTVECTOR& Loop2) ;
private :
ObjGraphicsMgr m_OGrMgr ; // gestore grafica dell'oggetto
Status m_nStatus ; // stato
double m_dLinTol ; // tolleranza lineare di costruzione
double m_dBoundaryAng ; // angolo limite per considerare un lato un contorno (in gradi)
double m_dCosBndAng ; // coseno dell'angolo limite per considerare un lato un contorno
double m_dSmoothAng ; // angolo limite per mediare le normali (in gradi)
double m_dCosSmAng ; // coseno dell'angolo limite per mediare le normali
bool m_bOriented ; // la superficie orientata consistentemente in tutte le sue parti
bool m_bClosed ; // la superficie racchiude un volume
bool m_bFaceted ; // flag di validit della sfaccettatura
VERTVECTOR m_vVert ; // vettore dei vertici
TRIAVECTOR m_vTria ; // vettore dei triangoli
INTVECTOR m_vFacet ; // vettore delle sfaccettature
mutable int m_nTimeStamp ; // orologio locale
int m_nTempProp[2] ; // vettore propriet temporanee
int m_nMaxTFlag ; // massimo valore dei TFlag dei triangoli
mutable int m_nParts ; // numero di parti connesse (-1 se da calcolare)
mutable HashGrids3d* m_pHGrd3d ; // Hash Grid 3d nel suo riferimento
mutable BBox3d m_b3HGrd3d ; // Box3d collegato a Hash Grid 3d
} ;
//-----------------------------------------------------------------------------
inline SurfTriMesh* CreateBasicSurfTriMesh( void)
{ return ( static_cast<SurfTriMesh*>( CreateGeoObj( SRF_TRIMESH))) ; }
inline SurfTriMesh* CloneBasicSurfTriMesh( const IGeoObj* pGObj)
{ if ( pGObj == nullptr || pGObj->GetType() != SRF_TRIMESH)
return nullptr ;
return ( static_cast<SurfTriMesh*>( pGObj->Clone())) ; }
inline const SurfTriMesh* GetBasicSurfTriMesh( const IGeoObj* pGObj)
{ if ( pGObj == nullptr || pGObj->GetType() != SRF_TRIMESH)
return nullptr ;
return ( static_cast<const SurfTriMesh*>( pGObj)) ; }
inline SurfTriMesh* GetBasicSurfTriMesh( IGeoObj* pGObj)
{ if ( pGObj == nullptr || pGObj->GetType() != SRF_TRIMESH)
return nullptr ;
return ( static_cast<SurfTriMesh*>( pGObj)) ; }