31 #include "DGtal/helpers/StdDefs.h"
32 #include "DGtal/shapes/MeshVoxelizer.h"
33 #include "DGtal/kernel/sets/CDigitalSet.h"
34 #include "DGtal/kernel/domains/HyperRectDomain.h"
35 #include "DGtal/io/readers/MeshReader.h"
36 #include "DGtal/io/Display3D.h"
37 #include "DGtal/io/writers/GenericWriter.h"
38 #include "DGtal/images/ImageContainerBySTLVector.h"
44 using namespace DGtal;
78 template<
unsigned int SEP >
79 void voxelizeAndExport(
const std::string& inputFilename,
80 const std::string& outputFilename,
81 const unsigned int resolution,
82 const unsigned int margin)
84 using Domain = Z3i::Domain;
85 using PointR3 = Z3i::RealPoint;
86 using PointZ3 = Z3i::Point;
88 trace.beginBlock(
"Preparing the mesh");
89 trace.info() <<
"Reading input file: " << inputFilename;
90 Mesh<PointR3> inputMesh;
91 MeshReader<PointR3>::importOFFFile(inputFilename.c_str(), inputMesh);
92 trace.info() <<
" [done]" << std::endl;
93 const std::pair<PointR3, PointR3> bbox = inputMesh.getBoundingBox();
94 trace.info()<<
"Mesh bounding box: "<<bbox.first <<
" "<<bbox.second<<std::endl;
96 const double smax = (bbox.second - bbox.first).max();
97 const double factor = resolution / smax;
98 const PointR3 translate = -bbox.first;
99 trace.info() <<
"Scale = "<<factor<<
" translate = "<<translate<<std::endl;
100 for(
auto it = inputMesh.vertexBegin(), itend = inputMesh.vertexEnd();
109 trace.beginBlock(
"Voxelization");
110 trace.info() <<
"Voxelization " << SEP <<
"-separated ; " << resolution <<
"^3 ";
111 Domain aDomain(PointZ3().
diagonal(-margin), PointZ3().
diagonal(resolution+margin));
114 Z3i::DigitalSet mySet(aDomain);
115 MeshVoxelizer<Z3i::DigitalSet, SEP> aVoxelizer;
116 aVoxelizer.voxelize(mySet, inputMesh, 1.0);
117 trace.info() <<
" [done] " << std::endl;
120 trace.beginBlock(
"Exporting");
122 trace.info()<<aDomain<<std::endl;
123 ImageContainerBySTLVector<Domain, unsigned char> image(aDomain);
125 image.setValue(p, 128);
126 image >> outputFilename.c_str();
130 int main(
int argc,
char** argv )
135 std::string inputFileName;
136 std::string outputFileName {
"result.vol"};
137 unsigned int margin {0};
138 unsigned int separation {6};
139 unsigned int resolution {128};
141 app.description(
"Convert a mesh file into a 26-separated or 6-separated volumetric voxelization in a given resolution grid. \n Example:\n mesh2vol ${DGtal}/examples/samples/tref.off output.vol --separation 26 --resolution 256 ");
143 app.add_option(
"-i,--input,1", inputFileName,
"mesh file (.off)." )
145 ->check(CLI::ExistingFile);
146 app.add_option(
"-o,--output,2", outputFileName,
"filename of ouput volumetric file (vol, pgm3d, ...).",
true);
147 app.add_option(
"-m,--margin", margin,
"add volume margin around the mesh bounding box.");
148 app.add_option(
"-s,--separation", separation,
"voxelization 6-separated or 26-separated.",
true)
149 -> check(CLI::IsMember({6, 26}));
150 app.add_option(
"-r,--resolution", resolution,
"digitization domain size (e.g. 128). The mesh will be scaled such that its bounding box maps to [0,resolution)^3.",
true);
153 app.get_formatter()->column_width(40);
154 CLI11_PARSE(app, argc, argv);
158 voxelizeAndExport<6>(inputFileName, outputFileName, resolution, margin);
160 voxelizeAndExport<26>(inputFileName, outputFileName, resolution, margin);
DGtal::LinearOperator< Calculus, dim, duality, dim, duality > diagonal(const DGtal::KForm< Calculus, dim, duality > &kform)