home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / tessel / part02 / tessel.txt < prev    next >
Text File  |  1993-06-21  |  9KB  |  230 lines

  1. From mango@nova.esd.sgi.com (Eric Manghise) to "comp.graphics"
  2.  
  3. |> 
  4. |>     - Is there a simple way of subdividing a 2D polygon with 
  5. |>         holes into convex pieces?  (Where the holes are
  6. |>         2D polygons that are wholly contained in the 
  7. |>         polygon that delineates the exterior boundary.)
  8.  
  9.         No there is no simple way to decompose a polygon
  10.         into convex pieces. I assume you want to decompose
  11.         it into triangles. There are alot of different ways
  12.         to do this. I might refer you to 
  13.  
  14.             Triangulating a Simple Polygon in Linear Time
  15.             Bernard Chazelle
  16.             Department of Computer Science
  17.             Princeton University.
  18.  
  19.         When you add holes you create a whole world of problems.
  20.         Also when you compound this with the case of concave polygons
  21.         life become very complicated.
  22.  
  23.  
  24.         Here is another article I sucked off the net along time ago.
  25.  
  26.  
  27. Concave or Convex?
  28.  
  29. The cruz of your problem is determining whether an angle is convex (< 180
  30. degrees) or not.  Given three distinct points P0, P1, and P2 in the plane,
  31. how do we tell it the angle formed by line segments P0P1 and P1P2 is con-
  32. vex or not?  Before we answer this, we must be clear about our point of
  33. view:  We assume that we are ABOVE the xy-plane (i.e., at some point with
  34. a positive z-coordinate), looking down.  In addition, we define the angle
  35. between P0P1 and P1P2 by measuring the arc swept out by rotating P1P2
  36. about P1 in a COUNTERCLOCKWISE direction until we meet segment P0P1.
  37.  
  38. We don't actually need to determine the exact angle.  We simply want to
  39. know if it is < 180 degrees or not.  Thus, we can conceptualize the pro-
  40. blem in a bit different way:  Consider the infinite line oriented FROM P0
  41. TO P1.  Where is P2 in relation to this line?
  42.  
  43.     P2 is on the left:   The angle is < 180 degrees
  44.     P2 is on the line:   The angle is = 180 degrees
  45.     P2 is on the right:  The angle is > 180 degrees
  46.  
  47. So now the problem boils down to figuring out which side of a line that
  48. a point is on.  This is easily determined.  If L(x, y) = 0 is the ORIEN-
  49. TED equation of a line, where L(x, y) = ax + by + c, then for any point P,
  50.  
  51.     L(P) > 0:  Point P is on the left of the line
  52.     L(P) = 0:  Point P is on the line
  53.     L(P) < 0:  Point P is on the right of the line
  54.  
  55. Thus, to tell whether angle P0P1P2 is convex, do this:  Determine the
  56. oriented equation of the line FROM P0 TO P1, and plug P2 into the equa-
  57. tion.  If the result is > 0, then the angle is convex.
  58.  
  59. WARNING:  As always when working with floating point arithmetic, all 
  60. comparisons should involve an epsilon.  Furthermore, to be safe, the
  61. equation of the line should be normalized by dividing all coefficients
  62. by sqrt(a*a + b*b).  Then when the equation is evalutated at a point,
  63. the result is actually the signed distance of the point from the line,
  64. which can be compared against an epsilon to see where the point is in
  65. relation to the line.
  66.  
  67. A shortcut to the above method of determining a line equation is to use
  68. this determinant, where Ph = (xh, yh), Pi = (xi, yi), and Pj = (xj, yj):
  69.  
  70.             | xh yh 1 |
  71.         D = | xi yi 1 |
  72.             | xj yj 1 |
  73.  
  74. If D > 0 then Pj lies on the left of the line FROM Ph TO Pi.  If D = 0,
  75. the three points are collinear.  If D < 0 then Pj lies on the right of the 
  76. line FROM Ph TO Pi.  (Lying on the left means angle PhPiPj is convex, and
  77. lying on the right means the angle is nonconvex.)
  78.  
  79. DISCLAIMER:  The above determinant should be checked.  I am recalling it 
  80. from memory, and it may not be quite right.
  81.  
  82. Using this determinant is not really different from the method already de-
  83. scribed.  Calculating the determinant is exactly the same as determining 
  84. the equation of the line from point Ph to point Pi and then plugging Pj 
  85. into the equation.  Also, a warning is in order, because no normalization
  86. is done.
  87.  
  88. So now we have a way to tell if an angle is convex or not.  This operation
  89. is the basic primitive for all the following algorithms.
  90.  
  91.  
  92.  
  93. Clockwise or Counterclockwise?
  94.  
  95. Say we are given an array of n >= 3 distinct points labeled P0, P1, ..., 
  96. Pn-1, which are the vertices of a well-formed simple polygon P.  Say that
  97. the n vertices are in order, but we don't know if the order is clockwise
  98. or counterclockwise.  How do we tell?  Here is a simple algorithm:
  99.  
  100. Scan the set of vertices and find the one with least x-coordinate.  If 
  101. there is more than one vertex with least x-coordinate, then pick the one 
  102. of them which has least y-coordinate.  The resulting vertex is unique.  
  103. (If not, the polygon is ill-formed because its vertices are not distinct 
  104. points.) Call this vertex Pi.
  105.  
  106. Let Ph be the vertex preceding Pi, and let Pj be the vertex following Pi.  
  107. Then test if angle PhPiPj is convex.  If it is, then the vertices of poly-
  108. gon P are oriented counterclockwise in our input array.  Otherwise, the
  109. vertices are ordered clockwise.  In the latter case, reverse the elements
  110. of P to put the vertices in counterclockwise order.
  111.  
  112.  
  113.  
  114. Decomposing a 2-d polygon in the xy-plane into triangles
  115.  
  116. Given a well-formed simple polygon P with n >= 3 vertices labeled P0, 
  117. P1, ..., Pn-1, we want to decompose P into triangles.  We assume that
  118. the vertices are oriented in a counterclockwise direction as seen from 
  119. above.
  120.  
  121. Before presenting a general algorithm which handles all cases, it is 
  122. worthwhile to consider the case when it is known that the polygon is
  123. convex (i.e., all angles < 180 degrees).  Then the polygon is easily
  124. decomposed into n-2 triangles by drawing diagonals from vertex 0 to 
  125. the n-3 vertices P2, P3, ..., Pn-2, resulting in these triangles:
  126.  
  127.     P0,P1,P2
  128.     P0,P2,P3
  129.     P0,P3,P4
  130.     . . .
  131.     P0,Pn-2,Pn-1
  132.  
  133. If it is known that many of the polygons to be processed are likely to
  134. be convex, then it is a good idea to test each polygon to see if it is
  135. convex, and if so, apply the above simple algorithm.  Only when a non-
  136. convex polygon is encountered is it necessary to apply the general al-
  137. gorithm.
  138.  
  139. Here is the general algorithm:
  140.  
  141. Let Pi be any vertex of the polygon.  Let Ph be the vertex preceding Pi,
  142. and let Pj be the vertex following Pi.  Test if angle PhPiPj is convex.
  143. If not, increment i, determine h and j from i, and test again.  If a 
  144. convex angle is not found by this process, there is an error in the 
  145. polygon.  It is either not well-formed, or it is oriented clockwise.
  146.  
  147. Once a convex angle PhPiPj is found, the triangle formed by the three 
  148. points is a candidate.  However, it is ONLY a candidate.  It must be
  149. tested like this:  For every vertex V of the polygon other than the three
  150. forming the triangle, test if V is OUTSIDE the triangle (note that V 
  151. must not be ON the boundary of the triangle).  If some vertex is found
  152. which is either on the boundary of or inside of the triangle, then the
  153. triangle must be rejected.  In this case, increment i and continue 
  154. searching for a convex angle.
  155.  
  156. If all V are outside of the triangle, then slice the triangle off the
  157. polygon by removing vertex Pi.  If the number of points in the resulting
  158. polygon is 3, then the decomposition is finished.  Otherwise, search
  159. again from a convex angle.
  160.  
  161. Here is a more detailed version of the algorithm:
  162.  
  163.   i = n-1;
  164.  
  165.   while (n > 3)
  166.     {
  167.     h = i;
  168.     i = (i == n-1 ? 0 : i+1);
  169.     j = (i == n-1 ? 0 : i+1);
  170.  
  171.     if (angle PhPiPj nonconvex)
  172.       continue;
  173.  
  174.     k = (j == n-1 ? 0 : j+1);         /* k is vertex after j  */
  175.     m = n-3;
  176.  
  177.     while (m--)
  178.       if (Pk outside triangle PhPiPj)
  179.         k = (k == n-1 ? 0 : k+1);
  180.       else
  181.         break;
  182.  
  183.       if (k != h)                      /* Vertex k not outside */
  184.         continue;                      /*   triangle PhPiPj    */
  185.  
  186.     Store triangle PhPiPj;
  187.  
  188.     Remove vertex Pi from polygon P;
  189.     n--;
  190.     i = (i == 0 ? n-1 : i-1);
  191.     }
  192.  
  193.   Store triangle P0P1P2 as the final triangle of the decomposition;
  194.  
  195.  
  196. The above algorithm always produces exactly n-2 triangles.  Also, every
  197. vertex of every triangle produced by the algorithm is a vertex of the 
  198. original polygon.  In other words, no new points are created by this al-
  199. gorithm.
  200.  
  201.  
  202.  
  203. Now Back to 3-Space
  204.  
  205. Since your original problem has to do with a polygon in 3-space, we need
  206. to consider how to get from there to the xy-plane.  Say that the input
  207. is an array Q of n >= 3 3-points, Q0, Q1, ..., Qn-1 which are the vertices 
  208. of a well-formed simple planar polygon.
  209.  
  210. If the plane of the polygon is not vertical (consider the xy-plane to be
  211. horizontal), then simply set up a 2-d polygon P with the points Q, ignor-
  212. ing the z-coordinate.  In effect, this projects polygon Q onto the xy-
  213. plane.  Now check if P is counterclockwise, and, if not, reverse its ele-
  214. ments so it is counterclockwise.  Then decompose P into triangles.  Note
  215. that if the z-coordinates are carried in polygon P (they will never be
  216. used because all algorithms above use only x- and y-coordinates), then
  217. the triangles produced by the decomposition will in fact be polygons in
  218. 3-space with correct z-coordinates.  Thus, the input polygon is decom-
  219. posed as desired.
  220.  
  221. If the input polygon is in a vertical plane (which can be determined by
  222. checking to see if the projection onto the xy-plane is a straight line),
  223. then simply swap the x- and z-coordinates of the input vertices, apply
  224. the algorithm in the preceding paragraph, and then swap the x- and z-
  225. coordinates of the resulting triangles.
  226.  
  227. Note that this method of going from 3-space to 2-space works because a 
  228. parallel projection of a polygon does not change any convex angles to
  229. nonconvex ones, or vice versa.
  230.