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:
10 ** http://oss.sgi.com/projects/FreeB
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.
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.
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.
36 ** Author: Eric Veach, July 1994.
38 ** $Date: 2001/03/17 00:25:41 $ $Revision: 1.1 $
39 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libtess/geom.c,v 1.1 2001/03/17 00:25:41 brianp Exp $
47 int __gl_vertLeq( GLUvertex
*u
, GLUvertex
*v
)
49 /* Returns TRUE if u is lexicographically <= v. */
51 return VertLeq( u
, v
);
54 GLdouble
__gl_edgeEval( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
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.
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).
68 assert( VertLeq( u
, v
) && VertLeq( v
, w
));
73 if( gapL
+ gapR
> 0 ) {
75 return (v
->t
- u
->t
) + (u
->t
- w
->t
) * (gapL
/ (gapL
+ gapR
));
77 return (v
->t
- w
->t
) + (w
->t
- u
->t
) * (gapR
/ (gapL
+ gapR
));
84 GLdouble
__gl_edgeSign( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
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.
92 assert( VertLeq( u
, v
) && VertLeq( v
, w
));
97 if( gapL
+ gapR
> 0 ) {
98 return (v
->t
- w
->t
) * gapL
+ (v
->t
- u
->t
) * gapR
;
105 /***********************************************************************
106 * Define versions of EdgeSign, EdgeEval with s and t transposed.
109 GLdouble
__gl_transEval( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
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.
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).
123 assert( TransLeq( u
, v
) && TransLeq( v
, w
));
128 if( gapL
+ gapR
> 0 ) {
130 return (v
->s
- u
->s
) + (u
->s
- w
->s
) * (gapL
/ (gapL
+ gapR
));
132 return (v
->s
- w
->s
) + (w
->s
- u
->s
) * (gapR
/ (gapL
+ gapR
));
139 GLdouble
__gl_transSign( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
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.
147 assert( TransLeq( u
, v
) && TransLeq( v
, w
));
152 if( gapL
+ gapR
> 0 ) {
153 return (v
->s
- w
->s
) * gapL
+ (v
->s
- u
->s
) * gapR
;
160 int __gl_vertCCW( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
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.
168 return (u
->s
*(v
->t
- w
->t
) + v
->s
*(w
->t
- u
->t
) + w
->s
*(u
->t
- v
->t
)) >= 0;
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.
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)))))
185 #ifndef FOR_TRITE_TEST_PROGRAM
186 #define Interpolate(a,x,b,y) RealInterpolate(a,x,b,y)
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.
193 extern int RandomInterpolate
;
195 GLdouble
Interpolate( GLdouble a
, GLdouble x
, GLdouble b
, GLdouble y
)
197 printf("*********************%d\n",RandomInterpolate
);
198 if( RandomInterpolate
) {
199 a
= 1.2 * drand48() - 0.1;
200 a
= (a
< 0) ? 0 : ((a
> 1) ? 1 : a
);
203 return RealInterpolate(a
,x
,b
,y
);
208 #define Swap(a,b) if (1) { GLUvertex *t = a; a = b; b = t; } else
210 void __gl_edgeIntersect( GLUvertex
*o1
, GLUvertex
*d1
,
211 GLUvertex
*o2
, GLUvertex
*d2
,
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.
220 /* This is certainly not the most efficient way to find the intersection
221 * of two line segments, but it is very numerically stable.
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.
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
); }
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
);
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
);
249 /* Now repeat the process for t */
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
); }
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
);
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
);