/* * p2d_area_holes.cpp - Fill an area with holes * * Written 2012, 2015 by Werner Almesberger * Copyright 2012, 2015 by Werner Almesberger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */ /* * References: * http://www.cgal.org/Manual/latest/examples/Straight_skeleton_2/ * Create_skeleton_and_offset_polygons_with_holes_2.cpp * http://www.cgal.org/Manual/latest/examples/Straight_skeleton_2/print.h */ extern "C" { #include #include "poly2d.h" } /* * @@@ Prevent spurious aborts with * * terminate called after throwing an instance of 'CGAL::Precondition_exception' * what(): CGAL ERROR: precondition violation! * Expr: is_simple_2(first, last, traits) * File: /usr/include/CGAL/Polygon_2/Polygon_2_algorithms_impl.h * Line: 420 * * Note that we also need to check the polygons for simplicity in recurse_area, * or this may still lead to assertion failures. */ #define CGAL_POLYGON_NO_PRECONDITIONS #include "cgal_helper.h" #include #include #include #include #include typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef CGAL::Polygon_2 Polygon_2; typedef CGAL::Polygon_with_holes_2 Polygon_with_holes; typedef boost::shared_ptr PolygonPtr; typedef std::vector PolygonPtrVector; struct p2d *res = NULL, **last = &res, *np; static void append_poly(Polygon_2 poly, struct p2d ***last) { **last = P2_to_p2d(poly); *last = &(**last)->next; } static void recurse_area(Polygon_with_holes poly, double current, double next, struct p2d ***last) { if (!poly.outer_boundary().is_simple()) return; for (Polygon_with_holes::Hole_const_iterator hit = poly.holes_begin(); hit != poly.holes_end(); ++hit) if (!hit->is_simple()) return; PolygonPtrVector tmp = CGAL::create_interior_skeleton_and_offset_polygons_with_holes_2( current, poly); for (PolygonPtrVector::const_iterator pit = tmp.begin(); pit != tmp.end(); ++pit) { append_poly((*pit)->outer_boundary(), last); recurse_area(**pit, next, next, last); for (Polygon_with_holes::Hole_const_iterator hit = (*pit)->holes_begin(); hit != (*pit)->holes_end(); ++hit) { append_poly(*hit, last); } } } extern "C" void p2d_area_holes_append(const struct p2d *p, const struct p2d *holes, double first, double next, struct p2d ***last) { const struct p2d *h; assert(p2d_is_closed(p)); Polygon_with_holes poly(p2d_to_P2(p, 1)); for (h = holes; h; h = h->next) { assert(p2d_is_closed(h)); poly.add_hole(p2d_to_P2(h, 0)); } recurse_area(poly, first, next, last); } extern "C" struct p2d *p2d_area_holes(const struct p2d *p, const struct p2d *holes, double first, double next) { struct p2d *res = NULL, **last = &res; p2d_area_holes_append(p, holes, first, next, &last); return res; }