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

Segment_3_Segment_3.C

00001 #include <CGAL/Segment_3.h>
00002 #include <CGAL/Point_3.h>
00003 #include <CGAL/predicates_on_points_3.h>
00004 #include <CGAL/Object.h>
00005 
00006 #include <CEP/intersection/Segment_3_Point_3.h>
00007 #include <CEP/intersection/Line_3_Line_3.h>
00008 
00009 
00010 namespace CEP {  
00011 namespace intersection {
00012 
00013     using CGAL::Segment_3;
00014 
00015     using CGAL::make_object;
00016     using CGAL::collinear;
00017     using CGAL::collinear_are_ordered_along_line;
00018     using CGAL::coplanar;
00019     using CGAL::coplanar_orientation;
00020 
00021 
00026     template <class R>
00027     inline
00028     bool
00029     collinear_do_intersect( const Segment_3<R>& s1, 
00030                             const Segment_3<R>& s2 )
00031     {
00032         CGAL_exactness_precondition( collinear(s1,s2.source()) );
00033         CGAL_exactness_precondition( collinear(s1,s2.target()) );
00034 
00035         // segment 1     s-------------t
00036         // segment 2  *----*
00037         // cases      *--------------------*
00038         //                  *------*
00039         //                     *-----------*
00040 
00041         return collinear_are_ordered_along_line
00042                ( s1.source(), s2.source(), s1.target() )
00043             || collinear_are_ordered_along_line
00044                ( s1.source(), s2.target(), s1.target() )
00045             || collinear_are_ordered_along_line
00046                ( s2.source(), s1.source(), s2.target() );
00047     }
00048 
00049 
00054     template <class R>
00055     bool
00056     coplanar_do_intersect( const Segment_3<R>& s1,
00057                            const Segment_3<R>& s2 )
00058     {
00059         if ( s1.is_degenerate() )
00060             return s2.has_on(s1.source());
00061         if ( s2.is_degenerate() )
00062             return s1.has_on(s2.source());
00063 
00064         CGAL_exactness_precondition ( coplanar(s1.source(),s1.target(),
00065                                                s2.source(),s2.target()) );
00066 
00067         // If two non-degenerate segments intersect, the intersection
00068         // either contains a segment endpoint (four possibilities to
00069         // check) or it is a point on the relative interior of both
00070         // segments.  We perform the first check now.
00071 
00072         if ( s1.has_on(s2.source()) ||
00073              s1.has_on(s2.target()) ||
00074              s2.has_on(s1.source()) ||
00075              s2.has_on(s1.target()) )
00076             return true;
00077 
00078         // At this point, the segments are guaranteed to be
00079         // non-degenerate, coplanar, and no endpoint lies on the other
00080         // segment.  The coplanar_orientation function requires that the
00081         // first three arguments (that define the plane and its orientation)
00082         // are NOT collinear.  In the case that the three points are
00083         // collinear, we can rule out an intersection, because such
00084         // an intersection would contain at least one segment endpoint,
00085         // and so would have been caught by the above tests.
00086 
00087         if ( collinear( s1.source(), s1.target(), s2.source() ) ||
00088              collinear( s2.source(), s2.target(), s1.source() ) )
00089             return false;
00090 
00091         return coplanar_orientation( s1.source(), s1.target(),
00092                                      s2.source(), s2.target() ) 
00093                == CGAL::NEGATIVE
00094             && coplanar_orientation( s2.source(), s2.target(),
00095                                      s1.source(), s1.target() ) 
00096                == CGAL::NEGATIVE;
00097     }
00098 
00099 
00102     template <class R>
00103     bool
00104     do_intersect( const Segment_3<R>& s1,
00105                   const Segment_3<R>& s2 )
00106     {
00107         if ( s1.is_degenerate() )
00108             return s2.has_on(s1.source());
00109         if ( s2.is_degenerate() )
00110             return s1.has_on(s2.source());
00111 
00112         // If two non-degenerate segments intersect, the intersection
00113         // either contains a segment endpoint (four possibilities to
00114         // check) or it is a point on the relative interior of both
00115         // segments.  The latter case is determined by checking (a) that
00116         // all four endpoints are coplanar, and (b) that the line through
00117         // a segment separates the two endpoints of the other segment (in
00118         // the common plane).
00119 
00120         if ( s1.has_on(s2.source()) ||
00121              s1.has_on(s2.target()) ||
00122              s2.has_on(s1.source()) ||
00123              s2.has_on(s1.target()) )
00124             return true;
00125 
00126         if ( !coplanar(s1.source(),s1.target(),
00127                        s2.source(),s2.target()) )
00128             return false;
00129 
00130         // At this point, the segments are guaranteed to be
00131         // non-degenerate, coplanar, and no endpoint lies on the other
00132         // segment.  The coplanar_orientation function requires that the
00133         // first three arguments (that define the plane and its orientation)
00134         // are NOT collinear.  In the case that the three points are
00135         // collinear, we can rule out an intersection, because such
00136         // an intersection would contain at least two segment endpoints,
00137         // and so would have been caught by the above tests.
00138 
00139         if ( collinear( s1.source(), s1.target(), s2.source() ) ||
00140              collinear( s2.source(), s2.target(), s1.source() ) )
00141             return false;
00142 
00143         return coplanar_orientation( s1.source(), s1.target(),
00144                                      s2.source(), s2.target() ) 
00145                == CGAL::NEGATIVE
00146             && coplanar_orientation( s2.source(), s2.target(),
00147                                      s1.source(), s1.target() ) 
00148                == CGAL::NEGATIVE;
00149     }
00150 
00151 
00152     namespace Segment_3_Segment_3 {
00153         
00172         template <class R>
00173         int classify_point( const Segment_3<R>& S,
00174                             const Point_3<R>& p )
00175         {
00176             CGAL_exactness_precondition( collinear(S,p) );
00177 
00178             const Point_3<R>& s = S.source();
00179             const Point_3<R>& t = S.target();
00180 
00181             if ( p == s )  return 2;
00182             if ( p == t )  return 4;
00183             
00184             if ( collinear_are_ordered_along_line( p, s, t ) )  return 1;
00185             if ( collinear_are_ordered_along_line( s, p, t ) )  return 3;
00186             CGAL_exactness_assertion( collinear_are_ordered_along_line( s,t,p ) );
00187             return 5;
00188         }
00189     }
00190 
00191 
00196     template <class R>
00197     CGAL::Object
00198     collinear_intersection( const Segment_3<R>& seg1,
00199                             const Segment_3<R>& seg2 )
00200     {
00201         const Point_3<R>& s1 = seg1.source();
00202         const Point_3<R>& t1 = seg1.target();
00203         const Point_3<R>& s2 = seg2.source();
00204         const Point_3<R>& t2 = seg2.target();
00205 
00206         CGAL_exactness_precondition( collinear(seg1,s2) );
00207         CGAL_exactness_precondition( collinear(seg1,t2) );
00208 
00209         int s1_class = Segment_3_Segment_3::classify_point( seg2, s1 );
00210         int t1_class = Segment_3_Segment_3::classify_point( seg2, t1 );
00211 
00212         switch( s1_class ) {
00213         case 1: 
00214             switch( t1_class ) {
00215             case 1: return Object();
00216             case 2: return make_object( s2 );
00217             case 3: return make_object( Segment_3<R>(s2,t1) );
00218             case 4: // fall through
00219             case 5: return make_object( seg2 );
00220             }
00221         case 2: 
00222             switch( t1_class ) {
00223             case 1: // fall through
00224             case 2: return make_object( s2 );
00225             case 3: // fall through
00226             case 4: return make_object( seg1 );
00227             case 5: return make_object( seg2 );
00228             }
00229         case 3: 
00230             switch( t1_class ) {
00231             case 1: // fall through
00232             case 2: return make_object( Segment_3<R>(s2,s1) );
00233             case 3: // fall through
00234             case 4: return make_object( seg1 );
00235             case 5: return make_object( Segment_3<R>(s1,t2) );
00236             }
00237         case 4: 
00238             switch( t1_class ) {
00239             case 1: // fall through
00240             case 2: return make_object( seg2 );
00241             case 3: return make_object( seg1 );
00242             case 4: // fall through
00243             case 5: return make_object( t2 );
00244             }
00245         case 5: 
00246             switch( t1_class ) {
00247             case 1: // fall through
00248             case 2: return make_object( seg2 );
00249             case 3: return make_object( Segment_3<R>(t1,t2) );
00250             case 4: return make_object( t2 );
00251             case 5: return Object();
00252             }
00253         }
00254         CGAL_assertion_msg(false, "internal error");
00255         return CGAL::Object();
00256     }
00257 
00258 
00259 
00262     template <class R>
00263     CGAL::Object
00264     intersection( const Segment_3<R>& s1,
00265                   const Segment_3<R>& s2 )
00266     {
00267         if ( s1.is_degenerate() ) 
00268             return intersection( s2, s1.source() );
00269         if ( s2.is_degenerate() )
00270             return intersection( s1, s2.source() );
00271 
00272         // Check all cases of collinear endpoints
00273 
00274         if ( collinear( s1, s2.source() )) {
00275             if ( collinear( s1, s2.target() )) {
00276                 return collinear_intersection( s1, s2 );
00277             } else {
00278                 return intersection( s1, s2.source() );
00279             }
00280         } else if ( collinear( s1, s2.target() )) {
00281             return intersection( s1, s2.target() );
00282         } else if ( collinear( s2, s1.source() )) {
00283             return intersection( s2, s1.source() );
00284         } else if ( collinear( s2, s1.target() )) {
00285             return intersection( s2, s1.target() );
00286         }
00287 
00288         // If no three points are collinear, the intersection can only
00289         // be a point in the relative interior of each segment, which
00290         // implies that the segments must be coplanar, and that the
00291         // line through a segment separates the endpoints of the
00292         // other, in the plane.
00293 
00294         if ( !coplanar(s1.source(),s1.target(),
00295                        s2.source(),s2.target()) )
00296             return CGAL::Object();
00297 
00298         if (    coplanar_orientation( s1.source(), s1.target(),
00299                                       s2.source(), s2.target() ) 
00300                 != CGAL::NEGATIVE
00301              || coplanar_orientation( s2.source(), s2.target(),
00302                                       s1.source(), s1.target() ) 
00303                 != CGAL::NEGATIVE )
00304             return CGAL::Object();
00305 
00306         return 
00307            CEP::intersection::Line_3_Line_3::coplanar_nonparallel_intersection
00308             ( s1.source(), s1.target(), s2.source(), s2.target() );
00309     }
00310 
00311 
00312 }
00313 }

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