tutorial_04.docu 1.57 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** \page tutorial_04 Using STL algorithms

Since the %OpenMesh iterators are (almost) conformant to STL iterators,
one can apply the STL algorithms on meshes. The following example
shows how to use the STL \c for_each construct, since it is
easier to read and may be more efficient than hand-written loops.

We will define a class which provides the smoothing algorithm, hence
define a reusable component. The class must be template class because there
is no such thing as a class %OpenMesh, but many different types of %OpenMesh:

\dontinclude 04-stl_algorithms/smooth_algo.hh 
\skipline template

The class SmootherT has two functors, one that computes the barycenter
for a given vertex, and a second that sets the vertex position to the
corresponding barycenter. A functor is simply a class with a function
<tt>operator()(...)</tt>. The first functor \c ComputeCOG
Mike Kremer's avatar
Mike Kremer committed
computes the barycenter and stores it in a custom vertex property \c
Jan Möbius's avatar
Jan Möbius committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

\skipline operator
\until }
\until }

Note, that \c ComputeCOG needs to have access to the mesh object and
the property handle. Here, both are references to member variables of
the smoother object.

The second functor \c class \c SetCOG, which sets the vertex position,
is constructed analogical.

Using these functors and <tt>std::for_each</tt> from the STL
the smoothing algorithm can be realized in a member function of 
\c SmootherT:

\dontinclude 04-stl_algorithms/smooth_algo.hh 
\skipline void smooth
\until }
\until }

<br>The complete example looks like this:

\include 04-stl_algorithms/smooth_algo.hh


\include 04-stl_algorithms/smooth.cc