00001 #include "Polyhedron_Interval.h"
00002 #include <CGAL/intersection_3.h>
00003
00004 #include <CGAL/Object.h>
00005 #include <CGAL/Point_3.h>
00006 #include <CGAL/Line_3.h>
00007 #include <CGAL/Plane_3.h>
00008
00009 #include <CGAL/IO/Verbose_ostream.h>
00010
00011 static CGAL::Verbose_ostream verr;
00012
00013
00014
00015 typedef Polyhedron::Halfedge Halfedge;
00016 typedef Polyhedron::Halfedge_handle Halfedge_handle;
00017 typedef Polyhedron::Point Point;
00018 typedef Point::R R;
00019 typedef R::RT RT;
00020
00021 typedef CGAL::Plane_3<R> Plane;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 static inline int
00032 crossing_code( const FT& tail, const FT& tip, const FT& boundary )
00033 {
00034 if ( tail < boundary && boundary <= tip ) return 0;
00035 if ( tip <= boundary && boundary < tail ) return 1;
00036 return -1;
00037 }
00038
00039
00040
00041
00042 static inline int
00043 crossing_code( const Point& tail, const Point& tip,
00044 int cindex, const FT& boundary )
00045 {
00046 return crossing_code( tail.cartesian(cindex),
00047 tip.cartesian(cindex),
00048 boundary );
00049 }
00050
00051
00052
00053
00054 inline Plane
00055 coordinate_plane( int cindex, const FT& boundary )
00056 {
00057 CGAL_precondition( 0 <= cindex );
00058 CGAL_precondition( cindex < 3 );
00059
00060
00061 CGAL_assertion( RT(1.5) == FT(1.5) );
00062
00063 RT a = 0;
00064 RT b = 0;
00065 RT c = 0;
00066 RT d = -boundary;
00067
00068 if ( cindex == 0 )
00069 a = 1;
00070 else if ( cindex == 1 )
00071 b = 1;
00072 else
00073 c = 1;
00074
00075 return Plane(a,b,c,d);
00076 }
00077
00078
00079
00080
00081
00082 static Halfedge_handle
00083 split_edge( Polyhedron& poly,
00084 Halfedge_handle h,
00085 int cindex,
00086 const FT& boundary )
00087 {
00088
00089
00090
00091
00092
00093
00094
00095
00096 CGAL::Line_3<Point::R> line( h->vertex()->point(),
00097 h->opposite()->vertex()->point() );
00098 CGAL::Plane_3<Point::R> plane = coordinate_plane(cindex,boundary);
00099
00100 CGAL::Object o_int = intersection(plane,line);
00101 if (o_int.is_empty()) {
00102 cerr << "split_edge(): line does not intersect plane!" << endl
00103 << "Edge: " << *h << endl
00104 << "Line: " << line << endl
00105 << "Plane: " << plane << endl;
00106 CGAL_assertion_msg(0,"Can not happen [sic]");
00107 }
00108
00109
00110
00111
00112
00113
00114 Point p_int;
00115 if ( !assign(p_int,o_int) ) {
00116 cerr << "Alert: line in plane." << endl;
00117 return h;
00118 }
00119
00120
00121 Halfedge_handle new_h = poly.split_vertex( h, h->opposite()->prev() );
00122 if ( new_h->vertex() == h->vertex() )
00123 new_h = new_h->opposite();
00124
00125 h->vertex()->point() = p_int;
00126 return new_h;
00127 }
00128
00129
00130
00143 FacetTriple
00144 cutting( Polyhedron& poly,
00145 Polyhedron::Facet& facet,
00146 int cindex, const FT& low, const FT& high )
00147 {
00148 CGAL_precondition( low < high );
00149 verr << "cutting with interval ("
00150 << low << ',' << high << ") on axis " << cindex << endl
00151 << poly << endl << endl;
00152
00153
00154
00155
00156 Halfedge_handle cut_edge[4];
00157 int cut_edges_seen = 0;
00158
00159 Halfedge_handle prev = facet.halfedge();
00160 Halfedge_handle curr = prev->next();
00161
00162 do {
00163 verr << "Considering halfedge: " << *curr << endl;
00164 int code = crossing_code( prev->vertex()->point(),
00165 curr->vertex()->point(),
00166 cindex, low );
00167 verr << " LOW code = " << code << endl;
00168
00169 if ( code >= 0 ) {
00170 cut_edge[code] = curr;
00171 cut_edges_seen |= (1 << code);
00172 }
00173
00174 code = crossing_code( prev->vertex()->point(),
00175 curr->vertex()->point(),
00176 cindex, high );
00177 verr << " HIGH code = " << code << endl;
00178
00179 if ( code >= 0 ) {
00180 code += 2;
00181 cut_edge[code] = curr;
00182 cut_edges_seen |= (1 << code);
00183 }
00184
00185 prev = curr;
00186 curr = curr->next();
00187 } while ( prev != facet.halfedge() );
00188
00189 verr << "*** cut_edges_seen = " << cut_edges_seen << endl << endl;
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 FacetTriple ret_val = {0,0,0};
00203
00204
00205 if ( (cut_edges_seen & 3) == 0 ) {
00206
00207
00208 const FT& testval = facet.halfedge()->vertex()->point().cartesian(cindex);
00209
00210 if ( testval < low )
00211 ret_val.low = &facet;
00212 else
00213 ret_val.middle = &facet;
00214 } else if ( (cut_edges_seen & 3) == 3 ) {
00215
00216
00217 verr << "edge 0 = " << *(cut_edge[0]) << endl;
00218 verr << "edge 1 = " << *(cut_edge[1]) << endl;
00219 Halfedge_handle new_cutedge0
00220 = split_edge( poly, cut_edge[0], cindex, low );
00221 split_edge( poly, cut_edge[1], cindex, low );
00222 poly.split_facet(cut_edge[0],cut_edge[1]);
00223
00224 ret_val.low = &*(cut_edge[0]->facet());
00225 ret_val.middle = &*(cut_edge[1]->facet());
00226
00227
00228 if ( (cut_edges_seen & 4) &&
00229 cut_edge[2] == cut_edge[0] )
00230 cut_edge[2] = new_cutedge0;
00231 } else {
00232
00233
00234
00235
00236 int i = ( (cut_edges_seen & 3) == 1 ? 0 : 1 );
00237 const FT& testval = cut_edge[i]->opposite()->vertex()->point().cartesian(cindex);
00238 if ( testval < low )
00239 ret_val.low = &facet;
00240 else
00241 ret_val.middle = &facet;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250 if ( (cut_edges_seen & 12) == 0 ) {
00251
00252
00253 const FT& testval = facet.halfedge()->vertex()->point().cartesian(cindex);
00254
00255 if ( testval > high ) {
00256 CGAL_assertion_msg( ret_val.low == 0,
00257 "Impossible: facet lies in low and high regions" );
00258 ret_val.middle = 0;
00259 ret_val.high = &facet;
00260 }
00261 } else if ( (cut_edges_seen & 12) == 12 ) {
00262
00263
00264 verr << "edge 2 = " << *(cut_edge[2]) << endl;
00265 verr << "edge 3 = " << *(cut_edge[3]) << endl;
00266 split_edge( poly, cut_edge[2], cindex, high );
00267 split_edge( poly, cut_edge[3], cindex, high );
00268 poly.split_facet(cut_edge[2],cut_edge[3]);
00269
00270 ret_val.middle = &*(cut_edge[2]->facet());
00271 ret_val.high = &*(cut_edge[3]->facet());
00272 } else {
00273
00274
00275 int i = ( (cut_edges_seen & 12) == 4 ? 2 : 3 );
00276 const FT& testval = cut_edge[i]->opposite()->vertex()->point().cartesian(cindex);
00277 if ( testval > high ) {
00278 CGAL_assertion_msg( ret_val.low == 0,
00279 "Impossible: facet lies in low and high regions" );
00280 ret_val.middle = 0;
00281 ret_val.high = &facet;
00282 }
00283 }
00284 return ret_val;
00285 }