#17: Add basic mathematical morphology filters
-----------------------+----------------------------------------------------
Reporter: levill_r | Owner: levill_r
Type: task | Status: closed
Priority: major | Milestone: Olena 2.0
Component: Olena | Version: 2.0
Resolution: fixed | Keywords: morpho erosion dilation opening closing
-----------------------+----------------------------------------------------
Changes (by levill_r):
* status: new => closed
* resolution: => fixed
Old description:
> Start with
> * erosion / dilation,
> * opening / closing.
>
> Write
> * simple versions using neighborhoods,
> * variants (binary, etc.) using using neighborhoods,
> * simple versions using dpoint sets (windows),
> * variants (binary, etc.) using using dpoint sets (windows).
New description:
Start with
* erosion / dilation,
* opening / closing.
Write
* ~~simple versions using neighborhoods~~ (postponed to a later version),
* ~~variants (binary, etc.) using using neighborhoods~~ (postponed to a
later version),
* simple versions using dpoint sets (windows),
* variants (binary, etc.) using using dpoint sets (windows).
Comment:
Those filters have been implemented for a while. They need some
refactoring though (see #74).
--
Ticket URL: <https://trac.lrde.org/olena/ticket/17#comment:4>
Olena <http://olena.lrde.epita.fr>
Olena, a generic and efficient C++ image processing library.
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena
Index: ChangeLog
from Thierry Geraud <thierry.geraud(a)lrde.epita.fr>
Add a default ordering (util::less); update (d)points and vec.
* mln/core/point.hh (todo): New in doc.
(to_vec): New; was missing.
(less_than): New specialization.
* mln/core/dpoint.hh (to_vec): Avoid copy at return.
(less_than): New specialization.
* mln/core/box.hh: Rely on util::less.
(is_valid): New.
(contract): Add pre- and post-conditions.
* mln/core/concept/point_site.hh (operator<): Remove.
* mln/algebra/vec.hh (less_than): New.
* mln/util/set.hh: Rely on util::less.
* mln/util/less.hh: New.
* mln/util/less_than.hh: New.
algebra/vec.hh | 48 +++++++++++++++++++++++++-
core/box.hh | 30 +++++++++++++++-
core/concept/point_site.hh | 42 -----------------------
core/dpoint.hh | 51 ++++++++++++++++++++++++++--
core/point.hh | 66 ++++++++++++++++++++++++++++++++++++
util/less.hh | 80 ++++++++++++++++++++++++++++++++++++++++++++
util/less_than.hh | 81 +++++++++++++++++++++++++++++++++++++++++++++
util/set.hh | 8 +++-
8 files changed, 356 insertions(+), 50 deletions(-)
Index: mln/core/point.hh
--- mln/core/point.hh (revision 2024)
+++ mln/core/point.hh (working copy)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007 EPITA Research and Development Laboratory
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -31,6 +31,8 @@
/*! \file mln/core/point.hh
*
* \brief Definition of the generic point class mln::point_.
+ *
+ * \todo Try to generalize less_than with Ml and Mr.
*/
# include <mln/core/concept/point.hh>
@@ -47,6 +49,7 @@
{
/// \{ Fwd decls.
+ template <typename M, typename C> struct point_;
template <typename M, typename C> struct dpoint_;
namespace literal {
struct zero_t;
@@ -154,6 +157,9 @@
operator typename internal::point_to_<M, C>::metal_vec () const;
operator algebra::vec<M::dim, float> () const;
+ /// Explicit conversion towards mln::algebra::vec.
+ const algebra::vec<M::dim, C>& to_vec() const;
+
/// Transform to point in homogene coordinate system.
h_vec<M::dim, C> to_h_vec() const;
@@ -162,6 +168,38 @@
};
+ namespace util
+ {
+
+ /*! \brief Ordering "less than" comparison between a couple of
+ * points.
+ *
+ * The ordering is based on a lexicographical ordering over
+ * coordinates.
+ *
+ * Both points have to be defined on the same topology.
+ *
+ * \warning In the general case this ordering relationship is \em
+ * not bound to the way of browsing a domain with a forward point
+ * iterator.
+ */
+ template <typename M, typename Cl, typename Cr>
+ struct less_than< point_<M,Cl>,
+ point_<M,Cr> >
+ {
+ /*! \brief Comparison between a couple of points \a lhs and \a
+ * rhs.
+ *
+ * \return True if \p lhs is before \p rhs in the sense of the
+ * coordinates lexicographic comparison, otherwise false.
+ */
+ bool operator()(const point_<M,Cl>& lhs,
+ const point_<M,Cr>& rhs) const;
+ };
+
+ } // end of namespace mln::util
+
+
# ifndef MLN_INCLUDE_ONLY
template <typename M, typename C>
@@ -327,6 +365,14 @@
template <typename M, typename C>
inline
+ const algebra::vec<M::dim, C>&
+ point_<M,C>::to_vec() const
+ {
+ return coord_;
+ }
+
+ template <typename M, typename C>
+ inline
h_vec<M::dim, C> point_<M,C>::to_h_vec() const
{
h_vec<M::dim, C> tmp;
@@ -336,6 +382,24 @@
return tmp;
}
+
+ namespace util
+ {
+
+ template <typename M, typename Cl, typename Cr>
+ bool
+ less_than< point_<M,Cl>,
+ point_<M,Cr> >::operator()(const point_<M,Cl>& lhs,
+ const point_<M,Cr>& rhs) const
+ {
+ enum { n = M::dim };
+ typedef less_than< algebra::vec<n,Cl>, algebra::vec<n,Cr> > less_t;
+ static const less_t op = less_t();
+ return op(lhs.to_vec(), rhs.to_vec());
+ }
+
+ } // end of namespace mln::util
+
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln
Index: mln/core/dpoint.hh
--- mln/core/dpoint.hh (revision 2024)
+++ mln/core/dpoint.hh (working copy)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007 EPITA Research and Development Laboratory
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -123,13 +123,41 @@
operator algebra::vec<M::dim, Q>() const;
/// Explicit conversion.
- algebra::vec<M::dim, C> to_vec() const;
+ const algebra::vec<M::dim, C>& to_vec() const;
protected:
algebra::vec<M::dim, C> coord_;
};
+ namespace util
+ {
+
+ /*! \brief Ordering "less than" comparison between a couple of
+ * delta-points.
+ *
+ * The ordering is based on a lexicographical ordering over
+ * coordinates.
+ *
+ * Both delta-points have to be defined on the same topology.
+ */
+ template <typename M, typename Cl, typename Cr>
+ struct less_than< dpoint_<M,Cl>,
+ dpoint_<M,Cr> >
+ {
+ /*! \brief Comparison between a couple of delta-points \a lhs
+ * and \a rhs.
+ *
+ * \return True if \p lhs is before \p rhs in the sense of the
+ * coordinates lexicographic comparison, otherwise false.
+ */
+ bool operator()(const dpoint_<M,Cl>& lhs,
+ const dpoint_<M,Cr>& rhs) const;
+ };
+
+ } // end of namespace mln::util
+
+
# ifndef MLN_INCLUDE_ONLY
template <typename M, typename C>
@@ -244,12 +272,29 @@
template <typename M, typename C>
inline
- algebra::vec<M::dim, C>
+ const algebra::vec<M::dim, C>&
dpoint_<M,C>::to_vec() const
{
return coord_;
}
+ namespace util
+ {
+
+ template <typename M, typename Cl, typename Cr>
+ bool
+ less_than< dpoint_<M,Cl>,
+ dpoint_<M,Cr> >::operator()(const dpoint_<M,Cl>& lhs,
+ const dpoint_<M,Cr>& rhs) const
+ {
+ enum { n = M::dim };
+ typedef less_than< algebra::vec<n,Cl>, algebra::vec<n,Cr> > less_t;
+ static const less_t op = less_t();
+ return op(lhs.to_vec(), rhs.to_vec());
+ }
+
+ } // end of namespace mln::util
+
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln
Index: mln/core/box.hh
--- mln/core/box.hh (revision 2024)
+++ mln/core/box.hh (working copy)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007 EPITA Research and Development Laboratory
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -37,6 +37,7 @@
# include <mln/core/internal/box_impl.hh>
# include <mln/core/point.hh>
# include <mln/literal/origin.hh>
+# include <mln/util/less.hh>
namespace mln
@@ -124,7 +125,12 @@
/// Give a larger box.
box_<P> to_larger(unsigned b) const;
+ /// Test that the box owns valid data, i.e., pmin is 'less-than'
+ /// pmax.
+ bool is_valid() const;
+
protected:
+
P pmin_, pmax_;
};
@@ -146,9 +152,20 @@
template <typename P>
inline
+ bool
+ box_<P>::is_valid() const
+ {
+ typedef util::less<P> less_t;
+ static const less_t op = less_t();
+ return op(pmin_, pmax_);
+ }
+
+ template <typename P>
+ inline
P
box_<P>::pmin() const
{
+ mln_precondition(is_valid());
return pmin_;
}
@@ -165,6 +182,7 @@
P
box_<P>::pmax() const
{
+ mln_precondition(is_valid());
return pmax_;
}
@@ -188,7 +206,7 @@
: pmin_(pmin),
pmax_(pmax)
{
-
+ mln_precondition(is_valid());
}
template <typename P>
@@ -207,6 +225,7 @@
metal::bool_<(dim == 2)>::check();
pmin_ = literal::origin;
pmax_ = P(nrows - 1, ncols - 1);
+ mln_postcondition(is_valid());
}
template <typename P>
@@ -216,6 +235,7 @@
metal::bool_<(dim == 3)>::check();
pmin_ = literal::origin;
pmax_ = P(nslis - 1, nrows - 1, ncols - 1);
+ mln_postcondition(is_valid());
}
template <typename P>
@@ -223,6 +243,7 @@
bool
box_<P>::has(const P& p) const
{
+ mln_precondition(is_valid());
for (unsigned i = 0; i < P::dim; ++i)
if (p[i] < pmin_[i] || p[i] > pmax_[i])
return false;
@@ -234,11 +255,13 @@
void
box_<P>::enlarge(unsigned b)
{
+ mln_precondition(is_valid());
for (unsigned i = 0; i < P::dim; ++i)
{
pmin_[i] -= b;
pmax_[i] += b;
}
+ mln_postcondition(is_valid());
}
@@ -247,6 +270,7 @@
box_<P>
box_<P>::to_larger(unsigned b) const
{
+ mln_precondition(is_valid());
box_<P> tmp(*this);
for (unsigned i = 0; i < P::dim; ++i)
@@ -254,6 +278,7 @@
tmp.pmin_[i] -= b;
tmp.pmax_[i] += b;
}
+ mln_postcondition(tmp.is_valid());
return tmp;
}
@@ -261,6 +286,7 @@
inline
std::ostream& operator<<(std::ostream& ostr, const box_<P>& b)
{
+ mln_precondition(b.is_valid());
return ostr << "[" << b.pmin() << ".." << b.pmax() << ']';
}
Index: mln/core/concept/point_site.hh
--- mln/core/concept/point_site.hh (revision 2024)
+++ mln/core/concept/point_site.hh (working copy)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007 EPITA Research and Development Laboratory
+// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -145,30 +145,6 @@
bool operator==(const Point_Site<L>& lhs, const Point_Site<R>& rhs);
- /*! \brief Ordering "less than" comparison between a couple of
- * point sites \p lhs and \p rhs.
- *
- * \param[in] lhs A first point site.
- * \param[in] rhs A second point site.
- *
- * This test is based on a lexicographical ordering over coordinates.
- *
- * \warning In the general case this ordering relationship is \em
- * not bound to the way of browsing a domain with a forward point
- * iterator.
- *
- * \pre Both \p lhs and \p rhs have to be defined on the same
- * topology; otherwise this test does not compile.
- *
- * \return True if \p lhs is before \p rhs in the sense of the
- * coordinates lexicographic comparison, otherwise false.
- *
- * \relates mln::Point_Site
- */
- template <typename L, typename R>
- bool operator<(const Point_Site<L>& lhs, const Point_Site<R>& rhs);
-
-
/*! \brief Difference between a couple of point site \p lhs and \p
* rhs.
*
@@ -281,22 +257,6 @@
template <typename L, typename R>
inline
- bool operator<(const Point_Site<L>& lhs_, const Point_Site<R>& rhs_)
- {
- mln::metal::bool_<(int(L::dim) == int(R::dim))>::check();
- const L& lhs = exact(lhs_);
- const R& rhs = exact(rhs_);
- for (unsigned i = 0; i < L::dim; ++i)
- {
- if (lhs[i] == rhs[i])
- continue;
- return lhs[i] < rhs[i];
- }
- return false;
- }
-
- template <typename L, typename R>
- inline
mln_dpoint(L) // FIXME: promote!
operator-(const Point_Site<L>& lhs_, const Point_Site<R>& rhs_)
{
Index: mln/algebra/vec.hh
--- mln/algebra/vec.hh (revision 2024)
+++ mln/algebra/vec.hh (working copy)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006 EPITA Research and Development Laboratory
+// Copyright (C) 2006, 2008 EPITA Research and Development Laboratory
//
// This file is part of the Olena Library. This library is free
// software; you can redistribute it and/or modify it under the terms
@@ -42,6 +42,7 @@
# include <mln/trait/all.hh>
# include <mln/trait/value_.hh>
# include <mln/fun/i2v/all_to.hh>
+# include <mln/util/less_than.hh>
# include <mln/debug/format.hh>
# include <mln/value/ops.hh>
@@ -63,6 +64,19 @@
template <unsigned d, typename C> struct h_vec;
+ namespace util
+ {
+
+ template <unsigned n, typename Tl, typename Tr>
+ struct less_than< algebra::vec<n,Tl>,
+ algebra::vec<n,Tr> >
+ {
+ bool operator()(const algebra::vec<n,Tl>& lhs,
+ const algebra::vec<n,Tr>& rhs) const;
+ };
+
+ } // end of namespace mln::util
+
namespace trait
{
@@ -326,9 +340,16 @@
vec<3, mln_trait_op_times(T,U)> // FIXME: Sum of product...
vprod(const vec<3, T>& lhs, const vec<3, U>& rhs);
+ } // end of namespace mln::algebra
+
+
+
# ifndef MLN_INCLUDE_ONLY
+ namespace algebra
+ {
+
template <unsigned n, typename T>
inline
vec<n,T>::vec()
@@ -561,10 +582,33 @@
return tmp;
}
-# endif // MLN_INCLUDE_ONLY
} // end of namespace mln::algebra
+
+ namespace util
+ {
+
+ template <unsigned n, typename Tl, typename Tr>
+ bool
+ less_than< algebra::vec<n,Tl>,
+ algebra::vec<n,Tr> >::operator()(const algebra::vec<n,Tl>& lhs,
+ const algebra::vec<n,Tr>& rhs) const
+ {
+ for (unsigned i = 0; i < n; ++i)
+ {
+ if (lhs[i] == rhs[i])
+ continue;
+ return lhs[i] < rhs[i];
+ }
+ return false;
+ }
+
+ } // end of namespace mln::util
+
+
+# endif // MLN_INCLUDE_ONLY
+
} // end of namespace mln
Index: mln/util/set.hh
--- mln/util/set.hh (revision 2024)
+++ mln/util/set.hh (working copy)
@@ -42,6 +42,7 @@
# include <iostream>
# include <mln/core/contract.hh>
+# include <mln/util/less.hh>
namespace mln
@@ -66,7 +67,12 @@
* The parameter \c T is the element type, which shall not be
* const-qualified.
*
+ * The unicity of set elements is handled by the mln::util::less
+ * function-object.
+ *
* \todo Add a remove method.
+ *
+ * \see mln::util::less
*/
template <typename T>
class set
@@ -174,7 +180,7 @@
*
* This structure is always up-to-date w.r.t. the set contents.
*/
- mutable std::set<T> s_;
+ mutable std::set< T, mln::util::less<T> > s_;
/*! \brief Freeze the contents of the set (update \a v_ from \a
Index: mln/util/less.hh
--- mln/util/less.hh (revision 0)
+++ mln/util/less.hh (revision 0)
@@ -0,0 +1,80 @@
+// Copyright (C) 2008 EPITA Research and Development Laboratory
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library 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 library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#ifndef MLN_UTIL_LESS_HH
+# define MLN_UTIL_LESS_HH
+
+/*! \file mln/util/less.hh
+ *
+ * \brief Definition of a "less" function-object with one type (T).
+ *
+ * \todo Use mln_trait_op_less as return type (?)
+ */
+
+# include <mln/util/less_than.hh>
+
+
+namespace mln
+{
+
+ namespace util
+ {
+
+ /*! \brief Function-object to define a less-than comparison
+ * between a couple of objects with the same type \p T.
+ *
+ * This implementation relies on mln::util::less_than so prefer to
+ * overload mln::util::less_than (because it is more general).
+ *
+ * \see mln::util::less_than
+ */
+ template <typename T>
+ struct less
+ {
+ bool operator()(const T& lhs, const T& rhs) const;
+ };
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename T>
+ inline
+ bool
+ less<T>::operator()(const T& lhs, const T& rhs) const
+ {
+ static const less_than<T,T> lt_ = less_than<T,T>();
+ return lt_(lhs, rhs);
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::util
+
+} // end of namespace mln
+
+
+#endif // ! MLN_UTIL_LESS_HH
Index: mln/util/less_than.hh
--- mln/util/less_than.hh (revision 0)
+++ mln/util/less_than.hh (revision 0)
@@ -0,0 +1,81 @@
+// Copyright (C) 2008 EPITA Research and Development Laboratory
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library 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 library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#ifndef MLN_UTIL_LESS_THAN_HH
+# define MLN_UTIL_LESS_THAN_HH
+
+/*! \file mln/util/less_than.hh
+ *
+ * \brief Definition of a "less-than" function-object with a couple of
+ * types (L and R).
+ *
+ * \todo Use mln_trait_op_less as return type (?)
+ */
+
+
+namespace mln
+{
+
+ namespace util
+ {
+
+ /*! \brief Function-object to define a less-than comparison
+ * between a couple of objects respectively with types \p L and
+ * \p R.
+ *
+ * This is a default implementation that relies on 'operator<'.
+ * It shall be overloaded for types that do not have such an
+ * operator when those types are involved with an ordered
+ * container.
+ *
+ * \see mln::util::less_than
+ */
+ template <typename L, typename R>
+ struct less_than
+ {
+ bool operator()(const L& lhs, const R& rhs) const;
+ };
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename L, typename R>
+ inline
+ bool
+ less_than<L,R>::operator()(const L& lhs, const R& rhs) const
+ {
+ return lhs < rhs;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::util
+
+} // end of namespace mln
+
+
+#endif // ! MLN_UTIL_LESS_THAN_HH
https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox
Index: ChangeLog
from Roland Levillain <roland(a)lrde.epita.fr>
Start to clean up levillain/statues/.
* levillain/statues/mesh-segm.cc (main): Process command line
arguments.
Typos and aesthetic changes.
(color2uchar, write_verts_asc, write_faces_asc_colored)
(write_off_colored): Move these I/O
routines...
* levillain/statues/io.hh: ...here.
* levillain/statues/test: New test.
* levillain/statues/Makefile (mesh-segm.o): Depend on io.hh
(check): Depend on test.
Run `test' instead of `mesh-segm'.
Makefile | 5 +-
io.hh | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
mesh-segm.cc | 139 +++++++++++++----------------------------------------------
test | 9 +++
4 files changed, 159 insertions(+), 109 deletions(-)
Index: levillain/statues/test
--- levillain/statues/test (revision 0)
+++ levillain/statues/test (revision 0)
@@ -0,0 +1,9 @@
+#! /bin/sh
+
+set -ex
+
+mesh_dir=../../../mesh
+
+time ./mesh-segm $mesh_dir/socket.off 25 socket-segm.off
+time ./mesh-segm $mesh_dir/teapot.off 50 teapot-segm.off
+time ./mesh-segm $mesh_dir/bunny-holefilled.off 500 bunny-holefilled-segm.off
Property changes on: levillain/statues/test
___________________________________________________________________
Name: svn:executable
+ *
Index: levillain/statues/io.hh
--- levillain/statues/io.hh (revision 0)
+++ levillain/statues/io.hh (revision 0)
@@ -0,0 +1,115 @@
+// Copyright (C) 2008 EPITA Research and Development Laboratory (LRDE)
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library 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 library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#ifndef SANDBOX_LEVILLAIN_IO_HH
+# define SANDBOX_LEVILLAIN_IO_HH
+
+/// \brief I/O routines adapted from TriMesh's ones.
+
+/// Taken from TriMesh_io.cc
+/// \{
+// Convert colors float -> uchar
+inline unsigned char color2uchar(float p)
+{
+ return min(max(int(255.0f * p + 0.5f), 0), 255);
+}
+
+// Write a bunch of vertices to an ASCII file
+inline void write_verts_asc(TriMesh *mesh, FILE *f,
+ const char *before_vert,
+ const char *before_norm,
+ const char *before_color,
+ bool float_color,
+ const char *before_conf,
+ const char *after_line)
+{
+ for (int i = 0; i < mesh->vertices.size(); i++) {
+ fprintf(f, "%s%.7g %.7g %.7g", before_vert,
+ mesh->vertices[i][0],
+ mesh->vertices[i][1],
+ mesh->vertices[i][2]);
+ if (!mesh->normals.empty() && before_norm)
+ fprintf(f, "%s%.7g %.7g %.7g", before_norm,
+ mesh->normals[i][0],
+ mesh->normals[i][1],
+ mesh->normals[i][2]);
+ if (!mesh->colors.empty() && before_color && float_color)
+ fprintf(f, "%s%.7g %.7g %.7g", before_color,
+ mesh->colors[i][0],
+ mesh->colors[i][1],
+ mesh->colors[i][2]);
+ if (!mesh->colors.empty() && before_color && !float_color)
+ fprintf(f, "%s%d %d %d", before_color,
+ color2uchar(mesh->colors[i][0]),
+ color2uchar(mesh->colors[i][1]),
+ color2uchar(mesh->colors[i][2]));
+ if (!mesh->confidences.empty() && before_conf)
+ fprintf(f, "%s%.7g", before_conf, mesh->confidences[i]);
+ fprintf(f, "%s\n", after_line);
+ }
+}
+/// \}
+
+
+/// Taken and adapted from TriMesh_io.cc
+/// \{
+/// Write a bunch of faces to an ASCII file with colors.
+inline void write_faces_asc_colored(TriMesh *mesh,
+ const std::vector<mln::value::rgb8>& colors,
+ FILE *f,
+ const char *before_face,
+ const char *after_line)
+{
+ mesh->need_faces();
+ for (int i = 0; i < mesh->faces.size(); i++)
+ {
+ fprintf(f, "%s%d %d %d %d %d %d%s\n",
+ before_face,
+ mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
+ int(colors[i].red()),
+ int(colors[i].green()),
+ int(colors[i].blue()),
+ after_line);
+ }
+}
+
+/// Write an off file with colors.
+inline void write_off_colored(TriMesh *mesh,
+ const std::vector<mln::value::rgb8>& colors,
+ FILE *f)
+{
+ fprintf(f, "OFF\n");
+ mesh->need_faces();
+ fprintf(f, "%lu %lu 0\n", (unsigned long) mesh->vertices.size(),
+ (unsigned long) mesh->faces.size());
+ write_verts_asc(mesh, f, "", 0, 0, false, 0, "");
+ write_faces_asc_colored(mesh, colors, f, "3 ", "");
+}
+/// \}
+
+
+#endif // ! SANDBOX_LEVILLAIN_IO_HH
Index: levillain/statues/mesh-segm.cc
--- levillain/statues/mesh-segm.cc (revision 2021)
+++ levillain/statues/mesh-segm.cc (working copy)
@@ -25,6 +25,7 @@
// reasons why the executable file might be covered by the GNU General
// Public License.
+#include <cstdlib>
#include <cmath>
#include <utility>
@@ -44,117 +45,45 @@
#include <mln/morpho/closing_area.hh>
#include <mln/morpho/meyer_wst.hh>
+#include "io.hh"
+
// Doesn't C++ have a better way to express Pi ?
const float pi = 4 * atanf(1);
-// -------------------------------------------------------------------
-// Taken from TriMesh_io.cc
-// -------------------------------------------------------------------
-// Convert colors float -> uchar
-static unsigned char color2uchar(float p)
-{
- return min(max(int(255.0f * p + 0.5f), 0), 255);
-}
-
-// Write a bunch of vertices to an ASCII file
-static void write_verts_asc(TriMesh *mesh, FILE *f,
- const char *before_vert,
- const char *before_norm,
- const char *before_color,
- bool float_color,
- const char *before_conf,
- const char *after_line)
-{
- for (int i = 0; i < mesh->vertices.size(); i++) {
- fprintf(f, "%s%.7g %.7g %.7g", before_vert,
- mesh->vertices[i][0],
- mesh->vertices[i][1],
- mesh->vertices[i][2]);
- if (!mesh->normals.empty() && before_norm)
- fprintf(f, "%s%.7g %.7g %.7g", before_norm,
- mesh->normals[i][0],
- mesh->normals[i][1],
- mesh->normals[i][2]);
- if (!mesh->colors.empty() && before_color && float_color)
- fprintf(f, "%s%.7g %.7g %.7g", before_color,
- mesh->colors[i][0],
- mesh->colors[i][1],
- mesh->colors[i][2]);
- if (!mesh->colors.empty() && before_color && !float_color)
- fprintf(f, "%s%d %d %d", before_color,
- color2uchar(mesh->colors[i][0]),
- color2uchar(mesh->colors[i][1]),
- color2uchar(mesh->colors[i][2]));
- if (!mesh->confidences.empty() && before_conf)
- fprintf(f, "%s%.7g", before_conf, mesh->confidences[i]);
- fprintf(f, "%s\n", after_line);
- }
-}
-// -------------------------------------------------------------------
-
-
-// -------------------------------------------------------------------
-// Taken and adapted from TriMesh_io.cc
-// -------------------------------------------------------------------
-// Write a bunch of faces to an ASCII file with colors.
-static void write_faces_asc_colored(TriMesh *mesh,
- const std::vector<mln::value::rgb8>& colors,
- FILE *f,
- const char *before_face,
- const char *after_line)
+int main(int argc, char* argv[])
{
- mesh->need_faces();
- for (int i = 0; i < mesh->faces.size(); i++)
+ if (argc != 4)
{
- fprintf(f, "%s%d %d %d %d %d %d%s\n",
- before_face,
- mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2],
- int(colors[i].red()),
- int(colors[i].green()),
- int(colors[i].blue()),
- after_line);
- }
+ std::cerr << "usage: " << argv[1] << " input.off lambda output.off";
+ exit(1);
}
-/// Write an off file with colors.
-static void write_off_colored(TriMesh *mesh,
- const std::vector<mln::value::rgb8>& colors,
- FILE *f)
-{
- fprintf(f, "OFF\n");
- mesh->need_faces();
- fprintf(f, "%lu %lu 0\n", (unsigned long) mesh->vertices.size(),
- (unsigned long) mesh->faces.size());
- write_verts_asc(mesh, f, "", 0, 0, false, 0, "");
- write_faces_asc_colored(mesh, colors, f, "3 ", "");
-}
-// -------------------------------------------------------------------
+ std::string input_filename = argv[1];
+ unsigned lambda = atoi(argv[2]);
+ std::string output_filename = argv[3];
-int main(int argc, char* argv[])
-{
/*-------.
| Mesh. |
`-------*/
-// std::string filename = "../../aroumougame/test/test.off";
- std::string filename = "bunny-holefilled.off";
-
- // TriMesh is a pain: it systematically allocate on the heap.
- TriMesh* mesh_ptr = TriMesh::read(filename.c_str());
+ // TriMesh is a pain: it systematically allocates on the heap.
+ // Introduce another name to manipulate the mesh as a (non-pointer)
+ // object.
+ TriMesh* mesh_ptr = TriMesh::read(input_filename.c_str());
if (!mesh_ptr)
- exit(1);
+ exit(2);
TriMesh& mesh = *mesh_ptr;
// Computes faces (triangles).
mesh.need_faces();
-
// Computation of the mean curvature on each vertex of the mesh.
mesh.need_curvatures();
- // FIXME: Our implementation of the WST doesn't work well with floats.
- // Convert to a proportional integer value for the moment.
+ /* FIXME: Our implementation of the WST doesn't work well with
+ floats. Convert floating point values to a proportional integer
+ value for the moment. */
typedef int curv_t;
std::vector<float> vertex_h_inv(mesh.vertices.size(), 0.f);
for (unsigned v = 0; v < mesh.vertices.size(); ++v)
@@ -162,8 +91,9 @@
float h = (mesh.curv1[v] + mesh.curv2[v]) / 2;
float h_inv = 1 / pi * atan(-h) + pi / 2;
/* FIXME: This coefficient is used to distinguish small
- curvature values. We sould get rid of it as soon as
- meyer_wst works correctly on image of float values. */
+ curvature values. We should get rid of it as soon as
+ morpho::meyer_wst works correctly on images of float
+ values. */
vertex_h_inv[v] = 1000 * h_inv;
}
@@ -202,7 +132,7 @@
// Find the edge (i.e., the two vertices) common to faces
// F and F_ADJ.
/* FIXME: We lack a proper interface from the TriMesh
- strucure to do this elegantly. */
+ structure to do this elegantly. */
std::vector<int> adj_vertices;
adj_vertices.reserve(2);
for (unsigned i = 0; i < 3; ++i)
@@ -239,22 +169,21 @@
nbh_t nbh;
ima_t closed_lg_ima (lg_ima.domain());
- mln::morpho::closing_area(lg_ima, nbh, 500, closed_lg_ima);
+ mln::morpho::closing_area(lg_ima, nbh, lambda, closed_lg_ima);
/*------.
| WST. |
`------*/
- // Perform a Watershed Transform.
typedef unsigned wst_val_t;
wst_val_t nbasins;
typedef mln::line_graph_image<mln::point3d, wst_val_t> wst_ima_t;
wst_ima_t wshed = mln::morpho::meyer_wst(closed_lg_ima, nbh, nbasins);
std::cout << "nbasins = " << nbasins << std::endl;
- /*------------------------------------.
- | Label graph vertices (mesh faces). |
- `------------------------------------*/
+ /*------------------------------------------.
+ | Label graph vertices (i.e., mesh faces). |
+ `------------------------------------------*/
/* FIXME: We should be using wshed.vertex_values_ if
mln::line_graph_image were fully functional... */
@@ -272,30 +201,26 @@
| Output. |
`---------*/
- // Get random colors for each basin.
+ // Choose random colors for each basin number.
std::vector<mln::value::rgb8> basin_color (nbasins + 1);
for (unsigned i = 0; i <= nbasins; ++i)
basin_color[i] = mln::value::rgb8(random() % 256,
random() % 256,
random() % 256);
-
- // Assign colors to faces.
+ // Assign colors to graph vertices (mesh faces).
std::vector<mln::value::rgb8> face_color (vertex_label.size());
for (unsigned i = 0; i < vertex_label.size() ; ++i)
face_color[i] = basin_color[vertex_label[i]];
- // FIXME: This is ugly; convert to C++ code ASAP.
// Taken and adapted from TriMesh_io.cc
- const char* out_filename = "out.off";
- FILE* f_out = fopen(out_filename, "wb");
+ FILE* f_out = fopen(output_filename.c_str(), "wb");
if (!f_out)
{
- perror("fopen");
- fprintf(stderr, "Error opening %s for writing.\n", out_filename);
- exit(1);
+ std::cerr << "Error opening " << output_filename.c_str()
+ << " for writing." << std::endl;
+ exit(2);
}
-
write_off_colored(mesh_ptr, face_color, f_out);
fclose(f_out);
Property changes on: levillain/statues/mesh-segm.cc
___________________________________________________________________
Name: svn:executable
- *
Index: levillain/statues/Makefile
--- levillain/statues/Makefile (revision 2021)
+++ levillain/statues/Makefile (working copy)
@@ -19,11 +19,12 @@
all: mesh-segm
+mesh-segm.o: mesh-segm.cc io.hh
mesh-segm: mesh-segm.o
$(CXX) -o $@ $(LDFLAGS) $<
-check: mesh-segm
- ./mesh-segm
+check: mesh-segm test
+ ./test
CLEANFILES = mesh-segm.o mesh-segm
clean: