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
35 #include <stdlib.h>
36 #include "geom.h"
37 #include "mesh.h"
38 #include "tessmono.h"
39 #include <assert.h>
40
41 #define AddWinding(eDst, eSrc) (eDst->winding+=eSrc->winding, \
42 eDst->Sym->winding+=eSrc->Sym->winding)
43
44 /* __gl_meshTessellateMonoRegion(face) tessellates a monotone region
45 * (what else would it do??) The region must consist of a single
46 * loop of half-edges (see mesh.h) oriented CCW. "Monotone" in this
47 * case means that any vertical line intersects the interior of the
48 * region in a single interval.
49 *
50 * Tessellation consists of adding interior edges (actually pairs of
51 * half-edges), to split the region into non-overlapping triangles.
52 *
53 * The basic idea is explained in Preparata and Shamos (which I don''t
54 * have handy right now), although their implementation is more
55 * complicated than this one. The are two edge chains, an upper chain
56 * and a lower chain. We process all vertices from both chains in order,
57 * from right to left.
58 *
59 * The algorithm ensures that the following invariant holds after each
60 * vertex is processed: the untessellated region consists of two
61 * chains, where one chain (say the upper) is a single edge, and
62 * the other chain is concave. The left vertex of the single edge
63 * is always to the left of all vertices in the concave chain.
64 *
65 * Each step consists of adding the rightmost unprocessed vertex to one
66 * of the two chains, and forming a fan of triangles from the rightmost
67 * of two chain endpoints. Determining whether we can add each triangle
68 * to the fan is a simple orientation test. By making the fan as large
69 * as possible, we restore the invariant (check it yourself).
70 */
__gl_meshTessellateMonoRegion(GLUESface * face)71 int __gl_meshTessellateMonoRegion(GLUESface* face)
72 {
73 GLUEShalfEdge* up;
74 GLUEShalfEdge* lo;
75
76 /* All edges are oriented CCW around the boundary of the region.
77 * First, find the half-edge whose origin vertex is rightmost.
78 * Since the sweep goes from left to right, face->anEdge should
79 * be close to the edge we want.
80 */
81 up=face->anEdge;
82 assert(up->Lnext!=up && up->Lnext->Lnext!=up);
83
84 for(; VertLeq(up->Dst, up->Org); up=up->Lprev);
85 for(; VertLeq(up->Org, up->Dst); up=up->Lnext);
86 lo=up->Lprev;
87
88 while(up->Lnext!=lo)
89 {
90 if (VertLeq(up->Dst, lo->Org))
91 {
92 /* up->Dst is on the left. It is safe to form triangles from lo->Org.
93 * The EdgeGoesLeft test guarantees progress even when some triangles
94 * are CW, given that the upper and lower chains are truly monotone.
95 */
96 while (lo->Lnext!=up && (EdgeGoesLeft(lo->Lnext) || EdgeSign(lo->Org, lo->Dst, lo->Lnext->Dst)<=0))
97 {
98 GLUEShalfEdge* tempHalfEdge=__gl_meshConnect(lo->Lnext, lo);
99 if (tempHalfEdge==NULL)
100 {
101 return 0;
102 }
103 lo=tempHalfEdge->Sym;
104 }
105 lo=lo->Lprev;
106 }
107 else
108 {
109 /* lo->Org is on the left. We can make CCW triangles from up->Dst. */
110 while(lo->Lnext!=up && (EdgeGoesRight(up->Lprev) || EdgeSign(up->Dst, up->Org, up->Lprev->Org)>=0))
111 {
112 GLUEShalfEdge* tempHalfEdge=__gl_meshConnect(up, up->Lprev);
113 if (tempHalfEdge==NULL)
114 {
115 return 0;
116 }
117 up=tempHalfEdge->Sym;
118 }
119 up=up->Lnext;
120 }
121 }
122
123 /* Now lo->Org == up->Dst == the leftmost vertex. The remaining region
124 * can be tessellated in a fan from this leftmost vertex.
125 */
126 assert(lo->Lnext!=up);
127 while(lo->Lnext->Lnext!=up)
128 {
129 GLUEShalfEdge* tempHalfEdge=__gl_meshConnect(lo->Lnext, lo);
130 if (tempHalfEdge==NULL)
131 {
132 return 0;
133 }
134 lo=tempHalfEdge->Sym;
135 }
136
137 return 1;
138 }
139
140 /* __gl_meshTessellateInterior( mesh ) tessellates each region of
141 * the mesh which is marked "inside" the polygon. Each such region
142 * must be monotone.
143 */
__gl_meshTessellateInterior(GLUESmesh * mesh)144 int __gl_meshTessellateInterior(GLUESmesh* mesh)
145 {
146 GLUESface* f;
147 GLUESface* next;
148
149 /*LINTED*/
150 for (f=mesh->fHead.next; f!=&mesh->fHead; f=next)
151 {
152 /* Make sure we don''t try to tessellate the new triangles. */
153 next=f->next;
154 if (f->inside)
155 {
156 if (!__gl_meshTessellateMonoRegion(f))
157 {
158 return 0;
159 }
160 }
161 }
162
163 return 1;
164 }
165
166
167 /* __gl_meshDiscardExterior( mesh ) zaps (ie. sets to NULL) all faces
168 * which are not marked "inside" the polygon. Since further mesh operations
169 * on NULL faces are not allowed, the main purpose is to clean up the
170 * mesh so that exterior loops are not represented in the data structure.
171 */
__gl_meshDiscardExterior(GLUESmesh * mesh)172 void __gl_meshDiscardExterior(GLUESmesh* mesh)
173 {
174 GLUESface* f;
175 GLUESface* next;
176
177 /*LINTED*/
178 for (f=mesh->fHead.next; f!=&mesh->fHead; f=next)
179 {
180 /* Since f will be destroyed, save its next pointer. */
181 next=f->next;
182 if (!f->inside)
183 {
184 __gl_meshZapFace(f);
185 }
186 }
187 }
188
189 #define MARKED_FOR_DELETION 0x7fffffff
190
191 /* __gl_meshSetWindingNumber( mesh, value, keepOnlyBoundary ) resets the
192 * winding numbers on all edges so that regions marked "inside" the
193 * polygon have a winding number of "value", and regions outside
194 * have a winding number of 0.
195 *
196 * If keepOnlyBoundary is TRUE, it also deletes all edges which do not
197 * separate an interior region from an exterior one.
198 */
__gl_meshSetWindingNumber(GLUESmesh * mesh,int value,GLboolean keepOnlyBoundary)199 int __gl_meshSetWindingNumber(GLUESmesh* mesh, int value, GLboolean keepOnlyBoundary)
200 {
201 GLUEShalfEdge* e;
202 GLUEShalfEdge* eNext;
203
204 for (e=mesh->eHead.next; e!=&mesh->eHead; e=eNext)
205 {
206 eNext=e->next;
207 if (e->Rface->inside!=e->Lface->inside)
208 {
209 /* This is a boundary edge (one side is interior, one is exterior). */
210 e->winding=(e->Lface->inside) ? value : -value;
211 }
212 else
213 {
214 /* Both regions are interior, or both are exterior. */
215 if (!keepOnlyBoundary)
216 {
217 e->winding = 0;
218 }
219 else
220 {
221 if (!__gl_meshDelete(e))
222 {
223 return 0;
224 }
225 }
226 }
227 }
228
229 return 1;
230 }
231