DGtalTools  0.9.2
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 
70 namespace po = boost::program_options;
72 
73 int main( int argc, char** argv )
74 {
75  // parse command line ----------------------------------------------
76  po::options_description general_opt("Allowed options are: ");
77  general_opt.add_options()
78  ("help,h", "display this message")
79  ("input,i", po::value<std::string>(), "vol file (.vol) , pgm3d (.p3d or .pgm3d, pgm (with 3 dims)) file or sdp (sequence of discrete points)" )
80  ("output,o", po::value<std::string>(), "Output OBJ filename" )
81  ("thresholdMin,m", po::value<int>()->default_value(0), "threshold min to define binary shape" )
82  ("thresholdMax,M", po::value<int>()->default_value(255), "threshold max to define binary shape" );
83 
84  bool parseOK=true;
85  po::variables_map vm;
86  try
87  {
88  po::store(po::parse_command_line(argc, argv, general_opt), vm);
89  } catch(const std::exception& ex)
90  {
91  parseOK=false;
92  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
93  }
94  po::notify(vm);
95  if( !parseOK || vm.count("help")||argc<=1)
96  {
97  std::cout << "Usage: " << argv[0] << " [input-file]\n"
98  << "Convert a volume file into OBJ format\n"
99  << general_opt << "\n";
100  return 0;
101  }
102 
103  if(! vm.count("input"))
104  {
105  trace.error() << " The input filename was defined" << endl;
106  return 0;
107  }
108  if(! vm.count("output"))
109  {
110  trace.error() << " The output filename was defined" << endl;
111  return 0;
112  }
113 
114  string inputFilename = vm["input"].as<std::string>();
115  string outputFilename = vm["output"].as<std::string>();
116  int thresholdMin = vm["thresholdMin"].as<int>();
117  int thresholdMax = vm["thresholdMax"].as<int>();
118 
119  Board3D<> board;
120 
121  typedef ImageSelector<Domain, unsigned char>::Type Image;
122  string extension = inputFilename.substr(inputFilename.find_last_of(".") + 1);
123  if(extension!="vol" && extension != "p3d" && extension != "pgm3D" &&
124  extension != "pgm3d" && extension != "sdp" && extension != "pgm")
125  {
126  trace.info() << "File extension not recognized: "<< extension << std::endl;
127  return 0;
128  }
129 
130  if(extension=="vol" || extension=="pgm3d" || extension=="pgm3D")
131  {
132  Image image = GenericReader<Image>::import (inputFilename );
133  trace.info() << "Image loaded: "<<image<< std::endl;
134  Domain domain = image.domain();
135  for(Domain::ConstIterator it = domain.begin(), itend=domain.end(); it!=itend; ++it){
136  unsigned char val= image( (*it) );
137  if(val<=thresholdMax && val >=thresholdMin){
138  board << *it;
139  }
140  }
141  }
142  else
143  if(extension=="sdp")
144  {
145  vector<Z3i::Point> vectVoxels = PointListReader<Z3i::Point>::getPointsFromFile(inputFilename);
146  for(unsigned int i=0;i< vectVoxels.size(); i++){
147  board << vectVoxels.at(i);
148  }
149  }
150 
151 
152  board.saveOBJ(outputFilename);
153  return 0;
154 }
STL namespace.