Warning, /include/Geant4/tools/glutess/geom is written in an unsupported language. File is not indexed.
0001 // see license file for original license.
0002
0003 #ifndef tools_glutess_geom
0004 #define tools_glutess_geom
0005
0006 #include "mesh"
0007
0008 #define VertEq(u,v) ((u)->s == (v)->s && (u)->t == (v)->t)
0009 #define VertLeq(u,v) (((u)->s < (v)->s) || ((u)->s == (v)->s && (u)->t <= (v)->t))
0010
0011 #define EdgeEval(u,v,w) __gl_edgeEval(u,v,w)
0012 #define EdgeSign(u,v,w) __gl_edgeSign(u,v,w)
0013
0014 /* Versions of VertLeq, EdgeSign, EdgeEval with s and t transposed. */
0015
0016 #define TransLeq(u,v) (((u)->t < (v)->t) || \
0017 ((u)->t == (v)->t && (u)->s <= (v)->s))
0018 #define TransEval(u,v,w) __gl_transEval(u,v,w)
0019 #define TransSign(u,v,w) __gl_transSign(u,v,w)
0020
0021
0022 #define EdgeGoesLeft(e) VertLeq( (e)->Dst, (e)->Org )
0023 #define EdgeGoesRight(e) VertLeq( (e)->Org, (e)->Dst )
0024
0025 #define VertL1dist(u,v) (GLU_ABS(u->s - v->s) + GLU_ABS(u->t - v->t))
0026
0027 #define VertCCW(u,v,w) __gl_vertCCW(u,v,w)
0028
0029 ////////////////////////////////////////////////////////
0030 /// inlined C code : ///////////////////////////////////
0031 ////////////////////////////////////////////////////////
0032
0033 inline int __gl_vertLeq( GLUvertex *u, GLUvertex *v )
0034 {
0035 /* Returns TOOLS_GLU_TRUE if u is lexicographically <= v. */
0036
0037 return VertLeq( u, v );
0038 }
0039
0040 inline GLUdouble __gl_edgeEval( GLUvertex *u, GLUvertex *v, GLUvertex *w )
0041 {
0042 /* Given three vertices u,v,w such that VertLeq(u,v) && VertLeq(v,w),
0043 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
0044 * Returns v->t - (uw)(v->s), ie. the signed distance from uw to v.
0045 * If uw is vertical (and thus passes thru v), the result is zero.
0046 *
0047 * The calculation is extremely accurate and stable, even when v
0048 * is very close to u or w. In particular if we set v->t = 0 and
0049 * let r be the negated result (this evaluates (uw)(v->s)), then
0050 * r is guaranteed to satisfy MIN(u->t,w->t) <= r <= MAX(u->t,w->t).
0051 */
0052 GLUdouble gapL, gapR;
0053
0054 assert( VertLeq( u, v ) && VertLeq( v, w ));
0055
0056 gapL = v->s - u->s;
0057 gapR = w->s - v->s;
0058
0059 if( gapL + gapR > 0 ) {
0060 if( gapL < gapR ) {
0061 return (v->t - u->t) + (u->t - w->t) * (gapL / (gapL + gapR));
0062 } else {
0063 return (v->t - w->t) + (w->t - u->t) * (gapR / (gapL + gapR));
0064 }
0065 }
0066 /* vertical line */
0067 return 0;
0068 }
0069
0070 inline GLUdouble __gl_edgeSign( GLUvertex *u, GLUvertex *v, GLUvertex *w )
0071 {
0072 /* Returns a number whose sign matches EdgeEval(u,v,w) but which
0073 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
0074 * as v is above, on, or below the edge uw.
0075 */
0076 GLUdouble gapL, gapR;
0077
0078 /*
0079 #define VertLeq(u,v) (((u)->s < (v)->s) || \
0080 ((u)->s == (v)->s && (u)->t <= (v)->t))
0081 */
0082 assert( VertLeq( u, v ) && VertLeq( v, w ));
0083
0084 gapL = v->s - u->s;
0085 gapR = w->s - v->s;
0086
0087 if( gapL + gapR > 0 ) {
0088 return (v->t - w->t) * gapL + (v->t - u->t) * gapR;
0089 }
0090 /* vertical line */
0091 return 0;
0092 }
0093
0094
0095 /***********************************************************************
0096 * Define versions of EdgeSign, EdgeEval with s and t transposed.
0097 */
0098
0099 inline GLUdouble __gl_transEval( GLUvertex *u, GLUvertex *v, GLUvertex *w )
0100 {
0101 /* Given three vertices u,v,w such that TransLeq(u,v) && TransLeq(v,w),
0102 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
0103 * Returns v->s - (uw)(v->t), ie. the signed distance from uw to v.
0104 * If uw is vertical (and thus passes thru v), the result is zero.
0105 *
0106 * The calculation is extremely accurate and stable, even when v
0107 * is very close to u or w. In particular if we set v->s = 0 and
0108 * let r be the negated result (this evaluates (uw)(v->t)), then
0109 * r is guaranteed to satisfy MIN(u->s,w->s) <= r <= MAX(u->s,w->s).
0110 */
0111 GLUdouble gapL, gapR;
0112
0113 assert( TransLeq( u, v ) && TransLeq( v, w ));
0114
0115 gapL = v->t - u->t;
0116 gapR = w->t - v->t;
0117
0118 if( gapL + gapR > 0 ) {
0119 if( gapL < gapR ) {
0120 return (v->s - u->s) + (u->s - w->s) * (gapL / (gapL + gapR));
0121 } else {
0122 return (v->s - w->s) + (w->s - u->s) * (gapR / (gapL + gapR));
0123 }
0124 }
0125 /* vertical line */
0126 return 0;
0127 }
0128
0129 inline GLUdouble __gl_transSign( GLUvertex *u, GLUvertex *v, GLUvertex *w )
0130 {
0131 /* Returns a number whose sign matches TransEval(u,v,w) but which
0132 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
0133 * as v is above, on, or below the edge uw.
0134 */
0135 GLUdouble gapL, gapR;
0136
0137 assert( TransLeq( u, v ) && TransLeq( v, w ));
0138
0139 gapL = v->t - u->t;
0140 gapR = w->t - v->t;
0141
0142 if( gapL + gapR > 0 ) {
0143 return (v->s - w->s) * gapL + (v->s - u->s) * gapR;
0144 }
0145 /* vertical line */
0146 return 0;
0147 }
0148
0149
0150 inline int __gl_vertCCW( GLUvertex *u, GLUvertex *v, GLUvertex *w )
0151 {
0152 /* For almost-degenerate situations, the results are not reliable.
0153 * Unless the floating-point arithmetic can be performed without
0154 * rounding errors, *any* implementation will give incorrect results
0155 * on some degenerate inputs, so the client must have some way to
0156 * handle this situation.
0157 */
0158 return (u->s*(v->t - w->t) + v->s*(w->t - u->t) + w->s*(u->t - v->t)) >= 0;
0159 }
0160
0161 /* Given parameters a,x,b,y returns the value (b*x+a*y)/(a+b),
0162 * or (x+y)/2 if a==b==0. It requires that a,b >= 0, and enforces
0163 * this in the rare case that one argument is slightly negative.
0164 * The implementation is extremely stable numerically.
0165 * In particular it guarantees that the result r satisfies
0166 * MIN(x,y) <= r <= MAX(x,y), and the results are very accurate
0167 * even when a and b differ greatly in magnitude.
0168 */
0169 #define Interpolate(a,x,b,y) \
0170 (a = (a < 0) ? 0 : a, b = (b < 0) ? 0 : b, \
0171 ((a <= b) ? ((b == 0) ? ((x+y) / 2) \
0172 : (x + (y-x) * (a/(a+b)))) \
0173 : (y + (x-y) * (b/(a+b)))))
0174
0175 //#define Swap(a,b) if (1) { GLUvertex *t = a; a = b; b = t; } else
0176 #define Swap(a,b) do { GLUvertex *t = a; a = b; b = t; } while(false)
0177
0178 inline void __gl_edgeIntersect( GLUvertex *o1, GLUvertex *d1,
0179 GLUvertex *o2, GLUvertex *d2,
0180 GLUvertex *v )
0181 /* Given edges (o1,d1) and (o2,d2), compute their point of intersection.
0182 * The computed point is guaranteed to lie in the intersection of the
0183 * bounding rectangles defined by each edge.
0184 */
0185 {
0186 GLUdouble z1, z2;
0187
0188 /* This is certainly not the most efficient way to find the intersection
0189 * of two line segments, but it is very numerically stable.
0190 *
0191 * Strategy: find the two middle vertices in the VertLeq ordering,
0192 * and interpolate the intersection s-value from these. Then repeat
0193 * using the TransLeq ordering to find the intersection t-value.
0194 */
0195
0196 if( ! VertLeq( o1, d1 )) { Swap( o1, d1 ); }
0197 if( ! VertLeq( o2, d2 )) { Swap( o2, d2 ); }
0198 if( ! VertLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); }
0199
0200 if( ! VertLeq( o2, d1 )) {
0201 /* Technically, no intersection -- do our best */
0202 v->s = (o2->s + d1->s) / 2;
0203 } else if( VertLeq( d1, d2 )) {
0204 /* Interpolate between o2 and d1 */
0205 z1 = EdgeEval( o1, o2, d1 );
0206 z2 = EdgeEval( o2, d1, d2 );
0207 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
0208 v->s = Interpolate( z1, o2->s, z2, d1->s );
0209 } else {
0210 /* Interpolate between o2 and d2 */
0211 z1 = EdgeSign( o1, o2, d1 );
0212 z2 = -EdgeSign( o1, d2, d1 );
0213 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
0214 v->s = Interpolate( z1, o2->s, z2, d2->s );
0215 }
0216
0217 /* Now repeat the process for t */
0218
0219 if( ! TransLeq( o1, d1 )) { Swap( o1, d1 ); }
0220 if( ! TransLeq( o2, d2 )) { Swap( o2, d2 ); }
0221 if( ! TransLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); }
0222
0223 if( ! TransLeq( o2, d1 )) {
0224 /* Technically, no intersection -- do our best */
0225 v->t = (o2->t + d1->t) / 2;
0226 } else if( TransLeq( d1, d2 )) {
0227 /* Interpolate between o2 and d1 */
0228 z1 = TransEval( o1, o2, d1 );
0229 z2 = TransEval( o2, d1, d2 );
0230 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
0231 v->t = Interpolate( z1, o2->t, z2, d1->t );
0232 } else {
0233 /* Interpolate between o2 and d2 */
0234 z1 = TransSign( o1, o2, d1 );
0235 z2 = -TransSign( o1, d2, d1 );
0236 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
0237 v->t = Interpolate( z1, o2->t, z2, d2->t );
0238 }
0239 }
0240
0241 #endif