Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

guge_intersect.cpp

Go to the documentation of this file.
00001 /* LIBGUL - Geometry Utility Library
00002  * Copyright (C) 1998-1999 Norbert Irmer
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
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 // not needed anymore 
00032 
00033 using gul::Max;
00034 using gul::Min;
00035 using gul::cross_product;
00036 
00037 /* ----------------------------------------------------------------------
00038   calculate the intersection point of two lines (in 3D). It must exist,
00039   or the result will be wrong.
00040 ----------------------------------------------------------------------- */
00041 template< class T >
00042 bool RegularIntersectLines(
00043   const point<T>& A,     /* point on line 1 */
00044   const point<T>& B,     /* direction vector of line 1 */
00045   const point<T>& a,     /* point on line 2 */
00046   const point<T>& b,     /* direction vector of line 2 */
00047   T *lambda,  /* parameter value of intersect. point (for line 1) */
00048   T *mu       /* parameter value of intersect. point (for line 2) */
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 /* bequemere Notation: */
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)) ) /* Berechnung ohne z */
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) )  /* Berechnung ohne y */
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 )   /* Berechnung ohne x */
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;  /* kein Schnittpunkt, oder Richtungsvektoren */ 
00089                        /* linear abhaengig                          */
00090       }      
00091     }
00092   }  
00093   return true;
00094 }
00095 /* ----------------------------------------------------------------------
00096   calculate the intersection point of two lines (in 3D). It must exist,
00097   or the result will be wrong.
00098 ----------------------------------------------------------------------- */
00099 template
00100 bool RegularIntersectLines(
00101   const point<float>& A,     /* point on line 1 */
00102   const point<float>& B,     /* direction vector of line 1 */
00103   const point<float>& a,     /* point on line 2 */
00104   const point<float>& b,     /* direction vector of line 2 */
00105   float *lambda,  /* parameter value of intersect. point (for line 1) */
00106   float *mu       /* parameter value of intersect. point (for line 2) */
00107 );
00108 template
00109 bool RegularIntersectLines(
00110   const point<double>& A,     /* point on line 1 */
00111   const point<double>& B,     /* direction vector of line 1 */
00112   const point<double>& a,     /* point on line 2 */
00113   const point<double>& b,     /* direction vector of line 2 */
00114   double *lambda,  /* parameter value of intersect. point (for line 1) */
00115   double *mu       /* parameter value of intersect. point (for line 2) */
00116 );
00117 
00118 /* ---------------------------------------------------------------------
00119   calculate the intersection line segment of two triangles
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 /* --- Ebenengleichung 1.Dreieck bestimmen ------------------------------ */
00134 /* Ax + By + Cz = R */
00135 
00136   v11 = tri0.P2 - tri0.P1;   /* 1.Richtungsvektor */
00137   v12 = tri0.P3 - tri0.P1;   /* 2.Richtungsvektor */
00138   v13 = tri0.P3 - tri0.P2;   /* (fuer spaeter)    */
00139   n = cross_product( v11, v12 ); /* Normalenvektor Ebene */
00140   A = n.x;  B = n.y;  C = n.z;
00141   R = n * tri0.P1;           /* Aufpunkt einbauen */
00142   
00143 /* --- Ebenengleichung 2.Dreieck bestimmen ------------------------------ */
00144 /* ax + by + cz = r */
00145   v21 = tri1.P2 - tri1.P1;   /* 1.Richtungsvektor */
00146   v22 = tri1.P3 - tri1.P1;   /* 2.Richtungsvektor */
00147   v23 = tri1.P3 - tri1.P2;   /* (fuer spaeter)    */
00148   n = cross_product( v21, v22 ); /* Normalenvektor Ebene */
00149   a = n.x;  b = n.y;  c = n.z;
00150   r = n * tri1.P1;           /* Aufpunkt einbauen */
00151    
00152 /* --- Schnittgerade G : x = g + lambda * t bestimmen ------------------ */
00153 
00154 /* falls Schnittgerade nicht parallel zur X-Achse g.x = 0 und t.x = 1 setzen, */
00155 /* ==> g.y,g.z und t.x,t.z                                                    */
00156    
00157   N = B*c - b*C;           /* Systemdeterminante */
00158   
00159   if( gul::rtr<T>::fabs(N) != 0.0 )     /* nicht parallel zur X-Achse */  
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 )   /* nicht parallel zur y-Achse */
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 )   /* nicht parallel zur z-Achse */
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        /* Normalenvektoren der beiden Ebenen linear abhaengig */
00195       {
00196         return false;
00197       }  
00198     }
00199   }
00200 
00201 /* Schnittgerade mit dem Rand des ersten Dreiecks schneiden:          */
00202 /* Ergebnis Intervall [l1,r1] im Parameterbereich  der Schnittgeraden */
00203 
00204 /* bequeme Notation: */
00205 
00206   P1 = tri0.P1;
00207   P2 = tri0.P2;
00208   P3 = tri0.P3;
00209 
00210   init = 0;
00211 
00212 /* Schnittgerade: X = g + mu * t                       */
00213 /* Geradengleichung Seite P1P2:  X = P1 + lambda * v11 */
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 /* Geradengleichung Seite P1P3:  X = P1 + lambda * v12 */
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 /* Geradengleichung Seite P2P3:  X = P2 + lambda * v13 */
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 /* Schnittgerade mit dem Rand des zweiten Dreiecks schneiden:        */
00266 /* Ergebnis Intervall [l2,r2] im Parameterbereich der Schnittgeraden */
00267 
00268 /* bequeme Notation: */
00269 
00270   P1 = tri1.P1;
00271   P2 = tri1.P2;
00272   P3 = tri1.P3;
00273 
00274   init = 0;
00275 
00276 /* Schnittgerade: X = g + mu * t                       */
00277 /* Geradengleichung Seite P1P2:  X = P1 + lambda * v21 */
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 /* Geradengleichung Seite P1P3:  X = P1 + lambda * v22 */
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 /* Geradengleichung Seite P2P3:  X = P2 + lambda * v23 */
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 /* Durchschnitt der Parameterintervalle liefert Schnittsegment: */ 
00328 
00329   l = Max( l1, l2 );
00330   r = Min( r1, r2 );
00331   if( r < l )
00332     return(0);
00333   
00334 /* Endpunkte bestimmen: */
00335 
00336   P3 = l * t;
00337   *retP1 = g + P3;
00338   P3 = r * t;
00339   *retP2 = g + P3;
00340   
00341 /*
00342   printf( 
00343 "Intersection-Segment: (%8.2f, %8.2f, %8.2f) -> (%8.2f, %8.2f, %8.2f)\n",
00344     P1.x, P1.y, P1.z, P2.x, P2.y, P2.z );
00345 */
00346 
00347   return true;
00348 }
00349 /* ---------------------------------------------------------------------
00350   calculate the intersection line segment of two triangles
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 

Generated on Mon Jan 21 04:17:18 2002 for GUL 0.6 - Geometry Utility Library by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001