00001 #include "poly_cut.h"
00002 #include <CGAL/Line_3.h>
00003 #include <CGAL/intersection_3.h>
00004 #include "set_point.h"
00005
00006
00007 #define DEBUGGING 0
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
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 ;
00092 } else if ( os_tail == CGAL::ON_ORIENTED_BOUNDARY ) {
00093 ;
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