1f220fa62Smrg/* 2f220fa62Smrg * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008) 3f220fa62Smrg * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved. 4f220fa62Smrg * 5f220fa62Smrg * Permission is hereby granted, free of charge, to any person obtaining a 6f220fa62Smrg * copy of this software and associated documentation files (the "Software"), 7f220fa62Smrg * to deal in the Software without restriction, including without limitation 8f220fa62Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9f220fa62Smrg * and/or sell copies of the Software, and to permit persons to whom the 10f220fa62Smrg * Software is furnished to do so, subject to the following conditions: 11f220fa62Smrg * 12f220fa62Smrg * The above copyright notice including the dates of first publication and 13f220fa62Smrg * either this permission notice or a reference to 14f220fa62Smrg * http://oss.sgi.com/projects/FreeB/ 15f220fa62Smrg * shall be included in all copies or substantial portions of the Software. 16f220fa62Smrg * 17f220fa62Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18f220fa62Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19f220fa62Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20f220fa62Smrg * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21f220fa62Smrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 22f220fa62Smrg * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23f220fa62Smrg * SOFTWARE. 24f220fa62Smrg * 25f220fa62Smrg * Except as contained in this notice, the name of Silicon Graphics, Inc. 26f220fa62Smrg * shall not be used in advertising or otherwise to promote the sale, use or 27f220fa62Smrg * other dealings in this Software without prior written authorization from 28f220fa62Smrg * Silicon Graphics, Inc. 29f220fa62Smrg */ 30f220fa62Smrg/* 31f220fa62Smrg** Author: Eric Veach, July 1994. 32f220fa62Smrg** 33f220fa62Smrg*/ 34f220fa62Smrg 35f220fa62Smrg#include "gluos.h" 36f220fa62Smrg#include <stdlib.h> 37f220fa62Smrg#include "geom.h" 38f220fa62Smrg#include "mesh.h" 39f220fa62Smrg#include "tessmono.h" 40f220fa62Smrg#include <assert.h> 41f220fa62Smrg 42f220fa62Smrg#define AddWinding(eDst,eSrc) (eDst->winding += eSrc->winding, \ 43f220fa62Smrg eDst->Sym->winding += eSrc->Sym->winding) 44f220fa62Smrg 45f220fa62Smrg/* __gl_meshTessellateMonoRegion( face ) tessellates a monotone region 46f220fa62Smrg * (what else would it do??) The region must consist of a single 47f220fa62Smrg * loop of half-edges (see mesh.h) oriented CCW. "Monotone" in this 48f220fa62Smrg * case means that any vertical line intersects the interior of the 49f220fa62Smrg * region in a single interval. 50f220fa62Smrg * 51f220fa62Smrg * Tessellation consists of adding interior edges (actually pairs of 52f220fa62Smrg * half-edges), to split the region into non-overlapping triangles. 53f220fa62Smrg * 54f220fa62Smrg * The basic idea is explained in Preparata and Shamos (which I don''t 55f220fa62Smrg * have handy right now), although their implementation is more 56f220fa62Smrg * complicated than this one. The are two edge chains, an upper chain 57f220fa62Smrg * and a lower chain. We process all vertices from both chains in order, 58f220fa62Smrg * from right to left. 59f220fa62Smrg * 60f220fa62Smrg * The algorithm ensures that the following invariant holds after each 61f220fa62Smrg * vertex is processed: the untessellated region consists of two 62f220fa62Smrg * chains, where one chain (say the upper) is a single edge, and 63f220fa62Smrg * the other chain is concave. The left vertex of the single edge 64f220fa62Smrg * is always to the left of all vertices in the concave chain. 65f220fa62Smrg * 66f220fa62Smrg * Each step consists of adding the rightmost unprocessed vertex to one 67f220fa62Smrg * of the two chains, and forming a fan of triangles from the rightmost 68f220fa62Smrg * of two chain endpoints. Determining whether we can add each triangle 69f220fa62Smrg * to the fan is a simple orientation test. By making the fan as large 70f220fa62Smrg * as possible, we restore the invariant (check it yourself). 71f220fa62Smrg */ 72f220fa62Smrgint __gl_meshTessellateMonoRegion( GLUface *face ) 73f220fa62Smrg{ 74f220fa62Smrg GLUhalfEdge *up, *lo; 75f220fa62Smrg 76f220fa62Smrg /* All edges are oriented CCW around the boundary of the region. 77f220fa62Smrg * First, find the half-edge whose origin vertex is rightmost. 78f220fa62Smrg * Since the sweep goes from left to right, face->anEdge should 79f220fa62Smrg * be close to the edge we want. 80f220fa62Smrg */ 81f220fa62Smrg up = face->anEdge; 82f220fa62Smrg assert( up->Lnext != up && up->Lnext->Lnext != up ); 83f220fa62Smrg 84f220fa62Smrg for( ; VertLeq( up->Dst, up->Org ); up = up->Lprev ) 85f220fa62Smrg ; 86f220fa62Smrg for( ; VertLeq( up->Org, up->Dst ); up = up->Lnext ) 87f220fa62Smrg ; 88f220fa62Smrg lo = up->Lprev; 89f220fa62Smrg 90f220fa62Smrg while( up->Lnext != lo ) { 91f220fa62Smrg if( VertLeq( up->Dst, lo->Org )) { 92f220fa62Smrg /* up->Dst is on the left. It is safe to form triangles from lo->Org. 93f220fa62Smrg * The EdgeGoesLeft test guarantees progress even when some triangles 94f220fa62Smrg * are CW, given that the upper and lower chains are truly monotone. 95f220fa62Smrg */ 96f220fa62Smrg while( lo->Lnext != up && (EdgeGoesLeft( lo->Lnext ) 97f220fa62Smrg || EdgeSign( lo->Org, lo->Dst, lo->Lnext->Dst ) <= 0 )) { 98f220fa62Smrg GLUhalfEdge *tempHalfEdge= __gl_meshConnect( lo->Lnext, lo ); 99f220fa62Smrg if (tempHalfEdge == NULL) return 0; 100f220fa62Smrg lo = tempHalfEdge->Sym; 101f220fa62Smrg } 102f220fa62Smrg lo = lo->Lprev; 103f220fa62Smrg } else { 104f220fa62Smrg /* lo->Org is on the left. We can make CCW triangles from up->Dst. */ 105f220fa62Smrg while( lo->Lnext != up && (EdgeGoesRight( up->Lprev ) 106f220fa62Smrg || EdgeSign( up->Dst, up->Org, up->Lprev->Org ) >= 0 )) { 107f220fa62Smrg GLUhalfEdge *tempHalfEdge= __gl_meshConnect( up, up->Lprev ); 108f220fa62Smrg if (tempHalfEdge == NULL) return 0; 109f220fa62Smrg up = tempHalfEdge->Sym; 110f220fa62Smrg } 111f220fa62Smrg up = up->Lnext; 112f220fa62Smrg } 113f220fa62Smrg } 114f220fa62Smrg 115f220fa62Smrg /* Now lo->Org == up->Dst == the leftmost vertex. The remaining region 116f220fa62Smrg * can be tessellated in a fan from this leftmost vertex. 117f220fa62Smrg */ 118f220fa62Smrg assert( lo->Lnext != up ); 119f220fa62Smrg while( lo->Lnext->Lnext != up ) { 120f220fa62Smrg GLUhalfEdge *tempHalfEdge= __gl_meshConnect( lo->Lnext, lo ); 121f220fa62Smrg if (tempHalfEdge == NULL) return 0; 122f220fa62Smrg lo = tempHalfEdge->Sym; 123f220fa62Smrg } 124f220fa62Smrg 125f220fa62Smrg return 1; 126f220fa62Smrg} 127f220fa62Smrg 128f220fa62Smrg 129f220fa62Smrg/* __gl_meshTessellateInterior( mesh ) tessellates each region of 130f220fa62Smrg * the mesh which is marked "inside" the polygon. Each such region 131f220fa62Smrg * must be monotone. 132f220fa62Smrg */ 133f220fa62Smrgint __gl_meshTessellateInterior( GLUmesh *mesh ) 134f220fa62Smrg{ 135f220fa62Smrg GLUface *f, *next; 136f220fa62Smrg 137f220fa62Smrg /*LINTED*/ 138f220fa62Smrg for( f = mesh->fHead.next; f != &mesh->fHead; f = next ) { 139f220fa62Smrg /* Make sure we don''t try to tessellate the new triangles. */ 140f220fa62Smrg next = f->next; 141f220fa62Smrg if( f->inside ) { 142f220fa62Smrg if ( !__gl_meshTessellateMonoRegion( f ) ) return 0; 143f220fa62Smrg } 144f220fa62Smrg } 145f220fa62Smrg 146f220fa62Smrg return 1; 147f220fa62Smrg} 148f220fa62Smrg 149f220fa62Smrg 150f220fa62Smrg/* __gl_meshDiscardExterior( mesh ) zaps (ie. sets to NULL) all faces 151f220fa62Smrg * which are not marked "inside" the polygon. Since further mesh operations 152f220fa62Smrg * on NULL faces are not allowed, the main purpose is to clean up the 153f220fa62Smrg * mesh so that exterior loops are not represented in the data structure. 154f220fa62Smrg */ 155f220fa62Smrgvoid __gl_meshDiscardExterior( GLUmesh *mesh ) 156f220fa62Smrg{ 157f220fa62Smrg GLUface *f, *next; 158f220fa62Smrg 159f220fa62Smrg /*LINTED*/ 160f220fa62Smrg for( f = mesh->fHead.next; f != &mesh->fHead; f = next ) { 161f220fa62Smrg /* Since f will be destroyed, save its next pointer. */ 162f220fa62Smrg next = f->next; 163f220fa62Smrg if( ! f->inside ) { 164f220fa62Smrg __gl_meshZapFace( f ); 165f220fa62Smrg } 166f220fa62Smrg } 167f220fa62Smrg} 168f220fa62Smrg 169f220fa62Smrg#define MARKED_FOR_DELETION 0x7fffffff 170f220fa62Smrg 171f220fa62Smrg/* __gl_meshSetWindingNumber( mesh, value, keepOnlyBoundary ) resets the 172f220fa62Smrg * winding numbers on all edges so that regions marked "inside" the 173f220fa62Smrg * polygon have a winding number of "value", and regions outside 174f220fa62Smrg * have a winding number of 0. 175f220fa62Smrg * 176f220fa62Smrg * If keepOnlyBoundary is TRUE, it also deletes all edges which do not 177f220fa62Smrg * separate an interior region from an exterior one. 178f220fa62Smrg */ 179f220fa62Smrgint __gl_meshSetWindingNumber( GLUmesh *mesh, int value, 180f220fa62Smrg GLboolean keepOnlyBoundary ) 181f220fa62Smrg{ 182f220fa62Smrg GLUhalfEdge *e, *eNext; 183f220fa62Smrg 184f220fa62Smrg for( e = mesh->eHead.next; e != &mesh->eHead; e = eNext ) { 185f220fa62Smrg eNext = e->next; 186f220fa62Smrg if( e->Rface->inside != e->Lface->inside ) { 187f220fa62Smrg 188f220fa62Smrg /* This is a boundary edge (one side is interior, one is exterior). */ 189f220fa62Smrg e->winding = (e->Lface->inside) ? value : -value; 190f220fa62Smrg } else { 191f220fa62Smrg 192f220fa62Smrg /* Both regions are interior, or both are exterior. */ 193f220fa62Smrg if( ! keepOnlyBoundary ) { 194f220fa62Smrg e->winding = 0; 195f220fa62Smrg } else { 196f220fa62Smrg if ( !__gl_meshDelete( e ) ) return 0; 197f220fa62Smrg } 198f220fa62Smrg } 199f220fa62Smrg } 200f220fa62Smrg return 1; 201f220fa62Smrg} 202