]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/example/03_polygon_example.cpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
13 #include <algorithm> // for reverse, unique
17 #include <boost/geometry/geometry.hpp>
18 #include <boost/geometry/geometries/point_xy.hpp>
19 #include <boost/geometry/geometries/polygon.hpp>
20 #include <boost/geometry/geometries/adapted/c_array.hpp>
21 #include <boost/geometry/geometries/multi_polygon.hpp>
23 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian
)
26 std::string
boolstr(bool v
)
28 return v
? "true" : "false";
33 using namespace boost::geometry
;
35 typedef model::d2::point_xy
<double> point_2d
;
36 typedef model::polygon
<point_2d
> polygon_2d
;
37 typedef model::box
<point_2d
> box_2d
;
39 // Define a polygon and fill the outer ring.
40 // In most cases you will read it from a file or database
43 const double coor
[][2] = {
44 {2.0, 1.3}, {2.4, 1.7}, {2.8, 1.8}, {3.4, 1.2}, {3.7, 1.6},
45 {3.4, 2.0}, {4.1, 3.0}, {5.3, 2.6}, {5.4, 1.2}, {4.9, 0.8}, {2.9, 0.7},
46 {2.0, 1.3} // closing point is opening point
48 assign_points(poly
, coor
);
51 // Polygons should be closed, and directed clockwise. If you're not sure if that is the case,
52 // call the correct algorithm
55 // Polygons can be streamed as text
56 // (or more precisely: as DSV (delimiter separated values))
57 std::cout
<< dsv(poly
) << std::endl
;
59 // As with lines, bounding box of polygons can be calculated
62 std::cout
<< dsv(b
) << std::endl
;
64 // The area of the polygon can be calulated
65 std::cout
<< "area: " << area(poly
) << std::endl
;
67 // And the centroid, which is the center of gravity
70 std::cout
<< "centroid: " << dsv(cent
) << std::endl
;
73 // The number of points can be requested per ring (using .size())
74 // or per polygon (using num_points)
75 std::cout
<< "number of points in outer ring: " << poly
.outer().size() << std::endl
;
77 // Polygons can have one or more inner rings, also called holes, islands, interior rings.
80 poly
.inners().resize(1);
81 model::ring
<point_2d
>& inner
= poly
.inners().back();
83 const double coor
[][2] = { {4.0, 2.0}, {4.2, 1.4}, {4.8, 1.9}, {4.4, 2.2}, {4.0, 2.0} };
84 assign_points(inner
, coor
);
89 std::cout
<< "with inner ring:" << dsv(poly
) << std::endl
;
90 // The area of the polygon is changed of course
91 std::cout
<< "new area of polygon: " << area(poly
) << std::endl
;
93 std::cout
<< "new centroid: " << dsv(cent
) << std::endl
;
95 // You can test whether points are within a polygon
96 std::cout
<< "point in polygon:"
97 << " p1: " << boolstr(within(make
<point_2d
>(3.0, 2.0), poly
))
98 << " p2: " << boolstr(within(make
<point_2d
>(3.7, 2.0), poly
))
99 << " p3: " << boolstr(within(make
<point_2d
>(4.4, 2.0), poly
))
102 // As with linestrings and points, you can derive from polygon to add, for example,
103 // fill color and stroke color. Or SRID (spatial reference ID). Or Z-value. Or a property map.
104 // We don't show this here.
106 // Clip the polygon using a box
107 box_2d
cb(make
<point_2d
>(1.5, 1.5), make
<point_2d
>(4.5, 2.5));
108 typedef std::vector
<polygon_2d
> polygon_list
;
111 intersection(cb
, poly
, v
);
112 std::cout
<< "Clipped output polygons" << std::endl
;
113 for (polygon_list::const_iterator it
= v
.begin(); it
!= v
.end(); ++it
)
115 std::cout
<< dsv(*it
) << std::endl
;
118 typedef model::multi_polygon
<polygon_2d
> polygon_set
;
120 union_(cb
, poly
, ps
);
123 convex_hull(poly
, hull
);
124 std::cout
<< "Convex hull:" << dsv(hull
) << std::endl
;
126 // If you really want:
127 // You don't have to use a vector, you can define a polygon with a deque
128 // You can specify the container for the points and for the inner rings independantly
130 typedef model::polygon
<point_2d
, true, true, std::deque
, std::deque
> deque_polygon
;
132 ring_type
<deque_polygon
>::type
& ring
= exterior_ring(poly2
);
133 append(ring
, make
<point_2d
>(2.8, 1.9));
134 append(ring
, make
<point_2d
>(2.9, 2.4));
135 append(ring
, make
<point_2d
>(3.3, 2.2));
136 append(ring
, make
<point_2d
>(3.2, 1.8));
137 append(ring
, make
<point_2d
>(2.8, 1.9));
138 std::cout
<< dsv(poly2
) << std::endl
;