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