gtl_property_merge_usage.htm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  4. <title>Property Merge 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>
  18. <br>//just a little meta-programming to get things off on the right foot<br>
  19. template &lt;typename T&gt;<br>
  20. struct lookup_polygon_set_type { typedef gtl::polygon_set_data&lt;int&gt; type; };<br>
  21. template &lt;typename T, typename T2&gt;<br>
  22. struct lookup_polygon_set_type&lt;gtl::property_merge_90&lt;T, T2&gt; &gt; { <br>
  23. &nbsp; typedef gtl::polygon_90_set_data&lt;int&gt; type; };<br>
  24. <br>
  25. //This function works with both the 90 and general versions<br>
  26. //of property merge/map overlay algorithm<br>
  27. template &lt;typename pm_type&gt;<br>
  28. void test_pm() {<br>
  29. &nbsp; std::vector&lt;gtl::rectangle_data&lt;int&gt; &gt; test_data;<br>
  30. &nbsp; test_data.push_back(gtl::rectangle_data&lt;int&gt;(11, 10, 31, 30));<br>
  31. &nbsp; test_data.push_back(gtl::rectangle_data&lt;int&gt;(1, 0, 21, 20));<br>
  32. &nbsp; test_data.push_back(gtl::rectangle_data&lt;int&gt;(6, 15, 16, 25));<br>
  33. <br>
  34. &nbsp; pm_type pm;<br>
  35. <br>
  36. &nbsp; //insert our test geometry into the property merge algorithm<br>
  37. &nbsp; for(unsigned int i = 0; i &lt; test_data.size(); ++i) {<br>
  38. &nbsp;&nbsp;&nbsp; pm.insert(test_data[i], i); //notice I use the index as the
  39. property value<br>
  40. &nbsp; }<br>
  41. <br>
  42. &nbsp; typedef typename lookup_polygon_set_type&lt;pm_type&gt;::type polygon_set_type;<br>
  43. &nbsp; typedef std::map&lt;std::set&lt;int&gt;, polygon_set_type&gt;
  44. property_merge_result_type;<br>
  45. <br>
  46. &nbsp; std::set&lt;int&gt; key;<br>
  47. <br>
  48. &nbsp; //There are 8 different combinations of our input geometries<br>
  49. &nbsp; //null combination is not interesting, so really 7<br>
  50. <br>
  51. &nbsp; property_merge_result_type result;<br>
  52. &nbsp; pm.merge(result);<br>
  53. <br>
  54. &nbsp; //lets enumerate boolean combinations of inputs (hold onto your hats)<br>
  55. &nbsp; for(unsigned int i = 0; i &lt; 8; ++i) {<br>
  56. &nbsp;&nbsp;&nbsp; bool bits[3] = {i &amp; 1, i &amp; 2, i &amp; 4}; //break out bit array<br>
  57. &nbsp;&nbsp;&nbsp; polygon_set_type test_set;<br>
  58. &nbsp;&nbsp;&nbsp; std::set&lt;int&gt; key;<br>
  59. &nbsp;&nbsp;&nbsp; for(unsigned int j = 0; j &lt; 3; ++j) {<br>
  60. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(bits[j]) {<br>
  61. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; key.insert(key.end(), j);<br>
  62. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test_set += test_data[j];<br>
  63. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
  64. &nbsp;&nbsp;&nbsp; }<br>
  65. &nbsp;&nbsp;&nbsp; for(unsigned int j = 0; j &lt; 3; ++j) {<br>
  66. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(bits[j]) {<br>
  67. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test_set *= test_data[j];<br>
  68. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
  69. &nbsp;&nbsp;&nbsp; }<br>
  70. &nbsp;&nbsp;&nbsp; for(unsigned int j = 0; j &lt; 3; ++j) {<br>
  71. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!bits[j])<br>
  72. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; test_set -= test_data[j];<br>
  73. &nbsp;&nbsp;&nbsp; }<br>
  74. &nbsp;&nbsp;&nbsp; if(test_set.empty()) {<br>
  75. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //only the null combination should not exist<br>
  76. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assert(i == 0);<br>
  77. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //a combination that does not exist should not<br>
  78. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //be present in result<br>
  79. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assert(result.find(key) == result.end());<br>
  80. &nbsp;&nbsp;&nbsp; } else {<br>
  81. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assert(gtl::equivalence(result[key], test_set));<br>
  82. &nbsp;&nbsp;&nbsp; }<br>
  83. &nbsp; }<br>
  84. <br>
  85. &nbsp; //Notice that we have to do O(2^n) booleans to compose the same<br>
  86. &nbsp; //result that is produced in one pass of property merge<br>
  87. &nbsp; //given n input layers (8 = 2^3 in this example)<br>
  88. }<br>
  89. <br>
  90. int main() {<br>
  91. &nbsp;
  92. test_pm&lt;gtl::property_merge_90&lt;int, int&gt; &gt;();<br>
  93. &nbsp;
  94. test_pm&lt;gtl::property_merge&lt;int, int&gt; &gt;();<br>
  95. &nbsp;
  96. return 0;<br>
  97. }<br>
  98. //Now you know how to use the manhattan and arbitrary angle property<br>
  99. //merge algorithms to perform map overlay on n layers of input geometry<br>
  100. &nbsp;</font></p>
  101. <table class="docinfo" rules="none" frame="void" id="table1">
  102. <colgroup>
  103. <col class="docinfo-name"><col class="docinfo-content">
  104. </colgroup>
  105. <tbody vAlign="top">
  106. <tr>
  107. <th class="docinfo-name">Copyright:</th>
  108. <td>Copyright © Intel Corporation 2008-2010.</td>
  109. </tr>
  110. <tr class="field">
  111. <th class="docinfo-name">License:</th>
  112. <td class="field-body">Distributed under the Boost Software License,
  113. Version 1.0. (See accompanying file <tt class="literal">
  114. <span class="pre">LICENSE_1_0.txt</span></tt> or copy at
  115. <a class="reference" target="_top" href="http://www.boost.org/LICENSE_1_0.txt">
  116. http://www.boost.org/LICENSE_1_0.txt</a>)</td>
  117. </tr>
  118. </table>
  119. </body>
  120. </html>