0

I have a CGAL::Polygon_2<Kernel> and iterate over its vertices with CGAL vertex circulator. In the CGAL documentation, I cannot find any information about how to use the erase function correctly. Deleting in the middle isn't a problem, but how to handle the circulator, when the first element of it is deleted?

The naive iterator erasing approach works only in the first case, in the second case, the loop finished after one iteration.

// p is of type CGAL::Polygon_2<Kernel>
auto ci = p.vertices_circulator();
auto start = ci;
int i = 0;

do {
  if (i == 0) {
    ci = p.erase(ci);
  } else {
    ++ci;
  }
  ++i;     
} while (ci != start);

This means start points to the formerly second element/now first element after erasing the first one?

How can I handle that case? Thanks!

Yannic
  • 698
  • 8
  • 22

1 Answers1

0

Using --start right before the do will work.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Andreas Fabri
  • 1,144
  • 6
  • 9
  • The loop doesn't terminate with this addition. The ```start``` pointer gets invalid after erasing (based on the default vector container class in ```CGAL::Polygon_2```?). But a solution that works is to replace ```start``` by ```--p.vertices_circulator()``` in the while condition. The disadvantage of this solution is that the last element has to be treated separately. – Yannic Apr 03 '19 at 11:48