DGtal  1.3.beta
shapes/exampleSurfaceMesh.cpp

This snippet shows how to build SurfaceMesh objects from different sources:

It also shows how to export it as OBJ files.

See also
Surface mesh data structure for representing manifold or non-manifold polygonal surfaces in R3
Visualisation of three surface meshes, using conversion from SurfaceMesh to Mesh
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#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"
using namespace std;
using namespace DGtal;
using namespace Z3i;
int main( int argc, char** argv )
{
// The following typedefs are useful
typedef SurfMesh::Vertices Vertices;
trace.beginBlock ( "Reading a mesh OBJ file" );
SurfMesh smesh;
std::string S = examplesPath + "samples/spot.obj";
std::ifstream input( S.c_str() );
input.close();
trace.info() << "Read " << ( ok_read ? "OK" : "ERROR" )
<< " mesh=" << smesh << std::endl;
trace.beginBlock ( "Building a torus" );
auto torus_mesh = Helper::makeTorus
( 2.5, 0.5, RealPoint { 0.0, 0.0, 0.0 }, 40, 40, 0, Helper::NormalsType::NO_NORMALS );
trace.beginBlock ( "Building a pyramid" );
std::vector< RealPoint > positions =
{ { 0, 0, 5 }, { 1, 1, 3 }, { -1, 1, 3 }, { -1, -1, 3 }, { 1, -1, 3 } };
std::vector< Vertices > faces =
{ { 0, 1, 2 }, { 0, 2, 3 }, { 0, 3, 4 }, { 0, 4, 1 }, { 4, 3, 2, 1 } };
auto pyramid_mesh = SurfMesh( positions.cbegin(), positions.cend(),
faces.cbegin(), faces.cend() );
trace.beginBlock ( "Traversing a mesh by BF" );
BreadthFirstVisitor< SurfMesh > visitor( smesh, 0 );
std::vector<double> distances( smesh.nbVertices() );
double biggest_d = 0.0;
while ( ! visitor.finished() )
{
auto v = visitor.current().first; // current vertex
auto d = visitor.current().second; // current distance
biggest_d = (double) d;
distances[ v ] = biggest_d;
visitor.expand();
}
trace.beginBlock ( "Colored output of BF traversal" );
// Displaying faces colored by their distance to vertex 0.
auto face_distances = smesh.computeFaceValuesFromVertexValues( distances );
auto cmap = GradientColorMap< double >( 0.0, biggest_d, CMAP_JET );
std::vector<Color> face_colors( smesh.nbFaces() );
for ( SurfMesh::Face j = 0; j < smesh.nbFaces(); ++j )
face_colors[ j ] = cmap( face_distances[ j ] );
Writer::writeOBJ( "spot-bft.obj", smesh, face_colors );
// Displaying three isolines.
Writer::writeIsoLinesOBJ( "spot-iso-0_25.obj", smesh,
face_distances, distances, distances.back() * 0.25, 0.2 );
Writer::writeIsoLinesOBJ( "spot-iso-0_5.obj", smesh,
face_distances, distances, distances.back() * 0.5, 0.2 );
Writer::writeIsoLinesOBJ( "spot-iso-0_75.obj", smesh,
face_distances, distances, distances.back() * 0.75, 0.2 );
// Conversion to Mesh for easy display.
Mesh< RealPoint > viewmesh(true);
Mesh< RealPoint > viewmesh2, viewmesh3;
MeshHelpers::surfaceMesh2Mesh( smesh, viewmesh , face_colors );
MeshHelpers::surfaceMesh2Mesh( torus_mesh, viewmesh2 );
MeshHelpers::surfaceMesh2Mesh( pyramid_mesh, viewmesh3 );
QApplication application(argc,argv);
Viewer3D<> viewer;
viewer.show();
viewer << viewmesh << viewmesh2 << viewmesh3;
viewer << Viewer3D<>::updateDisplay;
application.exec();
return 0;
}
// //
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:63
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::BreadthFirstVisitor
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
Definition: BreadthFirstVisitor.h:94
DGtal::SurfaceMesh
Aim: Represents an embedded mesh as faces and a list of vertices. Vertices may be shared among faces ...
Definition: SurfaceMesh.h:91
DGtal::SurfaceMeshWriter
Aim: An helper class for writing mesh file formats (Waverfront OBJ at this point) and creating a Surf...
Definition: SurfaceMeshWriter.h:64
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::info
std::ostream & info()
DGtal::Viewer3D
Definition: Viewer3D.h:135
DGtal::Mesh
Aim: This class is defined to represent a surface mesh through a set of vertices and faces....
Definition: Mesh.h:91
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
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:63
main
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
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:165
DGtal::BreadthFirstVisitor::expand
void expand()
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:119