//---------------------------------------------------------------------------- // EgalTech 2017-2018 //---------------------------------------------------------------------------- // File : DistPointTria.cpp Data : 14.12.18 Versione : 1.9l2 // Contenuto : Implementazione della classe distanza punto da triangolo. // // // // Modifiche : 19.10.17 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "ProjPlane.h" #include "/EgtDev/Include/EGkDistPointLine.h" #include "/EgtDev/Include/EGkDistPointTria.h" //---------------------------------------------------------------------------- DistPointTriangle::DistPointTriangle( const Point3d& ptP, const Triangle3d& Tria) { if ( &Tria == nullptr || ! Tria.IsValid()) { // distanza non calcolabile m_dSqDist = - 1 ; return ; } // eseguo calcoli e determino il quadrato della distanza Calculate( ptP, Tria) ; // dichiaro distanza non ancora calcolata m_dDist = - 1 ; } //---------------------------------------------------------------------------- void DistPointTriangle::Calculate( const Point3d& ptP, const Triangle3d& Tria) { // Proiezione del punto sul piano del triangolo Point3d ptQ = ptP - (( ptP - Tria.GetCentroid()) * Tria.GetN()) * Tria.GetN() ; // Verifico se il punto proiettato sta nel triangolo if ( PointInTria( ptQ, Tria) != PTT_OUT) { m_dSqDist = SqDist( ptP, ptQ) ; m_ptMinDist = ptQ ; return ; } // Determino la minima distanza dai tre lati m_dSqDist = - 1 ; for ( int i = 0 ; i < 3 ; ++ i) { DistPointLine dstPL( ptP, Tria.GetP( i), Tria.GetP( ( i + 1) % 3)) ; double dSqDist ; if ( dstPL.GetSqDist( dSqDist) && ( m_dSqDist < 0 || dSqDist < m_dSqDist)) { m_dSqDist = dSqDist ; dstPL.GetMinDistPoint( m_ptMinDist) ; } } } //---------------------------------------------------------------------------- bool DistPointTriangle::GetSqDist( double& dSqDist) { if ( m_dSqDist < 0) return false ; dSqDist = m_dSqDist ; return true ; } //---------------------------------------------------------------------------- bool DistPointTriangle::GetDist( double& dDist) { if ( m_dSqDist < 0) return false ; if ( m_dDist < 0) m_dDist = sqrt( m_dSqDist) ; dDist = m_dDist ; return true ; } //---------------------------------------------------------------------------- bool DistPointTriangle::GetMinDistPoint( Point3d& ptMinDist) { if ( m_dSqDist < 0) return false ; ptMinDist = m_ptMinDist ; return true ; }