DGtalTools  0.9.4
sdp2vol.cpp
1 
29 #include <iostream>
31 #include <fstream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/images/ImageContainerBySTLVector.h"
35 #include "DGtal/io/writers/GenericWriter.h"
36 
37 #include <boost/program_options/options_description.hpp>
38 #include <boost/program_options/parsers.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 
41 using namespace std;
42 using namespace DGtal;
43 
77 namespace po = boost::program_options;
79 
80 int main( int argc, char** argv )
81 {
83 
84  // parse command line ----------------------------------------------
85  po::options_description general_opt("Allowed options are");
86  general_opt.add_options()
87  ("help,h", "display this message")
88  ("input,i", po::value<std::string>(), "Sequence of 3d Discrete points (.sdp) " )
89  ("output,o", po::value<std::string>(), "Vol file (.vol, .longvol, .pgm3d) " )
90  ("foregroundVal,f", po::value<int>()->default_value(128), "value which will represent the foreground object in the resulting image (default 128)")
91  ("invertY", "Invert the Y axis (image flip in the y direction)")
92  ("backgroundVal,b", po::value<int>()->default_value(0), "value which will represent the background outside the object in the resulting image (default 0)")
93  ("domain,d", po::value<std::vector <int> >()->multitoken(), "customizes the domain of the resulting image xmin ymin zmin xmax ymax zmax (computed automatically by default) ");
94 
95  bool parseOK=true;
96  po::variables_map vm;
97  try{
98  po::store(po::parse_command_line(argc, argv, general_opt), vm);
99  }catch(const std::exception& ex){
100  parseOK=false;
101  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
102  }
103  po::notify(vm);
104  if( !parseOK || vm.count("help"))
105  {
106  std::cout << "Convert digital set of points into a volumic file.\n";
107  std::cout << "Usage: " << argv[0] << " [input] [output]\n"
108  << general_opt << "\n";
109  std::cout << "Example:\n"
110  << "sdp2vol -i volumePoints.sdp -o volume.vol -d 0 0 0 10 10 10 \n";
111  return 0;
112  }
113  if(! vm.count("input") ||! vm.count("output") )
114  {
115  trace.error() << " Input/ output filename and domain are needed to be defined" << endl;
116  return 0;
117  }
118 
119 
120 
121  string inputSDP = vm["input"].as<std::string>();
122  string outputFilename = vm["output"].as<std::string>();
123  int foregroundVal = vm["foregroundVal"].as<int>();
124  int backgroundVal = vm["backgroundVal"].as<int>();
125 
126 
127  vector<unsigned int> vPos;
128  vPos.push_back(0);
129  vPos.push_back(1);
130  vPos.push_back(2);
131  trace.info() << "Reading input SDP file: " << inputSDP ;
132  std::vector<Z3i::Point> vectPoints= PointListReader<Z3i::Point>::getPointsFromFile(inputSDP, vPos);
133  trace.info() << " [done] " << std::endl ;
134 
135  Z3i::Point ptLower;
136  Z3i::Point ptUpper;
137 
138 
139  struct BBCompPoints
140  {
141  explicit BBCompPoints(unsigned int d): myDim(d){};
142  bool operator() (const Z3i::Point &p1, const Z3i::Point &p2){return p1[myDim]<p2[myDim];};
143  unsigned int myDim;
144  };
145  if(!vm.count("domain"))
146  {
147  unsigned int marge = 1;
148  for(unsigned int i=0; i< 4; i++)
149  {
150  BBCompPoints cmp_points(i);
151  ptUpper[i] = (*(std::max_element(vectPoints.begin(), vectPoints.end(), cmp_points)))[i]+marge;
152  ptLower[i] = (*(std::min_element(vectPoints.begin(), vectPoints.end(), cmp_points)))[i]-marge;
153  }
154  }
155  else
156  {
157  std::vector<int> domainCoords= vm["domain"].as<std::vector <int> >();
158  ptLower = Z3i::Point(domainCoords[3],domainCoords[4], domainCoords[5]);
159  ptUpper = Z3i::Point(domainCoords[0],domainCoords[1], domainCoords[2]);
160  }
161 
162  Image3D::Domain imageDomain(ptLower, ptUpper);
163  trace.info() << "domain: "<<imageDomain<<std::endl;
164  Image3D imageResult(imageDomain);
165  for(Image3D::Domain::ConstIterator iter = imageResult.domain().begin();
166  iter!= imageResult.domain().end();
167  iter++)
168  {
169  imageResult.setValue(*iter, backgroundVal);
170  }
171 
172  for(unsigned int i=0; i<vectPoints.size(); i++)
173  {
174  if(vm.count("invertY"))
175  {
176  vectPoints[i][1]=ptUpper[1]-vectPoints[i][1];
177  }
178  if(imageResult.domain().isInside(vectPoints[i]))
179  {
180  imageResult.setValue(vectPoints[i], foregroundVal);
181  }
182  else
183  {
184  trace.warning() << "point " << vectPoints[i] << " outside the domain (ignored in the resulting volumic image)" << std::endl;
185  }
186  }
187  trace.info()<< "Exporting resulting volumic image ... ";
188  GenericWriter<Image3D>::exportFile(outputFilename, imageResult);
189  trace.info() << " [done]"<<std::endl;
190  return 0;
191 
192 }
193 
194 
195 
196 
STL namespace.
Trace trace(traceWriterTerm)
std::ostream & warning()
std::ostream & info()
std::vector< Value >::const_iterator ConstIterator
std::ostream & error()