29#include "DGtal/base/Common.h"
30#include "DGtal/helpers/StdDefs.h"
31#include "DGtal/io/readers/GenericReader.h"
32#include "DGtal/io/readers/PointListReader.h"
34#include "DGtal/images/ImageSelector.h"
77using Vertices = std::vector<std::array<double, 3>>;
78using Faces = std::vector<std::array<size_t, 3>>;
81void pushCube(
const T& center, Vertices& vertices, Faces& faces) {
82 constexpr static double size = 0.5;
83 constexpr static std::array<std::array<double, 3>, 8> coords {{
84 {-size, -size, -size},
85 { size, -size, -size},
93 constexpr static std::array<std::array<size_t, 3>, 12> indices {{
102 const double x = center[0];
103 const double y = center[1];
104 const double z = center[2];
106 const size_t startIndex = vertices.size() + 1;
107 for (
size_t i = 0; i < coords.size(); ++i) {
108 vertices.push_back({x + coords[i][0],
112 for (
size_t i = 0; i < indices.size(); ++i) {
113 faces.push_back({startIndex + indices[i][0],
114 startIndex + indices[i][1],
115 startIndex + indices[i][2]});
119int main(
int argc,
char** argv )
124 std::string inputFileName;
125 std::string outputFileName {
"result.obj"};
126 int thresholdMin {128};
127 int thresholdMax {255};
128 DGtal::int64_t rescaleInputMin {0};
129 DGtal::int64_t rescaleInputMax {255};
132 app.description(
" Converts any volumetric file (or .sdp file) to an OBJ one. Each grid point with value between [thresholdMin, thresholdMax] is exported as a unit cube.");
133 app.add_option(
"-i,--input,1", inputFileName,
"vol file (.vol, .longvol .p3d, .pgm3d or .sdp and if DGTAL_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." )
135 ->check(CLI::ExistingFile);
136 app.add_option(
"--output,-o,2",outputFileName ,
"output file (.obj or .off).");
137 app.add_option(
"--thresholdMin,-m", thresholdMin,
"threshold min (excluded) to define binary shape.");
138 app.add_option(
"--thresholdMax,-M", thresholdMax,
"threshold max (included) to define binary shape.");
139 app.add_option(
"--rescaleInputMin", rescaleInputMin,
"min value used to rescale the input intensity (to avoid basic cast into 8 bits image).");
140 app.add_option(
"--rescaleInputMax", rescaleInputMax,
"max value used to rescale the input intensity (to avoid basic cast into 8 bits image).");
143 app.get_formatter()->column_width(40);
144 CLI11_PARSE(app, argc, argv);
150 typedef ImageSelector<Domain, unsigned char>::Type Image;
151 string extension = inputFileName.substr(inputFileName.find_last_of(
".") + 1);
155 typedef DGtal::functors::Rescaling<DGtal::int64_t ,unsigned char > RescalFCT;
156 Image image = GenericReader< Image >::importWithValueFunctor( inputFileName,RescalFCT(rescaleInputMin,
159 trace.info() <<
"Image loaded: "<<image<< std::endl;
160 Domain domain = image.domain();
161 for(Domain::ConstIterator it = domain.begin(), itend=domain.end(); it!=itend; ++it){
162 unsigned char val= image( (*it) );
163 if(val<=thresholdMax && val >=thresholdMin){
164 pushCube(*it, vertices, faces);
171 vector<Z3i::Point> vectVoxels = PointListReader<Z3i::Point>::getPointsFromFile(inputFileName);
172 for(
unsigned int i=0;i< vectVoxels.size(); i++){
173 pushCube(vectVoxels.at(i), vertices, faces);
178 std::ofstream outFile(outputFileName);
180 trace.error() <<
"Can not open file: '" << outputFileName <<
"'\n";
184 outFile <<
"#This file was created by DGtalTools vol2obj object\n";
185 for (
size_t i = 0; i < vertices.size(); ++i) {
186 outFile <<
"v " << vertices[i][0] <<
' ' << vertices[i][1] <<
' ' << vertices[i][2] <<
'\n';
188 for (
size_t i = 0; i < faces.size(); ++i) {
189 outFile <<
"f " << faces[i][0] <<
' ' << faces[i][1] <<
' ' << faces[i][2] <<
'\n';