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

transfer-voxel-surface.cc

00001 
00004 #define OUTPUT_INVENTOR 1
00005 
00006 
00007 #include <iostream>
00008 #include <fstream>
00009 #include <iterator>
00010 #include <vector>
00011 #include <utility>
00012 #include <list>
00013 #include <map>
00014 
00015 #include "poly.h"
00016 #include "ObjFile.h"
00017 #include "Hexahedron_3.h"
00018 #include "set_point.h"
00019 #include "Box_3.h"
00020 #include "hexa_poly_intersect.h"
00021 #include "poly_intersect.h"
00022 
00023 #if OUTPUT_INVENTOR
00024 #   include <Inventor/nodes/SoSeparator.h>
00025 #   include "inventor/SceneViewer.h"
00026 #   include "poly_iv.h"
00027 #endif
00028 
00029 #include <CEP/intersection/Box_d_Box_d.h>
00030 #include <CEP/intersection/Box_d_Box_d_batch.h>
00031 #include <boost/function_output_iterator.hpp>
00032 
00033 
00034 using std::cout;
00035 using std::endl;
00036 
00037 
00038 typedef CGAL::Point_3<Exact_rep>           Point;
00039 typedef CGAL::Plane_3<Exact_rep>           Plane;
00040 typedef Polyhedron::Facet                  Facet;
00041 typedef std::istream_iterator<double>      InDoubleIter;
00042 
00043 typedef std::pair<IndexBox,IndexBox>       BoxPair;
00044 typedef std::vector< BoxPair >::iterator   BoxPair_iterator;
00045 
00046 
00047 
00048 class BoxArray 
00049 {
00050 public:
00051     void set( const IndexBox& box, Facet* fp )
00052     {
00053         array.resize(box.index()+1);
00054         array[box.index()] = new FacetPayload(fp);
00055     }
00056     void set( const IndexBox& box, Hexahedron_3* hp )
00057     {
00058         array.resize(box.index()+1);
00059         array[box.index()] = new HexahedronPayload(hp);
00060     }
00061 
00062     Facet* get_facet( const IndexBox& box ) const
00063     {
00064         return array[box.index()]->get_facet();
00065     }
00066     Hexahedron_3* get_hexahedron( const IndexBox& box ) const
00067     {
00068         return array[box.index()]->get_hexahedron();
00069     }
00070         
00071 
00072 private:
00073 
00074     class Payload
00075     {
00076     public:
00077         virtual Polyhedron::Facet* get_facet() = 0;
00078         virtual Hexahedron_3* get_hexahedron() = 0;
00079     };
00080 
00081     class FacetPayload : public Payload
00082     {
00083     public:
00084         FacetPayload( Facet* fp ) { facet = fp; }
00085         
00086         Polyhedron::Facet* get_facet() { return facet; }
00087         Hexahedron_3* get_hexahedron() { return 0; }
00088     private:
00089         Polyhedron::Facet* facet;
00090     };
00091 
00092 
00093     class HexahedronPayload : public Payload
00094     {
00095     public:
00096         HexahedronPayload( Hexahedron_3* hp ) { hexa = hp; }
00097         
00098         Polyhedron::Facet* get_facet() { return 0; }
00099         Hexahedron_3* get_hexahedron() { return hexa; }
00100     private:
00101         Hexahedron_3* hexa;
00102     };
00103 
00104     std::vector<Payload*> array;
00105 };
00106 
00107 
00108 BoxArray box_array;
00109 
00110 
00111 static inline Facet*
00112 get_facet( const BoxPair& bp )
00113 {
00114     Facet* fp = box_array.get_facet(bp.first);
00115     if ( fp == 0 )
00116         fp = box_array.get_facet(bp.second);
00117     return fp;
00118 }
00119 
00120 static inline Hexahedron_3*
00121 get_hexahedron( const BoxPair& bp )
00122 {
00123     Hexahedron_3* hp = box_array.get_hexahedron(bp.first);
00124     if ( hp == 0 )
00125         hp = box_array.get_hexahedron(bp.second);
00126     return hp;
00127 }
00128 
00129 
00130 
00131 // Is the BoxPair <Hexahedron,Facet> ?
00132 // i.e. does the first box contain a hexahedron?
00133 static inline bool
00134 is_hexa_facet( const BoxPair& bp )
00135 {
00136     return box_array.get_hexahedron(bp.first) != 0;
00137 }
00138 
00139 
00140 
00141 // Find facet index of a BoxPair
00142 static inline int 
00143 facet_index( const BoxPair& bp )
00144 {
00145     if ( is_hexa_facet(bp) ) 
00146         return bp.second.index();
00147     return bp.first.index();
00148 }
00149 
00150 
00151 // Compare by facet index
00152 static bool 
00153 compare_boxpair( const BoxPair& p, const BoxPair& q )
00154 {
00155     return facet_index(p) < facet_index(q);
00156 }
00157 
00158 
00159 static inline Hexahedron_3* 
00160 new_hexahedron( InDoubleIter& in )
00161 {
00162     Point vertices[8];
00163 
00164     for( int i = 0; i < 8; ++i ) {
00165         double x = *in++;
00166         double y = *in++;
00167         double z = *in++;
00168         set_point( vertices[i], x,y,z );
00169     }
00170 
00171     return new Hexahedron_3(vertices);
00172 }
00173 
00174 
00175 // Return true if facet is completely contained in some hexahedron
00176 static bool 
00177 is_inside_some_box( const Facet& facet, 
00178                     const std::list<Hexahedron_3*>& hexa_list )
00179 {
00180     std::list<Hexahedron_3*>::const_iterator hexa_iter;
00181     for( hexa_iter = hexa_list.begin(); hexa_iter != hexa_list.end(); ++hexa_iter )
00182     {
00183         if ( is_inside( facet, **hexa_iter ))
00184             return true;
00185     }
00186     return false;
00187 }
00188 
00189 
00190 static void usage( char* argv0 )
00191 {
00192     char* s = strrchr( argv0, '/' );
00193     if ( s ) argv0 = s+1;
00194 
00195     std::cerr << "usage: " << argv0 << " [options] voxels in_mesh" 
00196               << std::endl;
00197 }
00198 
00199 
00200 int main( int ac, char* av[] )
00201 {
00202     if ( ac != 3 ) {
00203         usage(av[0]);
00204         return 1;
00205     }
00206     char* filename_voxels = av[1];
00207     char* filename_inmesh = av[2];
00208 
00209     //CORE::setIncrementalEvalFlag(false);
00210     CGAL::set_pretty_mode( std::cout );
00211     //cout.precision(6);
00212     //CORE::setPositionalFormat(std::cout);
00213 
00214     // Load in list of voxels, create a bounding box for each,
00215     // and store the boxes in list hbox_list.
00216     //
00217     cout << "Reading voxel list from " << filename_voxels << flush;
00218 
00219     std::vector<IndexBox> hbox_list;
00220 
00221     std::ifstream instream( filename_voxels );
00222     InDoubleIter in( instream );
00223     InDoubleIter in_end;
00224 
00225     while ( in != in_end ) {
00226         Hexahedron_3* hp = new_hexahedron(in);
00227         IndexBox box( make_bounding_box(*hp) );
00228 
00229         hbox_list.push_back( box );
00230         box_array.set(box,hp);
00231     }
00232 
00233     cout << ":  " << hbox_list.size() << " voxels." << endl;
00234 
00235 
00236     // Load in mesh, create a bounding box for each facet,
00237     // and store the boxes in list fbox_list.
00238     //
00239     cout << "Reading surface from " << filename_inmesh << endl;
00240 
00241     std::vector<IndexBox> fbox_list;
00242 
00243     Polyhedron mesh;
00244     MNI::Object_file objfile( filename_inmesh );
00245     objfile.insert( 0, true, mesh );
00246 
00247     for( Polyhedron::Facet_iterator f = mesh.facets_begin();
00248          f != mesh.facets_end(); ++f )
00249     {
00250         f->label = 0;
00251         IndexBox box = make_bounding_box(*f);
00252 
00253         fbox_list.push_back( box );
00254         box_array.set(box,&*f);
00255     }
00256 
00257     std::cout << "    vertices: " << mesh.size_of_vertices() << std::endl
00258               << "    half-edges: " << mesh.size_of_halfedges() << std::endl
00259               << "    facets: " << mesh.size_of_facets() << std::endl;
00260 
00261 
00262 
00263     // Find the set of intersecting box pairs
00264     //
00265     std::vector< BoxPair > boxpair_list;
00266 
00267     cout << "Computing bounding-box intersections ... " << flush;
00268     
00269     CEP::intersection::Box_d_Box_d_batch( hbox_list.begin(), hbox_list.end(),
00270                                           fbox_list.begin(), fbox_list.end(),
00271                                           std::back_inserter( boxpair_list ));
00272 
00273     cout << "done" << endl;
00274 
00275     cout << "    " << boxpair_list.size() << " voxel/facet intersections." 
00276          << endl;
00277 
00278 
00279     // Process the intersected facets
00280     //
00281     cout << "Splitting and labelling facets ... " << flush;
00282 
00283     // Sort boxpair list with respect to facet index
00284     // Then process pairs in batches that involve same
00285     // facet.
00286     std::sort( boxpair_list.begin(), boxpair_list.end(),
00287                compare_boxpair );
00288 
00289     int red_count = 0;
00290     BoxPair_iterator bp_iter = boxpair_list.begin();
00291     while ( bp_iter != boxpair_list.end() ) {
00292         Polyhedron::Facet* fp = get_facet(*bp_iter);
00293 
00294         // Populate the list of hexahedron intersecting this facet
00295         int f_index = facet_index(*bp_iter);
00296         std::list<Hexahedron_3*> hexa_list;
00297         hexa_list.push_back( get_hexahedron(*bp_iter++) );
00298         while ( bp_iter != boxpair_list.end() &&
00299                 facet_index(*bp_iter) == f_index )
00300         {
00301             hexa_list.push_back( get_hexahedron(*bp_iter++) );
00302         }
00303 #if 1
00304         cout << "Facet: " << endl
00305              << *fp << endl
00306              << "    intersected by " << hexa_list.size() << " voxels." 
00307              << endl;
00308 #endif  
00309 
00310         // Slice up the facet using the hexahedra.
00311         // Store list of resulting facets for labelling purposes.
00312         std::list<Polyhedron::Facet*> cut_list;
00313         slice_poly_with_hexa( mesh, *fp,
00314                               hexa_list.begin(), hexa_list.end(),
00315                               std::back_inserter(cut_list) );
00316 
00317         // label the facets in cut_list...
00318         std::list<Polyhedron::Facet*>::iterator fp_iter;
00319         for( fp_iter = cut_list.begin(); fp_iter != cut_list.end(); ++fp_iter )
00320         {
00321             if ( is_inside_some_box( **fp_iter, hexa_list )) {
00322                 //cout << "Inside box." << endl;
00323                 ++red_count;
00324                 (*fp_iter)->label = 1;
00325             }
00326         }
00327     }
00328     cout << "done" << endl;
00329     cout << red_count << " red facet" << (red_count == 1 ? "" : "s") << endl;
00330 
00331     cout << "    vertices: " << mesh.size_of_vertices() << std::endl
00332          << "    half-edges: " << mesh.size_of_halfedges() << std::endl
00333          << "    facets: " << mesh.size_of_facets() << std::endl;
00334 
00335     red_count = 0;
00336     Polyhedron::Facet_const_iterator f_iter = mesh.facets_begin();
00337     while( f_iter != mesh.facets_end() ) {
00338         if ( f_iter->label )
00339             ++red_count;
00340         ++f_iter;
00341     }
00342 
00343     cout << red_count << " red facet" << (red_count == 1 ? "" : "s") << endl;
00344         
00345 
00346 
00347 #if OUTPUT_INVENTOR
00348     viewer_init( "transfer-voxel-surface" );
00349     SoSeparator* root = new SoSeparator;
00350     root->addChild( polyhedron_to_iv(mesh) );
00351     viewer_main(root);
00352 #endif
00353 
00354     return 0;
00355 }
00356 
00357 
00358 #ifdef _CORE_H
00359 // Workaround for a weird bug in CORE 1.4-beta.
00360 // Without this code, Expr output has only one signficant digit.
00361 void switchon()
00362 {
00363     CORE::Expr e(3.3);
00364     e.approx();
00365 }
00366 #endif
00367 
00368 

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