DGtalTools  1.2.0
volBoundary2obj.cpp
1 
32 #include <iostream>
33 #include <set>
34 #include "CLI11.hpp"
35 
36 #include "DGtal/base/Common.h"
37 #include "DGtal/base/BasicFunctors.h"
38 #include "DGtal/kernel/SpaceND.h"
39 #include "DGtal/kernel/domains/HyperRectDomain.h"
40 #include "DGtal/images/ImageSelector.h"
41 #include "DGtal/images/IntervalForegroundPredicate.h"
42 #include "DGtal/topology/KhalimskySpaceND.h"
43 #include "DGtal/topology/DigitalSurface.h"
44 #include "DGtal/topology/SetOfSurfels.h"
45 #include "DGtal/io/boards/Board3D.h"
46 #include "DGtal/io/readers/PointListReader.h"
47 #include "DGtal/io/readers/GenericReader.h"
48 #ifdef WITH_ITK
49 #include "DGtal/io/readers/DicomReader.h"
50 #endif
51 #include "DGtal/io/Color.h"
52 #include "DGtal/io/colormaps/GradientColorMap.h"
53 
54 #include "DGtal/helpers/StdDefs.h"
55 #include "DGtal/helpers/Shortcuts.h"
56 #include "DGtal/helpers/ShortcutsGeometry.h"
57 
58 using namespace std;
59 using namespace DGtal;
60 
62 
63 
108 int main( int argc, char** argv )
109 {
110 
111  CLI::App app;
112 
113 
114  // Using standard 3D digital space.
115  typedef Shortcuts<Z3i::KSpace> SH3;
116  typedef ShortcutsGeometry<Z3i::KSpace> SHG3;
117  auto params = SH3::defaultParameters() | SHG3::defaultParameters();
118 
119  int thresholdMin {128};
120  int thresholdMax {255};
121  string inputFilename;
122  DGtal::int64_t rescaleInputMin {0};
123  DGtal::int64_t rescaleInputMax {255};
124  std::vector<unsigned int > vectCol= {230, 230, 230, 255};
125  bool triangulatedSurface {false};
126  std::string outputFilename = "result.obj";
127 
128  app.description("Export the boundary of a volume file to OBJ format. By default the resulting mesh is defined from the surfels of the surface elements, a triangulated (dual)");
129 
130  app.add_option("-i,--input,1", inputFilename, "vol file (.vol, .longvol .p3d, .pgm3d and if WITH_ITK is selected: dicom, dcm, mha, mhd). For longvol, dicom, dcm, mha or mhd formats, the input values are linearly scaled between 0 and 255." )
131  ->required()
132  ->check(CLI::ExistingFile);
133  app.add_option("--output,-o,2",outputFilename ,"output file (.obj or .off).");
134  app.add_option("--thresholdMin,-m", thresholdMin, "threshold min (excluded) to define binary shape.", true);
135  app.add_option("--thresholdMax,-M", thresholdMax, "threshold max (included) to define binary shape.", true);
136  app.add_option("--rescaleInputMin", rescaleInputMin, "min value used to rescale the input intensity (to avoid basic cast into 8 bits image).", true);
137  app.add_option("--rescaleInputMax", rescaleInputMax, "max value used to rescale the input intensity (to avoid basic cast into 8 bits image).", true);
138  app.add_option("--customDiffuse,-c", vectCol, "set the R, G, B, A components of the diffuse colors of the mesh faces.", true)
139  ->expected(4);
140  app.add_flag("--triangulatedSurface,-t", triangulatedSurface, "save the dual triangulated surface instead instead the default digital surface.");
141  app.get_formatter()->column_width(40);
142  CLI11_PARSE(app, argc, argv);
143 
144 
145 
146  typedef DGtal::functors::Rescaling<DGtal::int64_t ,unsigned char > RescalFCT;
147  RescalFCT f (rescaleInputMin, rescaleInputMax,0, 255);
148 
149  trace.beginBlock( "Loading file.." );
150  SH3::GrayScaleImage image =
151  GenericReader< SH3::GrayScaleImage >::importWithValueFunctor(inputFilename, f );
152 
153  auto gimage = CountedPtr<SH3::GrayScaleImage>( new SH3::GrayScaleImage( image ) );
154  auto bimage = SH3::makeBinaryImage(gimage,params( "thresholdMin", thresholdMin )
155  ( "thresholdMax", thresholdMax ) );
156 
157 
158  trace.info() << "Image loaded: "<<gimage<< std::endl;
159  trace.endBlock();
160  params( "faceSubdivision", "Centroid" )( "surfelAdjacency", 1);
161  auto K = SH3::getKSpace( bimage);
162 
163  SH3::Color cD (vectCol[0], vectCol[1], vectCol[2], vectCol[3]);
164 
165 
166  auto surface = SH3::makeDigitalSurface( bimage, K );
167  const std::string extension = outputFilename.substr( outputFilename.find_last_of(".") + 1 );
168  if (extension != "obj" && extension != "off")
169  {
170  trace.warning() << "File extension not recognized, saving by default in objg format"<< std::endl;
171  }
172  if (triangulatedSurface)
173  {
174  auto tr = SH3::makeTriangulatedSurface(surface);
175  bool ok = true;
176  if (extension!="off")
177  {
178  ok = SH3::saveOBJ( tr, SH3::RealVectors(), SH3::Colors(), outputFilename, Color( 32, 32, 32 ), cD );
179  }
180  else
181  {
182  ok = SH3::saveOFF( tr, outputFilename, cD);
183  }
184  return ok ? EXIT_SUCCESS : EXIT_FAILURE ;
185  }
186  else
187  {
188  bool ok = true;
189  if (extension!="off")
190  {
191  ok = SH3::saveOBJ( surface, SH3::RealVectors(), SH3::Colors(), outputFilename, Color( 32, 32, 32 ), cD);
192  } else
193  {
194  ok = SH3::saveOFF( surface, outputFilename, cD);
195  }
196  return ok ? EXIT_SUCCESS : EXIT_FAILURE;
197  }
198  return EXIT_SUCCESS;
199 }
200 
201 
Definition: ATu0v1.h:57