Merge branch 'gallium-0.1' into gallium-0.2
[mesa.git] / src / glu / sgi / libtess / tessmono.c
1 /*
2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17 **
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
23 **
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
33 **
34 */
35 /*
36 ** Author: Eric Veach, July 1994.
37 **
38 */
39
40 #include "gluos.h"
41 #include <stdlib.h>
42 #include "geom.h"
43 #include "mesh.h"
44 #include "tessmono.h"
45 #include <assert.h>
46
47 #define AddWinding(eDst,eSrc) (eDst->winding += eSrc->winding, \
48 eDst->Sym->winding += eSrc->Sym->winding)
49
50 /* __gl_meshTessellateMonoRegion( face ) tessellates a monotone region
51 * (what else would it do??) The region must consist of a single
52 * loop of half-edges (see mesh.h) oriented CCW. "Monotone" in this
53 * case means that any vertical line intersects the interior of the
54 * region in a single interval.
55 *
56 * Tessellation consists of adding interior edges (actually pairs of
57 * half-edges), to split the region into non-overlapping triangles.
58 *
59 * The basic idea is explained in Preparata and Shamos (which I don''t
60 * have handy right now), although their implementation is more
61 * complicated than this one. The are two edge chains, an upper chain
62 * and a lower chain. We process all vertices from both chains in order,
63 * from right to left.
64 *
65 * The algorithm ensures that the following invariant holds after each
66 * vertex is processed: the untessellated region consists of two
67 * chains, where one chain (say the upper) is a single edge, and
68 * the other chain is concave. The left vertex of the single edge
69 * is always to the left of all vertices in the concave chain.
70 *
71 * Each step consists of adding the rightmost unprocessed vertex to one
72 * of the two chains, and forming a fan of triangles from the rightmost
73 * of two chain endpoints. Determining whether we can add each triangle
74 * to the fan is a simple orientation test. By making the fan as large
75 * as possible, we restore the invariant (check it yourself).
76 */
77 int __gl_meshTessellateMonoRegion( GLUface *face )
78 {
79 GLUhalfEdge *up, *lo;
80
81 /* All edges are oriented CCW around the boundary of the region.
82 * First, find the half-edge whose origin vertex is rightmost.
83 * Since the sweep goes from left to right, face->anEdge should
84 * be close to the edge we want.
85 */
86 up = face->anEdge;
87 assert( up->Lnext != up && up->Lnext->Lnext != up );
88
89 for( ; VertLeq( up->Dst, up->Org ); up = up->Lprev )
90 ;
91 for( ; VertLeq( up->Org, up->Dst ); up = up->Lnext )
92 ;
93 lo = up->Lprev;
94
95 while( up->Lnext != lo ) {
96 if( VertLeq( up->Dst, lo->Org )) {
97 /* up->Dst is on the left. It is safe to form triangles from lo->Org.
98 * The EdgeGoesLeft test guarantees progress even when some triangles
99 * are CW, given that the upper and lower chains are truly monotone.
100 */
101 while( lo->Lnext != up && (EdgeGoesLeft( lo->Lnext )
102 || EdgeSign( lo->Org, lo->Dst, lo->Lnext->Dst ) <= 0 )) {
103 GLUhalfEdge *tempHalfEdge= __gl_meshConnect( lo->Lnext, lo );
104 if (tempHalfEdge == NULL) return 0;
105 lo = tempHalfEdge->Sym;
106 }
107 lo = lo->Lprev;
108 } else {
109 /* lo->Org is on the left. We can make CCW triangles from up->Dst. */
110 while( lo->Lnext != up && (EdgeGoesRight( up->Lprev )
111 || EdgeSign( up->Dst, up->Org, up->Lprev->Org ) >= 0 )) {
112 GLUhalfEdge *tempHalfEdge= __gl_meshConnect( up, up->Lprev );
113 if (tempHalfEdge == NULL) return 0;
114 up = tempHalfEdge->Sym;
115 }
116 up = up->Lnext;
117 }
118 }
119
120 /* Now lo->Org == up->Dst == the leftmost vertex. The remaining region
121 * can be tessellated in a fan from this leftmost vertex.
122 */
123 assert( lo->Lnext != up );
124 while( lo->Lnext->Lnext != up ) {
125 GLUhalfEdge *tempHalfEdge= __gl_meshConnect( lo->Lnext, lo );
126 if (tempHalfEdge == NULL) return 0;
127 lo = tempHalfEdge->Sym;
128 }
129
130 return 1;
131 }
132
133
134 /* __gl_meshTessellateInterior( mesh ) tessellates each region of
135 * the mesh which is marked "inside" the polygon. Each such region
136 * must be monotone.
137 */
138 int __gl_meshTessellateInterior( GLUmesh *mesh )
139 {
140 GLUface *f, *next;
141
142 /*LINTED*/
143 for( f = mesh->fHead.next; f != &mesh->fHead; f = next ) {
144 /* Make sure we don''t try to tessellate the new triangles. */
145 next = f->next;
146 if( f->inside ) {
147 if ( !__gl_meshTessellateMonoRegion( f ) ) return 0;
148 }
149 }
150
151 return 1;
152 }
153
154
155 /* __gl_meshDiscardExterior( mesh ) zaps (ie. sets to NULL) all faces
156 * which are not marked "inside" the polygon. Since further mesh operations
157 * on NULL faces are not allowed, the main purpose is to clean up the
158 * mesh so that exterior loops are not represented in the data structure.
159 */
160 void __gl_meshDiscardExterior( GLUmesh *mesh )
161 {
162 GLUface *f, *next;
163
164 /*LINTED*/
165 for( f = mesh->fHead.next; f != &mesh->fHead; f = next ) {
166 /* Since f will be destroyed, save its next pointer. */
167 next = f->next;
168 if( ! f->inside ) {
169 __gl_meshZapFace( f );
170 }
171 }
172 }
173
174 #define MARKED_FOR_DELETION 0x7fffffff
175
176 /* __gl_meshSetWindingNumber( mesh, value, keepOnlyBoundary ) resets the
177 * winding numbers on all edges so that regions marked "inside" the
178 * polygon have a winding number of "value", and regions outside
179 * have a winding number of 0.
180 *
181 * If keepOnlyBoundary is TRUE, it also deletes all edges which do not
182 * separate an interior region from an exterior one.
183 */
184 int __gl_meshSetWindingNumber( GLUmesh *mesh, int value,
185 GLboolean keepOnlyBoundary )
186 {
187 GLUhalfEdge *e, *eNext;
188
189 for( e = mesh->eHead.next; e != &mesh->eHead; e = eNext ) {
190 eNext = e->next;
191 if( e->Rface->inside != e->Lface->inside ) {
192
193 /* This is a boundary edge (one side is interior, one is exterior). */
194 e->winding = (e->Lface->inside) ? value : -value;
195 } else {
196
197 /* Both regions are interior, or both are exterior. */
198 if( ! keepOnlyBoundary ) {
199 e->winding = 0;
200 } else {
201 if ( !__gl_meshDelete( e ) ) return 0;
202 }
203 }
204 }
205 return 1;
206 }