cae-tools/poly2d/p2d_offset.cpp

79 lines
2.0 KiB
C++

/*
* p2d_offset.cpp - Simple offsetting (without dogbones)
*
* Written 2012 by Werner Almesberger
* Copyright 2012 by Werner Almesberger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
/*
* References:
* http://www.cgal.org/Manual/latest/examples/Straight_skeleton_2/
* Create_saop_from_polygon_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_2> PolygonPtr;
typedef std::vector<PolygonPtr> PolygonPtrVector;
extern "C" struct p2d *p2d_offset_holes(const struct p2d *p,
const struct p2d *holes, double off)
{
const struct p2d *h;
struct p2d *res = NULL, **last = &res;
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));
}
PolygonPtrVector tmp = off > 0 ?
CGAL::create_exterior_skeleton_and_offset_polygons_2(off,
poly.outer_boundary()) :
CGAL::create_interior_skeleton_and_offset_polygons_2(-off, poly);
for (PolygonPtrVector::const_iterator pit = tmp.begin();
pit != tmp.end(); ++pit) {
*last = P2_to_p2d(**pit);
last = &(*last)->next;
}
return res;
}
extern "C" struct p2d *p2d_offset(const struct p2d *p, double off)
{
return p2d_offset_holes(p, NULL, off);
}