Main Page   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

poly_cut.cc

00001 #include "poly_cut.h"
00002 #include <CGAL/Line_3.h>
00003 #include <CGAL/intersection_3.h>
00004 #include "set_point.h"
00005 
00006 // Enable debugging output?
00007 #define DEBUGGING 0
00008 
00009 
00010 
00011 // static inline CGAL::Oriented_side 
00012 // plane_oriented_side( const CGAL::Plane_3<Exact_rep>& plane,
00013 //                   const Polyhedron::Point& point )
00014 // {
00015 //     cout << "    Checking point: " << point << flush;
00016 
00017 //     CGAL::Point_3<Exact_rep> p;
00018 //     set_point( p, point );
00019 
00020 //     CGAL::Oriented_side side = plane.oriented_side( p );
00021 //     cout << ": " << side << endl;
00022     
00023 //     return side;
00024 // }
00025 
00026 
00027 
00028 
00038 Polyhedron::Facet*
00039 cut( Polyhedron& poly,
00040      Polyhedron::Facet& facet,
00041      const CGAL::Plane_3<Exact_rep>& plane )
00042 {
00043     /* Check each halfedge of the facet against the plane.
00044      * The halfedge under test ("curr") points to the "head"
00045      * vertex, and the previous halfedge points to the "tail".
00046      * The halfedge is half-open: it includes the head vertex, but
00047      * not the tail.
00048      *
00049      * If the plane properly cuts the facet, a new edge
00050      * is inserted.  Since we're assuming convex, there will
00051      * be at most one new edge.  We store the endpoints of the new
00052      * edge in diag_endpoint[2].  Actually CGAL requires the halfedge
00053      * for which the vertices are incident, rather than the vertices 
00054      * themselves.
00055      */
00056 
00057 #if DEBUGGING
00058     cout << endl
00059          << "Cut facet: " << endl
00060          << facet
00061          << "with plane: " << plane << endl << endl;
00062 #endif
00063 
00064     Polyhedron::Halfedge_handle diag_endpoint[2];
00065     int end = 0;
00066 
00067     Polyhedron::Halfedge_handle prev = facet.halfedge();
00068     Polyhedron::Halfedge_handle curr = prev->next();
00069 
00070     CGAL::Point_3<Exact_rep> prev_point 
00071         = convert_to_exact( prev->vertex()->point() );
00072     CGAL::Point_3<Exact_rep> curr_point
00073         = convert_to_exact( curr->vertex()->point() );
00074 
00075 #if DEBUGGING
00076     cout << "    Checking point: " << prev_point << flush;
00077 #endif
00078     CGAL::Oriented_side os_tail = plane.oriented_side( prev_point );
00079 #if DEBUGGING
00080     cout << ": " << os_tail << endl;
00081 
00082     cout << "    Checking point: " << curr_point << flush;
00083 #endif
00084     CGAL::Oriented_side os_head = plane.oriented_side( curr_point );
00085 #if DEBUGGING
00086     cout << ": " << os_head << endl;
00087 #endif
00088 
00089     do {
00090         if ( os_tail == os_head ) {
00091             ; // do nothing: no proper intersection
00092         } else if ( os_tail == CGAL::ON_ORIENTED_BOUNDARY ) {
00093             ; // do nothing: the edge is half-open
00094         } else if ( os_head == CGAL::ON_ORIENTED_BOUNDARY ) {
00095             CGAL_assertion( end < 2 );
00096             diag_endpoint[end++] = curr;
00097         } else {
00098             CGAL_assertion( end < 2 );
00099             Polyhedron::Halfedge_handle h 
00100                 = poly.split_vertex( prev, curr->opposite() );
00101             if ( h->vertex() == prev->vertex() )
00102                 h = h->opposite();
00103             diag_endpoint[end++] = h;
00104             CGAL::Line_3<Exact_rep> line( prev_point, curr_point );
00105             CGAL::Object i = intersection( plane, line );
00106             CGAL::Point_3<Exact_rep> i_point;
00107             CGAL_assertion( assign( i_point, i ) );
00108             set_point( h->vertex()->point(), i_point );
00109         }
00110 
00111         prev = curr;
00112         curr = curr->next();
00113         prev_point = curr_point;
00114         curr_point = convert_to_exact( curr->vertex()->point() );
00115         os_tail = os_head;
00116 #if DEBUGGING
00117         cout << "    Checking point: " << curr_point << flush;
00118 #endif
00119         os_head = plane.oriented_side( curr_point );
00120 #if DEBUGGING
00121         cout << ": " << os_head << endl;
00122 #endif
00123     } while ( prev != facet.halfedge() );
00124 
00125     if ( end < 2 )    return NULL;
00126 
00127     Polyhedron::Halfedge_handle new_h = poly.split_facet( diag_endpoint[0],
00128                                                           diag_endpoint[1] );
00129 
00130     Polyhedron::Facet_handle new_f = new_h->facet();
00131     if ( &*new_f == &facet )
00132         new_f = new_h->opposite()->facet();
00133 
00134     return &*new_f;
00135 }
00136 
00137 

Generated on Fri Oct 26 23:38:11 2001 for bicobj-intersection by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001