Root/poly2d/p2d_offset.cpp

1/*
2 * p2d_offset.cpp - Simple offsetting (without dogbones)
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 by Werner Almesberger
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 */
12
13/*
14 * References:
15 * http://www.cgal.org/Manual/latest/examples/Straight_skeleton_2/
16 * Create_saop_from_polygon_with_holes_2.cpp
17 * http://www.cgal.org/Manual/latest/examples/Straight_skeleton_2/print.h
18 */
19
20extern "C" {
21    #include <assert.h>
22
23    #include "poly2d.h"
24}
25
26#include "cgal_helper.h"
27
28#include <vector>
29#include <boost/shared_ptr.hpp>
30
31#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
32#include <CGAL/Polygon_with_holes_2.h>
33#include <CGAL/create_offset_polygons_from_polygon_with_holes_2.h>
34
35
36typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
37
38typedef CGAL::Polygon_2<K> Polygon_2;
39typedef CGAL::Polygon_with_holes_2<K> Polygon_with_holes;
40
41typedef boost::shared_ptr<Polygon_2> PolygonPtr;
42
43typedef std::vector<PolygonPtr> PolygonPtrVector;
44
45
46extern "C" struct p2d *p2d_offset_holes(const struct p2d *p,
47    const struct p2d *holes, double off)
48{
49    const struct p2d *h;
50    struct p2d *res = NULL, **last = &res;
51
52    assert(p2d_is_closed(p));
53    Polygon_with_holes poly(p2d_to_P2(p, 1));
54
55    for (h = holes; h; h = h->next) {
56        assert(p2d_is_closed(h));
57        poly.add_hole(p2d_to_P2(h, 0));
58    }
59
60    PolygonPtrVector tmp = off > 0 ?
61         CGAL::create_exterior_skeleton_and_offset_polygons_2(off,
62            poly.outer_boundary()) :
63        CGAL::create_interior_skeleton_and_offset_polygons_2(-off, poly);
64
65    for (PolygonPtrVector::const_iterator pit = tmp.begin();
66        pit != tmp.end(); ++pit) {
67        *last = P2_to_p2d(**pit);
68        last = &(*last)->next;
69    }
70
71    return res;
72}
73
74
75extern "C" struct p2d *p2d_offset(const struct p2d *p, double off)
76{
77    return p2d_offset_holes(p, NULL, off);
78}
79

Archive Download this file

Branches:
master



interactive