CCW or CW oriented when using GDAL to add points to CGAL::Polygon_2
When loading points from a file using GDAL, a possible way is as such:
OGRLinearRing* outerRing = ogrPolygon->getExteriorRing();
int NumOuterPoints = outerRing->getNumPoints() - 1; // first point is the same as the last point
for (int i = 0; i < NumOuterPoints; ++i) // degenerate cases: NumOfouterRingPoints < 3?
{
polygon.outer_boundary().push_back(Point(outerRing->getX(i), outerRing->getY(i)));
}
Attention needs to be paid, the code snippet will add the points as a CW(clockwise) manner (or this is depending on the specific dataset), whereas CCW is mostly wanted in CGAL related code. Therefore, in order to do that, we can add the points from the end to the start:
OGRLinearRing* outerRing = ogrPolygon->getExteriorRing();
int NumOuterPoints = outerRing->getNumPoints() - 1; // first point is the same as the last point
for (int i = NumOuterPoints; i > 0; --i) // degenerate cases: NumOfouterRingPoints < 3?
{
polygon.outer_boundary().push_back(Point(outerRing->getX(i), outerRing->getY(i)));
}
Another thing worth mentioning is that
getNumPoints()
will return one more number than the actual number of vertices in the polygon. For example, if there is a square formed with 4 vertices,
getNumPoints()
will return 5. This means
(outerRing->getX(0), outerRing->getY(0))
and
(outerRing->getX(getNumPoints()-1), outerRing->getY(getNumPoints()-1))
both
refer to the same point.