DGtalTools  0.9.4
vol2obj.cpp
1 
27 #include <iostream>
29 #include "DGtal/base/Common.h"
30 #include "DGtal/helpers/StdDefs.h"
31 #include "DGtal/io/readers/GenericReader.h"
32 #include "DGtal/io/boards/Board3D.h"
33 #include "DGtal/io/DrawWithDisplay3DModifier.h"
34 #include "DGtal/io/readers/PointListReader.h"
35 
36 #include "DGtal/images/ImageSelector.h"
37 
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/parsers.hpp>
40 #include <boost/program_options/variables_map.hpp>
41 
42 using namespace std;
43 using namespace DGtal;
44 using namespace Z3i;
45 
78 namespace po = boost::program_options;
80 
81 int main( int argc, char** argv )
82 {
83  // parse command line ----------------------------------------------
84  po::options_description general_opt("Allowed options are: ");
85  general_opt.add_options()( "help,h", "display this message" )
86  ("input,i", po::value<std::string>(), "vol file (.vol, .longvol .p3d, .pgm3d and if WITH_ITK is selected: dicom, dcm, mha, mhd) or sdp (sequence of discrete points)." )
87  ("output,o", po::value<std::string>(),
88  "Output OBJ filename (each grid point with value between [ @a thresholdMin, @a thresholdMax ] is exported as a unit cube ) " )
89  ( "thresholdMin,m", po::value<int>()->default_value( 0 ),"threshold min to define binary shape" )
90  ("thresholdMax,M", po::value<int>()->default_value( 255 ),"threshold max to define binary shape" )
91  ("rescaleInputMin", po::value<DGtal::int64_t>()->default_value( 0 ), "min value used to rescale the input intensity (to avoid basic cast into 8 bits image)." )
92  ( "rescaleInputMax",po::value<DGtal::int64_t>()->default_value( 255 ),
93  "max value used to rescale the input intensity (to avoid "
94  "basic cast into 8 bits image)." );
95 
96  bool parseOK=true;
97  po::variables_map vm;
98  try
99  {
100  po::store(po::parse_command_line(argc, argv, general_opt), vm);
101  } catch(const std::exception& ex)
102  {
103  parseOK=false;
104  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
105  }
106  po::notify(vm);
107  if( !parseOK || vm.count("help")||argc<=1)
108  {
109  std::cout << "Usage: " << argv[0] << " [input-file]\n"
110  << "Convert a volume file into OBJ format\n"
111  << general_opt << "\n";
112  return 0;
113  }
114 
115  if(! vm.count("input"))
116  {
117  trace.error() << " The input filename was defined" << endl;
118  return 0;
119  }
120  if(! vm.count("output"))
121  {
122  trace.error() << " The output filename was defined" << endl;
123  return 0;
124  }
125 
126  string inputFilename = vm["input"].as<std::string>();
127  string outputFilename = vm["output"].as<std::string>();
128  int thresholdMin = vm["thresholdMin"].as<int>();
129  int thresholdMax = vm["thresholdMax"].as<int>();
130 
131  Board3D<> board;
132 
134  string extension = inputFilename.substr(inputFilename.find_last_of(".") + 1);
135 
136  if(extension!="sdp")
137  {
138  DGtal::int64_t rescaleInputMin = vm["rescaleInputMin"].as<DGtal::int64_t>();
139  DGtal::int64_t rescaleInputMax = vm["rescaleInputMax"].as<DGtal::int64_t>();
140 
142  Image image = GenericReader< Image >::importWithValueFunctor( inputFilename,RescalFCT(rescaleInputMin,
143  rescaleInputMax,
144  0, 255) );
145  trace.info() << "Image loaded: "<<image<< std::endl;
146  Domain domain = image.domain();
147  for(Domain::ConstIterator it = domain.begin(), itend=domain.end(); it!=itend; ++it){
148  unsigned char val= image( (*it) );
149  if(val<=thresholdMax && val >=thresholdMin){
150  board << *it;
151  }
152  }
153  }
154  else
155  if(extension=="sdp")
156  {
157  vector<Z3i::Point> vectVoxels = PointListReader<Z3i::Point>::getPointsFromFile(inputFilename);
158  for(unsigned int i=0;i< vectVoxels.size(); i++){
159  board << vectVoxels.at(i);
160  }
161  }
162 
163 
164  board.saveOBJ(outputFilename);
165  return 0;
166 }
STL namespace.
Trace trace(traceWriterTerm)
std::ostream & info()
const Domain & domain() const
std::ostream & error()
boost::int64_t int64_t
typename Self::Domain Domain