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