DGtalTools  0.9.2
3dDisplaySurfelData.cpp
1 
28 
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 <boost/program_options/options_description.hpp>
52 #include <boost/program_options/parsers.hpp>
53 #include <boost/program_options/variables_map.hpp>
54 
55 #include <limits>
56 
57 using namespace std;
58 using namespace DGtal;
59 using namespace Z3i;
60 
62 namespace po = boost::program_options;
63 
64 
116 template < typename Space = DGtal::Z3i::Space, typename KSpace = DGtal::Z3i::KSpace>
117 struct ViewerSnap: DGtal::Viewer3D <Space, KSpace>
118 {
119 
120  ViewerSnap(const KSpace &KSEmb, bool saveSnap): Viewer3D<Space, KSpace>(KSEmb), mySaveSnap(saveSnap){
121  };
122 
123  virtual void
124  init(){
125  DGtal::Viewer3D<>::init();
126  if(mySaveSnap){
127  QObject::connect(this, SIGNAL(drawFinished(bool)), this, SLOT(saveSnapshot(bool)));
128  }
129  };
130  bool mySaveSnap;
131 };
132 
133 
134 template < typename Point>
135 void
136 getBoundingUpperAndLowerPoint(const std::vector<Point> &vectorPt, Point &ptLower, Point &ptUpper){
137  for(unsigned int i =1; i<vectorPt.size(); i++){
138  if(vectorPt.at(i)[0] < ptLower[0]){
139  ptLower[0] = vectorPt.at(i)[0];
140  }
141  if(vectorPt.at(i)[1] < ptLower[1]){
142  ptLower[1] = vectorPt.at(i)[1];
143  }
144  if(vectorPt.at(i)[2] < ptLower[2]){
145  ptLower[2] =vectorPt.at(i)[2];
146  }
147  if(vectorPt.at(i)[0] < ptLower[0]){
148  ptLower[0] = vectorPt.at(i)[0];
149  }
150  if(vectorPt.at(i)[1] < ptLower[1]){
151  ptLower[1] = vectorPt.at(i)[1];
152  }
153  if(vectorPt.at(i)[2] < ptLower[2]){
154  ptLower[2] =vectorPt.at(i)[2];
155  }
156  }
157 }
158 
159 
160 int main( int argc, char** argv )
161 {
162 
163  typedef PointVector<4, double> Point4D;
164  typedef PointVector<1, int> Point1D;
165 
166  // parse command line ----------------------------------------------
167  po::options_description general_opt("Allowed options are: ");
168  general_opt.add_options()
169  ("help,h", "display this message")
170  ("input,i", po::value<std::string>(), "input file: sdp (sequence of discrete points with attribute)" )
171  ("noWindows,n", "Don't display Viewer windows." )
172  ("doSnapShotAndExit,d", po::value<std::string>(), "save display snapshot into file." )
173  ("fixMaxColorValue", po::value<double>(), "fix the maximal color value for the scale error display (else the scale is set from the maximal value)" )
174  ("fixMinColorValue", po::value<double>(), "fix the minimal color value for the scale error display (else the scale is set from the minimal value)" )
175  ("labelIndex", po::value<unsigned int>(), "set the index of the label (by default set to 3) " )
176  ("SDPindex", po::value<std::vector <unsigned int> >()->multitoken(), "specify the sdp index (by default 0,1,2).");
177 
178 
179  bool parseOK=true;
180  bool cannotStart= false;
181  po::variables_map vm;
182 
183 
184  try{
185  po::store(po::parse_command_line(argc, argv, general_opt), vm);
186  }catch(const std::exception& ex){
187  parseOK=false;
188  trace.error()<< "Error checking program options: "<< ex.what()<< endl;
189  }
190  po::notify(vm);
191  if(parseOK && ! vm.count("input"))
192  {
193  trace.error() << " The input file name was not defined" << endl;
194  cannotStart = true;
195  }
196 
197 
198  if( !parseOK || cannotStart || vm.count("help")||argc<=1)
199  {
200  trace.info() << "Usage: " << argv[0] << " [input]\n"
201  << "Display surfel data from SDP file with color attributes given as scalar interpreted as color. "
202  << general_opt << "\n";
203  return 0;
204  }
205  Z3i::KSpace K;
206  string inputFilename = vm["input"].as<std::string>();
207 
208 
209  std::vector<Point4D> surfelAndScalarInput;
210 
211 
212 
213  if(vm.count("SDPindex")) {
214  std::vector<unsigned int > vectIndex = vm["SDPindex"].as<std::vector<unsigned int > >();
215  if(vectIndex.size()!=4){
216  trace.error() << "you need to specify the three indexes of vertex." << std::endl;
217  return 0;
218  }
219  surfelAndScalarInput = PointListReader<Point4D>::getPointsFromFile(inputFilename, vectIndex);
220  }else{
221  surfelAndScalarInput = PointListReader<Point4D>::getPointsFromFile(inputFilename);
222  }
223 
224 
225  Point4D ptLower = surfelAndScalarInput.at(0);
226  Point4D ptUpper = surfelAndScalarInput.at(0);
227  getBoundingUpperAndLowerPoint(surfelAndScalarInput, ptLower, ptUpper);
228 
229 
230  K.init(Z3i::Point(2*ptLower[0]+1, 2*ptLower[1]+1, 2*ptLower[2]+1),
231  Z3i::Point(2*ptUpper[0]+1, 2*ptUpper[1]+1, 2*ptUpper[2]+1), true);
232 
233 
234  std::vector<Cell> vectSurfelsInput;
235 
236  // Construction of the set of surfels
237  for(unsigned int i =0; i<surfelAndScalarInput.size(); i++){
238  Point4D pt4d = surfelAndScalarInput.at(i);
239  Cell c = K.uCell(Z3i::Point(pt4d[0], pt4d[1], pt4d[2]));
240  vectSurfelsInput.push_back(c);
241  }
242 
243 
244  CanonicCellEmbedder<KSpace> embeder(K);
245  std::vector<unsigned int> vectIndexMinToReference;
246 
247 
248  //-------------------------
249  // Displaying input with color given from scalar values
250 
251  QApplication application(argc,argv);
252  typedef ViewerSnap<> Viewer;
253 
254  Viewer viewer(K, vm.count("doSnapShotAndExit"));
255  if(vm.count("doSnapShotAndExit")){
256  viewer.setSnapshotFileName(QString(vm["doSnapShotAndExit"].as<std::string>().c_str()));
257  }
258  viewer.setWindowTitle("3dCompSurfel Viewer");
259  viewer.show();
260  viewer.restoreStateFromFile();
261 
262  double minScalarVal=surfelAndScalarInput.at(0)[3];
263  double maxScalarVal=surfelAndScalarInput.at(0)[3];
264 
265  for(unsigned int i=1; i <surfelAndScalarInput.size(); i++){
266  double scalVal = surfelAndScalarInput.at(i)[3];
267  if(scalVal < minScalarVal){
268  minScalarVal = scalVal;
269  }
270  if(scalVal > maxScalarVal){
271  maxScalarVal = scalVal;
272  }
273  }
274  if(vm.count("fixMaxColorValue")){
275  maxScalarVal = vm["fixMaxColorValue"].as<double>();
276  }
277  if(vm.count("fixMinColorValue")){
278  minScalarVal = vm["fixMinColorValue"].as<double>();
279  }
280 
281  GradientColorMap<double> gradientColorMap( minScalarVal, maxScalarVal );
282  gradientColorMap.addColor( Color(255,0,0,100 ) );
283  gradientColorMap.addColor( Color(0,255,0,100 ) );
284  gradientColorMap.addColor( Color(0,0,255,100 ) );
285 
286  bool useGrad = minScalarVal!=maxScalarVal;
287 
288  viewer << SetMode3D(vectSurfelsInput.at(0).className(), "Basic");
289  for(unsigned int i=0; i <surfelAndScalarInput.size(); i++){
290  double valInput = surfelAndScalarInput.at(i)[3];
291  if(useGrad){
292  viewer.setFillColor(gradientColorMap(valInput));
293  }else{
294  viewer.setFillColor(Color::White);
295  }
296  viewer << vectSurfelsInput.at(i);
297  }
298 
299 
300 
301  viewer << Viewer::updateDisplay;
302  if(vm.count("doSnapShotAndExit")){
303  // Appy cleaning just save the last snap
304  std::string name = vm["doSnapShotAndExit"].as<std::string>();
305  std::string extension = name.substr(name.find_last_of(".") + 1);
306  std::string basename = name.substr(0, name.find_last_of("."));
307  for(int i=0; i< viewer.snapshotCounter()-1; i++){
308  std::stringstream s;
309  s << basename << "-"<< setfill('0') << setw(4)<< i << "." << extension;
310  trace.info() << "erase temp file: " << s.str() << std::endl;
311  remove(s.str().c_str());
312  }
313  std::stringstream s;
314  s << basename << "-"<< setfill('0') << setw(4)<< viewer.snapshotCounter()-1 << "." << extension;
315  rename(s.str().c_str(), name.c_str());
316  return 0;
317  }
318 
319  if(vm.count("noWindows")){
320  return 0;
321  }else{
322  return application.exec();
323  }
324 }
STL namespace.