00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "stdafx.h"
00021
00022 #include "gul_types.h"
00023 #include "gul_vector.h"
00024 #include "guge_intersect.h"
00025
00026 namespace guge {
00027
00028
00029 #if 0
00030
00031
00032
00033 using gul::Max;
00034 using gul::Min;
00035 using gul::cross_product;
00036
00037
00038
00039
00040
00041 template< class T >
00042 bool RegularIntersectLines(
00043 const point<T>& A,
00044 const point<T>& B,
00045 const point<T>& a,
00046 const point<T>& b,
00047 T *lambda,
00048 T *mu
00049 )
00050 {
00051 T A0,A1,A2,B0,B1,B2;
00052 T a0,a1,a2,b0,b1,b2;
00053 T N1,N2,N3;
00054
00055
00056
00057 A0 = A.x; A1 = A.y; A2 = A.z;
00058 B0 = B.x; B1 = B.y; B2 = B.z;
00059 a0 = a.x; a1 = a.y; a2 = a.z;
00060 b0 = b.x; b1 = b.y; b2 = b.z;
00061
00062 N1 = B1*b0 - b1*B0;
00063 N2 = B2*b0 - b2*B0;
00064 N3 = B2*b1 - b2*B1;
00065
00066 if( (gul::rtr<T>::fabs(N1) >= gul::rtr<T>::fabs(N2)) &&
00067 (gul::rtr<T>::fabs(N1) >= gul::rtr<T>::fabs(N3)) )
00068 {
00069 *lambda = (b0*(a1-A1) - b1*(a0-A0)) / N1;
00070 *mu = (B0*(a1-A1) - B1*(a0-A0)) / N1;
00071 }
00072 else
00073 {
00074 if( gul::rtr<T>::fabs(N2) >= gul::rtr<T>::fabs(N3) )
00075 {
00076 *lambda = (b0*(a2-A2) - b2*(a0-A0)) / N2;
00077 *mu = (B0*(a2-A2) - B2*(a0-A0)) / N2;
00078 }
00079 else
00080 {
00081 if( gul::rtr<T>::fabs(N3) != 0.0 )
00082 {
00083 *lambda = (b1*(a2-A2) - b2*(a1-A1)) / N3;
00084 *mu = (B1*(a2-A2) - B2*(a1-A1)) / N3;
00085 }
00086 else
00087 {
00088 return false;
00089
00090 }
00091 }
00092 }
00093 return true;
00094 }
00095
00096
00097
00098
00099 template
00100 bool RegularIntersectLines(
00101 const point<float>& A,
00102 const point<float>& B,
00103 const point<float>& a,
00104 const point<float>& b,
00105 float *lambda,
00106 float *mu
00107 );
00108 template
00109 bool RegularIntersectLines(
00110 const point<double>& A,
00111 const point<double>& B,
00112 const point<double>& a,
00113 const point<double>& b,
00114 double *lambda,
00115 double *mu
00116 );
00117
00118
00119
00120
00121 template< class T >
00122 bool IntersectTriangles( const triangle<T>& tri0, const triangle<T>& tri1,
00123 point<T> *retP1, point<T> *retP2 )
00124 {
00125 point<T> v11, v12, v13, v21, v22, v23, n;
00126 T A,B,C,a,b,c,R,r,N;
00127 point<T> g,t;
00128 point<T> P1,P2,P3;
00129 int init;
00130 T l1,l2,r1,r2,l,lambda,mu;
00131 bool result;
00132
00133
00134
00135
00136 v11 = tri0.P2 - tri0.P1;
00137 v12 = tri0.P3 - tri0.P1;
00138 v13 = tri0.P3 - tri0.P2;
00139 n = cross_product( v11, v12 );
00140 A = n.x; B = n.y; C = n.z;
00141 R = n * tri0.P1;
00142
00143
00144
00145 v21 = tri1.P2 - tri1.P1;
00146 v22 = tri1.P3 - tri1.P1;
00147 v23 = tri1.P3 - tri1.P2;
00148 n = cross_product( v21, v22 );
00149 a = n.x; b = n.y; c = n.z;
00150 r = n * tri1.P1;
00151
00152
00153
00154
00155
00156
00157 N = B*c - b*C;
00158
00159 if( gul::rtr<T>::fabs(N) != 0.0 )
00160 {
00161 g.x = 0.0;
00162 g.y = (R*c - r*C)/N;
00163 g.z = (B*r - b*R)/N;
00164 t.x = 1.0;
00165 t.y = (C*a - c*A)/N;
00166 t.z = (A*b - a*B)/N;
00167 }
00168 else
00169 {
00170 N = A*c - a*C;
00171
00172 if( gul::rtr<T>::fabs(N) != 0.0 )
00173 {
00174 g.x = (R*c - r*C) / N;
00175 g.y = 0.0;
00176 g.z = (A*r - a*R) / N;
00177 t.x = (C*b - c*B) / N;
00178 t.y = 1.0;
00179 t.z = (B*a - b*A) / N;
00180 }
00181 else
00182 {
00183 N = A*b - a*B;
00184
00185 if( gul::rtr<T>::fabs(N) != 0.0 )
00186 {
00187 g.x = (R*b - r*B) / N;
00188 g.y = (A*r - a*R) / N;
00189 g.z = 0.0;
00190 t.x = (B*c - b*C) / N;
00191 t.y = (C*a - c*A) / N;
00192 t.z = 1.0;
00193 }
00194 else
00195 {
00196 return false;
00197 }
00198 }
00199 }
00200
00201
00202
00203
00204
00205
00206 P1 = tri0.P1;
00207 P2 = tri0.P2;
00208 P3 = tri0.P3;
00209
00210 init = 0;
00211
00212
00213
00214
00215 result = RegularIntersectLines( P1, v11, g, t, &lambda, &mu );
00216 if( (result != 0) && (lambda >= 0.0) && (lambda <= 1.0) )
00217 {
00218 init = 1;
00219 l1 = mu;
00220 r1 = mu;
00221 }
00222
00223
00224
00225 result = RegularIntersectLines( P1, v12, g, t, &lambda, &mu );
00226 if( (result != 0) && (lambda >= 0.0) && (lambda <= 1.0) )
00227 {
00228 if( init != 0 )
00229 {
00230 if( mu < l1 ) l1 = mu;
00231 else if( mu > r1 ) r1 = mu;
00232 init += 1;
00233 }
00234 else
00235 {
00236 init = 1;
00237 l1 = mu;
00238 r1 = mu;
00239 }
00240 }
00241
00242
00243
00244 result = RegularIntersectLines( P2, v13, g, t, &lambda, &mu );
00245 if( (result != 0) && (lambda >= 0.0) && (lambda <= 1.0) )
00246 {
00247 if( init != 0 )
00248 {
00249 if( mu < l1 ) l1 = mu;
00250 else if( mu > r1 ) r1 = mu;
00251 init += 1;
00252 }
00253 else
00254 {
00255 init = 1;
00256 l1 = mu;
00257 r1 = mu;
00258 }
00259 }
00260
00261 if( init < 1 )
00262 return false;
00263
00264
00265
00266
00267
00268
00269
00270 P1 = tri1.P1;
00271 P2 = tri1.P2;
00272 P3 = tri1.P3;
00273
00274 init = 0;
00275
00276
00277
00278
00279 result = RegularIntersectLines( P1, v21, g, t, &lambda, &mu );
00280 if( (result != 0) && (lambda >= 0.0) && (lambda <= 1.0) )
00281 {
00282 init = 1;
00283 l2 = mu;
00284 r2 = mu;
00285 }
00286
00287
00288
00289 result = RegularIntersectLines( P1, v22, g, t, &lambda, &mu );
00290 if( (result != 0) && (lambda >= 0.0) && (lambda <= 1.0) )
00291 {
00292 if( init != 0 )
00293 {
00294 if( mu < l2 ) l2 = mu;
00295 else if( mu > r2 ) r2 = mu;
00296 init += 1;
00297 }
00298 else
00299 {
00300 init = 1;
00301 l2 = mu;
00302 r2 = mu;
00303 }
00304 }
00305
00306
00307
00308 result = RegularIntersectLines( P2, v23, g, t, &lambda, &mu );
00309 if( (result != 0) && (lambda >= 0.0) && (lambda <= 1.0) )
00310 {
00311 if( init != 0 )
00312 {
00313 if( mu < l2 ) l2 = mu;
00314 else if( mu > r2 ) r2 = mu;
00315 init += 1;
00316 }
00317 else
00318 {
00319 init = 1;
00320 l2 = mu;
00321 r2 = mu;
00322 }
00323 }
00324 if( init < 1 )
00325 return false;
00326
00327
00328
00329 l = Max( l1, l2 );
00330 r = Min( r1, r2 );
00331 if( r < l )
00332 return(0);
00333
00334
00335
00336 P3 = l * t;
00337 *retP1 = g + P3;
00338 P3 = r * t;
00339 *retP2 = g + P3;
00340
00341
00342
00343
00344
00345
00346
00347 return true;
00348 }
00349
00350
00351
00352 template
00353 bool IntersectTriangles(
00354 const triangle<float>& tri0, const triangle<float>& tri1,
00355 point<float> *retP1, point<float> *retP2 );
00356 template
00357 bool IntersectTriangles(
00358 const triangle<double>& tri0, const triangle<double>& tri1,
00359 point<double> *retP1, point<double> *retP2 );
00360
00361
00362 #endif
00363
00364
00365
00366
00367
00368
00369
00370 }
00371