voronoi_basic_tutorial.htm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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>Voronoi Basic Tutorial</title><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>
  4. <h1>Voronoi Basic Tutorial<br>
  5. </h1>
  6. <p>In this tutorial we will cover the basic usage of the Boost.Polygon
  7. Voronoi library that should be enough for the 95% of cases. Below we will
  8. discuss the following topics:<br>
  9. </p>
  10. <ul>
  11. <li>preparing input geometries<br>
  12. </li>
  13. <li>Voronoi diagram construction</li>
  14. <li>Voronoi graph traversal<br>
  15. </li>
  16. <li>associating the user data with the Voronoi primitives</li>
  17. <li>accessing input site inside the Voronoi cell</li>
  18. <li>Voronoi diagram rendering<br>
  19. </li>
  20. </ul>
  21. In the example that goes through this tutorial (<a href="../example/voronoi_basic_tutorial.cpp">voronoi_basic_tutorial.cpp</a>)
  22. we
  23. are going to construct the Voronoi diagram of a few points and
  24. segments.
  25. On the image below one may see the corresponding rendered Voronoi
  26. graph. The primary Voronoi edges
  27. are marked with
  28. the black color, non-primary with green, input geometries have blue
  29. color. We split each input segment onto three sites (segment
  30. itself and both endpoints), edges that go between the sites corresponding to the same segment are
  31. considered to be non-primary.<br>
  32. <br>
  33. <img style="border: 2px solid ; width: 300px; height: 300px;" alt="" src="images/voronoi4.png"><br>
  34. <br>
  35. And before you proceed don't forget to:<span style="font-family: Courier New,Courier,monospace;"><br>
  36. </span><span style="font-family: Courier New,Courier,monospace;"></span><br>
  37. <span style="font-family: Courier New,Courier,monospace;">#include "boost/polygon/voronoi.hpp"<br>
  38. using boost::polygon::voronoi_builder;<br>
  39. using boost::polygon::voronoi_diagram;<br>
  40. </span>
  41. <h2>Preparing Input Geometries</h2>Below is the example of how the user provided point and segment classes might look like:<br>
  42. <br><span style="font-family: Courier New,Courier,monospace;">struct Point {<br>&nbsp; int a;<br>
  43. &nbsp; int b;<br>
  44. &nbsp; Point (int x, int y) : a(x), b(y) {}<br>
  45. };</span><span style="font-family: Courier New,Courier,monospace;"><br>
  46. <br>struct Segment {</span><span style="font-family: Courier New,Courier,monospace;"></span><br>
  47. <span style="font-family: Courier New,Courier,monospace;">
  48. &nbsp; Point p0;<br>
  49. &nbsp; Point p1;<br>
  50. &nbsp; Segment (int x1, int y1, int x2, int y2) : p0(x1, y1), p1(x2, y2) {}<br>
  51. </span><span style="font-family: Courier New,Courier,monospace;"></span>
  52. <span style="font-family: Courier New,Courier,monospace;">};<br>
  53. <br>
  54. </span>As we are going to use the default routines defined in the
  55. voronoi.hpp header to construct the Voronoi diagram, we are required to map
  56. our point and segment classes to the corresponding Boost.Polygon concepts:<br>
  57. <br>
  58. <span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">template &lt;&gt;</span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;"><br>
  59. struct geometry_concept&lt;Point&gt; { typedef point_concept type; };</span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;"></span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&nbsp;&nbsp; <span class="Apple-converted-space"></span></span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;"></span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">template &lt;&gt;</span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">struct point_traits&lt;</span><span style="font-family: Courier New,Courier,monospace;">Point</span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&gt; {</span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&nbsp; typedef int coordinate_type;</span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;"><br>
  60. &nbsp; &nbsp;<span class="Apple-converted-space"> </span></span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&nbsp; static inline coordinate_type get(const </span><span style="font-family: Courier New,Courier,monospace;">Point</span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&amp; point,<span class="Apple-converted-space"> </span></span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">orientation_2d orient) {<br>
  61. &nbsp;&nbsp;&nbsp; return </span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">(orient == HORIZONTAL) ? point.a : point.b;</span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;"></span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;"><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&nbsp; }<br>
  62. };</span><br>
  63. <span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">
  64. <br>
  65. template &lt;&gt;<br>
  66. </span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">struct geometry_concept&lt;Segment&gt; { typedef segment_concept type; };<br>
  67. <br>
  68. </span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">template &lt;&gt;</span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">
  69. <span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">struct point_traits&lt;</span><span style="font-family: Courier New,Courier,monospace;">Segment</span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&gt; {</span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">
  70. <span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&nbsp; typedef int coordinate_type;<br>
  71. &nbsp; typedef Point point_type;<br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">
  72. </span>
  73. <span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span></span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">
  74. <span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&nbsp; static inline coordinate_type get(const </span><span style="font-family: Courier New,Courier,monospace;">Segment</span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&amp; segment,<span class="Apple-converted-space"> direction_1d dir</span></span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">) {<br>
  75. &nbsp;&nbsp;&nbsp; return </span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">dir.to_int() ? segment.p1() : segment.p0();</span><span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;"></span><br style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">
  76. <span style="color: rgb(0, 0, 0); font-family: 'Courier New'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium; display: inline ! important; float: none;">&nbsp; }<br>};</span><br>
  77. <br>
  78. It's also possible to use the native Boost.Polygon types as <a href="../../../boost/polygon/point_data.hpp">point_data</a> and <a href="../../../boost/polygon/segment_data.hpp">segment_data</a>, that won't require the above mapping.<br>
  79. <br>
  80. So once we are done we can create the sample input:<br>
  81. <br>
  82. <span style="font-family: Courier New,Courier,monospace;">std::vector&lt;Point&gt;
  83. points;<br>
  84. points.push_back(Point(0, 0));<br>
  85. points.push_back(Point(1, 6));<br>
  86. std::vector&lt;Segment&gt; segments;<br>
  87. segments.push_back(Segment(-4, 5, 5, -1));<br>
  88. segments.push_back(Segment(3, -11, 13, -1));</span><span style="font-family: Courier New,Courier,monospace;"><br>
  89. </span>
  90. <h2>Construction of the Voronoi Diagram<br>
  91. </h2>At this point we are ready to construct the Voronoi diagram:<br>
  92. <span style="font-family: Courier New,Courier,monospace;"><br>
  93. </span><span style="font-family: Courier New,Courier,monospace;">
  94. voronoi_diagram&lt;double&gt; vd;<br>
  95. construct_voronoi(points.begin(), points.end(), segments.begin(), segments.end(), &amp;vd);</span><br>
  96. <h2>Traversing Voronoi Graph</h2>
  97. Voronoi graph traversal is the basic
  98. operation one would like to do once the Voronoi diagram is constructed.
  99. There are three ways to do that and we are going to cover all of them:<br>
  100. <ul>
  101. <li>simply iterating over the Voronoi edges (counts each edge twice):<br>
  102. <span style="font-family: Courier New,Courier,monospace;"><br>
  103. int iterate_primary_edges1(const voronoi_diagram&lt;double&gt; &amp;vd)
  104. {<br>
  105. &nbsp; int result = 0;<br>
  106. &nbsp; for (voronoi_diagram&lt;double&gt;::const_edge_iterator it =
  107. vd.edges().begin();<br>
  108. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; it != vd.edges().end(); ++it) {<br>
  109. &nbsp;&nbsp;&nbsp; if (it-&gt;is_primary())<br>
  110. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++result;<br>
  111. &nbsp; }<br>
  112. &nbsp; return result;<br>
  113. }</span><br>
  114. <span style="font-family: Courier New,Courier,monospace;">&nbsp;</span><br>
  115. </li>
  116. <li>iterating over the Voronoi cells and then traversing edges around
  117. each cell (counts each edge twice):<br>
  118. <br>
  119. <span style="font-family: Courier New,Courier,monospace;">int
  120. iterate_primary_edges2(const voronoi_diagram&lt;double&gt; &amp;vd) {<br>
  121. &nbsp; int result = 0;<br>
  122. &nbsp; for (voronoi_diagram&lt;double&gt;::const_cell_iterator it =
  123. vd.cells().begin();<br>
  124. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; it != vd.cells().end(); ++it) {<br>
  125. &nbsp;&nbsp;&nbsp; const voronoi_diagram&lt;double&gt;::cell_type
  126. &amp;cell = *it;<br>
  127. &nbsp;&nbsp;&nbsp; const voronoi_diagram&lt;double&gt;::edge_type *edge
  128. = cell.incident_edge();<br>
  129. &nbsp;&nbsp;&nbsp; // This is convenient way to iterate edges around
  130. Voronoi cell.<br>
  131. &nbsp;&nbsp;&nbsp; do {<br>
  132. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (edge-&gt;is_primary())<br>
  133. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++result;<br>
  134. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; edge = edge-&gt;next();<br>
  135. &nbsp;&nbsp;&nbsp; } while (edge != cell.incident_edge());<br>
  136. &nbsp; }<br>
  137. &nbsp; return result;<br>
  138. }</span><br>
  139. <br>
  140. </li>
  141. <li>iterating over the Voronoi
  142. vertices and then traversing edges around each vertex (the number of the
  143. iterations through each edge is equal to the number of finite endpoints
  144. of the edge):<span style="font-family: Courier New,Courier,monospace;"></span><br>
  145. <span style="font-family: Courier New,Courier,monospace;">int
  146. iterate_primary_edges3(const voronoi_diagram&lt;double&gt; &amp;vd) {<br>
  147. &nbsp; int result = 0;<br>
  148. &nbsp; for (voronoi_diagram&lt;double&gt;::const_vertex_iterator it =
  149. vd.vertices().begin();<br>
  150. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; it != vd.vertices().end(); ++it) {<br>
  151. &nbsp;&nbsp;&nbsp; const voronoi_diagram&lt;double&gt;::vertex_type
  152. &amp;vertex = *it;<br>
  153. &nbsp;&nbsp;&nbsp; const voronoi_diagram&lt;double&gt;::edge_type *edge
  154. = vertex.incident_edge();<br>
  155. &nbsp;&nbsp;&nbsp; // This is convenient way to iterate edges around
  156. Voronoi vertex.<br>
  157. &nbsp;&nbsp;&nbsp; do {<br>
  158. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (edge-&gt;is_primary())<br>
  159. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++result;<br>
  160. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; edge = edge-&gt;rot_next();<br>
  161. &nbsp;&nbsp;&nbsp; } while (edge != vertex.incident_edge());<br>
  162. &nbsp; }<br>
  163. &nbsp; return result;<br>
  164. }</span></li>
  165. </ul>
  166. This should give a very nice idea on how to do the Voronoi
  167. diagram traversal. Notice that while the output from the first two methods should
  168. be the same, it wouldn't for the third one. The reason is that in the
  169. last case we will iterate only once through the edges with a single
  170. finite endpoint and will skip all the edges with no finite endpoints.<br>
  171. <h2>Associating User Data with Voronoi Primitives</h2>
  172. A few simple cases of associating the user data with the Voronoi primitives are
  173. following:<br>
  174. <ul>
  175. <li>associating number of incident edges with each cell, vertex;</li>
  176. <li>associating color information with each edge;</li>
  177. <li>using DFS or BFS on the Voronoi graph requires to mark visited
  178. edges/vertices/cells.</li>
  179. </ul>
  180. We will consider the first example and will associate the total number
  181. of incident edges with each cell.<br>
  182. Note: Each Voronoi primitive contains mutable color member,
  183. that allows to use it for the graph algorithms or associate user data via array indices.<br>
  184. <br style="font-family: Courier New,Courier,monospace;">
  185. <span style="font-family: Courier New,Courier,monospace;">// Using color member of the Voronoi primitives to store the average number<br>
  186. // of edges around each cell (including secondary edges).<br>
  187. {<br>
  188. &nbsp; printf("Number of edges (including secondary) around the Voronoi cells:\n");<br>
  189. &nbsp; for (voronoi_diagram&lt;double&gt;::const_edge_iterator it = vd.edges().begin();<br>
  190. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; it != vd.edges().end(); ++it) {<br>
  191. &nbsp;&nbsp;&nbsp; std::size_t cnt = it-&gt;cell()-&gt;color();<br>
  192. &nbsp;&nbsp;&nbsp; it-&gt;cell()-&gt;color(cnt + 1);<br>
  193. &nbsp; }<br>
  194. &nbsp; for (voronoi_diagram&lt;double&gt;::const_cell_iterator it = vd.cells().begin();<br>
  195. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; it != vd.cells().end(); ++it) {<br>
  196. &nbsp;&nbsp;&nbsp; printf("%lu ", it-&gt;color());<br>
  197. &nbsp; }<br>
  198. &nbsp; printf("\n");<br>
  199. &nbsp; printf("\n");<br>
  200. }</span><span style="font-family: Courier New,Courier,monospace;"></span><br>
  201. <h2>Accessing Input Site inside the Voronoi Cell</h2>
  202. As explained in the <a href="voronoi_diagram.htm">Voronoi diagram</a>
  203. section, Voronoi cells don't contain coordinates of the input
  204. geometries directly. Instead they contains source index and source
  205. category that uniquely identify input site. The below routines
  206. traverses over the all Voronoi cells, fetches input geometry
  207. corresponding to the Voronoi cell and prints coordinates of the input
  208. site.<br>
  209. <br>
  210. <span style="font-family: Courier New,Courier,monospace;">unsigned int cell_index = 0;</span><br style="font-family: Courier New,Courier,monospace;">
  211. <span style="font-family: Courier New,Courier,monospace;">for (voronoi_diagram&lt;double&gt;::const_cell_iterator it = vd.cells().begin();</span><br style="font-family: Courier New,Courier,monospace;">
  212. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp; it != vd.cells().end(); ++it) {</span><br style="font-family: Courier New,Courier,monospace;">
  213. <span style="font-family: Courier New,Courier,monospace;">&nbsp; if (it-&gt;contains_point()) {</span><br style="font-family: Courier New,Courier,monospace;">
  214. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; std::size_t index = it-&gt;source_index();</span><br style="font-family: Courier New,Courier,monospace;">
  215. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; Point p = points[index];</span><br style="font-family: Courier New,Courier,monospace;">
  216. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; printf("Cell #%ud contains a point: (%d, %d).\n",</span><br style="font-family: Courier New,Courier,monospace;">
  217. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cell_index, x(p), y(p));</span><br style="font-family: Courier New,Courier,monospace;">
  218. <span style="font-family: Courier New,Courier,monospace;">&nbsp; } else {</span><br style="font-family: Courier New,Courier,monospace;">
  219. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; std::size_t index = it-&gt;source_index() - points.size();</span><br style="font-family: Courier New,Courier,monospace;">
  220. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; Point p0 = low(segments[index]);</span><br style="font-family: Courier New,Courier,monospace;">
  221. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; Point p1 = high(segments[index]);</span><br style="font-family: Courier New,Courier,monospace;">
  222. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; if (it-&gt;source_category() ==</span><br style="font-family: Courier New,Courier,monospace;">
  223. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) {</span><br style="font-family: Courier New,Courier,monospace;">
  224. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Cell #%ud contains segment start point: (%d, %d).\n",</span><br style="font-family: Courier New,Courier,monospace;">
  225. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cell_index, x(p0), y(p0));</span><br style="font-family: Courier New,Courier,monospace;">
  226. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; } else if (it-&gt;source_category() ==</span><br style="font-family: Courier New,Courier,monospace;">
  227. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  228. boost::polygon::SOURCE_CATEGORY_SEGMENT_END_POINT) {</span><br style="font-family: Courier New,Courier,monospace;">
  229. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Cell #%ud contains segment end point: (%d, %d).\n",</span><br style="font-family: Courier New,Courier,monospace;">
  230. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cell_index, x(p0), y(p0));</span><br style="font-family: Courier New,Courier,monospace;">
  231. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; } else {</span><br style="font-family: Courier New,Courier,monospace;">
  232. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Cell #%ud contains a segment: ((%d, %d), (%d, %d)). \n",</span><br style="font-family: Courier New,Courier,monospace;">
  233. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cell_index, x(p0), y(p0), x(p1), y(p1));</span><br style="font-family: Courier New,Courier,monospace;">
  234. <span style="font-family: Courier New,Courier,monospace;">&nbsp;&nbsp;&nbsp; }</span><br style="font-family: Courier New,Courier,monospace;">
  235. <span style="font-family: Courier New,Courier,monospace;">&nbsp; }</span><br style="font-family: Courier New,Courier,monospace;">
  236. <span style="font-family: Courier New,Courier,monospace;">&nbsp; ++cell_index;</span><br style="font-family: Courier New,Courier,monospace;">
  237. }<br>
  238. <h2>Voronoi Diagram Rendering<br>
  239. </h2>
  240. There are two main issues that don't allow to strictly render the resulting
  241. Voronoi diagram using such rendering tools as OpenGL or DirectX.
  242. Those are:<br>
  243. <ul>
  244. <li>Some of the Voronoi edges are infinite, so should be clipped;</li>
  245. <li>Some of the Voronoi edge are parabolic arcs, so should be
  246. discretized.</li>
  247. </ul>
  248. Note: This would be the issues not only for rendering tools.
  249. Basically every task that requires diagram to be represented as a set
  250. of finite segments will fall into this category.<a href="../example/voronoi_visualizer.cpp"> voronoi_visualizer.cpp</a>
  251. contains a simple fully featured implementation of the Voronoi diagram
  252. renderer using the Qt libraries. It was used to generate all the .png
  253. drawings under the boost/libs/polygon/example directory.<span style="text-decoration: underline;"></span><span style="font-family: Courier New,Courier,monospace;"><br>
  254. </span>
  255. <h2>Summary<br>
  256. </h2>
  257. <span style="font-family: Courier New,Courier,monospace;">
  258. </span>I hope the reader managed to get to this point and found the
  259. basic tutorial to be useful (in the end it's not so basic). Worth
  260. to notice that construction of the Voronoi diagram takes only two lines
  261. of code, everything else is about initializing input data structures,
  262. traversing Voronoi graph, associating data with the diagram primitives. In the
  263. default mode the Voronoi diagram operates with the signed int (32-bit) input
  264. coordinate type and double (64-bit) output coordinate type. In the <a href="voronoi_advanced_tutorial.htm">advanced Voronoi tutorial</a> we
  265. explain why this is enough for the 95% of cases and how to expand the
  266. algorithm coordinate types for the other 5%.<br>
  267. <span style="font-family: Courier New,Courier,monospace;"></span><br>
  268. <table class="docinfo" id="table1" frame="void" rules="none">
  269. <colgroup> <col class="docinfo-name"><col class="docinfo-content"> </colgroup>
  270. <tbody valign="top">
  271. <tr>
  272. <th class="docinfo-name">Copyright:</th>
  273. <td>Copyright © Andrii Sydorchuk 2010-2012.</td>
  274. </tr>
  275. <tr class="field">
  276. <th class="docinfo-name">License:</th>
  277. <td class="field-body">Distributed under the Boost Software
  278. License, Version 1.0. (See accompanying file <tt class="literal"> <span class="pre">LICENSE_1_0.txt</span></tt> or copy at <a class="reference" target="_top" href="http://www.boost.org/LICENSE_1_0.txt">
  279. http://www.boost.org/LICENSE_1_0.txt</a>)</td>
  280. </tr>
  281. </tbody>
  282. </table>
  283. </body></html>