gtl_polygon_set_usage.htm 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  4. <title>Polygon Set Usage</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. namespace gtl = boost::polygon;<br>
  17. using namespace boost::polygon::operators;<br><br>
  18. int main() {<br>&nbsp; &nbsp;
  19. //lets declare ourselves a polygon set<br>&nbsp; &nbsp;
  20. using namespace gtl; //because of operators<br>&nbsp; &nbsp;
  21. typedef std::vector&lt;polygon_data&lt;int&gt; &gt; PolygonSet;<br>&nbsp; &nbsp;
  22. PolygonSet ps;<br>&nbsp; &nbsp;
  23. <br>&nbsp; &nbsp;
  24. //lets put some data in<br>&nbsp; &nbsp;
  25. ps += rectangle_data&lt;int&gt;(0, 0, 10, 10);<br>&nbsp; &nbsp;
  26. <br>&nbsp; &nbsp;
  27. //now lets do something interesting<br>&nbsp; &nbsp;
  28. PolygonSet ps2;<br>&nbsp; &nbsp;
  29. ps2 += rectangle_data&lt;int&gt;(5, 5, 15, 15);<br>&nbsp; &nbsp;
  30. PolygonSet ps3;<br>&nbsp; &nbsp;
  31. assign(ps3, ps * ps2); //woah, I just felt the room flex around me<br>&nbsp; &nbsp;
  32. PolygonSet ps4;<br>&nbsp; &nbsp;
  33. ps4 += ps + ps2;<br>&nbsp; &nbsp;
  34. <br>&nbsp; &nbsp;
  35. //assert that area of result is equal to sum of areas<br>&nbsp; &nbsp;
  36. //of input geometry minus the area of overlap between inputs<br>&nbsp; &nbsp;
  37. assert(area(ps4) == area(ps) + area(ps2) - area(ps3));<br>&nbsp; &nbsp;
  38. <br>&nbsp; &nbsp;
  39. //I don't even see the code anymore, all<br>&nbsp; &nbsp;
  40. //I see is bounding box...interval...triangle<br>&nbsp; &nbsp;
  41. <br>&nbsp; &nbsp;
  42. //lets try that again in slow motion shall we?<br>&nbsp;&nbsp;&nbsp;
  43. assert(equivalence((ps + ps2) - (ps * ps2), ps ^ ps2));<br>
  44. <br>
  45. &nbsp;&nbsp;&nbsp; //hmm, subtracting the intersection from the union<br>
  46. &nbsp;&nbsp;&nbsp; //is equivalent to the xor, all this in one line of code,<br>
  47. &nbsp;&nbsp;&nbsp; //now we're programming in bullet time<br>
  48. &nbsp;&nbsp;&nbsp; //(by the way, xor is implemented as one pass, not
  49. composition)&nbsp; <br>&nbsp; &nbsp;
  50. <br>&nbsp; &nbsp;
  51. //just for fun<br>&nbsp; &nbsp;
  52. rectangle_data&lt;int&gt; rect;<br>&nbsp; &nbsp;
  53. assert(extents(rect, ps ^ ps2));<br>&nbsp; &nbsp;
  54. assert(area(rect) == 225);<br>&nbsp; &nbsp;
  55. assert(area(rect ^ (ps ^ ps2)) == area(rect) - area(ps ^ ps2)); <br>&nbsp; &nbsp;
  56. return 0;<br>}<br>&nbsp; &nbsp;
  57. <br>//Now you know how to use the polygon set concept with library polygons<br>&nbsp; &nbsp;
  58. &nbsp;</font></p>
  59. <table class="docinfo" rules="none" frame="void" id="table1">
  60. <colgroup>
  61. <col class="docinfo-name"><col class="docinfo-content">
  62. </colgroup>
  63. <tbody vAlign="top">
  64. <tr>
  65. <th class="docinfo-name">Copyright:</th>
  66. <td>Copyright © Intel Corporation 2008-2010.</td>
  67. </tr>
  68. <tr class="field">
  69. <th class="docinfo-name">License:</th>
  70. <td class="field-body">Distributed under the Boost Software License,
  71. Version 1.0. (See accompanying file <tt class="literal">
  72. <span class="pre">LICENSE_1_0.txt</span></tt> or copy at
  73. <a class="reference" target="_top" href="http://www.boost.org/LICENSE_1_0.txt">
  74. http://www.boost.org/LICENSE_1_0.txt</a>)</td>
  75. </tr>
  76. </table>
  77. </body>
  78. </html>