wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / glu32 / geom.c
blob64b4068e6e8f6171e1766ac2c103a2fde79a2c53
1 /*
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
23 * SOFTWARE.
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.
35 #include <stdarg.h>
36 #include <assert.h>
38 #include "windef.h"
39 #include "winbase.h"
41 #include "tess.h"
43 int __gl_vertLeq( GLUvertex *u, GLUvertex *v )
45 /* Returns TRUE if u is lexicographically <= v. */
47 return VertLeq( u, v );
50 GLdouble __gl_edgeEval( GLUvertex *u, GLUvertex *v, GLUvertex *w )
52 /* Given three vertices u,v,w such that VertLeq(u,v) && VertLeq(v,w),
53 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
54 * Returns v->t - (uw)(v->s), i.e. the signed distance from uw to v.
55 * If uw is vertical (and thus passes through v), the result is zero.
57 * The calculation is extremely accurate and stable, even when v
58 * is very close to u or w. In particular if we set v->t = 0 and
59 * let r be the negated result (this evaluates (uw)(v->s)), then
60 * r is guaranteed to satisfy MIN(u->t,w->t) <= r <= MAX(u->t,w->t).
62 GLdouble gapL, gapR;
64 assert( VertLeq( u, v ) && VertLeq( v, w ));
66 gapL = v->s - u->s;
67 gapR = w->s - v->s;
69 if( gapL + gapR > 0 ) {
70 if( gapL < gapR ) {
71 return (v->t - u->t) + (u->t - w->t) * (gapL / (gapL + gapR));
72 } else {
73 return (v->t - w->t) + (w->t - u->t) * (gapR / (gapL + gapR));
76 /* vertical line */
77 return 0;
80 GLdouble __gl_edgeSign( GLUvertex *u, GLUvertex *v, GLUvertex *w )
82 /* Returns a number whose sign matches EdgeEval(u,v,w) but which
83 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
84 * as v is above, on, or below the edge uw.
86 GLdouble gapL, gapR;
88 assert( VertLeq( u, v ) && VertLeq( v, w ));
90 gapL = v->s - u->s;
91 gapR = w->s - v->s;
93 if( gapL + gapR > 0 ) {
94 return (v->t - w->t) * gapL + (v->t - u->t) * gapR;
96 /* vertical line */
97 return 0;
101 /***********************************************************************
102 * Define versions of EdgeSign, EdgeEval with s and t transposed.
105 GLdouble __gl_transEval( GLUvertex *u, GLUvertex *v, GLUvertex *w )
107 /* Given three vertices u,v,w such that TransLeq(u,v) && TransLeq(v,w),
108 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
109 * Returns v->s - (uw)(v->t), i.e. the signed distance from uw to v.
110 * If uw is vertical (and thus passes through v), the result is zero.
112 * The calculation is extremely accurate and stable, even when v
113 * is very close to u or w. In particular if we set v->s = 0 and
114 * let r be the negated result (this evaluates (uw)(v->t)), then
115 * r is guaranteed to satisfy MIN(u->s,w->s) <= r <= MAX(u->s,w->s).
117 GLdouble gapL, gapR;
119 assert( TransLeq( u, v ) && TransLeq( v, w ));
121 gapL = v->t - u->t;
122 gapR = w->t - v->t;
124 if( gapL + gapR > 0 ) {
125 if( gapL < gapR ) {
126 return (v->s - u->s) + (u->s - w->s) * (gapL / (gapL + gapR));
127 } else {
128 return (v->s - w->s) + (w->s - u->s) * (gapR / (gapL + gapR));
131 /* vertical line */
132 return 0;
135 GLdouble __gl_transSign( GLUvertex *u, GLUvertex *v, GLUvertex *w )
137 /* Returns a number whose sign matches TransEval(u,v,w) but which
138 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
139 * as v is above, on, or below the edge uw.
141 GLdouble gapL, gapR;
143 assert( TransLeq( u, v ) && TransLeq( v, w ));
145 gapL = v->t - u->t;
146 gapR = w->t - v->t;
148 if( gapL + gapR > 0 ) {
149 return (v->s - w->s) * gapL + (v->s - u->s) * gapR;
151 /* vertical line */
152 return 0;
156 int __gl_vertCCW( GLUvertex *u, GLUvertex *v, GLUvertex *w )
158 /* For almost-degenerate situations, the results are not reliable.
159 * Unless the floating-point arithmetic can be performed without
160 * rounding errors, *any* implementation will give incorrect results
161 * on some degenerate inputs, so the client must have some way to
162 * handle this situation.
164 return (u->s*(v->t - w->t) + v->s*(w->t - u->t) + w->s*(u->t - v->t)) >= 0;
167 /* Given parameters a,x,b,y returns the value (b*x+a*y)/(a+b),
168 * or (x+y)/2 if a==b==0. It requires that a,b >= 0, and enforces
169 * this in the rare case that one argument is slightly negative.
170 * The implementation is extremely stable numerically.
171 * In particular it guarantees that the result r satisfies
172 * MIN(x,y) <= r <= MAX(x,y), and the results are very accurate
173 * even when a and b differ greatly in magnitude.
175 #define Interpolate(a,x,b,y) \
176 (a = (a < 0) ? 0 : a, b = (b < 0) ? 0 : b, \
177 ((a <= b) ? ((b == 0) ? ((x+y) / 2) \
178 : (x + (y-x) * (a/(a+b)))) \
179 : (y + (x-y) * (b/(a+b)))))
181 #define Swap(a,b) do { GLUvertex *t = a; a = b; b = t; } while (0)
183 void __gl_edgeIntersect( GLUvertex *o1, GLUvertex *d1,
184 GLUvertex *o2, GLUvertex *d2,
185 GLUvertex *v )
186 /* Given edges (o1,d1) and (o2,d2), compute their point of intersection.
187 * The computed point is guaranteed to lie in the intersection of the
188 * bounding rectangles defined by each edge.
191 GLdouble z1, z2;
193 /* This is certainly not the most efficient way to find the intersection
194 * of two line segments, but it is very numerically stable.
196 * Strategy: find the two middle vertices in the VertLeq ordering,
197 * and interpolate the intersection s-value from these. Then repeat
198 * using the TransLeq ordering to find the intersection t-value.
201 if( ! VertLeq( o1, d1 )) { Swap( o1, d1 ); }
202 if( ! VertLeq( o2, d2 )) { Swap( o2, d2 ); }
203 if( ! VertLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); }
205 if( ! VertLeq( o2, d1 )) {
206 /* Technically, no intersection -- do our best */
207 v->s = (o2->s + d1->s) / 2;
208 } else if( VertLeq( d1, d2 )) {
209 /* Interpolate between o2 and d1 */
210 z1 = EdgeEval( o1, o2, d1 );
211 z2 = EdgeEval( o2, d1, d2 );
212 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
213 v->s = Interpolate( z1, o2->s, z2, d1->s );
214 } else {
215 /* Interpolate between o2 and d2 */
216 z1 = EdgeSign( o1, o2, d1 );
217 z2 = -EdgeSign( o1, d2, d1 );
218 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
219 v->s = Interpolate( z1, o2->s, z2, d2->s );
222 /* Now repeat the process for t */
224 if( ! TransLeq( o1, d1 )) { Swap( o1, d1 ); }
225 if( ! TransLeq( o2, d2 )) { Swap( o2, d2 ); }
226 if( ! TransLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); }
228 if( ! TransLeq( o2, d1 )) {
229 /* Technically, no intersection -- do our best */
230 v->t = (o2->t + d1->t) / 2;
231 } else if( TransLeq( d1, d2 )) {
232 /* Interpolate between o2 and d1 */
233 z1 = TransEval( o1, o2, d1 );
234 z2 = TransEval( o2, d1, d2 );
235 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
236 v->t = Interpolate( z1, o2->t, z2, d1->t );
237 } else {
238 /* Interpolate between o2 and d2 */
239 z1 = TransSign( o1, o2, d1 );
240 z2 = -TransSign( o1, d2, d1 );
241 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
242 v->t = Interpolate( z1, o2->t, z2, d2->t );