DGtal  1.5.beta
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 
49 #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
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 // //
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
const Node & current() const
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...
SurfaceMesh< RealPoint, RealVector > SurfMesh
int main(int argc, char **argv)
SMesh::Vertices Vertices
DGtal is the top-level namespace which contains all DGtal functions and types.
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