DGtal  1.4.beta
exampleSurfaceMesh.cpp File Reference

An example file for SurfaceMesh in 3D. More...

#include <string>
#include <iostream>
#include <fstream>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "ConfigExamples.h"
#include "DGtal/shapes/Mesh.h"
#include "DGtal/shapes/SurfaceMesh.h"
#include "DGtal/shapes/SurfaceMeshHelper.h"
#include "DGtal/shapes/MeshHelpers.h"
#include "DGtal/graph/BreadthFirstVisitor.h"
#include "DGtal/io/readers/SurfaceMeshReader.h"
#include "DGtal/io/writers/SurfaceMeshWriter.h"
#include "DGtal/io/viewers/Viewer3D.h"
#include "DGtal/io/Color.h"
#include "DGtal/io/colormaps/GradientColorMap.h"
Include dependency graph for exampleSurfaceMesh.cpp:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

An example file for SurfaceMesh in 3D.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2020/07/15

This file is part of the DGtal library.

Definition in file exampleSurfaceMesh.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

[exampleSurfaceMesh-typedef]

[exampleSurfaceMesh-typedef]

[exampleSurfaceMesh-read-mesh]

[exampleSurfaceMesh-read-mesh]

[exampleSurfaceMesh-make-torus]

[exampleSurfaceMesh-make-torus]

[exampleSurfaceMesh-make-pyramid]

[exampleSurfaceMesh-make-pyramid]

[exampleSurfaceMesh-graph-bft]

[exampleSurfaceMesh-graph-bft]

[exampleSurfaceMesh-write-obj]

[exampleSurfaceMesh-write-obj]

Definition at line 76 of file exampleSurfaceMesh.cpp.

77 {
79  // The following typedefs are useful
84 
85  trace.beginBlock ( "Reading a mesh OBJ file" );
87  SurfMesh smesh;
88  std::string S = examplesPath + "samples/spot.obj";
89  std::ifstream input( S.c_str() );
90  bool ok_read = SurfaceMeshReader< RealPoint, RealVector >::readOBJ( input, smesh );
91  input.close();
92  trace.info() << "Read " << ( ok_read ? "OK" : "ERROR" )
93  << " mesh=" << smesh << std::endl;
95  trace.endBlock();
96 
97  trace.beginBlock ( "Building a torus" );
99  auto torus_mesh = Helper::makeTorus
100  ( 2.5, 0.5, RealPoint { 0.0, 0.0, 0.0 }, 40, 40, 0, Helper::NormalsType::NO_NORMALS );
102  trace.endBlock();
103 
104  trace.beginBlock ( "Building a pyramid" );
106  std::vector< RealPoint > positions =
107  { { 0, 0, 5 }, { 1, 1, 3 }, { -1, 1, 3 }, { -1, -1, 3 }, { 1, -1, 3 } };
108  std::vector< Vertices > faces =
109  { { 0, 1, 2 }, { 0, 2, 3 }, { 0, 3, 4 }, { 0, 4, 1 }, { 4, 3, 2, 1 } };
110  auto pyramid_mesh = SurfMesh( positions.cbegin(), positions.cend(),
111  faces.cbegin(), faces.cend() );
113  trace.endBlock();
114 
115  trace.beginBlock ( "Traversing a mesh by BF" );
117  BreadthFirstVisitor< SurfMesh > visitor( smesh, 0 );
118  std::vector<double> distances( smesh.nbVertices() );
119  double biggest_d = 0.0;
120  while ( ! visitor.finished() )
121  {
122  auto v = visitor.current().first; // current vertex
123  auto d = visitor.current().second; // current distance
124  biggest_d = (double) d;
125  distances[ v ] = biggest_d;
126  visitor.expand();
127  }
129  trace.endBlock();
130 
131  trace.beginBlock ( "Colored output of BF traversal" );
133  // Displaying faces colored by their distance to vertex 0.
134  auto face_distances = smesh.computeFaceValuesFromVertexValues( distances );
135  auto cmap = GradientColorMap< double >( 0.0, biggest_d, CMAP_JET );
136  std::vector<Color> face_colors( smesh.nbFaces() );
137  for ( SurfMesh::Face j = 0; j < smesh.nbFaces(); ++j )
138  face_colors[ j ] = cmap( face_distances[ j ] );
140  Writer::writeOBJ( "spot-bft.obj", smesh, face_colors );
141 
142  // Displaying three isolines.
143  Writer::writeIsoLinesOBJ( "spot-iso-0_25.obj", smesh,
144  face_distances, distances, distances.back() * 0.25, 0.2 );
145  Writer::writeIsoLinesOBJ( "spot-iso-0_5.obj", smesh,
146  face_distances, distances, distances.back() * 0.5, 0.2 );
147  Writer::writeIsoLinesOBJ( "spot-iso-0_75.obj", smesh,
148  face_distances, distances, distances.back() * 0.75, 0.2 );
150  trace.endBlock();
151 
152  // Conversion to Mesh for easy display.
153  Mesh< RealPoint > viewmesh(true);
154  Mesh< RealPoint > viewmesh2, viewmesh3;
155  MeshHelpers::surfaceMesh2Mesh( smesh, viewmesh , face_colors );
156  MeshHelpers::surfaceMesh2Mesh( torus_mesh, viewmesh2 );
157  MeshHelpers::surfaceMesh2Mesh( pyramid_mesh, viewmesh3 );
158 
159  QApplication application(argc,argv);
160  Viewer3D<> viewer;
161  viewer.show();
162  viewer << viewmesh << viewmesh2 << viewmesh3;
163  viewer << Viewer3D<>::updateDisplay;
164  application.exec();
165 
166  return 0;
167 }
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: This class is defined to represent a surface mesh through a set of vertices and faces....
Definition: Mesh.h:92
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
SMesh::Vertices Vertices
Trace trace
Definition: Common.h:153
Aim: An helper class for building classical meshes.
Aim: An helper class for reading mesh files (Wavefront OBJ at this point) and creating a SurfaceMesh.
Aim: An helper class for writing mesh file formats (Waverfront OBJ at this point) and creating a Surf...
Aim: Represents an embedded mesh as faces and a list of vertices. Vertices may be shared among faces ...
Definition: SurfaceMesh.h:92
std::vector< Vertex > Vertices
The type that defines a list/range of vertices (e.g. to define faces)
Definition: SurfaceMesh.h:112
std::vector< AnyRing > computeFaceValuesFromVertexValues(const std::vector< AnyRing > &vvalues) const
Size nbFaces() const
Definition: SurfaceMesh.h:296
Size nbVertices() const
Definition: SurfaceMesh.h:288

References DGtal::Trace::beginBlock(), DGtal::CMAP_JET, DGtal::SurfaceMesh< TRealPoint, TRealVector >::computeFaceValuesFromVertexValues(), DGtal::BreadthFirstVisitor< TGraph, TMarkSet >::current(), DGtal::Trace::endBlock(), DGtal::BreadthFirstVisitor< TGraph, TMarkSet >::expand(), DGtal::BreadthFirstVisitor< TGraph, TMarkSet >::finished(), DGtal::Trace::info(), DGtal::SurfaceMesh< TRealPoint, TRealVector >::nbFaces(), DGtal::SurfaceMesh< TRealPoint, TRealVector >::nbVertices(), DGtal::Viewer3D< TSpace, TKSpace >::show(), and DGtal::trace.