gtl_custom_polygon.htm 7.1 KB

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