//---------------------------------------------------------------------------- // EgalTech 2014-2019 //---------------------------------------------------------------------------- // File : SurfTriMesh.h Data : 12.02.19 Versione : 2.1b2 // 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 #include //---------------------------------------------------------------------------- // 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 Chain ; // Tipo vettore di Chain typedef std::vector 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 per la ricerca delle T-Junction struct TEdgeId { int nTriaId ; int nEdge ; } ; // Overloading dell'operatore < per TEdgeSet inline bool operator<( const TEdgeId& TEdgeA, const TEdgeId& TEdgeB) { if ( TEdgeA.nTriaId != TEdgeB.nTriaId) return ( TEdgeA.nTriaId < TEdgeB.nTriaId) ; return ( TEdgeA.nEdge < TEdgeB.nEdge) ; } // Loop con T-Junction typedef std::vector TJuncLoop ; // Vettore di loop con T-Junction typedef std::vector TJuncLoopVec ; // Set di TEdgeId typedef std::set TJEdgeSet ; //---------------------------------------------------------------------------- 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) override { m_nTempProp = nProp ; } int GetTempProp( void) const override { return m_nTempProp ; } 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 ; 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) 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 ; bool GetLoops( POLYLINEVECTOR& vPL) const override ; bool GetSilhouette( const Vector3d& vtDir, 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 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 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 GetTriangleAdjacencies( int nId, int nIdAdjTriaId[3]) const ; bool GetTriangleFlag( int nId, int& nFlag) const ; bool GetTriangleTempInt( int nId, int& nTempInt) const ; bool ResetTempInt( void) const ; bool SetTFlag( int nId, int nTFlag) ; bool SetTempInt( int nId, int nTempInt) const ; private : typedef std::vector VERTVECTOR ; typedef std::vector TRIAVECTOR ; typedef std::deque 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 ResetFaceting( void) ; bool VerifyFaceting( void) const ; bool UpdateFaceting( void) ; bool UpdateTriaFaceting( int nRefT, int nFacet, const Plane3d& plPlane, int nT) ; bool SetFacet( int nInd, int nT) ; bool VerifyAdjacTriaFacet( int nT, 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 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 RemoveTJunctions( void) ; bool RemoveCaps( void) ; bool RemoveTripleTriangles( void) ; bool ScanForTripleTriangles( bool& bModified) ; bool FlipTriangles( int nTA, int nTB) ; 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 ; // proprietà temporanea 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( CreateGeoObj( SRF_TRIMESH))) ; } inline SurfTriMesh* CloneBasicSurfTriMesh( const IGeoObj* pGObj) { if ( pGObj == nullptr || pGObj->GetType() != SRF_TRIMESH) return nullptr ; return ( static_cast( pGObj->Clone())) ; } inline const SurfTriMesh* GetBasicSurfTriMesh( const IGeoObj* pGObj) { return ( dynamic_cast( pGObj)) ; } inline SurfTriMesh* GetBasicSurfTriMesh( IGeoObj* pGObj) { return ( dynamic_cast( pGObj)) ; }