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 <assert.h> 37f220fa62Smrg#include "mesh.h" 38f220fa62Smrg#include "geom.h" 39f220fa62Smrg 40f220fa62Smrgint __gl_vertLeq( GLUvertex *u, GLUvertex *v ) 41f220fa62Smrg{ 42f220fa62Smrg /* Returns TRUE if u is lexicographically <= v. */ 43f220fa62Smrg 44f220fa62Smrg return VertLeq( u, v ); 45f220fa62Smrg} 46f220fa62Smrg 47f220fa62SmrgGLdouble __gl_edgeEval( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 48f220fa62Smrg{ 49f220fa62Smrg /* Given three vertices u,v,w such that VertLeq(u,v) && VertLeq(v,w), 50f220fa62Smrg * evaluates the t-coord of the edge uw at the s-coord of the vertex v. 51f220fa62Smrg * Returns v->t - (uw)(v->s), ie. the signed distance from uw to v. 52f220fa62Smrg * If uw is vertical (and thus passes thru v), the result is zero. 53f220fa62Smrg * 54f220fa62Smrg * The calculation is extremely accurate and stable, even when v 55f220fa62Smrg * is very close to u or w. In particular if we set v->t = 0 and 56f220fa62Smrg * let r be the negated result (this evaluates (uw)(v->s)), then 57f220fa62Smrg * r is guaranteed to satisfy MIN(u->t,w->t) <= r <= MAX(u->t,w->t). 58f220fa62Smrg */ 59f220fa62Smrg GLdouble gapL, gapR; 60f220fa62Smrg 61f220fa62Smrg assert( VertLeq( u, v ) && VertLeq( v, w )); 62f220fa62Smrg 63f220fa62Smrg gapL = v->s - u->s; 64f220fa62Smrg gapR = w->s - v->s; 65f220fa62Smrg 66f220fa62Smrg if( gapL + gapR > 0 ) { 67f220fa62Smrg if( gapL < gapR ) { 68f220fa62Smrg return (v->t - u->t) + (u->t - w->t) * (gapL / (gapL + gapR)); 69f220fa62Smrg } else { 70f220fa62Smrg return (v->t - w->t) + (w->t - u->t) * (gapR / (gapL + gapR)); 71f220fa62Smrg } 72f220fa62Smrg } 73f220fa62Smrg /* vertical line */ 74f220fa62Smrg return 0; 75f220fa62Smrg} 76f220fa62Smrg 77f220fa62SmrgGLdouble __gl_edgeSign( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 78f220fa62Smrg{ 79f220fa62Smrg /* Returns a number whose sign matches EdgeEval(u,v,w) but which 80f220fa62Smrg * is cheaper to evaluate. Returns > 0, == 0 , or < 0 81f220fa62Smrg * as v is above, on, or below the edge uw. 82f220fa62Smrg */ 83f220fa62Smrg GLdouble gapL, gapR; 84f220fa62Smrg 85f220fa62Smrg assert( VertLeq( u, v ) && VertLeq( v, w )); 86f220fa62Smrg 87f220fa62Smrg gapL = v->s - u->s; 88f220fa62Smrg gapR = w->s - v->s; 89f220fa62Smrg 90f220fa62Smrg if( gapL + gapR > 0 ) { 91f220fa62Smrg return (v->t - w->t) * gapL + (v->t - u->t) * gapR; 92f220fa62Smrg } 93f220fa62Smrg /* vertical line */ 94f220fa62Smrg return 0; 95f220fa62Smrg} 96f220fa62Smrg 97f220fa62Smrg 98f220fa62Smrg/*********************************************************************** 99f220fa62Smrg * Define versions of EdgeSign, EdgeEval with s and t transposed. 100f220fa62Smrg */ 101f220fa62Smrg 102f220fa62SmrgGLdouble __gl_transEval( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 103f220fa62Smrg{ 104f220fa62Smrg /* Given three vertices u,v,w such that TransLeq(u,v) && TransLeq(v,w), 105f220fa62Smrg * evaluates the t-coord of the edge uw at the s-coord of the vertex v. 106f220fa62Smrg * Returns v->s - (uw)(v->t), ie. the signed distance from uw to v. 107f220fa62Smrg * If uw is vertical (and thus passes thru v), the result is zero. 108f220fa62Smrg * 109f220fa62Smrg * The calculation is extremely accurate and stable, even when v 110f220fa62Smrg * is very close to u or w. In particular if we set v->s = 0 and 111f220fa62Smrg * let r be the negated result (this evaluates (uw)(v->t)), then 112f220fa62Smrg * r is guaranteed to satisfy MIN(u->s,w->s) <= r <= MAX(u->s,w->s). 113f220fa62Smrg */ 114f220fa62Smrg GLdouble gapL, gapR; 115f220fa62Smrg 116f220fa62Smrg assert( TransLeq( u, v ) && TransLeq( v, w )); 117f220fa62Smrg 118f220fa62Smrg gapL = v->t - u->t; 119f220fa62Smrg gapR = w->t - v->t; 120f220fa62Smrg 121f220fa62Smrg if( gapL + gapR > 0 ) { 122f220fa62Smrg if( gapL < gapR ) { 123f220fa62Smrg return (v->s - u->s) + (u->s - w->s) * (gapL / (gapL + gapR)); 124f220fa62Smrg } else { 125f220fa62Smrg return (v->s - w->s) + (w->s - u->s) * (gapR / (gapL + gapR)); 126f220fa62Smrg } 127f220fa62Smrg } 128f220fa62Smrg /* vertical line */ 129f220fa62Smrg return 0; 130f220fa62Smrg} 131f220fa62Smrg 132f220fa62SmrgGLdouble __gl_transSign( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 133f220fa62Smrg{ 134f220fa62Smrg /* Returns a number whose sign matches TransEval(u,v,w) but which 135f220fa62Smrg * is cheaper to evaluate. Returns > 0, == 0 , or < 0 136f220fa62Smrg * as v is above, on, or below the edge uw. 137f220fa62Smrg */ 138f220fa62Smrg GLdouble gapL, gapR; 139f220fa62Smrg 140f220fa62Smrg assert( TransLeq( u, v ) && TransLeq( v, w )); 141f220fa62Smrg 142f220fa62Smrg gapL = v->t - u->t; 143f220fa62Smrg gapR = w->t - v->t; 144f220fa62Smrg 145f220fa62Smrg if( gapL + gapR > 0 ) { 146f220fa62Smrg return (v->s - w->s) * gapL + (v->s - u->s) * gapR; 147f220fa62Smrg } 148f220fa62Smrg /* vertical line */ 149f220fa62Smrg return 0; 150f220fa62Smrg} 151f220fa62Smrg 152f220fa62Smrg 153f220fa62Smrgint __gl_vertCCW( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 154f220fa62Smrg{ 155f220fa62Smrg /* For almost-degenerate situations, the results are not reliable. 156f220fa62Smrg * Unless the floating-point arithmetic can be performed without 157f220fa62Smrg * rounding errors, *any* implementation will give incorrect results 158f220fa62Smrg * on some degenerate inputs, so the client must have some way to 159f220fa62Smrg * handle this situation. 160f220fa62Smrg */ 161f220fa62Smrg return (u->s*(v->t - w->t) + v->s*(w->t - u->t) + w->s*(u->t - v->t)) >= 0; 162f220fa62Smrg} 163f220fa62Smrg 164f220fa62Smrg/* Given parameters a,x,b,y returns the value (b*x+a*y)/(a+b), 165f220fa62Smrg * or (x+y)/2 if a==b==0. It requires that a,b >= 0, and enforces 166f220fa62Smrg * this in the rare case that one argument is slightly negative. 167f220fa62Smrg * The implementation is extremely stable numerically. 168f220fa62Smrg * In particular it guarantees that the result r satisfies 169f220fa62Smrg * MIN(x,y) <= r <= MAX(x,y), and the results are very accurate 170f220fa62Smrg * even when a and b differ greatly in magnitude. 171f220fa62Smrg */ 172f220fa62Smrg#define RealInterpolate(a,x,b,y) \ 173f220fa62Smrg (a = (a < 0) ? 0 : a, b = (b < 0) ? 0 : b, \ 174f220fa62Smrg ((a <= b) ? ((b == 0) ? ((x+y) / 2) \ 175f220fa62Smrg : (x + (y-x) * (a/(a+b)))) \ 176f220fa62Smrg : (y + (x-y) * (b/(a+b))))) 177f220fa62Smrg 178f220fa62Smrg#ifndef FOR_TRITE_TEST_PROGRAM 179f220fa62Smrg#define Interpolate(a,x,b,y) RealInterpolate(a,x,b,y) 180f220fa62Smrg#else 181f220fa62Smrg 182f220fa62Smrg/* Claim: the ONLY property the sweep algorithm relies on is that 183f220fa62Smrg * MIN(x,y) <= r <= MAX(x,y). This is a nasty way to test that. 184f220fa62Smrg */ 185f220fa62Smrg#include <stdlib.h> 186f220fa62Smrgextern int RandomInterpolate; 187f220fa62Smrg 188f220fa62SmrgGLdouble Interpolate( GLdouble a, GLdouble x, GLdouble b, GLdouble y) 189f220fa62Smrg{ 190f220fa62Smrgprintf("*********************%d\n",RandomInterpolate); 191f220fa62Smrg if( RandomInterpolate ) { 192f220fa62Smrg a = 1.2 * drand48() - 0.1; 193f220fa62Smrg a = (a < 0) ? 0 : ((a > 1) ? 1 : a); 194f220fa62Smrg b = 1.0 - a; 195f220fa62Smrg } 196f220fa62Smrg return RealInterpolate(a,x,b,y); 197f220fa62Smrg} 198f220fa62Smrg 199f220fa62Smrg#endif 200f220fa62Smrg 201f220fa62Smrg#define Swap(a,b) do { GLUvertex *t = a; a = b; b = t; } while (0) 202f220fa62Smrg 203f220fa62Smrgvoid __gl_edgeIntersect( GLUvertex *o1, GLUvertex *d1, 204f220fa62Smrg GLUvertex *o2, GLUvertex *d2, 205f220fa62Smrg GLUvertex *v ) 206f220fa62Smrg/* Given edges (o1,d1) and (o2,d2), compute their point of intersection. 207f220fa62Smrg * The computed point is guaranteed to lie in the intersection of the 208f220fa62Smrg * bounding rectangles defined by each edge. 209f220fa62Smrg */ 210f220fa62Smrg{ 211f220fa62Smrg GLdouble z1, z2; 212f220fa62Smrg 213f220fa62Smrg /* This is certainly not the most efficient way to find the intersection 214f220fa62Smrg * of two line segments, but it is very numerically stable. 215f220fa62Smrg * 216f220fa62Smrg * Strategy: find the two middle vertices in the VertLeq ordering, 217f220fa62Smrg * and interpolate the intersection s-value from these. Then repeat 218f220fa62Smrg * using the TransLeq ordering to find the intersection t-value. 219f220fa62Smrg */ 220f220fa62Smrg 221f220fa62Smrg if( ! VertLeq( o1, d1 )) { Swap( o1, d1 ); } 222f220fa62Smrg if( ! VertLeq( o2, d2 )) { Swap( o2, d2 ); } 223f220fa62Smrg if( ! VertLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); } 224f220fa62Smrg 225f220fa62Smrg if( ! VertLeq( o2, d1 )) { 226f220fa62Smrg /* Technically, no intersection -- do our best */ 227f220fa62Smrg v->s = (o2->s + d1->s) / 2; 228f220fa62Smrg } else if( VertLeq( d1, d2 )) { 229f220fa62Smrg /* Interpolate between o2 and d1 */ 230f220fa62Smrg z1 = EdgeEval( o1, o2, d1 ); 231f220fa62Smrg z2 = EdgeEval( o2, d1, d2 ); 232f220fa62Smrg if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; } 233f220fa62Smrg v->s = Interpolate( z1, o2->s, z2, d1->s ); 234f220fa62Smrg } else { 235f220fa62Smrg /* Interpolate between o2 and d2 */ 236f220fa62Smrg z1 = EdgeSign( o1, o2, d1 ); 237f220fa62Smrg z2 = -EdgeSign( o1, d2, d1 ); 238f220fa62Smrg if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; } 239f220fa62Smrg v->s = Interpolate( z1, o2->s, z2, d2->s ); 240f220fa62Smrg } 241f220fa62Smrg 242f220fa62Smrg /* Now repeat the process for t */ 243f220fa62Smrg 244f220fa62Smrg if( ! TransLeq( o1, d1 )) { Swap( o1, d1 ); } 245f220fa62Smrg if( ! TransLeq( o2, d2 )) { Swap( o2, d2 ); } 246f220fa62Smrg if( ! TransLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); } 247f220fa62Smrg 248f220fa62Smrg if( ! TransLeq( o2, d1 )) { 249f220fa62Smrg /* Technically, no intersection -- do our best */ 250f220fa62Smrg v->t = (o2->t + d1->t) / 2; 251f220fa62Smrg } else if( TransLeq( d1, d2 )) { 252f220fa62Smrg /* Interpolate between o2 and d1 */ 253f220fa62Smrg z1 = TransEval( o1, o2, d1 ); 254f220fa62Smrg z2 = TransEval( o2, d1, d2 ); 255f220fa62Smrg if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; } 256f220fa62Smrg v->t = Interpolate( z1, o2->t, z2, d1->t ); 257f220fa62Smrg } else { 258f220fa62Smrg /* Interpolate between o2 and d2 */ 259f220fa62Smrg z1 = TransSign( o1, o2, d1 ); 260f220fa62Smrg z2 = -TransSign( o1, d2, d1 ); 261f220fa62Smrg if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; } 262f220fa62Smrg v->t = Interpolate( z1, o2->t, z2, d2->t ); 263f220fa62Smrg } 264f220fa62Smrg} 265