Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / polygon / doc / gtl_custom_polygon.htm
1 <html>
2
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
5 <title>Custom Polygon</title>
6 </head>
7
8 <body>
9
10 <p><font face="Courier New">/*<br>
11 Copyright 2008 Intel Corporation<br>
12 <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>
16 */<br>
17 #include &lt;boost/polygon/polygon.hpp&gt;<br>
18 #include &lt;cassert&gt;<br>
19 #include &lt;list&gt;<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 &lt;typename Polygon&gt;<br>
25 void test_polygon() {<br>
26 &nbsp; //lets construct a 10x10 rectangle shaped polygon<br>
27 &nbsp; typedef typename gtl::polygon_traits&lt;Polygon&gt;::point_type Point;<br>
28 &nbsp; Point pts[] = {gtl::construct&lt;Point&gt;(0, 0),<br>
29 &nbsp; gtl::construct&lt;Point&gt;(10, 0),<br>
30 &nbsp; gtl::construct&lt;Point&gt;(10, 10),<br>
31 &nbsp; gtl::construct&lt;Point&gt;(0, 10) };<br>
32 &nbsp; Polygon poly;<br>
33 &nbsp; gtl::set_points(poly, pts, pts+4);<br>
34 <br>
35 &nbsp; //now lets see what we can do with this polygon<br>
36 &nbsp; assert(gtl::area(poly) == 100.0f);<br>
37 &nbsp; assert(gtl::contains(poly, gtl::construct&lt;Point&gt;(5, 5)));<br>
38 &nbsp; assert(!gtl::contains(poly, gtl::construct&lt;Point&gt;(15, 5)));<br>
39 &nbsp; gtl::rectangle_data&lt;int&gt; rect;<br>
40 &nbsp; assert(gtl::extents(rect, poly)); //get bounding box of poly<br>
41 &nbsp; assert(gtl::equivalence(rect, poly)); //hey, that's slick<br>
42 &nbsp; assert(gtl::winding(poly) == gtl::COUNTERCLOCKWISE);<br>
43 &nbsp; assert(gtl::perimeter(poly) == 40.0f);<br>
44 <br>
45 &nbsp; //add 5 to all coords of poly<br>
46 &nbsp; gtl::convolve(poly, gtl::construct&lt;Point&gt;(5, 5));<br>
47 &nbsp; //multiply all coords of poly by 2<br>
48 &nbsp; gtl::scale_up(poly, 2);<br>
49 &nbsp; gtl::set_points(rect, gtl::point_data&lt;int&gt;(10, 10),<br>
50 &nbsp; gtl::point_data&lt;int&gt;(30, 30));<br>
51 &nbsp; assert(gtl::equivalence(poly, rect));<br>
52 }<br>
53 <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>
57 struct CPoint {<br>
58 &nbsp; int x;<br>
59 &nbsp; int y;<br>
60 };<br>
61 <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 &nbsp; template &lt;&gt;<br>
66 &nbsp; struct geometry_concept&lt;CPoint&gt; { typedef point_concept type; };<br>
67 &nbsp; template &lt;&gt;<br>
68 &nbsp; struct point_traits&lt;CPoint&gt; {<br>
69 &nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
70 <br>
71 &nbsp;&nbsp;&nbsp; static inline coordinate_type get(const CPoint&amp; point, <br>
72 &nbsp;&nbsp;&nbsp; orientation_2d orient) {<br>
73 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(orient == HORIZONTAL)<br>
74 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return point.x;<br>
75 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return point.y;<br>
76 &nbsp;&nbsp;&nbsp; }<br>
77 &nbsp; };<br>
78 <br>
79 &nbsp; template &lt;&gt;<br>
80 &nbsp; struct point_mutable_traits&lt;CPoint&gt; {<br>
81 &nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
82 <br>
83 &nbsp;&nbsp;&nbsp; static inline void set(CPoint&amp; point, orientation_2d orient, 
84 int value) {<br>
85 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(orient == HORIZONTAL)<br>
86 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; point.x = value;<br>
87 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>
88 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; point.y = value;<br>
89 &nbsp;&nbsp;&nbsp; }<br>
90 &nbsp;&nbsp;&nbsp; static inline CPoint construct(int x_value, int y_value) {<br>
91 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CPoint retval;<br>
92 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retval.x = x_value;<br>
93 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retval.y = y_value; <br>
94 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return retval;<br>
95 &nbsp;&nbsp;&nbsp; }<br>
96 &nbsp; };<br>
97 } }<br>
98 <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&lt;CPoint&gt;<br>
101 typedef std::list&lt;CPoint&gt; CPolygon;<br>
102 <br>
103 //we need to specialize our polygon concept mapping in boost polygon<br>
104 namespace boost { namespace polygon {<br>
105 &nbsp; //first register CPolygon as a polygon_concept type<br>
106 &nbsp; template &lt;&gt;<br>
107 &nbsp; struct geometry_concept&lt;CPolygon&gt;{ typedef polygon_concept type; };<br>
108 <br>
109 &nbsp; template &lt;&gt;<br>
110 &nbsp; struct polygon_traits&lt;CPolygon&gt; {<br>
111 &nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
112 &nbsp;&nbsp;&nbsp; typedef CPolygon::const_iterator iterator_type;<br>
113 &nbsp;&nbsp;&nbsp; typedef CPoint point_type;<br>
114 <br>
115 &nbsp;&nbsp;&nbsp; // Get the begin iterator<br>
116 &nbsp;&nbsp;&nbsp; static inline iterator_type begin_points(const CPolygon&amp; t) {<br>
117 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t.begin();<br>
118 &nbsp;&nbsp;&nbsp; }<br>
119 <br>
120 &nbsp;&nbsp;&nbsp; // Get the end iterator<br>
121 &nbsp;&nbsp;&nbsp; static inline iterator_type end_points(const CPolygon&amp; t) {<br>
122 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t.end();<br>
123 &nbsp;&nbsp;&nbsp; }<br>
124 <br>
125 &nbsp;&nbsp;&nbsp; // Get the number of sides of the polygon<br>
126 &nbsp;&nbsp;&nbsp; static inline std::size_t size(const CPolygon&amp; t) {<br>
127 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t.size();<br>
128 &nbsp;&nbsp;&nbsp; }<br>
129 <br>
130 &nbsp;&nbsp;&nbsp; // Get the winding direction of the polygon<br>
131 &nbsp;&nbsp;&nbsp; static inline winding_direction winding(const CPolygon&amp; t) {<br>
132 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return unknown_winding;<br>
133 &nbsp;&nbsp;&nbsp; }<br>
134 &nbsp; };<br>
135 <br>
136 &nbsp; template &lt;&gt;<br>
137 &nbsp; struct polygon_mutable_traits&lt;CPolygon&gt; {<br>
138 &nbsp;&nbsp;&nbsp; //expects stl style iterators<br>
139 &nbsp;&nbsp;&nbsp; template &lt;typename iT&gt;<br>
140 &nbsp;&nbsp;&nbsp; static inline CPolygon&amp; set_points(CPolygon&amp; t, <br>
141 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
142 iT input_begin, iT input_end) {<br>
143 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.clear();<br>
144 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.insert(t.end(), input_begin, input_end);<br>
145 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t;<br>
146 &nbsp;&nbsp;&nbsp; }<br>
147 <br>
148 &nbsp; };<br>
149 } }<br>
150 <br>
151 //now there's nothing left to do but test that our polygon<br>
152 //works with library interfaces<br>
153 int main() {<br>
154 &nbsp; test_polygon&lt;CPolygon&gt;(); //woot!<br>
155 &nbsp; return 0;<br>
156 }<br>
157 &nbsp;</font></p>
158
159
160 <table class="docinfo" rules="none" frame="void" id="table1">
161         <colgroup>
162                 <col class="docinfo-name"><col class="docinfo-content">
163         </colgroup>
164         <tbody vAlign="top">
165                 <tr>
166                         <th class="docinfo-name">Copyright:</th>
167                         <td>Copyright © Intel Corporation 2008-2010.</td>
168                 </tr>
169                 <tr class="field">
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>
176                 </tr>
177 </table>
178
179 </body>
180
181 </html>