2 Copyright 2008 Intel Corporation
4 Use, modification and distribution are subject to the Boost Software License,
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt).
8 #include <boost/polygon/polygon.hpp>
11 namespace gtl
= boost::polygon
;
12 using namespace boost::polygon::operators
;
14 //first lets turn our polygon usage code into a generic
15 //function parameterized by polygon type
16 template <typename Polygon
>
18 //lets construct a 10x10 rectangle shaped polygon
19 typedef typename
gtl::polygon_traits
<Polygon
>::point_type Point
;
20 Point pts
[] = {gtl::construct
<Point
>(0, 0),
21 gtl::construct
<Point
>(10, 0),
22 gtl::construct
<Point
>(10, 10),
23 gtl::construct
<Point
>(0, 10) };
25 gtl::set_points(poly
, pts
, pts
+4);
27 //now lets see what we can do with this polygon
28 assert(gtl::area(poly
) == 100.0f
);
29 assert(gtl::contains(poly
, gtl::construct
<Point
>(5, 5)));
30 assert(!gtl::contains(poly
, gtl::construct
<Point
>(15, 5)));
31 gtl::rectangle_data
<int> rect
;
32 assert(gtl::extents(rect
, poly
)); //get bounding box of poly
33 assert(gtl::equivalence(rect
, poly
)); //hey, that's slick
34 assert(gtl::winding(poly
) == gtl::COUNTERCLOCKWISE
);
35 assert(gtl::perimeter(poly
) == 40.0f
);
37 //add 5 to all coords of poly
38 gtl::convolve(poly
, gtl::construct
<Point
>(5, 5));
39 //multiply all coords of poly by 2
40 gtl::scale_up(poly
, 2);
41 gtl::set_points(rect
, gtl::point_data
<int>(10, 10),
42 gtl::point_data
<int>(30, 30));
43 assert(gtl::equivalence(poly
, rect
));
46 //Now lets declare our own polygon class
47 //Oops, we need a point class to support our polygon, lets borrow
54 //we have to get CPoint working with boost polygon to make our polygon
55 //that uses CPoint working with boost polygon
56 namespace boost
{ namespace polygon
{
58 struct geometry_concept
<CPoint
> { typedef point_concept type
; };
60 struct point_traits
<CPoint
> {
61 typedef int coordinate_type
;
63 static inline coordinate_type
get(const CPoint
& point
,
64 orientation_2d orient
) {
65 if(orient
== HORIZONTAL
)
72 struct point_mutable_traits
<CPoint
> {
73 typedef int coordinate_type
;
75 static inline void set(CPoint
& point
, orientation_2d orient
, int value
) {
76 if(orient
== HORIZONTAL
)
81 static inline CPoint
construct(int x_value
, int y_value
) {
90 //I'm lazy and use the stl everywhere to avoid writing my own classes
91 //my toy polygon is a std::list<CPoint>
92 typedef std::list
<CPoint
> CPolygon
;
94 //we need to specialize our polygon concept mapping in boost polygon
95 namespace boost
{ namespace polygon
{
96 //first register CPolygon as a polygon_concept type
98 struct geometry_concept
<CPolygon
>{ typedef polygon_concept type
; };
101 struct polygon_traits
<CPolygon
> {
102 typedef int coordinate_type
;
103 typedef CPolygon::const_iterator iterator_type
;
104 typedef CPoint point_type
;
106 // Get the begin iterator
107 static inline iterator_type
begin_points(const CPolygon
& t
) {
111 // Get the end iterator
112 static inline iterator_type
end_points(const CPolygon
& t
) {
116 // Get the number of sides of the polygon
117 static inline std::size_t size(const CPolygon
& t
) {
121 // Get the winding direction of the polygon
122 static inline winding_direction
winding(const CPolygon
& t
) {
123 return unknown_winding
;
128 struct polygon_mutable_traits
<CPolygon
> {
129 //expects stl style iterators
130 template <typename iT
>
131 static inline CPolygon
& set_points(CPolygon
& t
,
132 iT input_begin
, iT input_end
) {
134 t
.insert(t
.end(), input_begin
, input_end
);
141 //now there's nothing left to do but test that our polygon
142 //works with library interfaces
144 test_polygon
<CPolygon
>(); //woot!