DGtalTools 2.1.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
134// Variable globale pour activer/désactiver l'UI
135bool show_ui = false;
136
137void myCallback() {
138 ImGuiIO& io = ImGui::GetIO();
139
140
141 // Si la touche W est enfoncée ce frame
142 if (ImGui::IsKeyPressed(ImGuiKey_W)) {
143 show_ui = !show_ui;
144 polyscope::options::buildGui = show_ui;
145 }
146
147
148}
149
150
151
152int main(int argc, char **argv)
153{
154 // parse command line using CLI ----------------------------------------------
155 CLI::App app;
156 std::string inputFileName;
157 std::vector<unsigned int> vectIndexSDP{0, 1, 2};
158 Color lineColor(100, 100, 250);
159 Color pointColor(250, 250, 250);
160 std::vector<int> vectColorPt;
161 std::vector<int> vectColorLine;
162 std::string meshName;
163 std::string typePrimitive{"voxel"};
164 std::string vectorsFileName;
165 std::vector<unsigned int> vectColMesh;
166 std::vector<unsigned int> vectIndexColorImport{3, 4, 5};
167 bool importColorLabels{false};
168 bool noPointDisplay{false};
169 bool drawLines{false};
170 bool importColors{false};
171
172 float sx{1.0};
173 float sy{1.0};
174 float sz{1.0};
175 double lineSize{0.2};
176 double filterValue{100.0};
177 double constantNorm{0.0};
178 double percentageFilterVect{100.0};
179 unsigned int customAlphaMesh;
180
181 unsigned int colorLabelIndex = 3;
182
183 app.description("Display sequence of 3d discrete points by using PolyscopeViewer.");
184 app.add_option("-i,--input,1", inputFileName, "input file: sdp (sequence of discrete points).")
185 ->required()
186 ->check(CLI::ExistingFile);
187 app.add_option("--SDPindex", vectIndexSDP, "specify the sdp index (by default 0,1,2).")
188 ->expected(3);
189 app.add_option("--pointColor,-c", vectColorPt, "set the color of points: r g b a.")
190 ->expected(4);
191 app.add_option("--lineColor,-l", vectColorLine, "set the color of line: r g b a.")
192 ->expected(4);
193 app.add_option("--addMesh,-m", meshName, "append a mesh (off/obj) to the point set visualization.");
194 app.add_option("--customAlphaMesh", customAlphaMesh, "set the alpha components of the colors of the mesh faces (can be applied for each mesh).");
195 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).")
196 ->expected(4);
197 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).");
198 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.")
199 ->expected(3)
200 ->needs(importColOpt);
201
202 app.add_flag("--importColorLabels", importColorLabels, "import color labels from the input file (label index should be by default at index 3).");
203 app.add_option("--setColorLabelIndex", colorLabelIndex, "customize the index of the imported color labels in the source file (used by -importColorLabels).");
204 app.add_option("--filter,-f", filterValue, "filter input file in order to display only the [arg] percent of the input 3D points (uniformly selected).");
205 app.add_flag("--noPointDisplay", noPointDisplay, "usefull for instance to only display the lines between points.");
206 app.add_flag("--drawLines", drawLines, "draw the line between discrete points.");
207 app.add_option("--scaleX,-x", sx, "set the scale value in the X direction");
208 app.add_option("--scaleY,-y", sy, "set the scale value in the Y direction");
209 app.add_option("--scaleZ,-z", sy, "set the scale value in the Z direction");
210 app.add_option("--lineSize", lineSize, "defines the line size (used when the --drawLines or --drawVectors option is selected).");
211 app.add_option("--primitive,-p", typePrimitive, "set the primitive to display the set of points.")
212 ->check(CLI::IsMember({"voxel", "sphere"}));
213 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.");
214 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]).");
215 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). ");
216
217
218 app.get_formatter()->column_width(40);
219 CLI11_PARSE(app, argc, argv);
220 // END parse command line using CLI ----------------------------------------------
221
222 if (vectColorLine.size() == 4)
223 {
224 lineColor.setRGBi(vectColorLine[0], vectColorLine[1], vectColorLine[2], vectColorLine[3]);
225 }
226 if (vectColorPt.size() == 4)
227 {
228 pointColor.setRGBi(vectColorPt[0], vectColorPt[1], vectColorPt[2], vectColorPt[3]);
229 }
230 stringstream s;
231 s << "3dSDPViewer - DGtalTools: ";
232 string bname = inputFileName.substr(inputFileName.find_last_of("/")+1,inputFileName.size()) ;
233 s << " " << bname ;
234 polyscope::options::programName = s.str();
235
236 bool useUnitVector = constantNorm != 0.0;
237 polyscope::view::setNavigateStyle(polyscope::NavigateStyle::Free);
238 polyscope::options::groundPlaneMode = polyscope::GroundPlaneMode::None;
239 polyscope::options::buildGui=false;
240
241
242 typedef PolyscopeViewer<Z3i::Space, Z3i::KSpace> Viewer;
243 Z3i::KSpace K;
244 Viewer viewer(K);
245 viewer << pointColor;
246 viewer.allowReuseList = true;
247
248 if (typePrimitive == "sphere") {
249 viewer.drawAsBalls();
250 }
251
252 // Get vector of colors if imported.
253 std::vector<Color> vectColors;
254 if (importColors)
255 {
256 std::vector<unsigned int> r = TableReader<unsigned int>::getColumnElementsFromFile(inputFileName, vectIndexColorImport[0]);
257 std::vector<unsigned int> g = TableReader<unsigned int>::getColumnElementsFromFile(inputFileName, vectIndexColorImport[1]);
258 std::vector<unsigned int> b = TableReader<unsigned int>::getColumnElementsFromFile(inputFileName, vectIndexColorImport[2]);
259 for (unsigned int i = 0; i < r.size(); i++)
260 {
261 vectColors.push_back(Color(r[i], g[i], b[i]));
262 }
263 }
264
265 // Get vector of colors if imported.
266 std::vector<int> vectColorLabels;
267 unsigned int maxLabel = 1;
268 if (importColorLabels)
269 {
270 vectColorLabels = TableReader<int>::getColumnElementsFromFile(inputFileName, colorLabelIndex);
271 maxLabel = *(std::max_element(vectColorLabels.begin(), vectColorLabels.end()));
272 }
273 HueShadeColorMap<unsigned int> aColorMap(0, maxLabel);
274
275 vector<Z3i::RealPoint> vectVoxels;
276 vectVoxels = PointListReader<Z3i::RealPoint>::getPointsFromFile(inputFileName, vectIndexSDP);
277
278 int name = 0;
279 if (!noPointDisplay)
280 {
281 int step = max(1, (int)(100 / filterValue));
282 for (unsigned int i = 0; i < vectVoxels.size(); i = i + step)
283 {
284 if (importColors)
285 {
286 pointColor = vectColors[i];
287
288 }
289 else if (importColorLabels)
290 {
291 unsigned int index = vectColorLabels[i];
292 pointColor = aColorMap(index);
293 }
294 viewer << WithQuantity(Z3i::Point((int)vectVoxels.at(i)[0],
295 (int)vectVoxels.at(i)[1],
296 (int)vectVoxels.at(i)[2]), "label", pointColor);
297
298
299 }
300
301 viewer << lineColor;
302 if (drawLines)
303 {
304 for (unsigned int i = 1; i < vectVoxels.size(); i++)
305 {
306 viewer.drawLine(vectVoxels.at(i - 1), vectVoxels.at(i));
307 }
308 }
309
310 if (vectorsFileName != "")
311 {
312 std::vector<Z3i::RealPoint> vectorsPt = PointListReader<Z3i::RealPoint>::getPointsFromFile(vectorsFileName);
313 if (vectorsPt.size() % 2 == 1)
314 {
315 trace.info() << "Warning the two set of points doesn't contains the same number of points, some vectors will be skipped." << std::endl;
316 }
317
318 double percentage = percentageFilterVect;
319 int step = max(1, (int)(100 / percentage));
320
321 // Splits vectors into their own line groups
322 viewer.endCurrentGroup();
323 if (useUnitVector)
324 {
325 for (unsigned int i = 0; i < std::min(vectVoxels.size(), vectorsPt.size()); i = i + 2 * step)
326 {
327 viewer.drawLine(vectVoxels.at(i), vectVoxels.at(i) + vectorsPt.at(i) * constantNorm);
328 }
329 }
330 else
331 {
332 for (unsigned int i = 0; i < vectorsPt.size() - 1; i = i + 2 * step)
333 {
334 viewer.drawLine(vectorsPt.at(i), vectorsPt.at(i + 1));
335 }
336 }
337 }
338
339 if (meshName != "")
340 {
341 bool customColorMesh = vectColMesh.size() == 4;
342 if (customColorMesh)
343 {
344 viewer.drawColor(DGtal::Color(vectColMesh[0], vectColMesh[1], vectColMesh[2], vectColMesh[3]));
345 }
346
347 Mesh<Z3i::RealPoint> mesh(!customColorMesh);
348 mesh << meshName;
349 if (customAlphaMesh)
350 {
351 for (unsigned int j = 0; j < mesh.nbFaces(); j++)
352 {
353 auto c = mesh.getFaceColor(j);
354 mesh.setFaceColor(j, Color(c.red(), c.green(), c.blue(), customAlphaMesh));
355 }
356 viewer << mesh;
357 }
358 }
359 polyscope::state::userCallback = myCallback;
360 viewer.show();
361 return 0;
362 }
363}
Definition ATu0v1.h:57