4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=windows-1252">
5 <title>Custom Polygon
</title>
10 <p><font face=
"Courier New">/*
<br>
11 Copyright
2008 Intel Corporation
<br>
13 Use, modification and distribution are subject to the Boost Software License,
<br>
14 Version
1.0. (See accompanying file LICENSE_1_0.txt or copy at
<br>
15 http://www.boost.org/LICENSE_1_0.txt).
<br>
17 #include
<boost/polygon/polygon.hpp
><br>
18 #include
<cassert
><br>
19 #include
<list
><br>
20 namespace gtl = boost::polygon;
<br>
21 using namespace boost::polygon::operators;
<br><br>
22 //first lets turn our polygon usage code into a generic
<br>
23 //function parameterized by polygon type
<br>
24 template
<typename Polygon
><br>
25 void test_polygon() {
<br>
26 //lets construct a
10x10 rectangle shaped polygon
<br>
27 typedef typename gtl::polygon_traits
<Polygon
>::point_type Point;
<br>
28 Point pts[] = {gtl::construct
<Point
>(
0,
0),
<br>
29 gtl::construct
<Point
>(
10,
0),
<br>
30 gtl::construct
<Point
>(
10,
10),
<br>
31 gtl::construct
<Point
>(
0,
10) };
<br>
32 Polygon poly;
<br>
33 gtl::set_points(poly, pts, pts+
4);
<br>
35 //now lets see what we can do with this polygon
<br>
36 assert(gtl::area(poly) ==
100.0f);
<br>
37 assert(gtl::contains(poly, gtl::construct
<Point
>(
5,
5)));
<br>
38 assert(!gtl::contains(poly, gtl::construct
<Point
>(
15,
5)));
<br>
39 gtl::rectangle_data
<int
> rect;
<br>
40 assert(gtl::extents(rect, poly)); //get bounding box of poly
<br>
41 assert(gtl::equivalence(rect, poly)); //hey, that's slick
<br>
42 assert(gtl::winding(poly) == gtl::COUNTERCLOCKWISE);
<br>
43 assert(gtl::perimeter(poly) ==
40.0f);
<br>
45 //add
5 to all coords of poly
<br>
46 gtl::convolve(poly, gtl::construct
<Point
>(
5,
5));
<br>
47 //multiply all coords of poly by
2<br>
48 gtl::scale_up(poly,
2);
<br>
49 gtl::set_points(rect, gtl::point_data
<int
>(
10,
10),
<br>
50 gtl::point_data
<int
>(
30,
30));
<br>
51 assert(gtl::equivalence(poly, rect));
<br>
54 //Now lets declare our own polygon class
<br>
55 //Oops, we need a point class to support our polygon, lets borrow
<br>
56 //the CPoint example
<br>
62 //we have to get CPoint working with boost polygon to make our polygon
<br>
63 //that uses CPoint working with boost polygon
<br>
64 namespace boost { namespace polygon {
<br>
65 template
<><br>
66 struct geometry_concept
<CPoint
> { typedef point_concept type; };
<br>
67 template
<><br>
68 struct point_traits
<CPoint
> {
<br>
69 typedef int coordinate_type;
<br>
71 static inline coordinate_type get(const CPoint
& point,
<br>
72 orientation_2d orient) {
<br>
73 if(orient == HORIZONTAL)
<br>
74 return point.x;
<br>
75 return point.y;
<br>
76 }
<br>
79 template
<><br>
80 struct point_mutable_traits
<CPoint
> {
<br>
81 typedef int coordinate_type;
<br>
83 static inline void set(CPoint
& point, orientation_2d orient,
85 if(orient == HORIZONTAL)
<br>
86 point.x = value;
<br>
87 else
<br>
88 point.y = value;
<br>
89 }
<br>
90 static inline CPoint construct(int x_value, int y_value) {
<br>
91 CPoint retval;
<br>
92 retval.x = x_value;
<br>
93 retval.y = y_value;
<br>
94 return retval;
<br>
95 }
<br>
99 //I'm lazy and use the stl everywhere to avoid writing my own classes
<br>
100 //my toy polygon is a std::list
<CPoint
><br>
101 typedef std::list
<CPoint
> CPolygon;
<br>
103 //we need to specialize our polygon concept mapping in boost polygon
<br>
104 namespace boost { namespace polygon {
<br>
105 //first register CPolygon as a polygon_concept type
<br>
106 template
<><br>
107 struct geometry_concept
<CPolygon
>{ typedef polygon_concept type; };
<br>
109 template
<><br>
110 struct polygon_traits
<CPolygon
> {
<br>
111 typedef int coordinate_type;
<br>
112 typedef CPolygon::const_iterator iterator_type;
<br>
113 typedef CPoint point_type;
<br>
115 // Get the begin iterator
<br>
116 static inline iterator_type begin_points(const CPolygon
& t) {
<br>
117 return t.begin();
<br>
118 }
<br>
120 // Get the end iterator
<br>
121 static inline iterator_type end_points(const CPolygon
& t) {
<br>
122 return t.end();
<br>
123 }
<br>
125 // Get the number of sides of the polygon
<br>
126 static inline std::size_t size(const CPolygon
& t) {
<br>
127 return t.size();
<br>
128 }
<br>
130 // Get the winding direction of the polygon
<br>
131 static inline winding_direction winding(const CPolygon
& t) {
<br>
132 return unknown_winding;
<br>
133 }
<br>
136 template
<><br>
137 struct polygon_mutable_traits
<CPolygon
> {
<br>
138 //expects stl style iterators
<br>
139 template
<typename iT
><br>
140 static inline CPolygon
& set_points(CPolygon
& t,
<br>
141
142 iT input_begin, iT input_end) {
<br>
143 t.clear();
<br>
144 t.insert(t.end(), input_begin, input_end);
<br>
145 return t;
<br>
146 }
<br>
151 //now there's nothing left to do but test that our polygon
<br>
152 //works with library interfaces
<br>
154 test_polygon
<CPolygon
>(); //woot!
<br>
160 <table class=
"docinfo" rules=
"none" frame=
"void" id=
"table1">
162 <col class=
"docinfo-name"><col class=
"docinfo-content">
166 <th class=
"docinfo-name">Copyright:
</th>
167 <td>Copyright © Intel Corporation
2008-
2010.
</td>
170 <th class=
"docinfo-name">License:
</th>
171 <td class=
"field-body">Distributed under the Boost Software License,
172 Version
1.0. (See accompanying file
<tt class=
"literal">
173 <span class=
"pre">LICENSE_1_0.txt
</span></tt> or copy at
174 <a class=
"reference" target=
"_top" href=
"http://www.boost.org/LICENSE_1_0.txt">
175 http://www.boost.org/LICENSE_1_0.txt
</a>)
</td>