Fix and simplify polygon soup repairing subfunction "simplify_polygon"

This commit is contained in:
Mael Rouxel-Labbé 2020-07-28 09:54:47 +02:00
parent 655135011f
commit 5a6fc89495
1 changed files with 9 additions and 31 deletions

View File

@ -126,43 +126,21 @@ bool simplify_polygon(PointRange& points,
{ {
const std::size_t ini_polygon_size = polygon.size(); const std::size_t ini_polygon_size = polygon.size();
// Start at the last since if two points are identical, the second one gets removed. for(std::size_t i=0; i<polygon.size(); ++i)
// By starting at 'last', we ensure that 'to_remove' is ordered from closest to .begin()
// to closest to .end()
std::size_t last = ini_polygon_size - 1, i = last;
bool stop = false;
std::vector<std::size_t> to_remove;
do
{ {
std::size_t next_i = (i == last) ? 0 : i+1; const std::size_t s = polygon.size();
stop = (next_i == last); if(s == 1)
break;
while(polygon[i] == polygon[next_i] || // combinatorial equality const std::size_t ni = (i + 1) % s;
traits.equal_3_object()(points[polygon[i]], points[polygon[next_i]])) // geometric equality if(polygon[i] == polygon[ni] ||
traits.equal_3_object()(points[polygon[i]], points[polygon[ni]]))
{ {
to_remove.push_back(next_i);
#ifdef CGAL_PMP_REPAIR_POLYGON_SOUP_VERBOSE_PP #ifdef CGAL_PMP_REPAIR_POLYGON_SOUP_VERBOSE_PP
std::cout << "Duplicate point: polygon[" << next_i << "] = " << polygon[next_i] << std::endl; std::cout << "Duplicate point: polygon[" << ni << "] = " << polygon[ni] << std::endl;
#endif #endif
next_i = (next_i == last) ? 0 : next_i+1; polygon.erase(polygon.begin() + i--);
// Every duplicate in front of 'last' (circularly-speaking) has already been cleared
if(next_i == last)
{
stop = true;
break;
}
} }
i = next_i;
}
while(!stop);
while(!to_remove.empty())
{
polygon.erase(polygon.begin() + to_remove.back());
to_remove.pop_back();
} }
const std::size_t removed_points_n = ini_polygon_size - polygon.size(); const std::size_t removed_points_n = ini_polygon_size - polygon.size();