]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/polygon/doc/gtl_custom_polygon_set.htm
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / polygon / doc / gtl_custom_polygon_set.htm
CommitLineData
7c673cae
FG
1<html>
2
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
5<title>Custom Polygon Set</title>
6</head>
7
8<body>
9
10<p><font face="Courier New">/*<br>
11Copyright 2008 Intel Corporation<br>
12<br>
13Use, modification and distribution are subject to the Boost Software License,<br>
14Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at<br>
15http://www.boost.org/LICENSE_1_0.txt).<br>
16*/<br>
17#include &lt;boost/polygon/polygon.hpp&gt;<br>
18#include &lt;list&gt;<br>
19#include &lt;time.h&gt;<br>
20#include &lt;cassert&gt;<br>
21#include &lt;deque&gt;<br>
22#include &lt;iostream&gt;<br>
23namespace gtl = boost::polygon;<br>
24using namespace boost::polygon::operators;<br><br>
25//once again we make our usage of the library generic<br>
26//and parameterize it on the polygon set type<br>
27template &lt;typename PolygonSet&gt;<br>
28void test_polygon_set() {<br>
29&nbsp; using namespace gtl; <br>
30&nbsp; PolygonSet ps;<br>
31&nbsp; ps += rectangle_data&lt;int&gt;(0, 0, 10, 10);<br>
32&nbsp; PolygonSet ps2;<br>
33&nbsp; ps2 += rectangle_data&lt;int&gt;(5, 5, 15, 15);<br>
34&nbsp; PolygonSet ps3;<br>
35&nbsp; assign(ps3, ps * ps2); <br>
36&nbsp; PolygonSet ps4;<br>
37&nbsp; ps4 += ps + ps2;<br>
38&nbsp; assert(area(ps4) == area(ps) + area(ps2) - area(ps3));<br>
39&nbsp; assert(equivalence((ps + ps2) - (ps * ps2), ps ^ ps2));<br>
40&nbsp; rectangle_data&lt;int&gt; rect;<br>
41&nbsp; assert(extents(rect, ps ^ ps2));<br>
42&nbsp; assert(area(rect) == 225);<br>
43&nbsp; assert(area(rect ^ (ps ^ ps2)) == area(rect) - area(ps ^ ps2)); <br>
44}<br>
45<br>
46//first thing is first, lets include all the code from previous examples<br>
47<br>
48//the CPoint example<br>
49struct CPoint {<br>
50&nbsp; int x;<br>
51&nbsp; int y;<br>
52};<br>
53<br>
54namespace boost { namespace polygon {<br>
55&nbsp; template &lt;&gt;<br>
56&nbsp; struct geometry_concept&lt;CPoint&gt; { typedef point_concept type; };<br>
57&nbsp; template &lt;&gt;<br>
58&nbsp; struct point_traits&lt;CPoint&gt; {<br>
59&nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
60<br>
61&nbsp;&nbsp;&nbsp; static inline coordinate_type get(const CPoint&amp; point, <br>
62&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;
63orientation_2d orient) {<br>
64&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(orient == HORIZONTAL)<br>
65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return point.x;<br>
66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return point.y;<br>
67&nbsp;&nbsp;&nbsp; }<br>
68&nbsp; };<br>
69<br>
70&nbsp; template &lt;&gt;<br>
71&nbsp; struct point_mutable_traits&lt;CPoint&gt; {<br>
72&nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
73<br>
74&nbsp;&nbsp;&nbsp; static inline void set(CPoint&amp; point, orientation_2d orient,
75int value) {<br>
76&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(orient == HORIZONTAL)<br>
77&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; point.x = value;<br>
78&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>
79&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; point.y = value;<br>
80&nbsp;&nbsp;&nbsp; }<br>
81&nbsp;&nbsp;&nbsp; static inline CPoint construct(int x_value, int y_value) {<br>
82&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CPoint retval;<br>
83&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retval.x = x_value;<br>
84&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retval.y = y_value; <br>
85&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return retval;<br>
86&nbsp;&nbsp;&nbsp; }<br>
87&nbsp; };<br>
88} }<br>
89<br>
90//the CPolygon example<br>
91typedef std::list&lt;CPoint&gt; CPolygon;<br>
92<br>
93//we need to specialize our polygon concept mapping in boost polygon<br>
94namespace boost { namespace polygon {<br>
95&nbsp; //first register CPolygon as a polygon_concept type<br>
96&nbsp; template &lt;&gt;<br>
97&nbsp; struct geometry_concept&lt;CPolygon&gt;{ typedef polygon_concept type; };<br>
98<br>
99&nbsp; template &lt;&gt;<br>
100&nbsp; struct polygon_traits&lt;CPolygon&gt; {<br>
101&nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
102&nbsp;&nbsp;&nbsp; typedef CPolygon::const_iterator iterator_type;<br>
103&nbsp;&nbsp;&nbsp; typedef CPoint point_type;<br>
104<br>
105&nbsp;&nbsp;&nbsp; // Get the begin iterator<br>
106&nbsp;&nbsp;&nbsp; static inline iterator_type begin_points(const CPolygon&amp; t) {<br>
107&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t.begin();<br>
108&nbsp;&nbsp;&nbsp; }<br>
109<br>
110&nbsp;&nbsp;&nbsp; // Get the end iterator<br>
111&nbsp;&nbsp;&nbsp; static inline iterator_type end_points(const CPolygon&amp; t) {<br>
112&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t.end();<br>
113&nbsp;&nbsp;&nbsp; }<br>
114<br>
115&nbsp;&nbsp;&nbsp; // Get the number of sides of the polygon<br>
116&nbsp;&nbsp;&nbsp; static inline std::size_t size(const CPolygon&amp; t) {<br>
117&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t.size();<br>
118&nbsp;&nbsp;&nbsp; }<br>
119<br>
120&nbsp;&nbsp;&nbsp; // Get the winding direction of the polygon<br>
121&nbsp;&nbsp;&nbsp; static inline winding_direction winding(const CPolygon&amp; t) {<br>
122&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return unknown_winding;<br>
123&nbsp;&nbsp;&nbsp; }<br>
124&nbsp; };<br>
125<br>
126&nbsp; template &lt;&gt;<br>
127&nbsp; struct polygon_mutable_traits&lt;CPolygon&gt; {<br>
128&nbsp;&nbsp;&nbsp; //expects stl style iterators<br>
129&nbsp;&nbsp;&nbsp; template &lt;typename iT&gt;<br>
130&nbsp;&nbsp;&nbsp; static inline CPolygon&amp; set_points(CPolygon&amp; t, <br>
131&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
132iT input_begin, iT input_end) {<br>
133&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.clear();<br>
134&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(input_begin != input_end) {<br>
135&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.push_back(CPoint());<br>
136&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gtl::assign(t.back(), *input_begin);<br>
137&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++input_begin;<br>
138&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
139&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return t;<br>
140&nbsp;&nbsp;&nbsp; }<br>
141<br>
142&nbsp; };<br>
143} }<br>
144<br>
145//OK, finally we get to declare our own polygon set type<br>
146typedef std::deque&lt;CPolygon&gt; CPolygonSet;<br>
147<br>
148//deque isn't automatically a polygon set in the library<br>
149//because it is a standard container there is a shortcut<br>
150//for mapping it to polygon set concept, but I'll do it<br>
151//the long way that you would use in the general case.<br>
152namespace boost { namespace polygon {<br>
153&nbsp; //first we register CPolygonSet as a polygon set<br>
154&nbsp; template &lt;&gt;<br>
155&nbsp; struct geometry_concept&lt;CPolygonSet&gt; { typedef polygon_set_concept type;
156};<br>
157<br>
158&nbsp; //next we map to the concept through traits<br>
159&nbsp; template &lt;&gt;<br>
160&nbsp; struct polygon_set_traits&lt;CPolygonSet&gt; {<br>
161&nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
162&nbsp;&nbsp;&nbsp; typedef CPolygonSet::const_iterator iterator_type;<br>
163&nbsp;&nbsp;&nbsp; typedef CPolygonSet operator_arg_type;<br>
164<br>
165&nbsp;&nbsp;&nbsp; static inline iterator_type begin(const CPolygonSet&amp;
166polygon_set) {<br>
167&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return polygon_set.begin();<br>
168&nbsp;&nbsp;&nbsp; }<br>
169<br>
170&nbsp;&nbsp;&nbsp; static inline iterator_type end(const CPolygonSet&amp;
171polygon_set) {<br>
172&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return polygon_set.end();<br>
173&nbsp;&nbsp;&nbsp; }<br>
174<br>
175&nbsp;&nbsp;&nbsp; //don't worry about these, just return false from them<br>
176&nbsp;&nbsp;&nbsp; static inline bool clean(const CPolygonSet&amp; polygon_set) {
177return false; }<br>
178&nbsp;&nbsp;&nbsp; static inline bool sorted(const CPolygonSet&amp; polygon_set) {
179return false; }<br>
180&nbsp; };<br>
181<br>
182&nbsp; template &lt;&gt;<br>
183&nbsp; struct polygon_set_mutable_traits&lt;CPolygonSet&gt; {<br>
184&nbsp;&nbsp;&nbsp; template &lt;typename input_iterator_type&gt;<br>
185&nbsp;&nbsp;&nbsp; static inline void set(CPolygonSet&amp; polygon_set,
186input_iterator_type input_begin, input_iterator_type input_end) {<br>
187&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; polygon_set.clear();<br>
188&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //this is kind of cheesy. I am copying the
189unknown input geometry<br>
190&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //into my own polygon set and then calling get to
191populate the<br>
192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //deque<br>
193&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; polygon_set_data&lt;int&gt; ps;<br>
194&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps.insert(input_begin, input_end);<br>
195&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps.get(polygon_set);<br>
196&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //if you had your own odd-ball polygon set you
197would probably have<br>
198&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //to iterate through each polygon at this point
199and do something<br>
200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //extra<br>
201&nbsp;&nbsp;&nbsp; }<br>
202&nbsp; };<br>
203} }<br>
204<br>
205int main() {<br>
206&nbsp; long long c1 = clock();<br>
207&nbsp; for(int i = 0; i &lt; 1000; ++i) <br>
208&nbsp;&nbsp;&nbsp; test_polygon_set&lt;CPolygonSet&gt;();<br>
209&nbsp; long long c2 = clock();<br>
210&nbsp; for(int i = 0; i &lt; 1000; ++i) <br>
211&nbsp;&nbsp;&nbsp; test_polygon_set&lt;gtl::polygon_set_data&lt;int&gt; &gt;();<br>
212&nbsp; long long c3 = clock();<br>
213&nbsp; long long diff1 = c2 - c1;<br>
214&nbsp; long long diff2 = c3 - c2;<br>
215&nbsp; if(diff1 &gt; 0 &amp;&amp; diff2)<br>
216&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; &quot;library polygon_set_data is &quot; &lt;&lt;
217float(diff1)/float(diff2) &lt;&lt; &quot;X faster than custom polygon set deque of CPolygon&quot;
218&lt;&lt; std::endl;<br>
219&nbsp; else<br>
220&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; &quot;operation was too fast&quot; &lt;&lt; std::endl;<br>
221&nbsp; return 0;<br>
222}</font></p>
223<p><font face="Courier New">//Now you know how to map your own data type to
224polygon set concept<br>
225//Now you also know how to make your application code that operates on geometry<br>
226//data type agnostic from point through polygon set
227&nbsp;</font></p>
228
229
230<table class="docinfo" rules="none" frame="void" id="table1">
231 <colgroup>
232 <col class="docinfo-name"><col class="docinfo-content">
233 </colgroup>
234 <tbody vAlign="top">
235 <tr>
236 <th class="docinfo-name">Copyright:</th>
237