gtl_custom_point.htm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Custom Point</title></head><body>
  4. <p><font face="Courier New">/*<br>
  5. Copyright 2008 Intel Corporation<br>
  6. <br>
  7. Use, modification and distribution are subject to the Boost Software License,<br>
  8. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at<br>
  9. http://www.boost.org/LICENSE_1_0.txt).<br>
  10. */<br>
  11. #include &lt;boost/polygon/polygon.hpp&gt;<br>
  12. #include &lt;cassert&gt;<br>
  13. namespace gtl = boost::polygon;<br>
  14. using namespace boost::polygon::operators;<br><br>
  15. //lets make the body of main from point_usage.cpp<br>//a generic function parameterized by point type<br>template &lt;typename Point&gt;<br>void test_point() {<br>&nbsp;
  16. &nbsp; //constructing a gtl point<br>&nbsp; &nbsp;
  17. int x = 10;<br>&nbsp; &nbsp;
  18. int y = 20;<br>&nbsp; &nbsp;
  19. //Point pt(x, y);<br>&nbsp; &nbsp;
  20. Point pt = gtl::construct&lt;Point&gt;(x, y);<br>&nbsp; &nbsp;
  21. assert(gtl::x(pt) == 10);<br>&nbsp; &nbsp;
  22. assert(gtl::y(pt) == 20);<br>&nbsp; &nbsp;
  23. <br>&nbsp; &nbsp;
  24. //a quick primer in isotropic point access<br>&nbsp; &nbsp;
  25. typedef gtl::orientation_2d O;<br>&nbsp; &nbsp;
  26. using gtl::HORIZONTAL;<br>&nbsp; &nbsp;
  27. using gtl::VERTICAL;<br>&nbsp; &nbsp;
  28. O o = HORIZONTAL;<br>&nbsp; &nbsp;
  29. assert(gtl::x(pt) == gtl::get(pt, o));<br>&nbsp; &nbsp;
  30. <br>&nbsp; &nbsp;
  31. o = o.get_perpendicular();<br>&nbsp; &nbsp;
  32. assert(o == VERTICAL);<br>&nbsp; &nbsp;
  33. assert(gtl::y(pt) == gtl::get(pt, o));<br>&nbsp; &nbsp;
  34. <br>&nbsp; &nbsp;
  35. gtl::set(pt, o, 30);<br>&nbsp; &nbsp;
  36. assert(gtl::y(pt) == 30);<br>&nbsp; &nbsp;
  37. <br>&nbsp; &nbsp;
  38. //using some of the library functions<br>&nbsp; &nbsp;
  39. //Point pt2(10, 30);<br>&nbsp; &nbsp;
  40. Point pt2 = gtl::construct&lt;Point&gt;(10, 30);<br>&nbsp; &nbsp;
  41. assert(gtl::equivalence(pt, pt2));<br>&nbsp; &nbsp;
  42. <br>&nbsp; &nbsp;
  43. gtl::transformation&lt;int&gt; tr(gtl::axis_transformation::SWAP_XY);<br>&nbsp; &nbsp;
  44. gtl::transform(pt, tr);<br>&nbsp; &nbsp;
  45. assert(gtl::equivalence(pt, gtl::construct&lt;Point&gt;(30, 10)));<br>&nbsp; &nbsp;
  46. <br>&nbsp; &nbsp;
  47. gtl::transformation&lt;int&gt; tr2 = tr.inverse();<br>&nbsp; &nbsp;
  48. assert(tr == tr2); //SWAP_XY is its own inverse transform<br>&nbsp; &nbsp;
  49. <br>&nbsp; &nbsp;
  50. gtl::transform(pt, tr2);<br>&nbsp; &nbsp;
  51. assert(gtl::equivalence(pt, pt2)); //the two points are equal again<br>&nbsp; &nbsp;
  52. <br>&nbsp; &nbsp;
  53. gtl::move(pt, o, 10); //move pt 10 units in y<br>&nbsp; &nbsp;
  54. assert(gtl::euclidean_distance(pt, pt2) == 10.0f);<br>&nbsp; &nbsp;
  55. <br>&nbsp; &nbsp;
  56. gtl::move(pt, o.get_perpendicular(), 10); //move pt 10 units in x<br>&nbsp; &nbsp;
  57. assert(gtl::manhattan_distance(pt, pt2) == 20);<br>}<br>&nbsp; &nbsp;
  58. <br>//Now lets declare our own point type<br>//Bjarne says that if a class doesn't maintain an<br>//invariant just use a struct.<br>struct CPoint {<br>&nbsp; &nbsp;
  59. int x;<br>&nbsp; &nbsp;
  60. int y;<br>};<br>&nbsp; &nbsp;
  61. <br>//There, nice a simple...but wait, it doesn't do anything<br>//how do we use it to do all the things a point needs to do?<br>&nbsp; &nbsp;
  62. <br>&nbsp; &nbsp;
  63. <br>//First we register it as a point with boost polygon<br>namespace boost {
  64. namespace polygon {<br>&nbsp;&nbsp;&nbsp;
  65. template &lt;&gt;<br>&nbsp; &nbsp;
  66. struct geometry_concept&lt;CPoint&gt; { typedef point_concept type; };<br>&nbsp;<br>&nbsp; &nbsp;
  67. <br>&nbsp;&nbsp;&nbsp; //Then we specialize the gtl point traits for our point type<br>&nbsp; &nbsp;
  68. template &lt;&gt;<br>&nbsp; &nbsp;
  69. struct point_traits&lt;CPoint&gt; {<br>&nbsp; &nbsp;
  70. &nbsp; &nbsp;
  71. typedef int coordinate_type;<br>&nbsp; &nbsp;
  72. <br>&nbsp; &nbsp;
  73. &nbsp; &nbsp;
  74. static inline coordinate_type get(const CPoint&amp; point, <br>&nbsp; &nbsp;
  75. &nbsp; &nbsp;
  76. orientation_2d orient) {<br>&nbsp; &nbsp;
  77. &nbsp; &nbsp;
  78. &nbsp; &nbsp;
  79. if(orient == HORIZONTAL)<br>&nbsp; &nbsp;
  80. &nbsp; &nbsp;
  81. &nbsp; &nbsp;
  82. &nbsp; &nbsp;
  83. return point.x;<br>&nbsp; &nbsp;
  84. &nbsp; &nbsp;
  85. &nbsp; &nbsp;
  86. return point.y;<br>&nbsp; &nbsp;
  87. &nbsp; &nbsp;
  88. }<br>&nbsp; &nbsp;
  89. };<br>&nbsp; &nbsp;
  90. <br>&nbsp; &nbsp;
  91. template &lt;&gt;<br>&nbsp; &nbsp;
  92. struct point_mutable_traits&lt;CPoint&gt; {<br>
  93. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; typedef int coordinate_type;<br>
  94. <br>
  95. </font></p>
  96. <p><font face="Courier New">&nbsp; &nbsp;
  97. &nbsp; &nbsp;
  98. static inline void set(CPoint&amp; point, orientation_2d orient, int value) {<br>&nbsp; &nbsp;
  99. &nbsp; &nbsp;
  100. &nbsp; &nbsp;
  101. if(orient == HORIZONTAL)<br>&nbsp; &nbsp;
  102. &nbsp; &nbsp;
  103. &nbsp; &nbsp;
  104. &nbsp; &nbsp;
  105. point.x = value;<br>&nbsp; &nbsp;
  106. &nbsp; &nbsp;
  107. &nbsp; &nbsp;
  108. else<br>&nbsp; &nbsp;
  109. &nbsp; &nbsp;
  110. &nbsp; &nbsp;
  111. point.y = value;<br>&nbsp; &nbsp;
  112. &nbsp; &nbsp;
  113. }<br>&nbsp; &nbsp;
  114. &nbsp; &nbsp;
  115. static inline CPoint construct(int x_value, int y_value) {<br>&nbsp; &nbsp;
  116. &nbsp; &nbsp;
  117. &nbsp; &nbsp;
  118. CPoint retval;<br>&nbsp; &nbsp;
  119. &nbsp; &nbsp;
  120. &nbsp; &nbsp;
  121. retval.x = x_value;<br>&nbsp; &nbsp;
  122. &nbsp; &nbsp;
  123. &nbsp; &nbsp;
  124. retval.y = y_value; <br>&nbsp; &nbsp;
  125. &nbsp; &nbsp;
  126. &nbsp; &nbsp;
  127. return retval;<br>&nbsp; &nbsp;
  128. &nbsp; &nbsp;
  129. }<br>&nbsp; &nbsp;
  130. };<br>} }<br>&nbsp; &nbsp;
  131. <br>//Now lets see if the CPoint works with the library functions<br>int main() {<br>&nbsp; &nbsp;
  132. test_point&lt;CPoint&gt;(); //yay! All your testing is done for you.<br>&nbsp; &nbsp;
  133. return 0;<br>}<br>&nbsp; &nbsp;
  134. <br>//Now you know how to map a user type to the library point concept<br>//and how to write a generic function parameterized by point type<br>//using the library interfaces to access it.<br>&nbsp; &nbsp;
  135. &nbsp;</font></p>
  136. <table class="docinfo" id="table1" frame="void" rules="none">
  137. <colgroup>
  138. <col class="docinfo-name"><col class="docinfo-content">
  139. </colgroup>
  140. <tbody valign="top">
  141. <tr>
  142. <th class="docinfo-name">Copyright:</th>
  143. <td>Copyright © Intel Corporation 2008-2010.</td>
  144. </tr>
  145. <tr class="field">
  146. <th class="docinfo-name">License:</th>
  147. <td class="field-body">Distributed under the Boost Software License,
  148. Version 1.0. (See accompanying file <tt class="literal">
  149. <span class="pre">LICENSE_1_0.txt</span></tt> or copy at
  150. <a class="reference" target="_top" href="http://www.boost.org/LICENSE_1_0.txt">
  151. http://www.boost.org/LICENSE_1_0.txt</a>)</td>
  152. </tr>
  153. </tbody></table>
  154. </body></html>