mirror of
git://projects.qi-hardware.com/cae-tools.git
synced 2024-12-23 12:58:58 +02:00
102 lines
2.5 KiB
C++
102 lines
2.5 KiB
C++
|
/*
|
||
|
* 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;
|
||
|
}
|
||
|
|
||
|
|
||
|
static void recurse_area(Polygon_with_holes poly, double curr_off,
|
||
|
double next_off, struct p2d ***last)
|
||
|
{
|
||
|
PolygonPtrVector tmp =
|
||
|
CGAL::create_interior_skeleton_and_offset_polygons_with_holes_2(
|
||
|
curr_off, poly);
|
||
|
|
||
|
for (PolygonPtrVector::const_iterator pit = tmp.begin();
|
||
|
pit != tmp.end(); ++pit) {
|
||
|
append_poly((*pit)->outer_boundary(), last);
|
||
|
recurse_area(**pit, next_off, next_off, 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 offset, double overlap,
|
||
|
struct p2d ***last)
|
||
|
{
|
||
|
const struct p2d *h;
|
||
|
|
||
|
assert(p2d_is_closed(p));
|
||
|
Polygon_with_holes poly(p2d_to_P2(p));
|
||
|
|
||
|
for (h = holes; h; h = h->next) {
|
||
|
assert(p2d_is_closed(h));
|
||
|
poly.add_hole(p2d_to_P2(h));
|
||
|
}
|
||
|
|
||
|
recurse_area(poly, offset, offset-overlap, last);
|
||
|
}
|
||
|
|
||
|
|
||
|
extern "C" struct p2d *p2d_area_holes(const struct p2d *p,
|
||
|
const struct p2d *holes, double offset, double overlap)
|
||
|
{
|
||
|
struct p2d *res = NULL, **last = &res;
|
||
|
|
||
|
p2d_area_holes_append(p, holes, offset, overlap, &last);
|
||
|
return res;
|
||
|
}
|