Files
EgtGeomKernel/Polygon3d.cpp
T
Dario Sassi 64c954ad4b EgtGeomKernel 1.9l4 :
- fabs sostituito da abs
- in Zmap razionalizzazione operazioni taglio spilloni
- in SurfTriMesh UpdateFaceting senza più chiamate recursive.
2018-12-27 11:19:40 +00:00

316 lines
10 KiB
C++

//----------------------------------------------------------------------------
// EgalTech 2015-2017
//----------------------------------------------------------------------------
// File : Polygon3d.cpp Data : 15.10.17 Versione : 1.8j3
// Contenuto : Implementazione della classe Polygon3d.
//
//
//
// Modifiche : 30.08.15 DS Creazione modulo.
//
//
//----------------------------------------------------------------------------
//--------------------------- Include ----------------------------------------
#include "stdafx.h"
#include "/EgtDev/Include/EgkPolygon3d.h"
//----------------------------------------------------------------------------
bool
Polygon3d::Clear( void)
{
m_Plane.Reset() ;
m_vVert.clear() ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::FromRectangle( double dDimX, double dDimY)
{
// verifico i dati
if ( dDimX < EPS_SMALL || dDimY < EPS_SMALL)
return false ;
// assegno il piano
m_Plane.Set( 0, Z_AX) ;
// assegno i vertici del poligono
m_vVert.reserve( 4) ;
m_vVert.emplace_back( + 0.5 * dDimX, + 0.5 * dDimY, 0) ;
m_vVert.emplace_back( - 0.5 * dDimX, + 0.5 * dDimY, 0) ;
m_vVert.emplace_back( - 0.5 * dDimX, - 0.5 * dDimY, 0) ;
m_vVert.emplace_back( + 0.5 * dDimX, - 0.5 * dDimY, 0) ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::FromPolyLine( const PolyLine& PL)
{
// annullo il piano del poligono
m_Plane.Reset() ;
// verifico sia chiusa e piana
Plane3d plPlane ;
double dArea ;
if ( ! PL.IsClosedAndFlat( plPlane, dArea))
return false ;
// assegno il piano
m_Plane = plPlane ;
// assegno i punti
Point3d ptP ;
bool bPoint = PL.GetFirstPoint( ptP, true) ;
// inserisco i punti
while ( bPoint) {
m_vVert.emplace_back( ptP) ;
bPoint = PL.GetNextPoint( ptP, true) ;
}
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::FromPlaneTrimmedWithBox( const Plane3d& plPlane, const Point3d& ptMin, const Point3d& ptMax)
{
// il piano e il box devono essere validi
if ( plPlane.GetVersN().IsSmall() || AreSamePointApprox( ptMin, ptMax))
return false ;
// assegno il piano
m_Plane = plPlane ;
// centro del box proiettato nel piano
Point3d ptCen = Media( ptMin, ptMax, 0.5) ;
ptCen -= (( ptCen - ORIG) * plPlane.GetVersN() - plPlane.GetDist()) * plPlane.GetVersN() ;
// raggio del box
double dDiam = Dist( ptMin, ptMax) ;
// poligono nel suo piano
FromRectangle( dDiam, dDiam) ;
// riferimento del piano
Frame3d frRef ;
frRef.Set( ptCen, plPlane.GetVersN(), FromUprightOrtho( plPlane.GetVersN())) ;
ToGlob( frRef) ;
// lo trimmo con le facce del box
Plane3d plFace ;
plFace.Set( -ptMin.y, -Y_AX) ; // faccia 1 (Norm = Y-)
Trim( plFace, true, true) ;
plFace.Set( -ptMin.z, -Z_AX) ; // faccia 2 (Norm = Z-)
Trim( plFace, true, true) ;
plFace.Set( ptMax.y, Y_AX) ; // faccia 3 (Norm = Y+)
Trim( plFace, true, true) ;
plFace.Set( ptMax.z, Z_AX) ; // faccia 4 (Norm = Z+)
Trim( plFace, true, true) ;
plFace.Set( -ptMin.x, -X_AX) ; // faccia 5 (Norm = X-)
Trim( plFace, true, true) ;
plFace.Set( ptMax.x, X_AX) ; // faccia 6 (Norm = X+)
Trim( plFace, true, true) ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::FromPlaneTrimmedWithBox( const Point3d& ptOn, const Vector3d& vtN, const Point3d& ptMin, const Point3d& ptMax)
{
Plane3d plPlane ;
if ( plPlane.Set( ptOn, vtN))
return false ;
return FromPlaneTrimmedWithBox( plPlane, ptMin, ptMax) ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::Trim( const Plane3d& plPlane, bool bInVsOut, bool bOnEq)
{
// calcolo le distanze dei punti del poligono dal piano
DBLVECTOR vDist ;
vDist.reserve( m_vVert.capacity()) ;
for ( auto& ptP : m_vVert)
vDist.push_back( ( ptP - ORIG) * plPlane.GetVersN() - plPlane.GetDist()) ;
// verifico se il poligono giace nel piano
bool bOnPlane = true ;
for ( auto& dDist : vDist) {
if ( abs( dDist) > EPS_SMALL)
bOnPlane = false ;
}
if ( bOnPlane) {
// se non ammesso equiverso oppure controverso, lo annullo
if ( ! bOnEq || m_Plane.GetVersN() * plPlane.GetVersN() < 0)
m_vVert.clear() ;
return true ;
}
// determino le intersezioni dei lati con il piano
for ( size_t i = 0 ; i < m_vVert.size() ; ++ i) {
// indice del punto finale del lato
size_t j = ( ( i + 1 < m_vVert.size()) ? i + 1 : 0) ;
// se il lato attraversa il piano, inserisco il punto di intersezione nel poligono
if ( ( vDist[i] > EPS_SMALL && vDist[j] < - EPS_SMALL) ||
( vDist[i] < - EPS_SMALL && vDist[j] > EPS_SMALL)) {
double dCoeff = abs( vDist[i]) / ( abs( vDist[i]) + abs( vDist[j])) ;
Point3d ptInt = Media( m_vVert[i], m_vVert[j], dCoeff) ;
m_vVert.insert( m_vVert.begin() + i + 1, ptInt) ;
vDist.insert( vDist.begin() + i + 1, 0.0) ;
++ i ;
}
}
// elimino i punti che non rispettano la posizione rispetto al piano
for ( size_t i = 0 ; i < m_vVert.size() ; ++ i) {
if ( ( bInVsOut && vDist[i] > EPS_SMALL) ||
( ! bInVsOut && vDist[i] < - EPS_SMALL)) {
m_vVert.erase( m_vVert.begin() + i) ;
vDist.erase( vDist.begin() + i) ;
-- i ;
}
}
// se i punti rimasti giacciono tutti sul piano (quindi su una linea), annullo il poligono
bool bIsLine = true ;
for ( auto& dDist : vDist) {
if ( abs( dDist) > EPS_SMALL)
bIsLine = false ;
}
if ( bIsLine)
m_vVert.clear() ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::Trim( const Polygon3d& plyOther, bool bInVsOut, bool bOnEq)
{
if ( GetSideCount() == 0 || plyOther.GetSideCount() == 0)
return true ;
return Trim( plyOther.m_Plane, bInVsOut, bOnEq) ;
}
//----------------------------------------------------------------------------
PolyLine
Polygon3d::GetPolyLine( void) const
{
PolyLine PL ;
// se il piano non è definito, errore
if ( m_Plane.GetVersN().IsSmall())
return PL ;
// converto il poligono in PolyLine
for ( size_t i = 0 ; i < m_vVert.size() ; ++ i)
PL.AddUPoint( int( i), m_vVert[i]) ;
PL.Close() ;
return PL ;
}
//----------------------------------------------------------------------------
void
Polygon3d::Translate( const Vector3d& vtMove)
{
// traslo il piano
m_Plane.Translate( vtMove) ;
// traslo i punti
for ( auto& ptP : m_vVert)
ptP.Translate( vtMove) ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::Rotate( const Point3d& ptAx, const Vector3d& vtAx, double dCosAng, double dSinAng)
{
// ruoto il piano
if ( ! m_Plane.Rotate( ptAx, vtAx, dCosAng, dSinAng))
return false ;
// ruoto i punti
for ( auto& ptP : m_vVert)
ptP.Rotate( ptAx, vtAx, dCosAng, dSinAng) ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::Scale( const Frame3d& frRef, double dCoeffX, double dCoeffY, double dCoeffZ)
{
// verifico validità della scalatura
if ( abs( dCoeffX) < EPS_ZERO && abs( dCoeffY) < EPS_ZERO && abs( dCoeffZ) < EPS_ZERO)
return false ;
// scalo il piano
if ( ! m_Plane.Scale( frRef, dCoeffX, dCoeffY, dCoeffZ))
return false ;
// scalo i punti
for ( auto& ptP : m_vVert)
ptP.Scale( frRef, dCoeffX, dCoeffY, dCoeffZ) ;
// determino se contiene anche un mirror (numero dispari di coefficienti negativi)
bool bMirror = ( dCoeffX < 0) ;
bMirror = ( bMirror ? ( dCoeffY > 0) : ( dCoeffY < 0)) ;
bMirror = ( bMirror ? ( dCoeffZ > 0) : ( dCoeffZ < 0)) ;
if ( bMirror)
reverse( m_vVert.begin(), m_vVert.end()) ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::Mirror( const Point3d& ptOn, const Vector3d& vtNorm)
{
// specchio il piano
if ( ! m_Plane.Mirror( ptOn, vtNorm))
return false ;
// specchio i punti e ne inverto l'ordine
for ( auto& ptP : m_vVert)
ptP.Mirror( ptOn, vtNorm) ;
reverse( m_vVert.begin(), m_vVert.end()) ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::Shear( const Point3d& ptOn, const Vector3d& vtNorm, const Vector3d& vtDir, double dCoeff)
{
// stiro il piano
if ( ! m_Plane.Shear( ptOn, vtNorm, vtDir, dCoeff))
return false ;
// stiro i punti
for ( auto& ptP : m_vVert)
ptP.Shear( ptOn, vtNorm, vtDir, dCoeff) ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::ToGlob( const Frame3d& frRef)
{
// trasformo il piano
if ( ! m_Plane.ToGlob( frRef))
return false ;
// trasformo i punti
for ( auto& ptP : m_vVert)
ptP.ToGlob( frRef) ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::ToLoc( const Frame3d& frRef)
{
// trasformo il piano
if ( ! m_Plane.ToLoc( frRef))
return false ;
// trasformo i punti
for ( auto& ptP : m_vVert)
ptP.ToLoc( frRef) ;
return true ;
}
//----------------------------------------------------------------------------
bool
Polygon3d::LocToLoc( const Frame3d& frOri, const Frame3d& frDest)
{
// se i due riferimenti coincidono, non devo fare alcunché
if ( AreSameFrame( frOri, frDest))
return true ;
// trasformo il piano
if ( ! m_Plane.LocToLoc( frOri, frDest))
return false ;
// trasformo i punti
for ( auto& ptP : m_vVert)
ptP.LocToLoc( frOri, frDest) ;
return true ;
}