1/* 2** License Applicability. Except to the extent portions of this file are 3** made subject to an alternative license as permitted in the SGI Free 4** Software License B, Version 1.1 (the "License"), the contents of this 5** file are subject only to the provisions of the License. You may not use 6** this file except in compliance with the License. You may obtain a copy 7** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600 8** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at: 9** 10** http://oss.sgi.com/projects/FreeB 11** 12** Note that, as provided in the License, the Software is distributed on an 13** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS 14** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND 15** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A 16** PARTICULAR PURPOSE, AND NON-INFRINGEMENT. 17** 18** Original Code. The Original Code is: OpenGL Sample Implementation, 19** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics, 20** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc. 21** Copyright in any portions created by third parties is as indicated 22** elsewhere herein. All Rights Reserved. 23** 24** Additional Notice Provisions: The application programming interfaces 25** established by SGI in conjunction with the Original Code are The 26** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released 27** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version 28** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X 29** Window System(R) (Version 1.3), released October 19, 1998. This software 30** was created using the OpenGL(R) version 1.2.1 Sample Implementation 31** published by SGI, but has not been independently verified as being 32** compliant with the OpenGL(R) version 1.2.1 Specification. 33** 34*/ 35/* 36** Author: Eric Veach, July 1994. 37** 38** $Date$ $Revision$ 39** $Header: //depot/main/gfx/lib/glu/libtess/geom.c#5 $ 40*/ 41 42#include "gluos.h" 43#include <assert.h> 44#include "mesh.h" 45#include "geom.h" 46 47int __gl_vertLeq( GLUvertex *u, GLUvertex *v ) 48{ 49 /* Returns TRUE if u is lexicographically <= v. */ 50 51 return VertLeq( u, v ); 52} 53 54GLdouble __gl_edgeEval( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 55{ 56 /* Given three vertices u,v,w such that VertLeq(u,v) && VertLeq(v,w), 57 * evaluates the t-coord of the edge uw at the s-coord of the vertex v. 58 * Returns v->t - (uw)(v->s), ie. the signed distance from uw to v. 59 * If uw is vertical (and thus passes thru v), the result is zero. 60 * 61 * The calculation is extremely accurate and stable, even when v 62 * is very close to u or w. In particular if we set v->t = 0 and 63 * let r be the negated result (this evaluates (uw)(v->s)), then 64 * r is guaranteed to satisfy MIN(u->t,w->t) <= r <= MAX(u->t,w->t). 65 */ 66 GLdouble gapL, gapR; 67 68 assert( VertLeq( u, v ) && VertLeq( v, w )); 69 70 gapL = v->s - u->s; 71 gapR = w->s - v->s; 72 73 if( gapL + gapR > 0 ) { 74 if( gapL < gapR ) { 75 return (v->t - u->t) + (u->t - w->t) * (gapL / (gapL + gapR)); 76 } else { 77 return (v->t - w->t) + (w->t - u->t) * (gapR / (gapL + gapR)); 78 } 79 } 80 /* vertical line */ 81 return 0; 82} 83 84GLdouble __gl_edgeSign( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 85{ 86 /* Returns a number whose sign matches EdgeEval(u,v,w) but which 87 * is cheaper to evaluate. Returns > 0, == 0 , or < 0 88 * as v is above, on, or below the edge uw. 89 */ 90 GLdouble gapL, gapR; 91 92 assert( VertLeq( u, v ) && VertLeq( v, w )); 93 94 gapL = v->s - u->s; 95 gapR = w->s - v->s; 96 97 if( gapL + gapR > 0 ) { 98 return (v->t - w->t) * gapL + (v->t - u->t) * gapR; 99 } 100 /* vertical line */ 101 return 0; 102} 103 104 105/*********************************************************************** 106 * Define versions of EdgeSign, EdgeEval with s and t transposed. 107 */ 108 109GLdouble __gl_transEval( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 110{ 111 /* Given three vertices u,v,w such that TransLeq(u,v) && TransLeq(v,w), 112 * evaluates the t-coord of the edge uw at the s-coord of the vertex v. 113 * Returns v->s - (uw)(v->t), ie. the signed distance from uw to v. 114 * If uw is vertical (and thus passes thru v), the result is zero. 115 * 116 * The calculation is extremely accurate and stable, even when v 117 * is very close to u or w. In particular if we set v->s = 0 and 118 * let r be the negated result (this evaluates (uw)(v->t)), then 119 * r is guaranteed to satisfy MIN(u->s,w->s) <= r <= MAX(u->s,w->s). 120 */ 121 GLdouble gapL, gapR; 122 123 assert( TransLeq( u, v ) && TransLeq( v, w )); 124 125 gapL = v->t - u->t; 126 gapR = w->t - v->t; 127 128 if( gapL + gapR > 0 ) { 129 if( gapL < gapR ) { 130 return (v->s - u->s) + (u->s - w->s) * (gapL / (gapL + gapR)); 131 } else { 132 return (v->s - w->s) + (w->s - u->s) * (gapR / (gapL + gapR)); 133 } 134 } 135 /* vertical line */ 136 return 0; 137} 138 139GLdouble __gl_transSign( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 140{ 141 /* Returns a number whose sign matches TransEval(u,v,w) but which 142 * is cheaper to evaluate. Returns > 0, == 0 , or < 0 143 * as v is above, on, or below the edge uw. 144 */ 145 GLdouble gapL, gapR; 146 147 assert( TransLeq( u, v ) && TransLeq( v, w )); 148 149 gapL = v->t - u->t; 150 gapR = w->t - v->t; 151 152 if( gapL + gapR > 0 ) { 153 return (v->s - w->s) * gapL + (v->s - u->s) * gapR; 154 } 155 /* vertical line */ 156 return 0; 157} 158 159 160int __gl_vertCCW( GLUvertex *u, GLUvertex *v, GLUvertex *w ) 161{ 162 /* For almost-degenerate situations, the results are not reliable. 163 * Unless the floating-point arithmetic can be performed without 164 * rounding errors, *any* implementation will give incorrect results 165 * on some degenerate inputs, so the client must have some way to 166 * handle this situation. 167 */ 168 return (u->s*(v->t - w->t) + v->s*(w->t - u->t) + w->s*(u->t - v->t)) >= 0; 169} 170 171/* Given parameters a,x,b,y returns the value (b*x+a*y)/(a+b), 172 * or (x+y)/2 if a==b==0. It requires that a,b >= 0, and enforces 173 * this in the rare case that one argument is slightly negative. 174 * The implementation is extremely stable numerically. 175 * In particular it guarantees that the result r satisfies 176 * MIN(x,y) <= r <= MAX(x,y), and the results are very accurate 177 * even when a and b differ greatly in magnitude. 178 */ 179#define RealInterpolate(a,x,b,y) \ 180 (a = (a < 0) ? 0 : a, b = (b < 0) ? 0 : b, \ 181 ((a <= b) ? ((b == 0) ? ((x+y) / 2) \ 182 : (x + (y-x) * (a/(a+b)))) \ 183 : (y + (x-y) * (b/(a+b))))) 184 185#ifndef FOR_TRITE_TEST_PROGRAM 186#define Interpolate(a,x,b,y) RealInterpolate(a,x,b,y) 187#else 188 189/* Claim: the ONLY property the sweep algorithm relies on is that 190 * MIN(x,y) <= r <= MAX(x,y). This is a nasty way to test that. 191 */ 192#include <stdlib.h> 193extern int RandomInterpolate; 194 195GLdouble Interpolate( GLdouble a, GLdouble x, GLdouble b, GLdouble y) 196{ 197printf("*********************%d\n",RandomInterpolate); 198 if( RandomInterpolate ) { 199 a = 1.2 * drand48() - 0.1; 200 a = (a < 0) ? 0 : ((a > 1) ? 1 : a); 201 b = 1.0 - a; 202 } 203 return RealInterpolate(a,x,b,y); 204} 205 206#endif 207 208#define Swap(a,b) do { GLUvertex *t = a; a = b; b = t; } while(0) 209 210void __gl_edgeIntersect( GLUvertex *o1, GLUvertex *d1, 211 GLUvertex *o2, GLUvertex *d2, 212 GLUvertex *v ) 213/* Given edges (o1,d1) and (o2,d2), compute their point of intersection. 214 * The computed point is guaranteed to lie in the intersection of the 215 * bounding rectangles defined by each edge. 216 */ 217{ 218 GLdouble z1, z2; 219 220 /* This is certainly not the most efficient way to find the intersection 221 * of two line segments, but it is very numerically stable. 222 * 223 * Strategy: find the two middle vertices in the VertLeq ordering, 224 * and interpolate the intersection s-value from these. Then repeat 225 * using the TransLeq ordering to find the intersection t-value. 226 */ 227 228 if( ! VertLeq( o1, d1 )) { Swap( o1, d1 ); } 229 if( ! VertLeq( o2, d2 )) { Swap( o2, d2 ); } 230 if( ! VertLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); } 231 232 if( ! VertLeq( o2, d1 )) { 233 /* Technically, no intersection -- do our best */ 234 v->s = (o2->s + d1->s) / 2; 235 } else if( VertLeq( d1, d2 )) { 236 /* Interpolate between o2 and d1 */ 237 z1 = EdgeEval( o1, o2, d1 ); 238 z2 = EdgeEval( o2, d1, d2 ); 239 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; } 240 v->s = Interpolate( z1, o2->s, z2, d1->s ); 241 } else { 242 /* Interpolate between o2 and d2 */ 243 z1 = EdgeSign( o1, o2, d1 ); 244 z2 = -EdgeSign( o1, d2, d1 ); 245 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; } 246 v->s = Interpolate( z1, o2->s, z2, d2->s ); 247 } 248 249 /* Now repeat the process for t */ 250 251 if( ! TransLeq( o1, d1 )) { Swap( o1, d1 ); } 252 if( ! TransLeq( o2, d2 )) { Swap( o2, d2 ); } 253 if( ! TransLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); } 254 255 if( ! TransLeq( o2, d1 )) { 256 /* Technically, no intersection -- do our best */ 257 v->t = (o2->t + d1->t) / 2; 258 } else if( TransLeq( d1, d2 )) { 259 /* Interpolate between o2 and d1 */ 260 z1 = TransEval( o1, o2, d1 ); 261 z2 = TransEval( o2, d1, d2 ); 262 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; } 263 v->t = Interpolate( z1, o2->t, z2, d1->t ); 264 } else { 265 /* Interpolate between o2 and d2 */ 266 z1 = TransSign( o1, o2, d1 ); 267 z2 = -TransSign( o1, d2, d1 ); 268 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; } 269 v->t = Interpolate( z1, o2->t, z2, d2->t ); 270 } 271} 272