home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / graphics / 9360 < prev    next >
Encoding:
Text File  |  1992-09-01  |  9.0 KB  |  160 lines

  1. Newsgroups: comp.graphics
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!ames!pacbell.com!well!levine
  3. From: levine@well.sf.ca.us (Ron Levine)
  4. Subject: Re: Polygon normal direction?
  5. Message-ID: <BtxF72.83s@well.sf.ca.us>
  6. Sender: news@well.sf.ca.us
  7. Organization: Whole Earth 'Lectronic Link
  8. References: <1992Sep1.054514.2272@jyu.fi>
  9. Date: Wed, 2 Sep 1992 01:09:02 GMT
  10. Lines: 148
  11.  
  12. In article <1992Sep1.054514.2272@jyu.fi> ap@jyu.fi (Patric Aalto) writes:
  13. >Hi!
  14. >
  15. >Sorry for this FAQ-like question, but I didn't find a reference to it in
  16. >the FAQ, nor in any of the graphics books and articles I have.
  17. >
  18. >I have a set of 3D objects, consisting of polygons (triangles). I would
  19. >like to render these objects effectively on my 486. In order to do that,
  20. >I need to do backface elimination, among other things. The problem is,
  21. >the polygon vertices are in no particular order (about 50% are clockwise,
  22. >50% anti-clockwise, I could quess). I can easily calculate normal vectors
  23. >for the polygons, but I would need some algorithm to select the correct
  24. >direction to this normal. What is the correct algorithm?
  25. >
  26. >I tried to use common sense, and coded an algorithm that sends a ray from
  27. >a point in the middle of the polygon to the direction of the normal. I
  28. >then checked this ray against all the other polygons, and decided that
  29. >if the number of intersections is odd, I must negate the normal direction.
  30. >This method works somewhat (after this around 80% of the normals point
  31. >to the correct direction). But this obviously is not the final solution.
  32. >
  33. >Could you please help me with this, or point me to a book/article that
  34. >would be helpfull?
  35. >
  36. >Thanks in advance,
  37. >
  38. >Patrick Aalto
  39. >ap@tukki.jyu.fi
  40. >
  41.  
  42. Your question is a good one, and not in any FAQ that I know of.  But its 
  43. best answer depends on the exact nature of your "objects", which you don't 
  44. make precisely clear.   In any case, I don't agree that your proposed 
  45. algorithm makes "common sense". 
  46.  
  47. Before addressing your main question, I would like to point out that it is 
  48. important to understand that "clockwise" and "anti-clockwise" have no intrinsic 
  49. meaning in 3-space.  That is, the "clockwise" ordering for the vertices of a 
  50. plane polygon in 3-space is defined only with respect to a definite choice for 
  51. the normal vector direction.  
  52.  
  53. The key concept needed to address your question is "oriented polygon".   An 
  54. ordered set of coplanar vertices in 3-space defines an oriented polygon.  As 
  55. you already understand, an oriented polygon has a well-defined normal vector 
  56. direction, conventionally specified by a right hand rule.  Any cyclic 
  57. permutation of a given ordered set of vertices defines the same oriented 
  58. polygon.  The reverse ordering of a given ordered set of vertices, or any 
  59. cyclic permutation of the reverse ordering, defines the same polygon, 
  60. considered as a point set, but with the opposite orientation; it is 
  61. considered a different oriented polygon and its corresponding unit normal 
  62. vector is the negative of the unit normal vector of the given oriented 
  63. polygon.  Any other permutation of a given ordered set of vertices defines, in 
  64. general, a different polygon, a different point set. 
  65.  
  66. In addition to "oriented polygon", we have the concept of "oriented edge".  An 
  67. edge of a polygon is a line segment joining an adjacent pair of its vertices.  
  68. The edge is oriented by specifying the two vertices in a definite order.  
  69. Clearly, an orientation of a polygon induces an orientation on each of its 
  70. edges, and reversing the orientation of the polygon reverses the induced 
  71. orientation of each of its edges. 
  72.  
  73. Now, to address your question:
  74.  
  75. There is, of course, no canonical way to select an orientation for an isolated 
  76. polygon in 3-space.  Rather, your goal is to select orientations for all of 
  77. the polygons together in a coherent way.  To say what this means, we have to 
  78. be a little more explicit about the nature of your "objects". 
  79.  
  80. Let me suppose that each of your "objects" is defined by a set of simple 
  81. (i.e. non-self-intersecting) polygons that form a well-behaved connected 
  82. polyhedral surface.  That is, the simple polygons meet pairwise in common 
  83. edges, each edge belongs to at most two polygons, and any two polygons in an 
  84. object belong to a chain of pairwise adjacent polygons. The polygons of a 
  85. polyhedral surface are usually called "faces". 
  86.  
  87. It is probably also the case, although you don't state it explicitly, that 
  88. your "objects" are polyhedral solids.   The boundary surface of a solid is 
  89. closed, which means that every edge must belong to exactly two faces.  I do 
  90. not assume that your surfaces are closed in what follows, but will mention 
  91. some important consequences that follow when they are closed.
  92.  
  93. Now, if a selection of normal vector directions for all the faces of a 
  94. connected polyhedral surface is to have any usefulness at all, it must be 
  95. coherent.  This means that the normal vectors of any pair of adjacent faces 
  96. must be consistent, that is, they must point to the "same side" of the 
  97. dihedral surface formed by the two adjacent faces.  I won't try to give a 
  98. formal proof, but it should be easy to convince yourself that an adjacent pair 
  99. of oriented faces is oriented consistently if the two face orientations induce 
  100. opposite orientations on their common edge. 
  101.  
  102. Now we have the tools to describe the correct algorithm for orienting the 
  103. faces of a connected polyhedral surface.  First, you need some basis for 
  104. orienting at least one of the faces.  I'll come back to this later.  Given one 
  105. oriented face, check each of its edges.  I've assumed that each edge belongs 
  106. to at most one other face.  For each edge, find the other face, if any, 
  107. that contains that edge and orient it so that it induces the opposite 
  108. orientation on the common edge.  Then repeat the process for each of the 
  109. remaining edges of each of the faces meeting the first face, and so on.  
  110. Because the surface is connected, you will eventually reach all of its faces.  
  111. However, during this process, while trying to consistently orient all the 
  112. faces meeting the current face, you are practically certain to encounter faces 
  113. that have been previously encountered and oriented in the process.  What 
  114. happens if the orientation induced on this face by the current oriented face 
  115. does not agree with its previous orientation?  If this happens, it is 
  116. impossible to coherently orient the faces of the surface.  You have an example 
  117. of what is called a non-orientable surface. 
  118.  
  119. Non-orientable surfaces exist.  The Moebius strip is a non-closed non-
  120. orientable surface and the Klein bottle is a closed non-orientable surface.  I 
  121. don't know any examples of closed non-orientable surfaces in 3-space that are 
  122. not self-intersecting.   I believe that any polyhedral surface that is the 
  123. boundary surface of a solid is orientable.  I don't know your method of 
  124. generating the objects in your application, but if it is sensible, the chances 
  125. are you will not have to deal with non-orientability.
  126.  
  127. Now let's return to the question of selecting an orientation of an initial 
  128. polygon in the algorithm, and so an orientation of the surface.  Just as there 
  129. is no canonical way to select a proper orientation of an isolated face, there 
  130. is no intrinsic way to select a proper orientation of a non-closed orientable 
  131. surface--to distinguish a "top side" from a "bottom side".  You need some 
  132. external criterion.  However, for a bounded closed orientable surface, a 
  133. surface that is the boundary surface of a solid of finite extent (as I suspect 
  134. you are dealing with), there is a natural distinction between an "inside" and 
  135. an "outside".  Such a surface divides the 3-space into two pieces; the outside 
  136. is the part of space that runs out to infinity.  It is common practice to 
  137. orient such a surface so that the normal vector points from the inside towards 
  138. the outside.   However, an algorithm which could determine this direction in 
  139. the most general case would be very complex.   Hopefully, your application 
  140. adds special conditions which will make it easy to find at least one face for 
  141. which it is easy to distinguish the inside from the outside. 
  142.  
  143. The algorithm I've just described is appropriate if your model data structure 
  144. is some sort of face-vertex representation, in which you have a single array 
  145. of vertices (with floating point coordinates) and represent the faces by lists 
  146. of indices into the vertex array.  If you represent each polygon by an 
  147. independent list of vertex representation, then you have a real messy problem 
  148. in determining the adjacent faces.
  149.  
  150. I've never had to code up this algorithm in the extensive geometric modeling 
  151. work I've done, because I always take care to coherently orient the faces in 
  152. the process of generating the model objects.  Unfortunately, not all existing 
  153. libraries of 3D model objects take care to do this.  
  154.  
  155. One final point.  I've mentioned several times that I think you are modeling 
  156. solids and so your surface objects are closed.  If this is not the case then 
  157. you should think twice before using back face elimination.  For closed 
  158. polyhedral surfaces, back faces are hidden, so can be eliminated, but non-
  159. closed surfaces may well have back faces that are visible.
  160.