tessmono.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
  3. * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice including the dates of first publication and
  13. * either this permission notice or a reference to
  14. * http://oss.sgi.com/projects/FreeB/
  15. * shall be included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  22. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Except as contained in this notice, the name of Silicon Graphics, Inc.
  26. * shall not be used in advertising or otherwise to promote the sale, use or
  27. * other dealings in this Software without prior written authorization from
  28. * Silicon Graphics, Inc.
  29. */
  30. /*
  31. ** Author: Eric Veach, July 1994.
  32. **
  33. */
  34. #include "../prboom/SDL_opengl.h" // JDC
  35. //#include "gluos.h"
  36. #include <stdlib.h>
  37. #include "geom.h"
  38. #include "mesh.h"
  39. #include "tessmono.h"
  40. #include <assert.h>
  41. #define AddWinding(eDst,eSrc) (eDst->winding += eSrc->winding, \
  42. eDst->Sym->winding += eSrc->Sym->winding)
  43. /* __gl_meshTessellateMonoRegion( face ) tessellates a monotone region
  44. * (what else would it do??) The region must consist of a single
  45. * loop of half-edges (see mesh.h) oriented CCW. "Monotone" in this
  46. * case means that any vertical line intersects the interior of the
  47. * region in a single interval.
  48. *
  49. * Tessellation consists of adding interior edges (actually pairs of
  50. * half-edges), to split the region into non-overlapping triangles.
  51. *
  52. * The basic idea is explained in Preparata and Shamos (which I don''t
  53. * have handy right now), although their implementation is more
  54. * complicated than this one. The are two edge chains, an upper chain
  55. * and a lower chain. We process all vertices from both chains in order,
  56. * from right to left.
  57. *
  58. * The algorithm ensures that the following invariant holds after each
  59. * vertex is processed: the untessellated region consists of two
  60. * chains, where one chain (say the upper) is a single edge, and
  61. * the other chain is concave. The left vertex of the single edge
  62. * is always to the left of all vertices in the concave chain.
  63. *
  64. * Each step consists of adding the rightmost unprocessed vertex to one
  65. * of the two chains, and forming a fan of triangles from the rightmost
  66. * of two chain endpoints. Determining whether we can add each triangle
  67. * to the fan is a simple orientation test. By making the fan as large
  68. * as possible, we restore the invariant (check it yourself).
  69. */
  70. int __gl_meshTessellateMonoRegion( GLUface *face )
  71. {
  72. GLUhalfEdge *up, *lo;
  73. /* All edges are oriented CCW around the boundary of the region.
  74. * First, find the half-edge whose origin vertex is rightmost.
  75. * Since the sweep goes from left to right, face->anEdge should
  76. * be close to the edge we want.
  77. */
  78. up = face->anEdge;
  79. assert( up->Lnext != up && up->Lnext->Lnext != up );
  80. for( ; VertLeq( up->Dst, up->Org ); up = up->Lprev )
  81. ;
  82. for( ; VertLeq( up->Org, up->Dst ); up = up->Lnext )
  83. ;
  84. lo = up->Lprev;
  85. while( up->Lnext != lo ) {
  86. if( VertLeq( up->Dst, lo->Org )) {
  87. /* up->Dst is on the left. It is safe to form triangles from lo->Org.
  88. * The EdgeGoesLeft test guarantees progress even when some triangles
  89. * are CW, given that the upper and lower chains are truly monotone.
  90. */
  91. while( lo->Lnext != up && (EdgeGoesLeft( lo->Lnext )
  92. || EdgeSign( lo->Org, lo->Dst, lo->Lnext->Dst ) <= 0 )) {
  93. GLUhalfEdge *tempHalfEdge= __gl_meshConnect( lo->Lnext, lo );
  94. if (tempHalfEdge == NULL) return 0;
  95. lo = tempHalfEdge->Sym;
  96. }
  97. lo = lo->Lprev;
  98. } else {
  99. /* lo->Org is on the left. We can make CCW triangles from up->Dst. */
  100. while( lo->Lnext != up && (EdgeGoesRight( up->Lprev )
  101. || EdgeSign( up->Dst, up->Org, up->Lprev->Org ) >= 0 )) {
  102. GLUhalfEdge *tempHalfEdge= __gl_meshConnect( up, up->Lprev );
  103. if (tempHalfEdge == NULL) return 0;
  104. up = tempHalfEdge->Sym;
  105. }
  106. up = up->Lnext;
  107. }
  108. }
  109. /* Now lo->Org == up->Dst == the leftmost vertex. The remaining region
  110. * can be tessellated in a fan from this leftmost vertex.
  111. */
  112. assert( lo->Lnext != up );
  113. while( lo->Lnext->Lnext != up ) {
  114. GLUhalfEdge *tempHalfEdge= __gl_meshConnect( lo->Lnext, lo );
  115. if (tempHalfEdge == NULL) return 0;
  116. lo = tempHalfEdge->Sym;
  117. }
  118. return 1;
  119. }
  120. /* __gl_meshTessellateInterior( mesh ) tessellates each region of
  121. * the mesh which is marked "inside" the polygon. Each such region
  122. * must be monotone.
  123. */
  124. int __gl_meshTessellateInterior( GLUmesh *mesh )
  125. {
  126. GLUface *f, *next;
  127. /*LINTED*/
  128. for( f = mesh->fHead.next; f != &mesh->fHead; f = next ) {
  129. /* Make sure we don''t try to tessellate the new triangles. */
  130. next = f->next;
  131. if( f->inside ) {
  132. if ( !__gl_meshTessellateMonoRegion( f ) ) return 0;
  133. }
  134. }
  135. return 1;
  136. }
  137. /* __gl_meshDiscardExterior( mesh ) zaps (ie. sets to NULL) all faces
  138. * which are not marked "inside" the polygon. Since further mesh operations
  139. * on NULL faces are not allowed, the main purpose is to clean up the
  140. * mesh so that exterior loops are not represented in the data structure.
  141. */
  142. void __gl_meshDiscardExterior( GLUmesh *mesh )
  143. {
  144. GLUface *f, *next;
  145. /*LINTED*/
  146. for( f = mesh->fHead.next; f != &mesh->fHead; f = next ) {
  147. /* Since f will be destroyed, save its next pointer. */
  148. next = f->next;
  149. if( ! f->inside ) {
  150. __gl_meshZapFace( f );
  151. }
  152. }
  153. }
  154. #define MARKED_FOR_DELETION 0x7fffffff
  155. /* __gl_meshSetWindingNumber( mesh, value, keepOnlyBoundary ) resets the
  156. * winding numbers on all edges so that regions marked "inside" the
  157. * polygon have a winding number of "value", and regions outside
  158. * have a winding number of 0.
  159. *
  160. * If keepOnlyBoundary is TRUE, it also deletes all edges which do not
  161. * separate an interior region from an exterior one.
  162. */
  163. int __gl_meshSetWindingNumber( GLUmesh *mesh, int value,
  164. GLboolean keepOnlyBoundary )
  165. {
  166. GLUhalfEdge *e, *eNext;
  167. for( e = mesh->eHead.next; e != &mesh->eHead; e = eNext ) {
  168. eNext = e->next;
  169. if( e->Rface->inside != e->Lface->inside ) {
  170. /* This is a boundary edge (one side is interior, one is exterior). */
  171. e->winding = (e->Lface->inside) ? value : -value;
  172. } else {
  173. /* Both regions are interior, or both are exterior. */
  174. if( ! keepOnlyBoundary ) {
  175. e->winding = 0;
  176. } else {
  177. if ( !__gl_meshDelete( e ) ) return 0;
  178. }
  179. }
  180. }
  181. return 1;
  182. }