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 assert( VertLeq( u, v ) && VertLeq( v, w ));
0079
0080 gapL = v->s - u->s;
0081 gapR = w->s - v->s;
0082
0083 if( gapL + gapR > 0 ) {
0084 return (v->t - w->t) * gapL + (v->t - u->t) * gapR;
0085 }
0086 /* vertical line */
0087 return 0;
0088 }
0089
0090
0091 /***********************************************************************
0092 * Define versions of EdgeSign, EdgeEval with s and t transposed.
0093 */
0094
0095 inline GLUdouble __gl_transEval( GLUvertex *u, GLUvertex *v, GLUvertex *w )
0096 {
0097 /* Given three vertices u,v,w such that TransLeq(u,v) && TransLeq(v,w),
0098 * evaluates the t-coord of the edge uw at the s-coord of the vertex v.
0099 * Returns v->s - (uw)(v->t), ie. the signed distance from uw to v.
0100 * If uw is vertical (and thus passes thru v), the result is zero.
0101 *
0102 * The calculation is extremely accurate and stable, even when v
0103 * is very close to u or w. In particular if we set v->s = 0 and
0104 * let r be the negated result (this evaluates (uw)(v->t)), then
0105 * r is guaranteed to satisfy MIN(u->s,w->s) <= r <= MAX(u->s,w->s).
0106 */
0107 GLUdouble gapL, gapR;
0108
0109 assert( TransLeq( u, v ) && TransLeq( v, w ));
0110
0111 gapL = v->t - u->t;
0112 gapR = w->t - v->t;
0113
0114 if( gapL + gapR > 0 ) {
0115 if( gapL < gapR ) {
0116 return (v->s - u->s) + (u->s - w->s) * (gapL / (gapL + gapR));
0117 } else {
0118 return (v->s - w->s) + (w->s - u->s) * (gapR / (gapL + gapR));
0119 }
0120 }
0121 /* vertical line */
0122 return 0;
0123 }
0124
0125 inline GLUdouble __gl_transSign( GLUvertex *u, GLUvertex *v, GLUvertex *w )
0126 {
0127 /* Returns a number whose sign matches TransEval(u,v,w) but which
0128 * is cheaper to evaluate. Returns > 0, == 0 , or < 0
0129 * as v is above, on, or below the edge uw.
0130 */
0131 GLUdouble gapL, gapR;
0132
0133 assert( TransLeq( u, v ) && TransLeq( v, w ));
0134
0135 gapL = v->t - u->t;
0136 gapR = w->t - v->t;
0137
0138 if( gapL + gapR > 0 ) {
0139 return (v->s - w->s) * gapL + (v->s - u->s) * gapR;
0140 }
0141 /* vertical line */
0142 return 0;
0143 }
0144
0145
0146 inline int __gl_vertCCW( GLUvertex *u, GLUvertex *v, GLUvertex *w )
0147 {
0148 /* For almost-degenerate situations, the results are not reliable.
0149 * Unless the floating-point arithmetic can be performed without
0150 * rounding errors, *any* implementation will give incorrect results
0151 * on some degenerate inputs, so the client must have some way to
0152 * handle this situation.
0153 */
0154 return (u->s*(v->t - w->t) + v->s*(w->t - u->t) + w->s*(u->t - v->t)) >= 0;
0155 }
0156
0157 /* Given parameters a,x,b,y returns the value (b*x+a*y)/(a+b),
0158 * or (x+y)/2 if a==b==0. It requires that a,b >= 0, and enforces
0159 * this in the rare case that one argument is slightly negative.
0160 * The implementation is extremely stable numerically.
0161 * In particular it guarantees that the result r satisfies
0162 * MIN(x,y) <= r <= MAX(x,y), and the results are very accurate
0163 * even when a and b differ greatly in magnitude.
0164 */
0165 #define Interpolate(a,x,b,y) \
0166 (a = (a < 0) ? 0 : a, b = (b < 0) ? 0 : b, \
0167 ((a <= b) ? ((b == 0) ? ((x+y) / 2) \
0168 : (x + (y-x) * (a/(a+b)))) \
0169 : (y + (x-y) * (b/(a+b)))))
0170
0171 #define Swap(a,b) do { GLUvertex *t = a; a = b; b = t; } while(false)
0172
0173 inline void __gl_edgeIntersect( GLUvertex *o1, GLUvertex *d1,
0174 GLUvertex *o2, GLUvertex *d2,
0175 GLUvertex *v )
0176 /* Given edges (o1,d1) and (o2,d2), compute their point of intersection.
0177 * The computed point is guaranteed to lie in the intersection of the
0178 * bounding rectangles defined by each edge.
0179 */
0180 {
0181 GLUdouble z1, z2;
0182
0183 /* This is certainly not the most efficient way to find the intersection
0184 * of two line segments, but it is very numerically stable.
0185 *
0186 * Strategy: find the two middle vertices in the VertLeq ordering,
0187 * and interpolate the intersection s-value from these. Then repeat
0188 * using the TransLeq ordering to find the intersection t-value.
0189 */
0190
0191 if( ! VertLeq( o1, d1 )) { Swap( o1, d1 ); }
0192 if( ! VertLeq( o2, d2 )) { Swap( o2, d2 ); }
0193 if( ! VertLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); }
0194
0195 if( ! VertLeq( o2, d1 )) {
0196 /* Technically, no intersection -- do our best */
0197 v->s = (o2->s + d1->s) / 2;
0198 } else if( VertLeq( d1, d2 )) {
0199 /* Interpolate between o2 and d1 */
0200 z1 = EdgeEval( o1, o2, d1 );
0201 z2 = EdgeEval( o2, d1, d2 );
0202 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
0203 v->s = Interpolate( z1, o2->s, z2, d1->s );
0204 } else {
0205 /* Interpolate between o2 and d2 */
0206 z1 = EdgeSign( o1, o2, d1 );
0207 z2 = -EdgeSign( o1, d2, d1 );
0208 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
0209 v->s = Interpolate( z1, o2->s, z2, d2->s );
0210 }
0211
0212 /* Now repeat the process for t */
0213
0214 if( ! TransLeq( o1, d1 )) { Swap( o1, d1 ); }
0215 if( ! TransLeq( o2, d2 )) { Swap( o2, d2 ); }
0216 if( ! TransLeq( o1, o2 )) { Swap( o1, o2 ); Swap( d1, d2 ); }
0217
0218 if( ! TransLeq( o2, d1 )) {
0219 /* Technically, no intersection -- do our best */
0220 v->t = (o2->t + d1->t) / 2;
0221 } else if( TransLeq( d1, d2 )) {
0222 /* Interpolate between o2 and d1 */
0223 z1 = TransEval( o1, o2, d1 );
0224 z2 = TransEval( o2, d1, d2 );
0225 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
0226 v->t = Interpolate( z1, o2->t, z2, d1->t );
0227 } else {
0228 /* Interpolate between o2 and d2 */
0229 z1 = TransSign( o1, o2, d1 );
0230 z2 = -TransSign( o1, d2, d1 );
0231 if( z1+z2 < 0 ) { z1 = -z1; z2 = -z2; }
0232 v->t = Interpolate( z1, o2->t, z2, d2->t );
0233 }
0234 }
0235
0236 #endif