DGtalTools 2.0.0
Loading...
Searching...
No Matches
meshViewer.cpp
1
30#include <iostream>
31#include <sstream>
32#include "DGtal/base/Common.h"
33
34#include "DGtal/io/viewers/PolyscopeViewer.h"
35#include "DGtal/io/readers/MeshReader.h"
36#include "DGtal/helpers/StdDefs.h"
37#include "DGtal/io/readers/PointListReader.h"
38
39#include "CLI11.hpp"
40
41using namespace std;
42using namespace DGtal;
43
101// Variable globale pour activer/désactiver l'UI
102bool show_ui = false;
103
104void myCallback() {
105 ImGuiIO& io = ImGui::GetIO();
106
107
108 // Si la touche W est enfoncée ce frame
109 if (ImGui::IsKeyPressed(ImGuiKey_W)) {
110 show_ui = !show_ui;
111 polyscope::options::buildGui = show_ui;
112 }
113
114
115}
116
117
118int main(int argc, char **argv)
119{
120 float sx{1.0};
121 float sy{1.0};
122 float sz{1.0};
123
124 unsigned int meshColorR{240};
125 unsigned int meshColorG{240};
126 unsigned int meshColorB{240};
127 unsigned int meshColorA{255};
128
129 unsigned int meshColorRLine{0};
130 unsigned int meshColorGLine{0};
131 unsigned int meshColorBLine{0};
132 unsigned int meshColorALine{255};
133
134 unsigned int sdpColorR{240};
135 unsigned int sdpColorG{240};
136 unsigned int sdpColorB{240};
137 unsigned int sdpColorA{255};
138
139 float lineWidth{1.5};
140
141 std::vector<unsigned int> customColorMesh;
142 std::vector<unsigned int> customColorSDP;
143 std::vector<unsigned int> customLineColor;
144 std::vector<unsigned int> customBGColor;
145 std::vector<unsigned int> customAlphaMesh;
146 std::vector<unsigned int> vectFieldIndices = {0, 1, 2, 3, 4, 5};
147 std::string displayVectorField;
148
149 std::string snapshotFile;
150 std::string filenameSDP;
151 double ballRadius{0.5};
152 bool invertNormal{false};
153 bool drawVertex{false};
154
155 // parse command line using CLI ----------------------------------------------
156 CLI::App app;
157 std::vector<std::string> inputFileNames;
158 std::string outputFileName{"result.raw"};
159 app.description("Display OFF mesh file by using PolyscopeViewers");
160 app.add_option("-i,--input,1", inputFileNames, "inputFileNames.off files (.off), or OFS file (.ofs)")
161 ->check(CLI::ExistingFile)
162 ->required();
163 app.add_option("-x,--scaleX", sx, "set the scale value in the X direction (default 1.0)");
164 app.add_option("-y,--scaleY", sy, "set the scale value in the y direction (default 1.0)");
165 app.add_option("-z,--scaleZ", sz, "set the scale value in the z direction (default 1.0)");
166 app.add_option("--minLineWidth", lineWidth, "set the min line width of the mesh faces (default 1.5)");
167 app.add_option("--customColorMesh", customColorMesh, "set the R, G, B, A components of the colors of the mesh faces and eventually the color R, G, B, A of the mesh edge lines (set by default to black).");
168 app.add_option("--customAlphaMesh", customAlphaMesh, "set the alpha(A) components of the colors of the mesh faces (can be applied for each mesh).");
169 app.add_option("--customColorSDP", customColorSDP, "set the R, G, B, A components of the colors of the sdp view")
170 ->expected(4);
171 app.add_option("--displayVectorField,-f", displayVectorField, "display a vector field from a simple sdp file (two points per line)");
172 app.add_option("--vectorFieldIndex", vectFieldIndices, "specify special indices for the two point coordinates (instead usinf the default indices: 0 1, 2, 3, 4, 5)")
173 ->expected(6);
174 app.add_option("--customLineColor", customLineColor, "set the R, G, B components of the colors of the lines displayed from the --displayVectorField option (red by default).")
175 ->expected(4);
176 app.add_option("--SDPradius", ballRadius, "change the ball radius to display a set of discrete points (used with displaySDP option)");
177 app.add_option("--displaySDP,-s", filenameSDP, "add the display of a set of discrete points as ball of radius 0.5.");
178 app.add_flag("--invertNormal,-n", invertNormal, "invert face normal vectors.");
179 app.add_flag("--drawVertex,-v", drawVertex, "draw the vertex of the mesh");
180
181 app.get_formatter()->column_width(40);
182 CLI11_PARSE(app, argc, argv);
183 // END parse command line using CLI ----------------------------------------------
184
185 DGtal::Color vFieldLineColor = DGtal::Color::Red;
186 if (customLineColor.size() == 4)
187 {
188 vFieldLineColor.setRGBi(customLineColor[0], customLineColor[1], customLineColor[2], 255);
189 }
190
191 if (customColorMesh.size() != 0)
192 {
193 if (customColorMesh.size() < 4)
194 {
195 trace.error() << "colors specification should contain R,G,B and Alpha values" << std::endl;
196 }
197 }
198
199 if (customColorSDP.size() == 4)
200 {
201 sdpColorR = customColorSDP[0];
202 sdpColorG = customColorSDP[1];
203 sdpColorB = customColorSDP[2];
204 sdpColorA = customColorSDP[3];
205 }
206
207 stringstream s;
208 s << "meshViewer - DGtalTools: ";
209 string name = inputFileNames[0].substr(inputFileNames[0].find_last_of("/")+1,inputFileNames[0].size()) ;
210 s << " " << name << " (W key to display settings)";
211 polyscope::options::programName = s.str();
212 polyscope::options::buildGui=false;
213 polyscope::options::groundPlaneMode = polyscope::GroundPlaneMode::None;
214
215 // Masquer le sol aussi
216
217 PolyscopeViewer viewer;
218
219 viewer.allowReuseList = true;
220 trace.info() << "Importing mesh... ";
221
222 std::vector<Mesh<DGtal::Z3i::RealPoint>> vectMesh;
223 for (unsigned int i = 0; i < inputFileNames.size(); i++)
224 {
225 Mesh<DGtal::Z3i::RealPoint> aMesh(customColorMesh.size() != 4 && customColorMesh.size() != 8);
226 aMesh << inputFileNames[i];
227 // for obj mesh by default the mesh color face are not necessary uniform.
228 if (aMesh.isStoringFaceColors() && customColorMesh.size() >= 4)
229 {
230 if (i * 8 < customColorMesh.size())
231 {
232 meshColorR = customColorMesh[i * 8];
233 }
234 if (i * 8 + 1 < customColorMesh.size())
235 {
236 meshColorG = customColorMesh[i * 8 + 1];
237 }
238 if (i * 8 + 2 < customColorMesh.size())
239 {
240 meshColorB = customColorMesh[i * 8 + 2];
241 }
242 if (i * 8 + 3 < customColorMesh.size())
243 {
244 meshColorA = customColorMesh[i * 8 + 3];
245 }
246 for (unsigned int j = 0; j < aMesh.nbFaces(); j++)
247 {
248 aMesh.setFaceColor(j, Color(meshColorR, meshColorG, meshColorB, meshColorA));
249 }
250 }
251 else if (customAlphaMesh.size() > 0)
252 {
253 for (unsigned int j = 0; j < aMesh.nbFaces(); j++)
254 {
255 auto c = aMesh.getFaceColor(j);
256 aMesh.setFaceColor(j, Color(c.red(), c.green(), c.blue(), customAlphaMesh.at(i < customAlphaMesh.size() ? i : 0)));
257 }
258 }
259
260 vectMesh.push_back(aMesh);
261 }
262
263 bool import = vectMesh.size() == inputFileNames.size();
264 if (!import)
265 {
266 trace.info() << "File import failed. " << std::endl;
267 return 0;
268 }
269
270 trace.info() << "[done]. " << std::endl;
271 if (filenameSDP != "")
272 {
273 if (customAlphaMesh.size() > 0)
274 {
275 trace.info() << "New meshViewer" << std::endl;
276 auto vOrigins = PointListReader<PointVector<1, DGtal::int32_t>>::getPolygonsFromFile(filenameSDP);
277 viewer << Color(sdpColorR, sdpColorG, sdpColorB, sdpColorA);
278 for (auto l : vOrigins)
279 {
280 DGtal::Z3i::Point pt(l[0][0], l[1][0], l[2][0]);
281 DGtal::Color cl(l[3][0], l[4][0], l[5][0], sdpColorA);
282 viewer.drawColor(cl);
283 viewer.drawBall(pt);
284 }
285 }
286 else
287 {
288 vector<Z3i::RealPoint> vectPoints;
289 vectPoints = PointListReader<Z3i::RealPoint>::getPointsFromFile(filenameSDP);
290 viewer << Color(sdpColorR, sdpColorG, sdpColorB, sdpColorA);
291 for (unsigned int i = 0; i < vectPoints.size(); i++)
292 {
293 viewer.drawBall(vectPoints.at(i));
294 }
295 }
296 }
297 if (invertNormal)
298 {
299 for (unsigned int i = 0; i < vectMesh.size(); i++)
300 {
301 vectMesh[i].invertVertexFaceOrder();
302 }
303 }
304
305 for (unsigned int i = 0; i < vectMesh.size(); i++)
306 {
307 if (i * 8 < customColorMesh.size())
308 {
309 meshColorR = customColorMesh[i * 8];
310 }
311 if (i * 8 + 1 < customColorMesh.size())
312 {
313 meshColorG = customColorMesh[i * 8 + 1];
314 }
315 if (i * 8 + 2 < customColorMesh.size())
316 {
317 meshColorB = customColorMesh[i * 8 + 2];
318 }
319 if (i * 8 + 3 < customColorMesh.size())
320 {
321 meshColorA = customColorMesh[i * 8 + 3];
322 }
323 if (i * 8 + 4 < customColorMesh.size())
324 {
325 meshColorALine = customColorMesh[i * 8 + 4];
326 }
327 if (i * 8 + 5 < customColorMesh.size())
328 {
329 meshColorBLine = customColorMesh[i * 8 + 5];
330 }
331 if (i * 8 + 6 < customColorMesh.size())
332 {
333 meshColorRLine = customColorMesh[i * 8 + 6];
334 }
335 if (i * 8 + 7 < customColorMesh.size())
336 {
337 meshColorALine = customColorMesh[i * 8 + 7];
338 }
339
340 viewer << Color(meshColorR, meshColorG, meshColorB, meshColorA);
341 viewer << vectMesh[i];
342 }
343
344 if (drawVertex)
345 {
346 for (unsigned int i = 0; i < vectMesh.size(); i++)
347 {
348 for (Mesh<DGtal::Z3i::RealPoint>::VertexStorage::const_iterator it = vectMesh[i].vertexBegin();
349 it != vectMesh[i].vertexEnd(); ++it)
350 {
351 DGtal::Z3i::Point pt;
352 pt[0] = (*it)[0];
353 pt[1] = (*it)[1];
354 pt[2] = (*it)[2];
355 viewer << pt;
356 }
357 }
358 }
359
360 if (displayVectorField != "")
361 {
362 std::vector<unsigned int> vectFieldIndices1 = {vectFieldIndices[0], vectFieldIndices[1], vectFieldIndices[2]};
363 std::vector<unsigned int> vectFieldIndices2 = {vectFieldIndices[3], vectFieldIndices[4], vectFieldIndices[5]};
364 std::vector<DGtal::Z3i::RealPoint> vectPt1 = PointListReader<DGtal::Z3i::RealPoint>::getPointsFromFile(displayVectorField, vectFieldIndices1);
365 std::vector<DGtal::Z3i::RealPoint> vectPt2 = PointListReader<DGtal::Z3i::RealPoint>::getPointsFromFile(displayVectorField, vectFieldIndices2);
366 for (unsigned int i = 0; i < vectPt1.size(); i++)
367 {
368 viewer.drawColor(vFieldLineColor);
369 viewer.drawLine(vectPt1[i], vectPt2[i]);
370 }
371 }
372 unsigned int nbVertex = 0;
373 unsigned int nbFaces = 0;
374 for (auto const &m : vectMesh)
375 {
376 nbVertex += m.nbVertex();
377 nbFaces += m.nbFaces();
378 }
379 stringstream ss;
380 ss << "# faces: " << std::fixed << nbFaces << " #vertex: " << nbVertex;
381 trace.info() << "[display ready]" << std::endl;
382 polyscope::state::userCallback = myCallback;
383
384 viewer.show();
385 return 0;
386}
Definition ATu0v1.h:57