DGtalTools  1.2.0
3dDisplaySurfelData.cpp
1 
29 
30 #include <iostream>
31 #include <QGLViewer/qglviewer.h>
32 #include <stdio.h>
33 
34 #include "DGtal/base/Common.h"
35 #include "DGtal/helpers/StdDefs.h"
36 #include "DGtal/io/viewers/Viewer3D.h"
37 #include "DGtal/io/DrawWithDisplay3DModifier.h"
38 #include "DGtal/io/readers/PointListReader.h"
39 #include "DGtal/io/readers/MeshReader.h"
40 #include "DGtal/io/colormaps/GradientColorMap.h"
41 
42 #include "DGtal/topology/helpers/Surfaces.h"
43 #include "DGtal/topology/SurfelAdjacency.h"
44 #include "DGtal/topology/CanonicCellEmbedder.h"
45 #include "DGtal/math/Statistic.h"
46 
47 #include "DGtal/io/Color.h"
48 #include "DGtal/io/colormaps/GradientColorMap.h"
49 #include "DGtal/io/readers/GenericReader.h"
50 
51 #include "CLI11.hpp"
52 
53 #include <limits>
54 
55 using namespace std;
56 using namespace DGtal;
57 using namespace Z3i;
58 
59 
112 template < typename Space = DGtal::Z3i::Space, typename KSpace = DGtal::Z3i::KSpace>
113 struct ViewerSnap: DGtal::Viewer3D <Space, KSpace>
114 {
115 
116  ViewerSnap(const KSpace &KSEmb, bool saveSnap): Viewer3D<Space, KSpace>(KSEmb), mySaveSnap(saveSnap){
117  };
118 
119  virtual void
120  init(){
121  DGtal::Viewer3D<>::init();
122  if(mySaveSnap)
123  {
124  QObject::connect(this, SIGNAL(drawFinished(bool)), this, SLOT(saveSnapshot(bool)));
125  }
126  };
127  bool mySaveSnap;
128 };
129 
130 
131 template < typename Point>
132 void
133 getBoundingUpperAndLowerPoint(const std::vector<Point> &vectorPt, Point &ptLower, Point &ptUpper){
134  for(unsigned int i =1; i<vectorPt.size(); i++)
135  {
136  if(vectorPt.at(i)[0] < ptLower[0])
137  {
138  ptLower[0] = vectorPt.at(i)[0];
139  }
140  if(vectorPt.at(i)[1] < ptLower[1])
141  {
142  ptLower[1] = vectorPt.at(i)[1];
143  }
144  if(vectorPt.at(i)[2] < ptLower[2])
145  {
146  ptLower[2] =vectorPt.at(i)[2];
147  }
148  if(vectorPt.at(i)[0] < ptLower[0])
149  {
150  ptLower[0] = vectorPt.at(i)[0];
151  }
152  if(vectorPt.at(i)[1] < ptLower[1])
153  {
154  ptLower[1] = vectorPt.at(i)[1];
155  }
156  if(vectorPt.at(i)[2] < ptLower[2])
157  {
158  ptLower[2] =vectorPt.at(i)[2];
159  }
160  }
161 }
162 
163 
164 int main( int argc, char** argv )
165 {
166  typedef PointVector<4, double> Point4D;
167  typedef PointVector<1, int> Point1D;
168 
169  // parse command line using CLI ----------------------------------------------
170  CLI::App app;
171  std::string inputFileName;
172  std::string doSnapShotName;
173  bool noWindows {false};
174  double minScalarVal;
175  double maxScalarVal;
176  unsigned int labelIndex;
177  std::vector<unsigned int> vectSDPindex {0, 1, 2};
178 
179  app.description("Display surfel data from SDP file with color attributes given as scalar interpreted as color. \n Example of use: \n First you have to generate a file containing a set of surfels with, for instance, their associated curvature values: \n 3dCurvatureViewer -i $DGtal/examples/samples/cat10.vol -r 3 --exportOnly -d curvatureCat10R3.dat \n Then, we can use this tool to display the set of surfel with their associated values: 3dDisplaySurfelData -i curvatureCat10R3.dat");
180  app.add_option("-i,--input,1", inputFileName, "input file: sdp (sequence of discrete points with attribute)" )
181  ->required()
182  ->check(CLI::ExistingFile);
183  app.add_flag("--noWindows,-n",noWindows, "Don't display Viewer windows.");
184  app.add_option("--doSnapShotAndExit,-d", doSnapShotName, "save display snapshot into file.");
185  auto fixMaxOption = app.add_option("--fixMaxColorValue,-M", maxScalarVal, "fix manually the maximal color value for the scale error display (else the scale is set from the maximal value)");
186  auto fixMinOption = app.add_option("--fixMinColorValue,-m", minScalarVal, "fix manually the maximal color value for the scale error display (else the scale is set from the minimal value)");
187  app.add_option("--labelIndex", labelIndex , "set the index of the label.", true);
188  app.add_option("--SDPindex", vectSDPindex, "specify the sdp index.", true);
189 
190  app.get_formatter()->column_width(40);
191  CLI11_PARSE(app, argc, argv);
192  // END parse command line using CLI ----------------------------------------------
193 
194  std::vector<Point4D> surfelAndScalarInput;
195  Z3i::KSpace K;
196 
197  surfelAndScalarInput = PointListReader<Point4D>::getPointsFromFile(inputFileName, vectSDPindex);
198 
199  Point4D ptLower = surfelAndScalarInput.at(0);
200  Point4D ptUpper = surfelAndScalarInput.at(0);
201  getBoundingUpperAndLowerPoint(surfelAndScalarInput, ptLower, ptUpper);
202 
203  K.init(Z3i::Point(2*ptLower[0]+1, 2*ptLower[1]+1, 2*ptLower[2]+1),
204  Z3i::Point(2*ptUpper[0]+1, 2*ptUpper[1]+1, 2*ptUpper[2]+1), true);
205 
206 
207  std::vector<Cell> vectSurfelsInput;
208 
209  // Construction of the set of surfels
210  for(unsigned int i =0; i<surfelAndScalarInput.size(); i++){
211  Point4D pt4d = surfelAndScalarInput.at(i);
212  Cell c = K.uCell(Z3i::Point(pt4d[0], pt4d[1], pt4d[2]));
213  vectSurfelsInput.push_back(c);
214  }
215 
216  CanonicCellEmbedder<KSpace> embeder(K);
217  std::vector<unsigned int> vectIndexMinToReference;
218 
219  //-------------------------
220  // Displaying input with color given from scalar values
221 
222  QApplication application(argc,argv);
223  typedef ViewerSnap<> Viewer;
224 
225  Viewer viewer(K, doSnapShotName != "");
226  if(doSnapShotName != "")
227  {
228  viewer.setSnapshotFileName(QString(doSnapShotName.c_str()));
229  }
230  viewer.setWindowTitle("3dCompSurfel Viewer");
231  viewer.show();
232  viewer.restoreStateFromFile();
233 
234  if( fixMinOption->count() == 0 )
235  {
236  minScalarVal=surfelAndScalarInput.at(0)[3];
237  }
238  if( fixMaxOption->count() == 0 )
239  {
240  maxScalarVal=surfelAndScalarInput.at(0)[3];
241  }
242 
243  if( fixMinOption->count() == 0 || fixMaxOption->count() == 0)
244  {
245  for(unsigned int i=1; i <surfelAndScalarInput.size(); i++)
246  {
247  double scalVal = surfelAndScalarInput.at(i)[3];
248  if(scalVal < minScalarVal && fixMinOption->count() == 0)
249  {
250  minScalarVal = scalVal;
251  }
252  if(scalVal > maxScalarVal && fixMaxOption->count() == 0)
253  {
254  maxScalarVal = scalVal;
255  }
256  }
257  }
258 
259  GradientColorMap<double> gradientColorMap( minScalarVal, maxScalarVal );
260  gradientColorMap.addColor( Color(255,0,0,100 ) );
261  gradientColorMap.addColor( Color(0,255,0,100 ) );
262  gradientColorMap.addColor( Color(0,0,255,100 ) );
263 
264  bool useGrad = minScalarVal!=maxScalarVal;
265 
266  viewer << SetMode3D(vectSurfelsInput.at(0).className(), "Basic");
267  for(unsigned int i=0; i <surfelAndScalarInput.size(); i++)
268  {
269  double valInput = surfelAndScalarInput.at(i)[3];
270  if(useGrad)
271  {
272  viewer.setFillColor(gradientColorMap(valInput));
273  }else
274  {
275  viewer.setFillColor(Color::White);
276  }
277  viewer << vectSurfelsInput.at(i);
278  }
279 
280 
281 
282  viewer << Viewer::updateDisplay;
283  if(doSnapShotName != "")
284  {
285  // Appy cleaning just save the last snap
286  std::string name = doSnapShotName;
287  std::string extension = name.substr(name.find_last_of(".") + 1);
288  std::string basename = name.substr(0, name.find_last_of("."));
289  for(int i=0; i< viewer.snapshotCounter()-1; i++){
290  std::stringstream s;
291  s << basename << "-"<< setfill('0') << setw(4)<< i << "." << extension;
292  trace.info() << "erase temp file: " << s.str() << std::endl;
293  remove(s.str().c_str());
294  }
295  std::stringstream s;
296  s << basename << "-"<< setfill('0') << setw(4)<< viewer.snapshotCounter()-1 << "." << extension;
297  rename(s.str().c_str(), name.c_str());
298  return 0;
299  }
300 
301  if(noWindows)
302  {
303  return 0;
304  }
305  else
306  {
307  return application.exec();
308  }
309 }
Definition: ATu0v1.h:57