2012-05-05 03:40:55 +03:00
|
|
|
/*
|
|
|
|
* p2d_area_holes.cpp - Fill an area with holes
|
|
|
|
*
|
|
|
|
* Written 2012 by Werner Almesberger
|
|
|
|
* Copyright 2012 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 <assert.h>
|
|
|
|
|
|
|
|
#include "poly2d.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "cgal_helper.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
|
|
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
|
|
|
|
#include <CGAL/Polygon_with_holes_2.h>
|
|
|
|
#include <CGAL/create_offset_polygons_from_polygon_with_holes_2.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
|
|
|
|
|
|
|
|
typedef CGAL::Polygon_2<K> Polygon_2;
|
|
|
|
typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes;
|
|
|
|
|
|
|
|
typedef boost::shared_ptr<Polygon_with_holes> PolygonPtr;
|
|
|
|
|
|
|
|
typedef std::vector<PolygonPtr> 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-08 04:45:39 +03:00
|
|
|
static void recurse_area(Polygon_with_holes poly, double current,
|
|
|
|
double next, struct p2d ***last)
|
2012-05-05 03:40:55 +03:00
|
|
|
{
|
|
|
|
PolygonPtrVector tmp =
|
|
|
|
CGAL::create_interior_skeleton_and_offset_polygons_with_holes_2(
|
2012-05-08 04:45:39 +03:00
|
|
|
current, poly);
|
2012-05-05 03:40:55 +03:00
|
|
|
|
|
|
|
for (PolygonPtrVector::const_iterator pit = tmp.begin();
|
|
|
|
pit != tmp.end(); ++pit) {
|
|
|
|
append_poly((*pit)->outer_boundary(), last);
|
2012-05-08 04:45:39 +03:00
|
|
|
recurse_area(**pit, next, next, last);
|
2012-05-05 03:40:55 +03:00
|
|
|
|
|
|
|
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,
|
2012-05-08 04:45:39 +03:00
|
|
|
const struct p2d *holes, double first, double next,
|
2012-05-05 03:40:55 +03:00
|
|
|
struct p2d ***last)
|
|
|
|
{
|
|
|
|
const struct p2d *h;
|
|
|
|
|
|
|
|
assert(p2d_is_closed(p));
|
2012-05-08 04:12:21 +03:00
|
|
|
Polygon_with_holes poly(p2d_to_P2(p, 1));
|
2012-05-05 03:40:55 +03:00
|
|
|
|
|
|
|
for (h = holes; h; h = h->next) {
|
|
|
|
assert(p2d_is_closed(h));
|
2012-05-08 04:12:21 +03:00
|
|
|
poly.add_hole(p2d_to_P2(h, 0));
|
2012-05-05 03:40:55 +03:00
|
|
|
}
|
|
|
|
|
2012-05-08 04:45:39 +03:00
|
|
|
recurse_area(poly, first, next, last);
|
2012-05-05 03:40:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" struct p2d *p2d_area_holes(const struct p2d *p,
|
2012-05-08 04:45:39 +03:00
|
|
|
const struct p2d *holes, double first, double next)
|
2012-05-05 03:40:55 +03:00
|
|
|
{
|
|
|
|
struct p2d *res = NULL, **last = &res;
|
|
|
|
|
2012-05-08 04:45:39 +03:00
|
|
|
p2d_area_holes_append(p, holes, first, next, &last);
|
2012-05-05 03:40:55 +03:00
|
|
|
return res;
|
|
|
|
}
|