DGtalTools 2.0.0
Loading...
Searching...
No Matches
volscope.cpp
1
30#include <vector>
31#include <array>
32
33#include <DGtal/base/Common.h>
34#include <DGtal/helpers/StdDefs.h>
35#include <DGtal/helpers/Shortcuts.h>
36
37#include <polyscope/polyscope.h>
38#include "polyscope/surface_mesh.h"
39#include "polyscope/volume_mesh.h"
40#include "polyscope/point_cloud.h"
41
42#include "CLI11.hpp"
43
44// Using standard 3D digital space.
45using namespace DGtal;
46using namespace Z3i;
47typedef Shortcuts<Z3i::KSpace> SH3;
48
49
85int main(int argc, char**argv)
86{
87 // parse command line using CLI ----------------------------------------------
88 CLI::App app;
89 app.description("Vol file vizualization using polyscope");
90
91 std::string inputFileName;
92 app.add_option("-i,--input,1", inputFileName, "Input vol file.")->required()->check(CLI::ExistingFile);
93
94 bool volumetricMode = false;
95 app.add_flag("--volumetricMode",volumetricMode, "Activate the volumetric mode instead of the isosurface visualization.");
96
97 bool pclOnly = false;
98 app.add_flag("--point-cloud-only",pclOnly, "In the volumetric mode, visualize the vol file as a point cloud instead of an hex mesh (default: false)");
99
100 int thresholdMin=0;
101 app.add_option("--min,--thresholdMin,-m", thresholdMin, "For isosurface visualization and voxel filtering, specifies the threshold min (excluded) (default: 0).");
102 int thresholdMax=255;
103 app.add_option("--max, --thresholdMax,-M", thresholdMax, "For isosurface visualization and voxel filtering, specifies the threshold max (included) (default: 255).");
104
105
106 app.get_formatter()->column_width(40);
107 CLI11_PARSE(app, argc, argv);
108
109 polyscope::options::programName = "volscope " + inputFileName + " - (DGtalTools)";
110 polyscope::init();
111
112 if(!volumetricMode)
113 {
114 trace.beginBlock("Loading vol");
115 auto params = SH3::defaultParameters();
116 params("thresholdMin",thresholdMin)("thresholdMax",thresholdMax);
117 auto volimage = SH3::makeBinaryImage(inputFileName, params );
118 auto K = SH3::getKSpace( volimage );
119 auto surface = SH3::makeLightDigitalSurface( volimage, K, params );
120 auto primalSurface = SH3::makePrimalSurfaceMesh(surface);
121 trace.endBlock();
122
123 trace.beginBlock("Creating polyscope surface");
124 std::vector<std::vector<SH3::SurfaceMesh::Vertex>> faces;
125 for(auto face= 0 ; face < primalSurface->nbFaces(); ++face)
126 faces.push_back(primalSurface->incidentVertices( face ));
127 polyscope::registerSurfaceMesh("Vol file", primalSurface->positions(), faces);
128 trace.endBlock();
129 }
130 else
131 {
132 std::vector<RealPoint> vertexPos;
133 std::vector<Point> pclPos;
134 std::vector<int> pclData;
135 std::vector<std::array<size_t,8>> hexIndices;
136 std::vector<int> hexData;
137
138 trace.beginBlock("Loading vol");
139 auto volimage = SH3::makeGrayScaleImage(inputFileName);
140 trace.endBlock();
141 trace.beginBlock("Creating polyscope HexMesh/Point Cloud");
142 auto dom = volimage->domain();
143 auto W = dom.upperBound() - dom.lowerBound() + Point::diagonal(1);
144 auto WW = W + Point::diagonal(1); //for dual grid
145 trace.info()<<W<<std::endl;
146 trace.info()<<dom<<std::endl;
147
148 size_t cpt=0;
149 unsigned char val;
150 Point p;
151 std::array<size_t,8> hex;
152 for(auto k=0; k <= W[2]; ++k)
153 for(auto j=0; j <= W[1]; ++j)
154 for(auto i=0; i <= W[0]; ++i)
155 {
156 p=Point(i,j,k);
157 if ((i<W[0]) && (j < W[1]) &&(k<W[2]))
158 val = (*volimage)(p);
159
160 if (pclOnly)
161 {
162 if ((p < W) && (val>thresholdMin) && (val <=thresholdMax))
163 {
164 pclPos.push_back(p);
165 pclData.push_back(val);
166 }
167 }
168 else
169 {
170 vertexPos.push_back(p+dom.lowerBound() -RealPoint::diagonal(0.5) );
171 hex = { cpt, cpt +1 , cpt + 1 + WW[0] , cpt +WW[0] , cpt + WW[0]*WW[1], cpt +1 + WW[0]*WW[1], cpt + 1 + WW[0]*WW[1]+WW[0] , cpt + WW[0]*WW[1]+WW[0]};
172
173 if (((i+1)< WW[0]) && ((j+1)< WW[1]) && ((k+1)< WW[2])&& (val>thresholdMin) && (val <=thresholdMax))
174 {
175 hexData.push_back(val);
176 hexIndices.push_back(hex);
177 }
178 ++cpt;
179 }
180 }
181
182 if (pclOnly)
183 {
184 auto ps = polyscope::registerPointCloud("Vol file", pclPos);
185 ps->addScalarQuantity("values", pclData);
186 trace.info()<<"Nb vertices ="<<vertexPos.size()<<std::endl;
187 trace.info()<<"Nb hexes ="<<hexIndices.size()<<std::endl;
188 }
189 else
190 {
191 auto ps = polyscope::registerHexMesh("Vol file", vertexPos, hexIndices);
192 ps->addCellScalarQuantity("values", hexData);
193 trace.info()<<"Nb points ="<<pclPos.size()<<std::endl;
194 }
195 trace.endBlock();
196 }
197 polyscope::show();
198 return 0;
199}
Definition ATu0v1.h:57