DGtal  1.1.0
exampleSurfaceMesh.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  */
16 
48 #include <string>
50 #include <iostream>
51 #include <fstream>
52 
53 #include "DGtal/base/Common.h"
54 #include "DGtal/helpers/StdDefs.h"
55 #include "ConfigExamples.h"
56 
57 #include "DGtal/shapes/Mesh.h"
58 #include "DGtal/shapes/SurfaceMesh.h"
59 #include "DGtal/shapes/SurfaceMeshHelper.h"
60 #include "DGtal/shapes/MeshHelpers.h"
61 #include "DGtal/graph/BreadthFirstVisitor.h"
62 #include "DGtal/io/readers/SurfaceMeshReader.h"
63 #include "DGtal/io/writers/SurfaceMeshWriter.h"
64 #include "DGtal/io/viewers/Viewer3D.h"
65 #include "DGtal/io/Color.h"
66 #include "DGtal/io/colormaps/GradientColorMap.h"
67 
69 
70 using namespace std;
71 using namespace DGtal;
72 using namespace Z3i;
73 
74 
76 int main( int argc, char** argv )
77 {
79  // The following typedefs are useful
80  typedef SurfaceMesh< RealPoint, RealVector > SurfMesh;
82  typedef SurfMesh::Vertices Vertices;
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 }
168 // //
DGtal::Trace::endBlock
double endBlock()
Face
TriMesh::Face Face
Definition: testTriangulatedSurface.cpp:56
DGtal::SurfaceMeshHelper
Aim: An helper class for building classical meshes.
Definition: SurfaceMeshHelper.h:64
DGtal::trace
Trace trace
Definition: Common.h:150
DGtal::BreadthFirstVisitor
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
Definition: BreadthFirstVisitor.h:95
DGtal::SurfaceMesh
Aim: Represents an embedded mesh as faces and a list of vertices. Vertices may be shared among faces ...
Definition: SurfaceMesh.h:92
DGtal::SurfaceMeshWriter
Aim: An helper class for writing mesh file formats (Waverfront OBJ at this point) and creating a Surf...
Definition: SurfaceMeshWriter.h:65
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::info
std::ostream & info()
DGtal::Viewer3D
Definition: Viewer3D.h:133
DGtal::Mesh
Aim: This class is defined to represent a surface mesh through a set of vertices and faces....
Definition: Mesh.h:92
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
DGtal::CMAP_JET
@ CMAP_JET
Definition: GradientColorMap.h:69
DGtal::BreadthFirstVisitor::finished
bool finished() const
DGtal::SurfaceMeshReader
Aim: An helper class for reading mesh files (Wavefront OBJ at this point) and creating a SurfaceMesh.
Definition: SurfaceMeshReader.h:64
DGtal::BreadthFirstVisitor::current
const Node & current() const
DGtal::PointVector
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
DGtal::BreadthFirstVisitor::expand
void expand()
main
int main(int argc, char **argv)
Definition: exampleSurfaceMesh.cpp:76
DGtal::Viewer3D::show
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
DGtal::GradientColorMap
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Definition: GradientColorMap.h:120