Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
9.6 KiB

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