OSDN Git Service

Remove CVS keywords.
[android-x86/external-mesa.git] / src / glu / sgi / libtess / geom.c
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 */
39
40 #include "gluos.h"
41 #include <assert.h>
42 #include "mesh.h"
43 #include "geom.h"
44
45 int __gl_vertLeq( GLUvertex *u, GLUvertex *v )
46 {
47   /* Returns TRUE if u is lexicographically <= v. */
48
49   return VertLeq( u, v );
50 }
51
52 GLdouble __gl_edgeEval( GLUvertex *u, GLUvertex *v, GLUvertex *w )
53 {
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.
58    *
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).
63    */
64   GLdouble gapL, gapR;
65
66   assert( VertLeq( u, v ) && VertLeq( v, w ));
67   
68   gapL = v->s - u->s;
69   gapR = w->s - v->s;
70
71   if( gapL + gapR > 0 ) {
72     if( gapL < gapR ) {
73       return (v->t - u->t) + (u->t - w->t) * (gapL / (gapL + gapR));
74     } else {
75       return (v->t - w->t) + (w->t - u->t) * (gapR / (gapL + gapR));
76     }
77   }
78   /* vertical line */
79   return 0;
80 }
81
82 GLdouble __gl_edgeSign( GLUvertex *u, GLUvertex *v, GLUvertex *w )
83 {
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.
87    */
88   GLdouble gapL, gapR;
89
90   assert( VertLeq( u, v ) && VertLeq( v, w ));
91   
92   gapL = v->s - u->s;
93   gapR = w->s - v->s;
94
95   if( gapL + gapR > 0 ) {
96     return (v->t - w->t) * gapL + (v->t - u->t) * gapR;
97   }
98   /* vertical line */
99   return 0;
100 }
101
102
103 /***********************************************************************
104  * Define versions of EdgeSign, EdgeEval with s and t transposed.
105  */
106
107 GLdouble __gl_transEval( GLUvertex *u, GLUvertex *v, GLUvertex *w )
108 {
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.
113    *
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).
118    */
119   GLdouble gapL, gapR;
120
121   assert( TransLeq( u, v ) && TransLeq( v, w ));
122   
123   gapL = v->t - u->t;
124   gapR = w->t - v->t;
125
126   if( gapL + gapR > 0 ) {
127     if( gapL < gapR ) {
128       return (v->s - u->s) + (u->s - w->s) * (gapL / (gapL + gapR));
129     } else {
130       return (v->s - w->s) + (w->s - u->s) * (gapR / (gapL + gapR));
131     }
132   }
133   /* vertical line */
134   return 0;
135 }
136
137 GLdouble __gl_transSign( GLUvertex *u, GLUvertex *v, GLUvertex *w )
138 {
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.
142    */
143   GLdouble gapL, gapR;
144
145   assert( TransLeq( u, v ) && TransLeq( v, w ));
146   
147   gapL = v->t - u->t;
148   gapR = w->t - v->t;
149
150   if( gapL + gapR > 0 ) {
151     return (v->s - w->s) * gapL + (v->s - u->s) * gapR;
152   }
153   /* vertical line */
154   return 0;
155 }
156
157
158 int __gl_vertCCW( GLUvertex *u, GLUvertex *v, GLUvertex *w )
159 {
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.
165    */
166   return (u->s*(v->t - w->t) + v->s*(w->t - u->t) + w->s*(u->t - v->t)) >= 0;
167 }
168
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.
176  */
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)))))
182
183 #ifndef FOR_TRITE_TEST_PROGRAM
184 #define Interpolate(a,x,b,y)    RealInterpolate(a,x,b,y)
185 #else
186
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.
189  */
190 #include <stdlib.h>
191 extern int RandomInterpolate;
192
193 GLdouble Interpolate( GLdouble a, GLdouble x, GLdouble b, GLdouble y)
194 {
195 printf("*********************%d\n",RandomInterpolate);
196   if( RandomInterpolate ) {
197     a = 1.2 * drand48() - 0.1;
198     a = (a < 0) ? 0 : ((a > 1) ? 1 : a);
199     b = 1.0 - a;
200   }
201   return RealInterpolate(a,x,b,y);
202 }
203
204 #endif
205
206 #define Swap(a,b)       if (1) { GLUvertex *t = a; a = b; b = t; } else
207
208 void __gl_edgeIntersect( GLUvertex *o1, GLUvertex *d1,
209                          GLUvertex *o2, GLUvertex *d2,
210                          GLUvertex *v )
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.
214  */
215 {
216   GLdouble z1, z2;
217
218   /* This is certainly not the most efficient way to find the intersection
219    * of two line segments, but it is very numerically stable.
220    *
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.
224    */
225
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 ); }
229
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 );
239   } else {
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 );
245   }
246
247   /* Now repeat the process for t */
248
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 ); }
252
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 );
262   } else {
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 );
268   }
269 }