31#include "DGtal/base/Common.h"
32#include "DGtal/helpers/StdDefs.h"
33#include "DGtal/io/viewers/PolyscopeViewer.h"
34#include "DGtal/io/readers/TableReader.h"
35#include "DGtal/io/readers/PointListReader.h"
36#include "DGtal/io/readers/MeshReader.h"
37#include "DGtal/topology/helpers/Surfaces.h"
38#include "DGtal/topology/SurfelAdjacency.h"
39#include "DGtal/shapes/Mesh.h"
40#include "DGtal/io/Color.h"
41#include "DGtal/io/colormaps/HueShadeColorMap.h"
42#include "DGtal/io/readers/GenericReader.h"
50typedef PolyscopeViewer<Z3i::Space, Z3i::KSpace> Viewer;
137int main(
int argc,
char **argv)
141 std::string inputFileName;
142 std::vector<unsigned int> vectIndexSDP{0, 1, 2};
143 Color lineColor(100, 100, 250);
144 Color pointColor(250, 250, 250);
145 std::vector<int> vectColorPt;
146 std::vector<int> vectColorLine;
147 std::string meshName;
148 std::string typePrimitive{
"voxel"};
149 std::string vectorsFileName;
150 std::vector<unsigned int> vectColMesh;
151 std::vector<unsigned int> vectIndexColorImport{3, 4, 5};
152 bool importColorLabels{
false};
153 bool noPointDisplay{
false};
154 bool drawLines{
false};
155 bool importColors{
false};
156 bool interactiveDisplayVoxCoords{
false};
160 double lineSize{0.2};
161 double filterValue{100.0};
162 double constantNorm{0.0};
163 double percentageFilterVect{100.0};
164 unsigned int customAlphaMesh;
166 unsigned int colorLabelIndex = 3;
168 app.description(
"Display sequence of 3d discrete points by using PolyscopeViewer.");
169 app.add_option(
"-i,--input,1", inputFileName,
"input file: sdp (sequence of discrete points).")
171 ->check(CLI::ExistingFile);
172 app.add_option(
"--SDPindex", vectIndexSDP,
"specify the sdp index (by default 0,1,2).")
174 app.add_option(
"--pointColor,-c", vectColorPt,
"set the color of points: r g b a.")
176 app.add_option(
"--lineColor,-l", vectColorLine,
"set the color of line: r g b a.")
178 app.add_option(
"--addMesh,-m", meshName,
"append a mesh (off/obj) to the point set visualization.");
179 app.add_option(
"--customAlphaMesh", customAlphaMesh,
"set the alpha components of the colors of the mesh faces (can be applied for each mesh).");
180 auto optMesh = app.add_option(
"--customColorMesh", vectColMesh,
"set the R, G, B, A components of the colors of the mesh faces (mesh added with option --addMesh).")
182 auto importColOpt = app.add_flag(
"--importColors", importColors,
"import point colors from the input file (R G B colors should be by default at index 3, 4, 5).");
183 app.add_option(
"--setColorsIndex", vectIndexColorImport,
"customize the index of the imported colors in the source file (used by --importColor). By default the initial indexes are 3, 4, 5.")
185 ->needs(importColOpt);
187 app.add_flag(
"--importColorLabels", importColorLabels,
"import color labels from the input file (label index should be by default at index 3).");
188 app.add_option(
"--setColorLabelIndex", colorLabelIndex,
"customize the index of the imported color labels in the source file (used by -importColorLabels).");
189 app.add_option(
"--filter,-f", filterValue,
"filter input file in order to display only the [arg] percent of the input 3D points (uniformly selected).");
190 app.add_flag(
"--noPointDisplay", noPointDisplay,
"usefull for instance to only display the lines between points.");
191 app.add_flag(
"--drawLines", drawLines,
"draw the line between discrete points.");
192 app.add_option(
"--scaleX,-x", sx,
"set the scale value in the X direction");
193 app.add_option(
"--scaleY,-y", sy,
"set the scale value in the Y direction");
194 app.add_option(
"--scaleZ,-z", sy,
"set the scale value in the Z direction");
195 app.add_option(
"--lineSize", lineSize,
"defines the line size (used when the --drawLines or --drawVectors option is selected).");
196 app.add_option(
"--primitive,-p", typePrimitive,
"set the primitive to display the set of points.")
197 ->check(CLI::IsMember({
"voxel",
"sphere"}));
198 app.add_option(
"--drawVectors,-v", vectorsFileName,
"SDP vector file: draw a set of vectors from the given file (each vector are determined by two consecutive point given, each point represented by its coordinates on a single line.");
200 app.add_option(
"--unitVector,-u", constantNorm,
"specifies that the SDP vector file format (of --drawVectors option) should be interpreted as unit vectors (each vector position is be defined from the input point (with input order) with a constant norm defined by [arg]).");
202 app.add_option(
"--filterVectors", percentageFilterVect,
"filters vector input file in order to display only the [arg] percent of the input vectors (uniformly selected, to be used with option --drawVectors else no effect). ");
203 app.add_flag(
"--interactiveDisplayVoxCoords", interactiveDisplayVoxCoords,
" by using this option the coordinates can be displayed after selection (shift+left click on voxel).");
205 app.get_formatter()->column_width(40);
206 CLI11_PARSE(app, argc, argv);
209 if (vectColorLine.size() == 4)
211 lineColor.setRGBi(vectColorLine[0], vectColorLine[1], vectColorLine[2], vectColorLine[3]);
213 if (vectColorPt.size() == 4)
215 pointColor.setRGBi(vectColorPt[0], vectColorPt[1], vectColorPt[2], vectColorPt[3]);
218 s <<
"3dSDPViewer - DGtalTools: ";
219 string bname = inputFileName.substr(inputFileName.find_last_of(
"/")+1,inputFileName.size()) ;
221 polyscope::options::programName = s.str();
223 bool useUnitVector = constantNorm != 0.0;
225 typedef PolyscopeViewer<Z3i::Space, Z3i::KSpace> Viewer;
228 viewer << pointColor;
229 viewer.allowReuseList =
true;
231 if (typePrimitive ==
"sphere") {
232 viewer.drawAsBalls();
236 std::vector<Color> vectColors;
239 std::vector<unsigned int> r = TableReader<unsigned int>::getColumnElementsFromFile(inputFileName, vectIndexColorImport[0]);
240 std::vector<unsigned int> g = TableReader<unsigned int>::getColumnElementsFromFile(inputFileName, vectIndexColorImport[1]);
241 std::vector<unsigned int> b = TableReader<unsigned int>::getColumnElementsFromFile(inputFileName, vectIndexColorImport[2]);
242 for (
unsigned int i = 0; i < r.size(); i++)
244 vectColors.push_back(Color(r[i], g[i], b[i]));
249 std::vector<int> vectColorLabels;
250 unsigned int maxLabel = 1;
251 if (importColorLabels)
253 vectColorLabels = TableReader<int>::getColumnElementsFromFile(inputFileName, colorLabelIndex);
254 maxLabel = *(std::max_element(vectColorLabels.begin(), vectColorLabels.end()));
256 HueShadeColorMap<unsigned int> aColorMap(0, maxLabel);
258 vector<Z3i::RealPoint> vectVoxels;
259 vectVoxels = PointListReader<Z3i::RealPoint>::getPointsFromFile(inputFileName, vectIndexSDP);
264 int step = max(1, (
int)(100 / filterValue));
265 for (
unsigned int i = 0; i < vectVoxels.size(); i = i + step)
269 Color col = vectColors[i];
270 viewer.drawColor(col);
272 else if (importColorLabels)
274 unsigned int index = vectColorLabels[i];
275 Color col = aColorMap(index);
276 viewer.drawColor(col);
279 viewer << Z3i::Point((
int)vectVoxels.at(i)[0],
280 (
int)vectVoxels.at(i)[1],
281 (
int)vectVoxels.at(i)[2]);
287 for (
unsigned int i = 1; i < vectVoxels.size(); i++)
289 viewer.drawLine(vectVoxels.at(i - 1), vectVoxels.at(i));
293 if (vectorsFileName !=
"")
295 std::vector<Z3i::RealPoint> vectorsPt = PointListReader<Z3i::RealPoint>::getPointsFromFile(vectorsFileName);
296 if (vectorsPt.size() % 2 == 1)
298 trace.info() <<
"Warning the two set of points doesn't contains the same number of points, some vectors will be skipped." << std::endl;
301 double percentage = percentageFilterVect;
302 int step = max(1, (
int)(100 / percentage));
305 viewer.endCurrentGroup();
308 for (
unsigned int i = 0; i < std::min(vectVoxels.size(), vectorsPt.size()); i = i + 2 * step)
310 viewer.drawLine(vectVoxels.at(i), vectVoxels.at(i) + vectorsPt.at(i) * constantNorm);
315 for (
unsigned int i = 0; i < vectorsPt.size() - 1; i = i + 2 * step)
317 viewer.drawLine(vectorsPt.at(i), vectorsPt.at(i + 1));
324 bool customColorMesh = vectColMesh.size() == 4;
327 viewer.drawColor(DGtal::Color(vectColMesh[0], vectColMesh[1], vectColMesh[2], vectColMesh[3]));
330 Mesh<Z3i::RealPoint> mesh(!customColorMesh);
334 for (
unsigned int j = 0; j < mesh.nbFaces(); j++)
336 auto c = mesh.getFaceColor(j);
337 mesh.setFaceColor(j, Color(c.red(), c.green(), c.blue(), customAlphaMesh));