b77587661f79ed6a544cb9f1b440123b0dfb602b
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
31 ** Author: Eric Veach, July 1994.
33 ** $Date: 2001/03/17 00:25:41 $ $Revision: 1.1 $
34 ** $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 $
42 int __gl_vertLeq( GLUvertex
*u
, GLUvertex
*v
)
44 /* Returns TRUE if u is lexicographically <= v. */
46 return VertLeq( u
, v
);
49 GLdouble
__gl_edgeEval( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
51 /* Given three vertices u,v,w such that VertLeq(u,v) && VertLeq(v,w),
52 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
53 * Returns v->t - (uw)(v->s), ie. the signed distance from uw to v.
54 * If uw is vertical (and thus passes thru v), the result is zero.
56 * The calculation is extremely accurate and stable, even when v
57 * is very close to u or w. In particular if we set v->t = 0 and
58 * let r be the negated result (this evaluates (uw)(v->s)), then
59 * r is guaranteed to satisfy MIN(u->t,w->t) <= r <= MAX(u->t,w->t).
63 assert( VertLeq( u
, v
) && VertLeq( v
, w
));
68 if( gapL
+ gapR
> 0 ) {
70 return (v
->t
- u
->t
) + (u
->t
- w
->t
) * (gapL
/ (gapL
+ gapR
));
72 return (v
->t
- w
->t
) + (w
->t
- u
->t
) * (gapR
/ (gapL
+ gapR
));
79 GLdouble
__gl_edgeSign( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
81 /* Returns a number whose sign matches EdgeEval(u,v,w) but which
82 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
83 * as v is above, on, or below the edge uw.
87 assert( VertLeq( u
, v
) && VertLeq( v
, w
));
92 if( gapL
+ gapR
> 0 ) {
93 return (v
->t
- w
->t
) * gapL
+ (v
->t
- u
->t
) * gapR
;
100 /***********************************************************************
101 * Define versions of EdgeSign, EdgeEval with s and t transposed.
104 GLdouble
__gl_transEval( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
106 /* Given three vertices u,v,w such that TransLeq(u,v) && TransLeq(v,w),
107 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
108 * Returns v->s - (uw)(v->t), ie. the signed distance from uw to v.
109 * If uw is vertical (and thus passes thru v), the result is zero.
111 * The calculation is extremely accurate and stable, even when v
112 * is very close to u or w. In particular if we set v->s = 0 and
113 * let r be the negated result (this evaluates (uw)(v->t)), then
114 * r is guaranteed to satisfy MIN(u->s,w->s) <= r <= MAX(u->s,w->s).
118 assert( TransLeq( u
, v
) && TransLeq( v
, w
));
123 if( gapL
+ gapR
> 0 ) {
125 return (v
->s
- u
->s
) + (u
->s
- w
->s
) * (gapL
/ (gapL
+ gapR
));
127 return (v
->s
- w
->s
) + (w
->s
- u
->s
) * (gapR
/ (gapL
+ gapR
));
134 GLdouble
__gl_transSign( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
136 /* Returns a number whose sign matches TransEval(u,v,w) but which
137 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
138 * as v is above, on, or below the edge uw.
142 assert( TransLeq( u
, v
) && TransLeq( v
, w
));
147 if( gapL
+ gapR
> 0 ) {
148 return (v
->s
- w
->s
) * gapL
+ (v
->s
- u
->s
) * gapR
;
155 int __gl_vertCCW( GLUvertex
*u
, GLUvertex
*v
, GLUvertex
*w
)
157 /* For almost-degenerate situations, the results are not reliable.
158 * Unless the floating-point arithmetic can be performed without
159 * rounding errors, *any* implementation will give incorrect results
160 * on some degenerate inputs, so the client must have some way to
161 * handle this situation.
163 return (u
->s
*(v
->t
- w
->t
) + v
->s
*(w
->t
- u
->t
) + w
->s
*(u
->t
- v
->t
)) >= 0;
166 /* Given parameters a,x,b,y returns the value (b*x+a*y)/(a+b),
167 * or (x+y)/2 if a==b==0. It requires that a,b >= 0, and enforces
168 * this in the rare case that one argument is slightly negative.
169 * The implementation is extremely stable numerically.
170 * In particular it guarantees that the result r satisfies
171 * MIN(x,y) <= r <= MAX(x,y), and the results are very accurate
172 * even when a and b differ greatly in magnitude.
174 #define RealInterpolate(a,x,b,y) \
175 (a = (a < 0) ? 0 : a, b = (b < 0) ? 0 : b, \
176 ((a <= b) ? ((b == 0) ? ((x+y) / 2) \
177 : (x + (y-x) * (a/(a+b)))) \
178 : (y + (x-y) * (b/(a+b)))))
180 #ifndef FOR_TRITE_TEST_PROGRAM
181 #define Interpolate(a,x,b,y) RealInterpolate(a,x,b,y)
184 /* Claim: the ONLY property the sweep algorithm relies on is that
185 * MIN(x,y) <= r <= MAX(x,y). This is a nasty way to test that.
188 extern int RandomInterpolate
;
190 GLdouble
Interpolate( GLdouble a
, GLdouble x
, GLdouble b
, GLdouble y
)
192 printf("*********************%d\n",RandomInterpolate
);
193 if( RandomInterpolate
) {
194 a
= 1.2 * drand48() - 0.1;
195 a
= (a
< 0) ? 0 : ((a
> 1) ? 1 : a
);
198 return RealInterpolate(a
,x
,b
,y
);
203 #define Swap(a,b) if (1) { GLUvertex *t = a; a = b; b = t; } else
205 void __gl_edgeIntersect( GLUvertex
*o1
, GLUvertex
*d1
,
206 GLUvertex
*o2
, GLUvertex
*d2
,
208 /* Given edges (o1,d1) and (o2,d2), compute their point of intersection.
209 * The computed point is guaranteed to lie in the intersection of the
210 * bounding rectangles defined by each edge.
215 /* This is certainly not the most efficient way to find the intersection
216 * of two line segments, but it is very numerically stable.
218 * Strategy: find the two middle vertices in the VertLeq ordering,
219 * and interpolate the intersection s-value from these. Then repeat
220 * using the TransLeq ordering to find the intersection t-value.
223 if( ! VertLeq( o1
, d1
)) { Swap( o1
, d1
); }
224 if( ! VertLeq( o2
, d2
)) { Swap( o2
, d2
); }
225 if( ! VertLeq( o1
, o2
)) { Swap( o1
, o2
); Swap( d1
, d2
); }
227 if( ! VertLeq( o2
, d1
)) {
228 /* Technically, no intersection -- do our best */
229 v
->s
= (o2
->s
+ d1
->s
) / 2;
230 } else if( VertLeq( d1
, d2
)) {
231 /* Interpolate between o2 and d1 */
232 z1
= EdgeEval( o1
, o2
, d1
);
233 z2
= EdgeEval( o2
, d1
, d2
);
234 if( z1
+z2
< 0 ) { z1
= -z1
; z2
= -z2
; }
235 v
->s
= Interpolate( z1
, o2
->s
, z2
, d1
->s
);
237 /* Interpolate between o2 and d2 */
238 z1
= EdgeSign( o1
, o2
, d1
);
239 z2
= -EdgeSign( o1
, d2
, d1
);
240 if( z1
+z2
< 0 ) { z1
= -z1
; z2
= -z2
; }
241 v
->s
= Interpolate( z1
, o2
->s
, z2
, d2
->s
);
244 /* Now repeat the process for t */
246 if( ! TransLeq( o1
, d1
)) { Swap( o1
, d1
); }
247 if( ! TransLeq( o2
, d2
)) { Swap( o2
, d2
); }
248 if( ! TransLeq( o1
, o2
)) { Swap( o1
, o2
); Swap( d1
, d2
); }
250 if( ! TransLeq( o2
, d1
)) {
251 /* Technically, no intersection -- do our best */
252 v
->t
= (o2
->t
+ d1
->t
) / 2;
253 } else if( TransLeq( d1
, d2
)) {
254 /* Interpolate between o2 and d1 */
255 z1
= TransEval( o1
, o2
, d1
);
256 z2
= TransEval( o2
, d1
, d2
);
257 if( z1
+z2
< 0 ) { z1
= -z1
; z2
= -z2
; }
258 v
->t
= Interpolate( z1
, o2
->t
, z2
, d1
->t
);
260 /* Interpolate between o2 and d2 */
261 z1
= TransSign( o1
, o2
, d1
);
262 z2
= -TransSign( o1
, d2
, d1
);
263 if( z1
+z2
< 0 ) { z1
= -z1
; z2
= -z2
; }
264 v
->t
= Interpolate( z1
, o2
->t
, z2
, d2
->t
);