I want to check if a point lies within, or outside a polygon with holes. Specifically, I am interested if a given point would be inside the "filled area" of a polygon with holes; if the point lies within a hole, I will consider that to be outside of the polygon with holes.
I understand that there is a CGAL function check_inside
that checks if a point lies inside a polygon (without holes). There is also a CGAL function connect_holes
that plots a path from topmost vertex in each hole in the polygon with holes to the polygon itself. I can see two workarounds with these functions to achieve my objective, but I was wondering if there is a CGAL function that does this directly.
See oriented_side() member function of CGAL::General_polygon_set_2<>. Apparently, there are also a free (overloaded) functions (for some reason not documented).
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Boolean_set_operations_2.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Polygon_with_holes_2<Kernel> Polygon_with_holes_2;
# define nice(os) ((os == CGAL::ON_ORIENTED_BOUNDARY) ? "on boundary" : \
(os == CGAL::POSITIVE) ? "inside" : "outside")
int main() {
Polygon_2 hole;
hole.push_back(Point_2(1, 1));
hole.push_back(Point_2(1, 2));
hole.push_back(Point_2(2, 2));
hole.push_back(Point_2(2, 1));
Polygon_2 out;
out.push_back(Point_2(0, 0));
out.push_back(Point_2(3, 0));
out.push_back(Point_2(3, 3));
out.push_back(Point_2(0, 3));
Polygon_with_holes_2 pwh(out, &hole, &hole+1);
std::cout << pwh << std::endl;
auto os = CGAL::oriented_side(Point_2(0, 0), pwh);
std::cout << "(0,0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(0.5, 0.5), pwh);
std::cout << "(0,0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(1, 1), pwh);
std::cout << "(0,0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(2.5, 2.5), pwh);
std::cout << "(0,0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(3, 3), pwh);
std::cout << "(0,0) is : " << nice(os) << std::endl;
os = CGAL::oriented_side(Point_2(3.5, 3.5), pwh);
std::cout << "(0,0) is : " << nice(os) << std::endl;
return 0;
}