DGtalTools  1.2.0
sdp2vol.cpp
1 
30 #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 "CLI11.hpp"
38 
39 using namespace std;
40 using namespace DGtal;
41 
79 int main( int argc, char** argv )
80 {
81  typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3D;
82 
83 // parse command line using CLI ----------------------------------------------
84  CLI::App app;
85  std::string inputSDP;
86  std::string outputFileName {"result.vol"};
87  DGtal::int64_t rescaleInputMin {0};
88  DGtal::int64_t rescaleInputMax {255};
89 
90  int foregroundVal {128};
91  int backgroundVal {0};
92  bool invertY {false};
93  std::vector<int> domainCoords;
94 
95  app.description("Convert digital set of points into a volumic file.\n Example:\n sdp2vol volumePoints.sdp volume.vol -d 0 0 0 10 10 10 \n");
96  app.add_option("-i,--input,1", inputSDP, "Sequence of 3d Discrete points (.sdp)." )
97  ->required()
98  ->check(CLI::ExistingFile);
99  app.add_option("-o,--output,2", outputFileName, "Vol file (.vol, .longvol, .pgm3d) ");
100  app.add_option("-f,--foregroundVal", foregroundVal, "value which will represent the foreground object in the resulting image (default 128)");
101  app.add_option("-b,--backgroundVal", backgroundVal, "value which will represent the background outside the object in the resulting image (default 0)");
102  app.add_option("-d,--domain", domainCoords, "customizes the domain of the resulting image xmin ymin zmin xmax ymax zmax (computed automatically by default)")
103  ->expected(6);
104 
105  app.add_flag("--invertY", invertY, "Invert the Y axis (image flip in the y direction)");
106 
107 
108  app.get_formatter()->column_width(40);
109  CLI11_PARSE(app, argc, argv);
110  // END parse command line using CLI ----------------------------------------------
111 
112  vector<unsigned int> vPos;
113  vPos.push_back(0);
114  vPos.push_back(1);
115  vPos.push_back(2);
116  trace.info() << "Reading input SDP file: " << inputSDP ;
117  std::vector<Z3i::Point> vectPoints= PointListReader<Z3i::Point>::getPointsFromFile(inputSDP, vPos);
118  trace.info() << " [done] " << std::endl ;
119 
120  Z3i::Point ptLower;
121  Z3i::Point ptUpper;
122 
123  struct BBCompPoints
124  {
125  explicit BBCompPoints(unsigned int d): myDim(d){};
126  bool operator() (const Z3i::Point &p1, const Z3i::Point &p2){return p1[myDim]<p2[myDim];};
127  unsigned int myDim;
128  };
129  if(domainCoords.size() != 6 )
130  {
131  unsigned int marge = 1;
132  for(unsigned int i=0; i< 4; i++)
133  {
134  BBCompPoints cmp_points(i);
135  ptUpper[i] = (*(std::max_element(vectPoints.begin(), vectPoints.end(), cmp_points)))[i]+marge;
136  ptLower[i] = (*(std::min_element(vectPoints.begin(), vectPoints.end(), cmp_points)))[i]-marge;
137  }
138  }
139  else
140  {
141  ptLower = Z3i::Point(domainCoords[0],domainCoords[1], domainCoords[2]);
142  ptUpper = Z3i::Point(domainCoords[3],domainCoords[4], domainCoords[5]);
143  }
144 
145  Image3D::Domain imageDomain(ptLower, ptUpper);
146  trace.info() << "domain: "<<imageDomain<<std::endl;
147  Image3D imageResult(imageDomain);
148  for(Image3D::Domain::ConstIterator iter = imageResult.domain().begin();
149  iter!= imageResult.domain().end();
150  iter++)
151  {
152  imageResult.setValue(*iter, backgroundVal);
153  }
154 
155  for(unsigned int i=0; i<vectPoints.size(); i++)
156  {
157  if(invertY)
158  {
159  vectPoints[i][1]=ptUpper[1]-vectPoints[i][1];
160  }
161  if(imageResult.domain().isInside(vectPoints[i]))
162  {
163  imageResult.setValue(vectPoints[i], foregroundVal);
164  }
165  else
166  {
167  trace.warning() << "point " << vectPoints[i] << " outside the domain (ignored in the resulting volumic image)" << std::endl;
168  }
169  }
170  trace.info()<< "Exporting resulting volumic image ... ";
171  GenericWriter<Image3D>::exportFile(outputFileName, imageResult);
172  trace.info() << " [done]"<<std::endl;
173  return EXIT_SUCCESS;
174 }
Definition: ATu0v1.h:57