80afddec1eb8e0309b98d2d410b83ca228d4e72e
[mesa.git] / src / glu / sgi / libtess / mesh.h
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 ** $Date: 2001/03/17 00:25:41 $ $Revision: 1.1 $
34 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libtess/mesh.h,v 1.1 2001/03/17 00:25:41 brianp Exp $
35 */
36
37 #ifndef __mesh_h_
38 #define __mesh_h_
39
40 #include <GL/glu.h>
41
42 typedef struct GLUmesh GLUmesh;
43
44 typedef struct GLUvertex GLUvertex;
45 typedef struct GLUface GLUface;
46 typedef struct GLUhalfEdge GLUhalfEdge;
47
48 typedef struct ActiveRegion ActiveRegion; /* Internal data */
49
50 /* The mesh structure is similar in spirit, notation, and operations
51 * to the "quad-edge" structure (see L. Guibas and J. Stolfi, Primitives
52 * for the manipulation of general subdivisions and the computation of
53 * Voronoi diagrams, ACM Transactions on Graphics, 4(2):74-123, April 1985).
54 * For a simplified description, see the course notes for CS348a,
55 * "Mathematical Foundations of Computer Graphics", available at the
56 * Stanford bookstore (and taught during the fall quarter).
57 * The implementation also borrows a tiny subset of the graph-based approach
58 * use in Mantyla's Geometric Work Bench (see M. Mantyla, An Introduction
59 * to Sold Modeling, Computer Science Press, Rockville, Maryland, 1988).
60 *
61 * The fundamental data structure is the "half-edge". Two half-edges
62 * go together to make an edge, but they point in opposite directions.
63 * Each half-edge has a pointer to its mate (the "symmetric" half-edge Sym),
64 * its origin vertex (Org), the face on its left side (Lface), and the
65 * adjacent half-edges in the CCW direction around the origin vertex
66 * (Onext) and around the left face (Lnext). There is also a "next"
67 * pointer for the global edge list (see below).
68 *
69 * The notation used for mesh navigation:
70 * Sym = the mate of a half-edge (same edge, but opposite direction)
71 * Onext = edge CCW around origin vertex (keep same origin)
72 * Dnext = edge CCW around destination vertex (keep same dest)
73 * Lnext = edge CCW around left face (dest becomes new origin)
74 * Rnext = edge CCW around right face (origin becomes new dest)
75 *
76 * "prev" means to substitute CW for CCW in the definitions above.
77 *
78 * The mesh keeps global lists of all vertices, faces, and edges,
79 * stored as doubly-linked circular lists with a dummy header node.
80 * The mesh stores pointers to these dummy headers (vHead, fHead, eHead).
81 *
82 * The circular edge list is special; since half-edges always occur
83 * in pairs (e and e->Sym), each half-edge stores a pointer in only
84 * one direction. Starting at eHead and following the e->next pointers
85 * will visit each *edge* once (ie. e or e->Sym, but not both).
86 * e->Sym stores a pointer in the opposite direction, thus it is
87 * always true that e->Sym->next->Sym->next == e.
88 *
89 * Each vertex has a pointer to next and previous vertices in the
90 * circular list, and a pointer to a half-edge with this vertex as
91 * the origin (NULL if this is the dummy header). There is also a
92 * field "data" for client data.
93 *
94 * Each face has a pointer to the next and previous faces in the
95 * circular list, and a pointer to a half-edge with this face as
96 * the left face (NULL if this is the dummy header). There is also
97 * a field "data" for client data.
98 *
99 * Note that what we call a "face" is really a loop; faces may consist
100 * of more than one loop (ie. not simply connected), but there is no
101 * record of this in the data structure. The mesh may consist of
102 * several disconnected regions, so it may not be possible to visit
103 * the entire mesh by starting at a half-edge and traversing the edge
104 * structure.
105 *
106 * The mesh does NOT support isolated vertices; a vertex is deleted along
107 * with its last edge. Similarly when two faces are merged, one of the
108 * faces is deleted (see __gl_meshDelete below). For mesh operations,
109 * all face (loop) and vertex pointers must not be NULL. However, once
110 * mesh manipulation is finished, __gl_MeshZapFace can be used to delete
111 * faces of the mesh, one at a time. All external faces can be "zapped"
112 * before the mesh is returned to the client; then a NULL face indicates
113 * a region which is not part of the output polygon.
114 */
115
116 struct GLUvertex {
117 GLUvertex *next; /* next vertex (never NULL) */
118 GLUvertex *prev; /* previous vertex (never NULL) */
119 GLUhalfEdge *anEdge; /* a half-edge with this origin */
120 void *data; /* client's data */
121
122 /* Internal data (keep hidden) */
123 GLdouble coords[3]; /* vertex location in 3D */
124 GLdouble s, t; /* projection onto the sweep plane */
125 long pqHandle; /* to allow deletion from priority queue */
126 };
127
128 struct GLUface {
129 GLUface *next; /* next face (never NULL) */
130 GLUface *prev; /* previous face (never NULL) */
131 GLUhalfEdge *anEdge; /* a half edge with this left face */
132 void *data; /* room for client's data */
133
134 /* Internal data (keep hidden) */
135 GLUface *trail; /* "stack" for conversion to strips */
136 GLboolean marked; /* flag for conversion to strips */
137 GLboolean inside; /* this face is in the polygon interior */
138 };
139
140 struct GLUhalfEdge {
141 GLUhalfEdge *next; /* doubly-linked list (prev==Sym->next) */
142 GLUhalfEdge *Sym; /* same edge, opposite direction */
143 GLUhalfEdge *Onext; /* next edge CCW around origin */
144 GLUhalfEdge *Lnext; /* next edge CCW around left face */
145 GLUvertex *Org; /* origin vertex (Overtex too long) */
146 GLUface *Lface; /* left face */
147
148 /* Internal data (keep hidden) */
149 ActiveRegion *activeRegion; /* a region with this upper edge (sweep.c) */
150 int winding; /* change in winding number when crossing
151 from the right face to the left face */
152 };
153
154 #define Rface Sym->Lface
155 #define Dst Sym->Org
156
157 #define Oprev Sym->Lnext
158 #define Lprev Onext->Sym
159 #define Dprev Lnext->Sym
160 #define Rprev Sym->Onext
161 #define Dnext Rprev->Sym /* 3 pointers */
162 #define Rnext Oprev->Sym /* 3 pointers */
163
164
165 struct GLUmesh {
166 GLUvertex vHead; /* dummy header for vertex list */
167 GLUface fHead; /* dummy header for face list */
168 GLUhalfEdge eHead; /* dummy header for edge list */
169 GLUhalfEdge eHeadSym; /* and its symmetric counterpart */
170 };
171
172 /* The mesh operations below have three motivations: completeness,
173 * convenience, and efficiency. The basic mesh operations are MakeEdge,
174 * Splice, and Delete. All the other edge operations can be implemented
175 * in terms of these. The other operations are provided for convenience
176 * and/or efficiency.
177 *
178 * When a face is split or a vertex is added, they are inserted into the
179 * global list *before* the existing vertex or face (ie. e->Org or e->Lface).
180 * This makes it easier to process all vertices or faces in the global lists
181 * without worrying about processing the same data twice. As a convenience,
182 * when a face is split, the "inside" flag is copied from the old face.
183 * Other internal data (v->data, v->activeRegion, f->data, f->marked,
184 * f->trail, e->winding) is set to zero.
185 *
186 * ********************** Basic Edge Operations **************************
187 *
188 * __gl_meshMakeEdge( mesh ) creates one edge, two vertices, and a loop.
189 * The loop (face) consists of the two new half-edges.
190 *
191 * __gl_meshSplice( eOrg, eDst ) is the basic operation for changing the
192 * mesh connectivity and topology. It changes the mesh so that
193 * eOrg->Onext <- OLD( eDst->Onext )
194 * eDst->Onext <- OLD( eOrg->Onext )
195 * where OLD(...) means the value before the meshSplice operation.
196 *
197 * This can have two effects on the vertex structure:
198 * - if eOrg->Org != eDst->Org, the two vertices are merged together
199 * - if eOrg->Org == eDst->Org, the origin is split into two vertices
200 * In both cases, eDst->Org is changed and eOrg->Org is untouched.
201 *
202 * Similarly (and independently) for the face structure,
203 * - if eOrg->Lface == eDst->Lface, one loop is split into two
204 * - if eOrg->Lface != eDst->Lface, two distinct loops are joined into one
205 * In both cases, eDst->Lface is changed and eOrg->Lface is unaffected.
206 *
207 * __gl_meshDelete( eDel ) removes the edge eDel. There are several cases:
208 * if (eDel->Lface != eDel->Rface), we join two loops into one; the loop
209 * eDel->Lface is deleted. Otherwise, we are splitting one loop into two;
210 * the newly created loop will contain eDel->Dst. If the deletion of eDel
211 * would create isolated vertices, those are deleted as well.
212 *
213 * ********************** Other Edge Operations **************************
214 *
215 * __gl_meshAddEdgeVertex( eOrg ) creates a new edge eNew such that
216 * eNew == eOrg->Lnext, and eNew->Dst is a newly created vertex.
217 * eOrg and eNew will have the same left face.
218 *
219 * __gl_meshSplitEdge( eOrg ) splits eOrg into two edges eOrg and eNew,
220 * such that eNew == eOrg->Lnext. The new vertex is eOrg->Dst == eNew->Org.
221 * eOrg and eNew will have the same left face.
222 *
223 * __gl_meshConnect( eOrg, eDst ) creates a new edge from eOrg->Dst
224 * to eDst->Org, and returns the corresponding half-edge eNew.
225 * If eOrg->Lface == eDst->Lface, this splits one loop into two,
226 * and the newly created loop is eNew->Lface. Otherwise, two disjoint
227 * loops are merged into one, and the loop eDst->Lface is destroyed.
228 *
229 * ************************ Other Operations *****************************
230 *
231 * __gl_meshNewMesh() creates a new mesh with no edges, no vertices,
232 * and no loops (what we usually call a "face").
233 *
234 * __gl_meshUnion( mesh1, mesh2 ) forms the union of all structures in
235 * both meshes, and returns the new mesh (the old meshes are destroyed).
236 *
237 * __gl_meshDeleteMesh( mesh ) will free all storage for any valid mesh.
238 *
239 * __gl_meshZapFace( fZap ) destroys a face and removes it from the
240 * global face list. All edges of fZap will have a NULL pointer as their
241 * left face. Any edges which also have a NULL pointer as their right face
242 * are deleted entirely (along with any isolated vertices this produces).
243 * An entire mesh can be deleted by zapping its faces, one at a time,
244 * in any order. Zapped faces cannot be used in further mesh operations!
245 *
246 * __gl_meshCheckMesh( mesh ) checks a mesh for self-consistency.
247 */
248
249 GLUhalfEdge *__gl_meshMakeEdge( GLUmesh *mesh );
250 int __gl_meshSplice( GLUhalfEdge *eOrg, GLUhalfEdge *eDst );
251 int __gl_meshDelete( GLUhalfEdge *eDel );
252
253 GLUhalfEdge *__gl_meshAddEdgeVertex( GLUhalfEdge *eOrg );
254 GLUhalfEdge *__gl_meshSplitEdge( GLUhalfEdge *eOrg );
255 GLUhalfEdge *__gl_meshConnect( GLUhalfEdge *eOrg, GLUhalfEdge *eDst );
256
257 GLUmesh *__gl_meshNewMesh( void );
258 GLUmesh *__gl_meshUnion( GLUmesh *mesh1, GLUmesh *mesh2 );
259 void __gl_meshDeleteMesh( GLUmesh *mesh );
260 void __gl_meshZapFace( GLUface *fZap );
261
262 #ifdef NDEBUG
263 #define __gl_meshCheckMesh( mesh )
264 #else
265 void __gl_meshCheckMesh( GLUmesh *mesh );
266 #endif
267
268 #endif