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