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:
10 ** http://oss.sgi.com/projects/FreeB
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.
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.
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.
36 ** Author: Eric Veach, July 1994.
47 #define AddWinding(eDst,eSrc) (eDst->winding += eSrc->winding, \
48 eDst->Sym->winding += eSrc->Sym->winding)
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.
56 * Tessellation consists of adding interior edges (actually pairs of
57 * half-edges), to split the region into non-overlapping triangles.
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,
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.
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).
77 int __gl_meshTessellateMonoRegion( GLUface
*face
)
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.
87 assert( up
->Lnext
!= up
&& up
->Lnext
->Lnext
!= up
);
89 for( ; VertLeq( up
->Dst
, up
->Org
); up
= up
->Lprev
)
91 for( ; VertLeq( up
->Org
, up
->Dst
); up
= up
->Lnext
)
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.
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
;
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
;
120 /* Now lo->Org == up->Dst == the leftmost vertex. The remaining region
121 * can be tessellated in a fan from this leftmost vertex.
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
;
134 /* __gl_meshTessellateInterior( mesh ) tessellates each region of
135 * the mesh which is marked "inside" the polygon. Each such region
138 int __gl_meshTessellateInterior( GLUmesh
*mesh
)
143 for( f
= mesh
->fHead
.next
; f
!= &mesh
->fHead
; f
= next
) {
144 /* Make sure we don''t try to tessellate the new triangles. */
147 if ( !__gl_meshTessellateMonoRegion( f
) ) return 0;
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.
160 void __gl_meshDiscardExterior( GLUmesh
*mesh
)
165 for( f
= mesh
->fHead
.next
; f
!= &mesh
->fHead
; f
= next
) {
166 /* Since f will be destroyed, save its next pointer. */
169 __gl_meshZapFace( f
);
174 #define MARKED_FOR_DELETION 0x7fffffff
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.
181 * If keepOnlyBoundary is TRUE, it also deletes all edges which do not
182 * separate an interior region from an exterior one.
184 int __gl_meshSetWindingNumber( GLUmesh
*mesh
, int value
,
185 GLboolean keepOnlyBoundary
)
187 GLUhalfEdge
*e
, *eNext
;
189 for( e
= mesh
->eHead
.next
; e
!= &mesh
->eHead
; e
= eNext
) {
191 if( e
->Rface
->inside
!= e
->Lface
->inside
) {
193 /* This is a boundary edge (one side is interior, one is exterior). */
194 e
->winding
= (e
->Lface
->inside
) ? value
: -value
;
197 /* Both regions are interior, or both are exterior. */
198 if( ! keepOnlyBoundary
) {
201 if ( !__gl_meshDelete( e
) ) return 0;