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