alg-outline 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. */
  3. This is only a very brief overview. There is quite a bit of
  4. additional documentation in the source code itself.
  5. Goals of robust tesselation
  6. ---------------------------
  7. The tesselation algorithm is fundamentally a 2D algorithm. We
  8. initially project all data into a plane; our goal is to robustly
  9. tesselate the projected data. The same topological tesselation is
  10. then applied to the input data.
  11. Topologically, the output should always be a tesselation. If the
  12. input is even slightly non-planar, then some triangles will
  13. necessarily be back-facing when viewed from some angles, but the goal
  14. is to minimize this effect.
  15. The algorithm needs some capability of cleaning up the input data as
  16. well as the numerical errors in its own calculations. One way to do
  17. this is to specify a tolerance as defined above, and clean up the
  18. input and output during the line sweep process. At the very least,
  19. the algorithm must handle coincident vertices, vertices incident to an
  20. edge, and coincident edges.
  21. Phases of the algorithm
  22. -----------------------
  23. 1. Find the polygon normal N.
  24. 2. Project the vertex data onto a plane. It does not need to be
  25. perpendicular to the normal, eg. we can project onto the plane
  26. perpendicular to the coordinate axis whose dot product with N
  27. is largest.
  28. 3. Using a line-sweep algorithm, partition the plane into x-monotone
  29. regions. Any vertical line intersects an x-monotone region in
  30. at most one interval.
  31. 4. Triangulate the x-monotone regions.
  32. 5. Group the triangles into strips and fans.
  33. Finding the normal vector
  34. -------------------------
  35. A common way to find a polygon normal is to compute the signed area
  36. when the polygon is projected along the three coordinate axes. We
  37. can't do this, since contours can have zero area without being
  38. degenerate (eg. a bowtie).
  39. We fit a plane to the vertex data, ignoring how they are connected
  40. into contours. Ideally this would be a least-squares fit; however for
  41. our purpose the accuracy of the normal is not important. Instead we
  42. find three vertices which are widely separated, and compute the normal
  43. to the triangle they form. The vertices are chosen so that the
  44. triangle has an area at least 1/sqrt(3) times the largest area of any
  45. triangle formed using the input vertices.
  46. The contours do affect the orientation of the normal; after computing
  47. the normal, we check that the sum of the signed contour areas is
  48. non-negative, and reverse the normal if necessary.
  49. Projecting the vertices
  50. -----------------------
  51. We project the vertices onto a plane perpendicular to one of the three
  52. coordinate axes. This helps numerical accuracy by removing a
  53. transformation step between the original input data and the data
  54. processed by the algorithm. The projection also compresses the input
  55. data; the 2D distance between vertices after projection may be smaller
  56. than the original 2D distance. However by choosing the coordinate
  57. axis whose dot product with the normal is greatest, the compression
  58. factor is at most 1/sqrt(3).
  59. Even though the *accuracy* of the normal is not that important (since
  60. we are projecting perpendicular to a coordinate axis anyway), the
  61. *robustness* of the computation is important. For example, if there
  62. are many vertices which lie almost along a line, and one vertex V
  63. which is well-separated from the line, then our normal computation
  64. should involve V otherwise the results will be garbage.
  65. The advantage of projecting perpendicular to the polygon normal is
  66. that computed intersection points will be as close as possible to
  67. their ideal locations. To get this behavior, define TRUE_PROJECT.
  68. The Line Sweep
  69. --------------
  70. There are three data structures: the mesh, the event queue, and the
  71. edge dictionary.
  72. The mesh is a "quad-edge" data structure which records the topology of
  73. the current decomposition; for details see the include file "mesh.h".
  74. The event queue simply holds all vertices (both original and computed
  75. ones), organized so that we can quickly extract the vertex with the
  76. minimum x-coord (and among those, the one with the minimum y-coord).
  77. The edge dictionary describes the current intersection of the sweep
  78. line with the regions of the polygon. This is just an ordering of the
  79. edges which intersect the sweep line, sorted by their current order of
  80. intersection. For each pair of edges, we store some information about
  81. the monotone region between them -- these are call "active regions"
  82. (since they are crossed by the current sweep line).
  83. The basic algorithm is to sweep from left to right, processing each
  84. vertex. The processed portion of the mesh (left of the sweep line) is
  85. a planar decomposition. As we cross each vertex, we update the mesh
  86. and the edge dictionary, then we check any newly adjacent pairs of
  87. edges to see if they intersect.
  88. A vertex can have any number of edges. Vertices with many edges can
  89. be created as vertices are merged and intersection points are
  90. computed. For unprocessed vertices (right of the sweep line), these
  91. edges are in no particular order around the vertex; for processed
  92. vertices, the topological ordering should match the geometric ordering.
  93. The vertex processing happens in two phases: first we process are the
  94. left-going edges (all these edges are currently in the edge
  95. dictionary). This involves:
  96. - deleting the left-going edges from the dictionary;
  97. - relinking the mesh if necessary, so that the order of these edges around
  98. the event vertex matches the order in the dictionary;
  99. - marking any terminated regions (regions which lie between two left-going
  100. edges) as either "inside" or "outside" according to their winding number.
  101. When there are no left-going edges, and the event vertex is in an
  102. "interior" region, we need to add an edge (to split the region into
  103. monotone pieces). To do this we simply join the event vertex to the
  104. rightmost left endpoint of the upper or lower edge of the containing
  105. region.
  106. Then we process the right-going edges. This involves:
  107. - inserting the edges in the edge dictionary;
  108. - computing the winding number of any newly created active regions.
  109. We can compute this incrementally using the winding of each edge
  110. that we cross as we walk through the dictionary.
  111. - relinking the mesh if necessary, so that the order of these edges around
  112. the event vertex matches the order in the dictionary;
  113. - checking any newly adjacent edges for intersection and/or merging.
  114. If there are no right-going edges, again we need to add one to split
  115. the containing region into monotone pieces. In our case it is most
  116. convenient to add an edge to the leftmost right endpoint of either
  117. containing edge; however we may need to change this later (see the
  118. code for details).
  119. Invariants
  120. ----------
  121. These are the most important invariants maintained during the sweep.
  122. We define a function VertLeq(v1,v2) which defines the order in which
  123. vertices cross the sweep line, and a function EdgeLeq(e1,e2; loc)
  124. which says whether e1 is below e2 at the sweep event location "loc".
  125. This function is defined only at sweep event locations which lie
  126. between the rightmost left endpoint of {e1,e2}, and the leftmost right
  127. endpoint of {e1,e2}.
  128. Invariants for the Edge Dictionary.
  129. - Each pair of adjacent edges e2=Succ(e1) satisfies EdgeLeq(e1,e2)
  130. at any valid location of the sweep event.
  131. - If EdgeLeq(e2,e1) as well (at any valid sweep event), then e1 and e2
  132. share a common endpoint.
  133. - For each e in the dictionary, e->Dst has been processed but not e->Org.
  134. - Each edge e satisfies VertLeq(e->Dst,event) && VertLeq(event,e->Org)
  135. where "event" is the current sweep line event.
  136. - No edge e has zero length.
  137. - No two edges have identical left and right endpoints.
  138. Invariants for the Mesh (the processed portion).
  139. - The portion of the mesh left of the sweep line is a planar graph,
  140. ie. there is *some* way to embed it in the plane.
  141. - No processed edge has zero length.
  142. - No two processed vertices have identical coordinates.
  143. - Each "inside" region is monotone, ie. can be broken into two chains
  144. of monotonically increasing vertices according to VertLeq(v1,v2)
  145. - a non-invariant: these chains may intersect (slightly) due to
  146. numerical errors, but this does not affect the algorithm's operation.
  147. Invariants for the Sweep.
  148. - If a vertex has any left-going edges, then these must be in the edge
  149. dictionary at the time the vertex is processed.
  150. - If an edge is marked "fixUpperEdge" (it is a temporary edge introduced
  151. by ConnectRightVertex), then it is the only right-going edge from
  152. its associated vertex. (This says that these edges exist only
  153. when it is necessary.)
  154. Robustness
  155. ----------
  156. The key to the robustness of the algorithm is maintaining the
  157. invariants above, especially the correct ordering of the edge
  158. dictionary. We achieve this by:
  159. 1. Writing the numerical computations for maximum precision rather
  160. than maximum speed.
  161. 2. Making no assumptions at all about the results of the edge
  162. intersection calculations -- for sufficiently degenerate inputs,
  163. the computed location is not much better than a random number.
  164. 3. When numerical errors violate the invariants, restore them
  165. by making *topological* changes when necessary (ie. relinking
  166. the mesh structure).
  167. Triangulation and Grouping
  168. --------------------------
  169. We finish the line sweep before doing any triangulation. This is
  170. because even after a monotone region is complete, there can be further
  171. changes to its vertex data because of further vertex merging.
  172. After triangulating all monotone regions, we want to group the
  173. triangles into fans and strips. We do this using a greedy approach.
  174. The triangulation itself is not optimized to reduce the number of
  175. primitives; we just try to get a reasonable decomposition of the
  176. computed triangulation.