diff --git a/PolyLine.cpp b/PolyLine.cpp index 97fe40a..b89aae7 100644 --- a/PolyLine.cpp +++ b/PolyLine.cpp @@ -783,11 +783,11 @@ PolyLine::RemoveAlignedPoints( double dToler) dToler = max( dToler, LIN_TOL_MIN) ; double dSqTol = dToler * dToler ; // vettore contenente i punti della polyline - PNTUVECTOR vPtU ; vPtU.reserve( int( m_lUPoints.size())) ; + PNTUVECTOR vPtU ; vPtU.resize( int( m_lUPoints.size())) ; // interatore primo punto auto ptAct = m_lUPoints.begin() ; for ( int i = 0 ; i < int( m_lUPoints.size()) ; ++ i) { - vPtU[i] = *ptAct ; + vPtU[i] = *ptAct; ptAct = next( ptAct) ; // incremento iteratore } @@ -797,25 +797,41 @@ PolyLine::RemoveAlignedPoints( double dToler) // POLYLINE APERTA if ( ! IsClosed()) { // considero tutti i punti della PolyLine + vPtU_simple.push_back( vPtU.front()) ; if ( ! douglasPeuckerSimplification( vPtU, dSqTol, 0, int( vPtU.size()) - 1, vPtU_simple)) return false ; + vPtU_simple.push_back( vPtU.back()) ; } // POLYLINE CHIUSA else { - // considero tutti i punti della PolyLine ad eccezione degli ultimi due - if ( ! douglasPeuckerSimplification( vPtU, dSqTol, 0, int( vPtU.size()) - 2, vPtU_simple)) + // cerco il punto pił distante dal primo + double dMaxDist = 0. ; + int nMaxInd = 0 ; + for ( int i = 1 ; i < int( vPtU.size()) ; ++ i) { + double dCurrDist = Dist( vPtU[0].first, vPtU[i].first) ; + if ( dCurrDist > dMaxDist) { + dMaxDist = dCurrDist ; + nMaxInd = i ; + } + } + // recupero due PolyLine di approssimazione + vPtU_simple.push_back( vPtU.front()) ; + if ( ! douglasPeuckerSimplification( vPtU, dSqTol, 0, nMaxInd, vPtU_simple)) return false ; - // controllo se includere l'ultimo punto, calcolando la distanza tra esso e la retta - // definita dagli estremi dei punti semplificati ( primo ed ultimo) - double dLastSqDist = 0. ; - DistPointLine DPL( vPtU.back().first, vPtU_simple.front().first, vPtU_simple.back().first) ; - if ( DPL.GetSqDist( dLastSqDist) && dLastSqDist > dSqTol) - vPtU_simple.push_back( vPtU.back()) ; + vPtU_simple.push_back( vPtU[ nMaxInd]) ; + if ( ! douglasPeuckerSimplification( vPtU, dSqTol, nMaxInd, int( vPtU.size()) - 1, vPtU_simple)) + return false ; + vPtU_simple.push_back( vPtU.back()) ; } + // ordino + sort( vPtU_simple.begin(), vPtU_simple.end(), + []( const POINTU& a , const POINTU& b) { return ( a.second < b.second) ; }) ; + // pulisco e aggiungo i parametri ricavati m_lUPoints.clear() ; - copy( vPtU_simple.begin(), vPtU_simple.end(), m_lUPoints.begin()) ; + for ( int i = 0 ; i < int( vPtU_simple.size()) ; ++ i) + m_lUPoints.push_back( vPtU_simple[i]) ; return true ; }