DGtalTools 2.0.0
Loading...
Searching...
No Matches
3dSDPViewer.cpp
1
29#include <iostream>
30
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"
43
44#include "CLI11.hpp"
45
46using namespace std;
47using namespace DGtal;
48using namespace Z3i;
49
50typedef PolyscopeViewer<Z3i::Space, Z3i::KSpace> Viewer;
51
137int main(int argc, char **argv)
138{
139 // parse command line using CLI ----------------------------------------------
140 CLI::App app;
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};
157 float sx{1.0};
158 float sy{1.0};
159 float sz{1.0};
160 double lineSize{0.2};
161 double filterValue{100.0};
162 double constantNorm{0.0};
163 double percentageFilterVect{100.0};
164 unsigned int customAlphaMesh;
165
166 unsigned int colorLabelIndex = 3;
167
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).")
170 ->required()
171 ->check(CLI::ExistingFile);
172 app.add_option("--SDPindex", vectIndexSDP, "specify the sdp index (by default 0,1,2).")
173 ->expected(3);
174 app.add_option("--pointColor,-c", vectColorPt, "set the color of points: r g b a.")
175 ->expected(4);
176 app.add_option("--lineColor,-l", vectColorLine, "set the color of line: r g b a.")
177 ->expected(4);
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).")
181 ->expected(4);
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.")
184 ->expected(3)
185 ->needs(importColOpt);
186
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.");
199
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]).");
201
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).");
204
205 app.get_formatter()->column_width(40);
206 CLI11_PARSE(app, argc, argv);
207 // END parse command line using CLI ----------------------------------------------
208
209 if (vectColorLine.size() == 4)
210 {
211 lineColor.setRGBi(vectColorLine[0], vectColorLine[1], vectColorLine[2], vectColorLine[3]);
212 }
213 if (vectColorPt.size() == 4)
214 {
215 pointColor.setRGBi(vectColorPt[0], vectColorPt[1], vectColorPt[2], vectColorPt[3]);
216 }
217 stringstream s;
218 s << "3dSDPViewer - DGtalTools: ";
219 string bname = inputFileName.substr(inputFileName.find_last_of("/")+1,inputFileName.size()) ;
220 s << " " << bname ;
221 polyscope::options::programName = s.str();
222
223 bool useUnitVector = constantNorm != 0.0;
224
225 typedef PolyscopeViewer<Z3i::Space, Z3i::KSpace> Viewer;
226 Z3i::KSpace K;
227 Viewer viewer(K);
228 viewer << pointColor;
229 viewer.allowReuseList = true;
230
231 if (typePrimitive == "sphere") {
232 viewer.drawAsBalls();
233 }
234
235 // Get vector of colors if imported.
236 std::vector<Color> vectColors;
237 if (importColors)
238 {
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++)
243 {
244 vectColors.push_back(Color(r[i], g[i], b[i]));
245 }
246 }
247
248 // Get vector of colors if imported.
249 std::vector<int> vectColorLabels;
250 unsigned int maxLabel = 1;
251 if (importColorLabels)
252 {
253 vectColorLabels = TableReader<int>::getColumnElementsFromFile(inputFileName, colorLabelIndex);
254 maxLabel = *(std::max_element(vectColorLabels.begin(), vectColorLabels.end()));
255 }
256 HueShadeColorMap<unsigned int> aColorMap(0, maxLabel);
257
258 vector<Z3i::RealPoint> vectVoxels;
259 vectVoxels = PointListReader<Z3i::RealPoint>::getPointsFromFile(inputFileName, vectIndexSDP);
260
261 int name = 0;
262 if (!noPointDisplay)
263 {
264 int step = max(1, (int)(100 / filterValue));
265 for (unsigned int i = 0; i < vectVoxels.size(); i = i + step)
266 {
267 if (importColors)
268 {
269 Color col = vectColors[i];
270 viewer.drawColor(col);
271 }
272 else if (importColorLabels)
273 {
274 unsigned int index = vectColorLabels[i];
275 Color col = aColorMap(index);
276 viewer.drawColor(col);
277 }
278
279 viewer << Z3i::Point((int)vectVoxels.at(i)[0],
280 (int)vectVoxels.at(i)[1],
281 (int)vectVoxels.at(i)[2]);
282 }
283
284 viewer << lineColor;
285 if (drawLines)
286 {
287 for (unsigned int i = 1; i < vectVoxels.size(); i++)
288 {
289 viewer.drawLine(vectVoxels.at(i - 1), vectVoxels.at(i));
290 }
291 }
292
293 if (vectorsFileName != "")
294 {
295 std::vector<Z3i::RealPoint> vectorsPt = PointListReader<Z3i::RealPoint>::getPointsFromFile(vectorsFileName);
296 if (vectorsPt.size() % 2 == 1)
297 {
298 trace.info() << "Warning the two set of points doesn't contains the same number of points, some vectors will be skipped." << std::endl;
299 }
300
301 double percentage = percentageFilterVect;
302 int step = max(1, (int)(100 / percentage));
303
304 // Splits vectors into their own line groups
305 viewer.endCurrentGroup();
306 if (useUnitVector)
307 {
308 for (unsigned int i = 0; i < std::min(vectVoxels.size(), vectorsPt.size()); i = i + 2 * step)
309 {
310 viewer.drawLine(vectVoxels.at(i), vectVoxels.at(i) + vectorsPt.at(i) * constantNorm);
311 }
312 }
313 else
314 {
315 for (unsigned int i = 0; i < vectorsPt.size() - 1; i = i + 2 * step)
316 {
317 viewer.drawLine(vectorsPt.at(i), vectorsPt.at(i + 1));
318 }
319 }
320 }
321
322 if (meshName != "")
323 {
324 bool customColorMesh = vectColMesh.size() == 4;
325 if (customColorMesh)
326 {
327 viewer.drawColor(DGtal::Color(vectColMesh[0], vectColMesh[1], vectColMesh[2], vectColMesh[3]));
328 }
329
330 Mesh<Z3i::RealPoint> mesh(!customColorMesh);
331 mesh << meshName;
332 if (customAlphaMesh)
333 {
334 for (unsigned int j = 0; j < mesh.nbFaces(); j++)
335 {
336 auto c = mesh.getFaceColor(j);
337 mesh.setFaceColor(j, Color(c.red(), c.green(), c.blue(), customAlphaMesh));
338 }
339 viewer << mesh;
340 }
341 }
342
343 viewer.show();
344 return 0;
345 }
346}
Definition ATu0v1.h:57