00001
00004 #define OUTPUT_INVENTOR 1
00005
00006
00007 #include <iostream>
00008
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 "set_point.h"
00018 #include "Box_3.h"
00019 #include "poly_intersect.h"
00020 #include "extract-voxels.h"
00021
00022 #if OUTPUT_INVENTOR
00023 # include <Inventor/SoDB.h>
00024 # include <Inventor/actions/SoSearchAction.h>
00025 # include <Inventor/actions/SoWriteAction.h>
00026 # include <Inventor/nodes/SoSeparator.h>
00027 # include "inventor/SceneViewer.h"
00028 # include "poly_iv.h"
00029 #endif
00030
00031 #include <CEP/intersection/Box_d_Box_d.h>
00032 #include <CEP/intersection/Box_d_Box_d_batch.h>
00033 #include <boost/function_output_iterator.hpp>
00034 #include <cutting/Polyhedron_Box_3.h>
00035
00036 #include <CGAL/IO/Verbose_ostream.h>
00037
00038 extern "C" {
00039 # include "ParseArgv.h"
00040 }
00041
00042
00043
00044 char* output_filename = NULL;
00045
00046 ArgvInfo argTable[] = {
00047 { "-out_file", ARGV_STRING, (char*)1, (char*)&output_filename,
00048 "write output to file instead of displaying onscreen" },
00049 { NULL, ARGV_END, NULL, NULL, NULL }
00050 };
00051
00052
00053
00054 using std::cout;
00055 using std::endl;
00056
00057 static CGAL::Verbose_ostream verr(false,cout);
00058
00059
00060 typedef CGAL::Point_3<Exact_rep> Point;
00061 typedef CGAL::Plane_3<Exact_rep> Plane;
00062 typedef Polyhedron::Facet Facet;
00063
00064 typedef std::pair<DataIndexBox,DataIndexBox> BoxPair;
00065 typedef std::vector< BoxPair >::iterator BoxPair_iterator;
00066
00067
00068
00069
00070
00071
00072
00073 int first_facet_index = 0;
00074
00075
00076 static inline bool
00077 is_facet_first( const BoxPair& bp )
00078 {
00079 return bp.first.index() >= first_facet_index;
00080 }
00081
00082
00083
00084 static inline int
00085 facet_index( const BoxPair& bp )
00086 {
00087 if ( is_facet_first(bp) )
00088 return bp.first.index();
00089 return bp.second.index();
00090 }
00091
00092
00093 static inline Facet*
00094 get_facet( const BoxPair& bp )
00095 {
00096 if ( is_facet_first(bp) )
00097 return (Facet*)bp.first.data.ptr;
00098 return (Facet*)bp.second.data.ptr;
00099 }
00100
00101
00102 static inline int
00103 get_label( const BoxPair& bp )
00104 {
00105 if ( is_facet_first(bp) )
00106 return bp.second.data.dat;
00107 return bp.first.data.dat;
00108 }
00109
00110
00111 static inline const DataIndexBox*
00112 get_box( const BoxPair& bp )
00113 {
00114 if ( is_facet_first(bp) )
00115 return &bp.second;
00116 return &bp.first;
00117 }
00118
00119
00120
00121 static bool
00122 compare_boxpair( const BoxPair& p, const BoxPair& q )
00123 {
00124 return facet_index(p) < facet_index(q);
00125 }
00126
00127
00128
00129
00130
00131
00132 static void
00133 process( Polyhedron& poly, std::vector<Facet*>& facet_list,
00134 const DataIndexBox& box )
00135 {
00136 verr << "Processing with box " << box << endl;
00137 std::vector<Facet*> newlist;
00138
00139 std::vector<Facet*>::iterator f_iter = facet_list.begin();
00140 for ( ; f_iter != facet_list.end(); ++f_iter ) {
00141 verr << "Check facet: " << endl
00142 << **f_iter << endl;
00143 Facet* interior = cutting( poly, **f_iter, box,
00144 std::back_inserter(newlist) );
00145 if ( interior != 0 ) {
00146 interior->label = box.data.dat;
00147 verr << "Labelled!" << endl;
00148 }
00149 }
00150
00151 facet_list = newlist;
00152 }
00153
00154
00155 static void usage( char* argv0 )
00156 {
00157 char* s = strrchr( argv0, '/' );
00158 if ( s ) argv0 = s+1;
00159
00160 std::cerr << "usage: " << argv0 << " [options] in_label in_mesh"
00161 << std::endl;
00162 }
00163
00164
00165 int main( int ac, char* av[] )
00166 {
00167 if ( ParseArgv( &ac, av, argTable, 0 ) || ac != 3 ) {
00168 usage( av[0] );
00169 return 1;
00170 }
00171 char* filename_inlabel = av[1];
00172 char* filename_inmesh = av[2];
00173
00174 CGAL::set_pretty_mode( std::cout );
00175
00176
00177
00178 cout << "Scanning " << filename_inlabel << " for label voxels ..." << endl;
00179 std::vector<DataIndexBox> vbox_list;
00180 insert_voxels( filename_inlabel, vbox_list );
00181 cout << vbox_list.size() << " voxels." << endl;
00182
00183 first_facet_index = vbox_list.size();
00184
00185
00186
00187
00188
00189 cout << "Reading surface from " << filename_inmesh << endl;
00190
00191 std::vector<DataIndexBox> fbox_list;
00192
00193 Polyhedron mesh;
00194 MNI::Object_file objfile( filename_inmesh );
00195 objfile.insert( 0, true, mesh );
00196
00197 for( Polyhedron::Facet_iterator f = mesh.facets_begin();
00198 f != mesh.facets_end(); ++f )
00199 {
00200 f->label = 0;
00201 DataIndexBox box( make_bounding_box(*f), &*f );
00202 fbox_list.push_back( box );
00203 }
00204 CGAL_assertion( first_facet_index == fbox_list[0].index() );
00205
00206 std::cout << " vertices: " << mesh.size_of_vertices() << std::endl
00207 << " half-edges: " << mesh.size_of_halfedges() << std::endl
00208 << " facets: " << mesh.size_of_facets() << std::endl;
00209
00210
00211
00212
00213 std::vector< BoxPair > boxpair_list;
00214
00215 cout << "Computing bounding-box intersections ... " << flush;
00216
00217 CEP::intersection::Box_d_Box_d_batch( vbox_list.begin(), vbox_list.end(),
00218 fbox_list.begin(), fbox_list.end(),
00219 std::back_inserter( boxpair_list ));
00220
00221 cout << "done" << endl;
00222
00223 cout << " " << boxpair_list.size() << " voxel/facet intersections."
00224 << endl;
00225
00226
00227
00228
00229 cout << "Splitting and labelling facets ... " << flush;
00230
00231
00232
00233
00234 std::sort( boxpair_list.begin(), boxpair_list.end(),
00235 compare_boxpair );
00236
00237 int pairs_remaining = boxpair_list.size();
00238 int f_index = -1;
00239 std::vector<Facet*> subfacet_list;
00240
00241 BoxPair_iterator bp_iter = boxpair_list.begin();
00242 for ( ; bp_iter != boxpair_list.end(); ++bp_iter )
00243 {
00244
00245 if ( facet_index(*bp_iter) != f_index ) {
00246 verr << "*** New facet (index " << f_index << ")" << endl
00247 << *get_facet(*bp_iter) << endl;
00248 f_index = facet_index(*bp_iter);
00249 subfacet_list.clear();
00250 subfacet_list.push_back( get_facet(*bp_iter) );
00251 }
00252
00253 process( mesh, subfacet_list, *get_box(*bp_iter) );
00254 }
00255 cout << "done" << endl;
00256
00257 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 int red_count = 0;
00262 Polyhedron::Facet_const_iterator f_iter = mesh.facets_begin();
00263 while( f_iter != mesh.facets_end() ) {
00264 if ( f_iter->label )
00265 ++red_count;
00266 ++f_iter;
00267 }
00268
00269 cout << red_count << " coloured facet" << (red_count == 1 ? "" : "s") << endl;
00270
00271
00272
00273 #if OUTPUT_INVENTOR
00274 if ( output_filename ) {
00275 SoDB::init();
00276
00277
00278 SoWriteAction wa;
00279 SoOutput* out = wa.getOutput();
00280 if ( out->openFile(output_filename) == 0 ) {
00281 cerr << "cannot open file: " << output_filename << endl;
00282 return 1;
00283 }
00284
00285 SoSeparator* root = new SoSeparator;
00286 root->ref();
00287 root->addChild( polyhedron_to_iv(mesh) );
00288 wa.apply(root);
00289 } else {
00290
00291 viewer_init( "transfer-voxel-surface" );
00292 SoSeparator* root = new SoSeparator;
00293 root->addChild( polyhedron_to_iv(mesh) );
00294 viewer_main(root);
00295 }
00296 #endif
00297
00298 return 0;
00299 }
00300
00301
00302 #ifdef _CORE_H
00303
00304
00305 void switchon()
00306 {
00307 CORE::Expr e(3.3);
00308 e.approx();
00309 }
00310 #endif
00311
00312