gtl_custom_polygon_set.htm 10 KB

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