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.
45 int __gl_vertLeq( GLUvertex
*u
, GLUvertex
*v
)
47 /* Returns TRUE if u is lexicographically <= v. */
49 return VertLeq( u
, v
);
52 GLdouble
__gl_edgeEval( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
54 /* Given three vertices u,v,w such that VertLeq(u,v) && VertLeq(v,w),
55 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
56 * Returns v->t - (uw)(v->s), ie. the signed distance from uw to v.
57 * If uw is vertical (and thus passes thru v), the result is zero.
59 * The calculation is extremely accurate and stable, even when v
60 * is very close to u or w. In particular if we set v->t = 0 and
61 * let r be the negated result (this evaluates (uw)(v->s)), then
62 * r is guaranteed to satisfy MIN(u->t,w->t) <= r <= MAX(u->t,w->t).
66 assert( VertLeq( u
, v
) && VertLeq( v
, w
));
71 if( gapL
+ gapR
> 0 ) {
73 return (v
->t
- u
->t
) + (u
->t
- w
->t
) * (gapL
/ (gapL
+ gapR
));
75 return (v
->t
- w
->t
) + (w
->t
- u
->t
) * (gapR
/ (gapL
+ gapR
));
82 GLdouble
__gl_edgeSign( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
84 /* Returns a number whose sign matches EdgeEval(u,v,w) but which
85 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
86 * as v is above, on, or below the edge uw.
90 assert( VertLeq( u
, v
) && VertLeq( v
, w
));
95 if( gapL
+ gapR
> 0 ) {
96 return (v
->t
- w
->t
) * gapL
+ (v
->t
- u
->t
) * gapR
;
103 /***********************************************************************
104 * Define versions of EdgeSign, EdgeEval with s and t transposed.
107 GLdouble
__gl_transEval( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
109 /* Given three vertices u,v,w such that TransLeq(u,v) && TransLeq(v,w),
110 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
111 * Returns v->s - (uw)(v->t), ie. the signed distance from uw to v.
112 * If uw is vertical (and thus passes thru v), the result is zero.
114 * The calculation is extremely accurate and stable, even when v
115 * is very close to u or w. In particular if we set v->s = 0 and
116 * let r be the negated result (this evaluates (uw)(v->t)), then
117 * r is guaranteed to satisfy MIN(u->s,w->s) <= r <= MAX(u->s,w->s).
121 assert( TransLeq( u
, v
) && TransLeq( v
, w
));
126 if( gapL
+ gapR
> 0 ) {
128 return (v
->s
- u
->s
) + (u
->s
- w
->s
) * (gapL
/ (gapL
+ gapR
));
130 return (v
->s
- w
->s
) + (w
->s
- u
->s
) * (gapR
/ (gapL
+ gapR
));
137 GLdouble
__gl_transSign( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
139 /* Returns a number whose sign matches TransEval(u,v,w) but which
140 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
141 * as v is above, on, or below the edge uw.
145 assert( TransLeq( u
, v
) && TransLeq( v
, w
));
150 if( gapL
+ gapR
> 0 ) {
151 return (v
->s
- w
->s
) * gapL
+ (v
->s
- u
->s
) * gapR
;
158 int __gl_vertCCW( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
160 /* For almost-degenerate situations, the results are not reliable.
161 * Unless the floating-point arithmetic can be performed without
162 * rounding errors, *any* implementation will give incorrect results
163 * on some degenerate inputs, so the client must have some way to
164 * handle this situation.
166 return (u
->s
*(v
->t
- w
->t
) + v
->s
*(w
->t
- u
->t
) + w
->s
*(u
->t
- v
->t
)) >= 0;
169 /* Given parameters a,x,b,y returns the value (b*x+a*y)/(a+b),
170 * or (x+y)/2 if a==b==0. It requires that a,b >= 0, and enforces
171 * this in the rare case that one argument is slightly negative.
172 * The implementation is extremely stable numerically.
173 * In particular it guarantees that the result r satisfies
174 * MIN(x,y) <= r <= MAX(x,y), and the results are very accurate
175 * even when a and b differ greatly in magnitude.
177 #define RealInterpolate(a,x,b,y) \
178 (a = (a < 0) ? 0 : a, b = (b < 0) ? 0 : b, \
179 ((a <= b) ? ((b == 0) ? ((x+y) / 2) \
180 : (x + (y-x) * (a/(a+b)))) \
181 : (y + (x-y) * (b/(a+b)))))
183 #ifndef FOR_TRITE_TEST_PROGRAM
184 #define Interpolate(a,x,b,y) RealInterpolate(a,x,b,y)
187 /* Claim: the ONLY property the sweep algorithm relies on is that
188 * MIN(x,y) <= r <= MAX(x,y). This is a nasty way to test that.
191 extern int RandomInterpolate
;
193 GLdouble
Interpolate( GLdouble a
, GLdouble x
, GLdouble b
, GLdouble y
)
195 printf("*********************%d\n",RandomInterpolate
);
196 if( RandomInterpolate
) {
197 a
= 1.2 * drand48() - 0.1;
198 a
= (a
< 0) ? 0 : ((a
> 1) ? 1 : a
);
201 return RealInterpolate(a
,x
,b
,y
);
206 #define Swap(a,b) if (1) { GLUvertex *t = a; a = b; b = t; } else
208 void __gl_edgeIntersect( GLUvertex
*o1
, GLUvertex
*d1
,
209 GLUvertex
*o2
, GLUvertex
*d2
,
211 /* Given edges (o1,d1) and (o2,d2), compute their point of intersection.
212 * The computed point is guaranteed to lie in the intersection of the
213 * bounding rectangles defined by each edge.
218 /* This is certainly not the most efficient way to find the intersection
219 * of two line segments, but it is very numerically stable.
221 * Strategy: find the two middle vertices in the VertLeq ordering,
222 * and interpolate the intersection s-value from these. Then repeat
223 * using the TransLeq ordering to find the intersection t-value.
226 if( ! VertLeq( o1
, d1
)) { Swap( o1
, d1
); }
227 if( ! VertLeq( o2
, d2
)) { Swap( o2
, d2
); }
228 if( ! VertLeq( o1
, o2
)) { Swap( o1
, o2
); Swap( d1
, d2
); }
230 if( ! VertLeq( o2
, d1
)) {
231 /* Technically, no intersection -- do our best */
232 v
->s
= (o2
->s
+ d1
->s
) / 2;
233 } else if( VertLeq( d1
, d2
)) {
234 /* Interpolate between o2 and d1 */
235 z1
= EdgeEval( o1
, o2
, d1
);
236 z2
= EdgeEval( o2
, d1
, d2
);
237 if( z1
+z2
< 0 ) { z1
= -z1
; z2
= -z2
; }
238 v
->s
= Interpolate( z1
, o2
->s
, z2
, d1
->s
);
240 /* Interpolate between o2 and d2 */
241 z1
= EdgeSign( o1
, o2
, d1
);
242 z2
= -EdgeSign( o1
, d2
, d1
);
243 if( z1
+z2
< 0 ) { z1
= -z1
; z2
= -z2
; }
244 v
->s
= Interpolate( z1
, o2
->s
, z2
, d2
->s
);
247 /* Now repeat the process for t */
249 if( ! TransLeq( o1
, d1
)) { Swap( o1
, d1
); }
250 if( ! TransLeq( o2
, d2
)) { Swap( o2
, d2
); }
251 if( ! TransLeq( o1
, o2
)) { Swap( o1
, o2
); Swap( d1
, d2
); }
253 if( ! TransLeq( o2
, d1
)) {
254 /* Technically, no intersection -- do our best */
255 v
->t
= (o2
->t
+ d1
->t
) / 2;
256 } else if( TransLeq( d1
, d2
)) {
257 /* Interpolate between o2 and d1 */
258 z1
= TransEval( o1
, o2
, d1
);
259 z2
= TransEval( o2
, d1
, d2
);
260 if( z1
+z2
< 0 ) { z1
= -z1
; z2
= -z2
; }
261 v
->t
= Interpolate( z1
, o2
->t
, z2
, d1
->t
);
263 /* Interpolate between o2 and d2 */
264 z1
= TransSign( o1
, o2
, d1
);
265 z2
= -TransSign( o1
, d2
, d1
);
266 if( z1
+z2
< 0 ) { z1
= -z1
; z2
= -z2
; }
267 v
->t
= Interpolate( z1
, o2
->t
, z2
, d2
->t
);