home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / graphics / 9034 < prev    next >
Encoding:
Text File  |  1992-08-20  |  10.8 KB  |  277 lines

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