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

Polyhedron_Interval.cc

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 //typedef R::FT FT;
00021 typedef CGAL::Plane_3<R> Plane;
00022 
00023 
00024 
00025 /* Check whether the real interval (tail,tip] crosses the boundary.
00026  *
00027  * Return 0 if tail < boundary <= tip
00028  * Return 1 if tip <= boundary < tail
00029  * else return -1;
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 /* Check whether the real interval (tail,tip] crosses the boundary.
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 // FIXME: will truncate silently (or give compile error?) if
00053 // FT != RT.
00054 inline Plane
00055 coordinate_plane( int cindex, const FT& boundary )
00056 {
00057     CGAL_precondition( 0 <= cindex );
00058     CGAL_precondition( cindex < 3 );
00059 
00060     // attempt to catch truncation troubles.
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 /* Split an edge at the given boundary value of the cindex'th coordinate.
00081  */
00082 static Halfedge_handle
00083 split_edge( Polyhedron& poly, 
00084             Halfedge_handle h,
00085             int cindex, 
00086             const FT& boundary )
00087 {
00088     /* Method: split a vertex, then set coordinates for the newly
00089      * constructed point in one copy.
00090      *
00091      * The vertex to split is denoted by two half edges (e1,e2).
00092      * The newly inserted edge will border e1.facet() and e2.facet().
00093      * We need this to be the same as h.facet() and h.opposite().facet(),
00094      * so we call split_vertex on h and h.opposite().prev().
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     // If the outcome of the intersection is not a point (and not empty)
00111     // then it must be a line; i.e. the halfedge lies on the plane.
00112     // We don't want to do any cutting in this case.
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     // Fixme: avoid prev()
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     /* Method: scan halfedges around facet, looking the (up to four)
00154      * halfedges that intersect the low and high planes.
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     // Decide whether to split the facet.  Categorize the 1, 2, or 3
00193     // resulting facets as "low", "middle", or "high".
00194     //
00195     // First we inspect the low boundary.  If the facet straddles it, then
00196     // we split into two, setting the "low" and "middle" results.  (The middle
00197     // facet may be split again if it straddles the high boundary.)  If
00198     // the facet does not straddle the low boundary, it is not split.  We
00199     // categorize it as low or middle.  Again, the middle facet may be updated
00200     // when looking at the high boundary.
00201     //
00202     FacetTriple ret_val = {0,0,0};
00203 
00204 
00205     if ( (cut_edges_seen & 3) == 0 ) {
00206         // Low boundary is not crossed; category is determined by any
00207         // vertex of the facet.
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         // Facet straddles low boundary.  Split facet, setting both low
00216         // and middle pointers.  
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         // If cut_edge[2] is the same as cut_edge[0], need to update it.
00228         if ( (cut_edges_seen & 4) && 
00229              cut_edge[2] == cut_edge[0] )
00230             cut_edge[2] = new_cutedge0;
00231     } else {
00232         // Low boundary is crossed by one (half-open) edge.  Category is
00233         // determined by the location of the tail vertex of this edge.
00234         // Low boundary is not crossed; category is determined by any
00235         // vertex of the facet.
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     // Proceed similarly when checking the high boundary.  The low 
00246     // facet has been set correctly by the above code.  The middle and
00247     // high facets only need updating if some part of the facet lies
00248     // above the interval.
00249 
00250     if ( (cut_edges_seen & 12) == 0 ) {
00251         // High boundary is not crossed; category is determined by any
00252         // vertex of the facet.
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         // Facet straddles high boundary.  Split facet, setting both middle
00263         // and high pointers.  
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         // High boundary is crossed by one (half-open) edge.  Category is
00274         // determined by the location of the tail vertex of this edge.
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 }

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