Main Page   Modules   Namespace List   Compound List   File List   Namespace Members   Compound Members  

Triangle_3_Triangle_3.C

00001 // Copyright 2001 Steven M. Robbins
00002 
00003 #include <vector>
00004 #include <iterator>
00005 
00006 #include <CGAL/Triangle_2.h>
00007 #include <CGAL/Triangle_2_Triangle_2_intersection.h>
00008 #include <CGAL/circulator.h>
00009 #include <CGAL/predicates_on_points_3.h>
00010 
00011 #include <CEP/intersection/Plane_3_Triangle_3.h>
00012 #include <CEP/intersection/Segment_3_Segment_3.h>
00013 #include <CEP/intersection/Segment_3_Point_3.h>
00014 #include <CEP/intersection/Line_3_Line_3.h>
00015 
00016 
00017 namespace CEP {    namespace intersection {
00018 
00019 
00020     using CGAL::Plane_3;
00021     using CGAL::Triangle_3;
00022     using CGAL::Triangle_2;
00023 
00024     using CGAL::assign;
00025     using CGAL::make_object;
00026     using CGAL::collinear_are_ordered_along_line;
00027 
00028 
00032     namespace Triangle_3_Triangle_3 {
00033 
00035 
00042         template <class R>
00043         Triangle_2<R>
00044         _project_triangle( const Triangle_3<R>& T, int projection_dir )
00045         {
00046             int i1 = (projection_dir + 1) %3;
00047             int i2 = (projection_dir + 2) %3;
00048             
00049             CGAL::Point_2<R> p( T[0].homogeneous(i1), 
00050                                 T[0].homogeneous(i2), 
00051                                 T[0].hw() );
00052             CGAL::Point_2<R> q( T[1].homogeneous(i1), 
00053                                 T[1].homogeneous(i2), 
00054                                 T[1].hw() );
00055             CGAL::Point_2<R> r( T[2].homogeneous(i1), 
00056                                 T[2].homogeneous(i2),
00057                                 T[2].hw() );
00058             
00059             return CGAL::Triangle_2<R>(p,q,r);
00060         }
00061 
00062 
00063         // \ingroup grp_coplanar_do_intersect
00069         template <class R>
00070         bool
00071         coplanar_do_intersect( const Plane_3<R>& P,
00072                                const Triangle_3<R>& T1, 
00073                                const Triangle_3<R>& T2 )
00074         {
00075             CGAL_exactness_precondition( P.has_on(T1[0]) );
00076             CGAL_exactness_precondition( P.has_on(T1[1]) );
00077             CGAL_exactness_precondition( P.has_on(T1[2]) );
00078             
00079             CGAL_exactness_precondition( P.has_on(T2[0]) );
00080             CGAL_exactness_precondition( P.has_on(T2[1]) );
00081             CGAL_exactness_precondition( P.has_on(T2[2]) );
00082             
00083             // Project triangles to some axis-aligned plane, then use
00084             // 2D routines.  Any projection direction is fine, as long
00085             // as the triangles do not project to a line segment.
00086             // Equivalently, any direction for which the triangles'
00087             // plane normal does not vanish is acceptable.  Choose
00088             // projection direction that maximizes the plane normal
00089             // projection.
00090             CGAL::Direction_3<R> normal = P.orthogonal_direction();
00091             int proj_dir;
00092 
00093             double dx = fabs(CGAL::to_double(normal.dx()));
00094             double dy = fabs(CGAL::to_double(normal.dy()));
00095             double dz = fabs(CGAL::to_double(normal.dz()));
00096 
00097             if ( dx > dy ) {
00098                 if ( dx > dz )
00099                     proj_dir = 0;
00100                 else
00101                     proj_dir = 2;
00102             } else {
00103                 if ( dy > dz )
00104                     proj_dir = 1;
00105                 else
00106                     proj_dir = 2;
00107             }
00108             
00109             Triangle_2<R> T1p = _project_triangle(T1,proj_dir);
00110             Triangle_2<R> T2p = _project_triangle(T2,proj_dir);
00111             
00112             return CGAL::do_intersect( T1p,T2p );
00113         }
00114 
00115 
00116 
00117         // \ingroup grp_do_intersect
00124         template <class R>
00125         bool
00126         do_intersect( const Triangle_3<R>& T1, const Plane_3<R>& P1,
00127                       const Triangle_3<R>& T2, const Plane_3<R>& P2 )
00128         {
00129             CGAL_exactness_precondition( !T1.is_degenerate() );
00130             CGAL_exactness_precondition( !T2.is_degenerate() );
00131             
00132             CGAL_exactness_precondition( P1.has_on(T1[0]) );
00133             CGAL_exactness_precondition( P1.has_on(T1[1]) );
00134             CGAL_exactness_precondition( P1.has_on(T1[2]) );
00135             
00136             CGAL_exactness_precondition( P2.has_on(T2[0]) );
00137             CGAL_exactness_precondition( P2.has_on(T2[1]) );
00138             CGAL_exactness_precondition( P2.has_on(T2[2]) );
00139 
00140 
00141             CGAL::Object P1_int_T2 = CEP::intersection::intersection( P1, T2 );
00142             if ( P1_int_T2.is_empty() )
00143                 return false;
00144 
00145             CGAL::Object P2_int_T1 = CEP::intersection::intersection( P2, T1 );
00146             if ( P2_int_T1.is_empty() )
00147                 return false;
00148 
00149 
00150             // Either both intersections are a triangle, or neither.
00151             Triangle_3<R> t1, t2;
00152             if ( assign(t1,P2_int_T1) ) {
00153                 bool intersection_is_triangle = assign( t2, P1_int_T2 );
00154                 CGAL_assertion( intersection_is_triangle );
00155                 return coplanar_do_intersect(P1,T1,T2);
00156             }
00157             CGAL_assertion( !assign(t2,P1_int_T2) );
00158 
00159 
00160             // Only point and segment cases left.
00161             CGAL::Point_3<R>    p1, p2;  
00162             CGAL::Segment_3<R>  s1, s2;  
00163             
00164             if ( assign(p1,P2_int_T1) ) {
00165                 if ( assign(p2,P1_int_T2) ) {
00166                     return p1 == p2;
00167                 } 
00168                 if ( assign(s2,P1_int_T2) ) {
00169                     return collinear_are_ordered_along_line( s2.source(), 
00170                                                              p1, 
00171                                                              s2.target() );
00172                 }
00173             } else if ( assign(s1,P2_int_T1) ) {
00174                 if ( assign(p2,P1_int_T2) ) {
00175                     return collinear_are_ordered_along_line( s1.source(), 
00176                                                              p2, 
00177                                                              s1.target() );
00178                 } 
00179                 if ( assign(s2,P1_int_T2) ) {
00180                     return collinear_do_intersect( s1, s2 );
00181                 }
00182             }
00183 
00184             // Cannot get here.
00185             CGAL_assertion_msg(0,"should not reach here");
00186             return true;
00187         }
00188 
00189 
00190         /*
00191          * Input is a polygon P given as a sequence [points_begin,points_end)
00192          * of Point_3<R>, and a half-plane H specified using three points s,
00193          * t, u.  H is half the plane incident to (s,t,u), with s and t on the
00194          * boundary, and u on the interior of H.  H is relatively closed;
00195          * i.e. it includes the line through (s,t).
00196          *
00197          * All points must lie in a common plane (not checked).
00198          *
00199          * The function inserts points of the intersection of P and H into the
00200          * container associated with output.
00201          * 
00202          */
00203         template <class R, class InPointIterator, class OutPointIterator>
00204         void _slice_in_plane( InPointIterator points_begin, 
00205                               InPointIterator points_end,
00206                               const CGAL::Point_3<R>& s,
00207                               const CGAL::Point_3<R>& t,
00208                               const CGAL::Point_3<R>& u,
00209                               OutPointIterator output )
00210         {
00211             if ( points_begin == points_end )
00212                 return;
00213             
00214             typedef CGAL::Circulator_from_iterator<InPointIterator> Circulator;
00215             typedef typename 
00216                 std::iterator_traits<InPointIterator>::value_type Point_;
00217             
00218             Circulator p_begin( points_begin, points_end );
00219             Circulator p = p_begin;
00220             
00221             do {
00222                 CGAL::Orientation side 
00223                     = CGAL::coplanar_orientation( s,t,u, *p );
00224                 
00225                 if ( side != CGAL::NEGATIVE )
00226                     *output++ = *p;
00227                 
00228                 if ( side != CGAL::COLLINEAR ) {
00229                     Circulator q(p);  
00230                     ++q;
00231                     CGAL::Orientation q_side 
00232                         = CGAL::coplanar_orientation( s,t,u, *q );
00233                     
00234                     if ( q_side != side && q_side != CGAL::COLLINEAR ) {
00235                         CGAL::Object o_int 
00236                             = CEP::intersection::Line_3_Line_3::coplanar_intersection( s,t, *p,*q );
00237                         Point_ p_int;
00238                         bool intersection_is_point = assign(p_int,o_int);
00239                         CGAL_assertion( intersection_is_point );
00240                         *output++ = p_int;
00241                     }
00242                 }
00243             } while( ++p != p_begin );
00244         }
00245         
00246 
00257         template <class R>
00258         CGAL::Object
00259         coplanar_intersection( const Plane_3<R>& P, 
00260                                const Triangle_3<R>& T1, 
00261                                const Triangle_3<R>& T2 )
00262         {
00263             typedef CGAL::Point_3<R> Point;
00264             
00265             CGAL_exactness_precondition( P.has_on(T1[0]) );
00266             CGAL_exactness_precondition( P.has_on(T1[1]) );
00267             CGAL_exactness_precondition( P.has_on(T1[2]) );
00268             
00269             CGAL_exactness_precondition( P.has_on(T2[0]) );
00270             CGAL_exactness_precondition( P.has_on(T2[1]) );
00271             CGAL_exactness_precondition( P.has_on(T2[2]) );
00272 
00273             std::vector<Point> poly1(6);
00274             std::vector<Point> poly2(6);
00275 
00276             // Let poly1 = T2
00277             // Let poly2 = poly1 intersect halfplane bounded by T1[0],T1[1]
00278             poly1.clear();
00279             poly1.push_back( T2[0] );
00280             poly1.push_back( T2[1] );
00281             poly1.push_back( T2[2] );
00282 
00283             poly2.clear();
00284             _slice_in_plane( poly1.begin(), poly1.end(),
00285                              T1[0], T1[1], T1[2],
00286                              std::back_inserter(poly2) );
00287             
00288             // Let poly1 = poly2 intersect halfplane bounded by T1[1],T1[2]
00289             poly1.clear();
00290             _slice_in_plane( poly2.begin(), poly2.end(),
00291                              T1[1], T1[2], T1[0],
00292                              std::back_inserter(poly1) );
00293 
00294             // Let poly2 = poly1 intersect halfplane bounded by T1[2],T1[0]
00295             poly2.clear();
00296             _slice_in_plane( poly1.begin(), poly1.end(),
00297                              T1[2], T1[0], T1[1],
00298                              std::back_inserter(poly2) );
00299 
00300             switch( poly2.size() ) {
00301             case 0 : return CGAL::Object();
00302             case 1 : return make_object( poly2[0] );
00303             case 2 : return make_object( Segment_3<R>(poly2[0],poly2[1]) );
00304             case 3 : return make_object( Triangle_3<R>(poly2[0],
00305                                                        poly2[1],
00306                                                        poly2[2]) );
00307             }
00308             
00309             CGAL_assertion( poly2.size() <= 6 );
00310             std::vector<Point>* ret = new std::vector<Point>( poly2 );
00311             return make_object( *ret );
00312         }
00313 
00314 
00315 
00316         // \ingroup grp_intersection
00322         template <class R>
00323         CGAL::Object
00324         intersection( const Triangle_3<R>& T1, const Plane_3<R>& P1,
00325                       const Triangle_3<R>& T2, const Plane_3<R>& P2 )
00326         {
00327             CGAL_exactness_precondition( P1.has_on(T1[0]) );
00328             CGAL_exactness_precondition( P1.has_on(T1[1]) );
00329             CGAL_exactness_precondition( P1.has_on(T1[2]) );
00330             
00331             CGAL_exactness_precondition( P2.has_on(T2[0]) );
00332             CGAL_exactness_precondition( P2.has_on(T2[1]) );
00333             CGAL_exactness_precondition( P2.has_on(T2[2]) );
00334 
00335             CGAL::Object P1_int_T2 = CEP::intersection::intersection( P1, T2 );
00336             if ( P1_int_T2.is_empty() )
00337                 return CGAL::Object();
00338 
00339             CGAL::Object P2_int_T1 = CEP::intersection::intersection( P2, T1 );
00340             if ( P2_int_T1.is_empty() )
00341                 return CGAL::Object();
00342 
00343             // Either both intersections are a triangle, or neither.
00344             Triangle_3<R> t1, t2;
00345             if ( assign(t1,P2_int_T1) ) {
00346                 bool intersection_is_triangle = assign( t2, P1_int_T2);
00347                 CGAL_assertion( intersection_is_triangle );
00348                 return coplanar_intersection(P1,T1,T2);
00349             }
00350             CGAL_assertion( !assign(t2,P1_int_T2) );
00351 
00352             // Only point and segment cases left.
00353             CGAL::Point_3<R>    p1, p2;  
00354             CGAL::Segment_3<R>  s1, s2;  
00355             
00356             if ( assign(p1,P2_int_T1) ) {
00357                 if ( assign(p2,P1_int_T2) ) {
00358                     return p1 == p2 ? make_object(p1) : CGAL::Object();
00359                 } 
00360                 bool intersection_is_segment = assign(s2,P1_int_T2);
00361                 CGAL_assertion( intersection_is_segment );
00362                 return CEP::intersection::intersection( s2,p1 );
00363             }
00364             
00365             bool intersection_is_segment = assign(s1,P2_int_T1);
00366             CGAL_assertion( intersection_is_segment );
00367             if ( assign(p2,P1_int_T2) )
00368                 return CEP::intersection::intersection( s1,p2 );
00369 
00370             intersection_is_segment = assign(s2,P1_int_T2);
00371             CGAL_assertion( intersection_is_segment );
00372             return CEP::intersection::intersection(s1,s2);
00373         }
00374         
00375     }
00376     
00377 }
00378 }

Generated on Sun Jun 8 17:38:22 2003 for CEP::intersection by doxygen1.3-rc3