r3779: Make clean in tree files and procedures

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox ChangeLog: 2009-05-10 Edwin Carlinet <carlinet@lrde.epita.fr> Make clean in tree files and procedures. * edwin/mln/morpho/tree/filter.hh: group strategies for non-increasing attribute filtering. * edwin/attributes/bbox.hh, * edwin/attributes/occupation.hh, * edwin/attributes, * edwin/mln/morpho/attribute, * edwin/mln/morpho/tree/components.hh, * edwin/mln/morpho/tree/propagate.hh, * edwin/mln/morpho/tree/propagate_if.hh, * edwin/mln/morpho/tree/propagate_node.hh, * edwin/mln/morpho/tree, * edwin/mln/morpho, * edwin/mln, * edwin/rush/exo2/test.cc, * edwin/tests, * edwin/tree/components.hh, * edwin/tree/propagate.hh, * edwin/tree/propagate_if.hh, * edwin/tree/propagate_node.hh, move all tree materials in edwin/mln/morpho/tree. --- mln/morpho/attribute/bbox.hh | 203 ++++++++++++++++++++++ mln/morpho/attribute/occupation.hh | 252 ++++++++++++++++++++++++++++ mln/morpho/tree/components.hh | 319 ++++++++++++++++++++++++++++++++++++ mln/morpho/tree/filter.hh | 205 +++++++++++++++++++++++ mln/morpho/tree/propagate.hh | 124 ++++++++++++++ mln/morpho/tree/propagate_if.hh | 327 +++++++++++++++++++++++++++++++++++++ mln/morpho/tree/propagate_node.hh | 209 +++++++++++++++++++++++ rush/exo2/test.cc | 1 8 files changed, 1640 insertions(+) Index: trunk/milena/sandbox/edwin/tree/propagate.hh (deleted) =================================================================== Index: trunk/milena/sandbox/edwin/tree/components.hh (deleted) =================================================================== Index: trunk/milena/sandbox/edwin/tree/propagate_node.hh (deleted) =================================================================== Index: trunk/milena/sandbox/edwin/tree/propagate_if.hh (deleted) =================================================================== Index: trunk/milena/sandbox/edwin/rush/exo2/test.cc =================================================================== --- trunk/milena/sandbox/edwin/rush/exo2/test.cc (revision 3778) +++ trunk/milena/sandbox/edwin/rush/exo2/test.cc (revision 3779) @@ -18,6 +18,7 @@ #include <mln/fun/p2v/ternary.hh> #include <mln/pw/all.hh> + namespace mln { // Sharpness Attribute -> Height Attribute Index: trunk/milena/sandbox/edwin/mln/morpho/tree/propagate.hh =================================================================== --- trunk/milena/sandbox/edwin/mln/morpho/tree/propagate.hh (revision 0) +++ trunk/milena/sandbox/edwin/mln/morpho/tree/propagate.hh (revision 3779) @@ -0,0 +1,124 @@ +// Copyright (C) 2008, 2009 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 MLN_MORPHO_TREE_PROPAGATE_HH_ +# define MLN_MORPHO_TREE_PROPAGATE_HH_ + +/// \file mln/morpho/tree/propagate.hh +/// +/// Functions to propagate a node value in the tree. + +# include <mln/morpho/tree/data.hh> +# include <mln/morpho/tree/propagate_node.hh> + +namespace mln { + namespace morpho { + namespace tree { + + + /// Propagate the representative point's value to + /// non-representative node points. + template <typename T, typename A> + void + propagate_representative(const T& t, Image<A>& a_) + { + A a = exact(a_); + mln_up_site_piter(T) p(t); + for_all(p) + if (! t.is_a_node(p)) + { + mln_assertion(t.is_a_node(t.parent(p))); + a(p) = a(t.parent(p)); + } + } + + /** + ** For each component in the list \p component_list, it + ** propagates the representant value to the remaining nodes of the + ** component. The value of a node that doesn't belong to a component is + ** set to \p null. + ** + ** @param attr_image The attribute image. + ** @param tree The component tree used to propagate value. + ** @param component_list The list of components. + ** @param null The nodes that don't belong to components will be set + ** with this value. + ** + ** @return The resulting component image. + */ + template <typename A, typename T> + inline + A propagate_components(const Image<A>& attr_image, + const T& tree, + const p_array< mln_psite(A) >& component_list, + const mln_value(A)& null) + { + const A& a = exact(attr_image); + A out; + initialize(out, a); + mln::data::fill(out, null); + + mln_piter(p_array<mln_psite(A)>) p(component_list); + for_all(p) + { + out(p) = a(p); + morpho::tree::propagate_node_to_descendants(p, tree, out, a(p)); + } + morpho::tree::propagate_representative(tree, out); + return out; + } + + + template <typename T, typename V> + inline + mln_ch_value(typename T::function, V) + set_value_to_components(const T& tree, + const p_array< mln_psite(T) >& component_list, + const V& value, + const V& null) + { + mln_ch_value(typename T::function, V) out; + initialize(out, tree.f()); + mln::data::fill(out, null); + + mln_piter(p_array< mln_psite(T) >) p(component_list); + for_all(p) + { + out(p) = value; + morpho::tree::propagate_node_to_descendants(p, tree, out); + } + morpho::tree::propagate_representative(tree, out); + return out; + } + + } // end of namespace mln::morpho::tree + } // end of namespace mln::morpho +} // end of namespace mln + +#endif /* !MLN_MORPHO_TREE_PROPAGATE_HH_ */ + + Property changes on: trunk/milena/sandbox/edwin/mln/morpho/tree/propagate.hh ___________________________________________________________________ Added: svn:mergeinfo Index: trunk/milena/sandbox/edwin/mln/morpho/tree/components.hh =================================================================== --- trunk/milena/sandbox/edwin/mln/morpho/tree/components.hh (revision 0) +++ trunk/milena/sandbox/edwin/mln/morpho/tree/components.hh (revision 3779) @@ -0,0 +1,319 @@ +// Copyright (C) 2007, 2008, 2009 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 MLN_MORPHO_TREE_COMPONENTS_HH_ +# define MLN_MORPHO_TREE_COMPONENTS_HH_ + +# include <mln/core/concept/image.hh> +# include <mln/core/concept/function.hh> +# include <mln/data/fill.hh> + +# include <mln/morpho/tree/data.hh> +# include "propagate_node.hh" + +# include <mln/trace/entering.hh> +# include <mln/trace/exiting.hh> + + +/** +** \file mln/morpho/tree/components.hh +** +** Routines that offers different way of retrieving tree components. +** Tree components are nodes maximising the attribute. +*/ + + +namespace mln { + namespace morpho { + namespace tree { + + /** + ** Retrieve components from the tree until all leaves belong to + ** components. + ** + ** @param tree Component tree. + ** @param attr_image Attribute image. + ** + ** @return Array of components. + */ + template <typename T, typename A> + p_array< mln_psite(A) > + get_components(const T& tree, const Image<A>& attr_image); + + /** + ** Retrieve the \p n most important components from the tree. n + ** should be lesser than the maximum number of components. If + ** not, the functions stops when there's no more components. + ** + ** @param tree Component tree. + ** @param attr_image Attribute image. + ** @param n Number of components to get. + ** + ** @return Array of components. + */ + template <typename T, typename A> + p_array< mln_psite(A) > + get_components(const T& tree, const Image<A>& attr_image, unsigned n); + + /** + ** Retrieve the most important components that check + ** predicate \p pred. + ** + ** @param tree Component tree. + ** @param attr_image Attribute image. + ** @param pred Predicate that components must check. + ** + ** @return Array of components. + */ + template <typename T, typename A, typename P2B> + p_array< mln_psite(A) > + get_components(const T& tree, const Image<A>& attr_image, const Function_p2b<P2B>& pred); + + +# ifndef MLN_INCLUDE_ONLY + + namespace internal + { + template <typename P, typename I> + struct desc_sort + { + bool operator()(const P& p1, const P& p2) const + { + return ima(p1) > ima(p2); + } + const I& ima; + + desc_sort(const I& ima) : ima(ima) {} + }; + + template <typename P, typename I> + inline + void + sort(p_array<P>& arr, const I& ima_) + { + mlc_converts_to(P, mln_psite(I))::check(); + + const I& ima = exact(ima_); + std::vector<P>& v = arr.hook_std_vector_(); + std::sort(v.begin(), v.end(), desc_sort<P,I>(ima)); + } + + template <typename A> + inline + bool + get_max(const p_array< mln_psite(A) >& arr, const A& activity, unsigned& pos) + { + while (pos < arr.nsites() && !activity(arr[pos])) + ++pos; + return (pos < arr.nsites()); + } + + + ////////////////////////// + // Predifined predicate // + ////////////////////////// + + /// Predicate used to gets components until tree leaves last. + template <typename T> + class pred_tree_leaves_last + { + public: + pred_tree_leaves_last(const T& tree) : + n_ (tree.leaves().nsites()) + { + } + + template <typename P> + bool operator() (const P& p, unsigned n, unsigned* nb_leaves) const + { + (void)p; + (void)n; + mln_assertion(nb_leaves != 0); + + return (*nb_leaves < n_); + } + + + private: + unsigned n_; + }; + + /// Predicate used to get n components. + class pred_n_components + { + public: + pred_n_components(unsigned n) : + n_(n) + { + } + + template <typename P> + bool operator() (const P& p, unsigned n, unsigned* nb_leaves) const + { + (void)p; + (void)nb_leaves; + + return (n < n_); + } + + + private: + unsigned n_; + }; + + /// Predicate used to get components that check a p2b function predicate. + template <typename P2B> + class pred_p2b + { + public: + pred_p2b(const P2B& f) : + f_ (f) + { + } + + template <typename P> + bool operator() (const P& p, unsigned n, unsigned* nb_leaves) const + { + (void)n; + (void)nb_leaves; + + return (f_(p)); + } + + + private: + const P2B& f_; + }; + + + + + template <typename T, typename A, class P> + p_array< mln_psite(A) > + get_components(const T& tree, const A& a, const P& pred, bool uses_leaves = 0) + { + + mln_psite(A) p; + p_array< mln_psite(A) > components; + mln_ch_value(A, bool) activity; + p_array< mln_psite(A) > max_arr = tree.nodes(); + unsigned arr_pos = 0; //position in max_array + unsigned n = 0; + unsigned* nb_leaves = uses_leaves ? new unsigned(0) : 0; + + initialize(activity, a); + mln::data::fill(activity, true); + internal::sort(max_arr, a); + + do + { + if (!internal::get_max(max_arr, activity, arr_pos)) + break; + p = max_arr[arr_pos]; + if (a(p) == 0) + break; + std::cout << p << " " << a(p) << std::endl; + components.insert(p); + morpho::tree::propagate_node_to_descendants(p, tree, activity, false, nb_leaves); + morpho::tree::propagate_node_to_ancestors(p, tree, activity, false); + activity(p) = false; + n++; + mln_assertion(nb_leaves == 0 || *nb_leaves < mln_max(unsigned)); + } + while (pred(p, n, nb_leaves)); + + if (uses_leaves) + delete nb_leaves; + return components; + } + + } // end of namespace mln::morpho::tree::internal + + + template <typename T, typename A> + p_array< mln_psite(A) > + get_components(const T& tree, const Image<A>& attr_image) + { + trace::entering("mln::morpho::tree::get_components"); + + const A& a = exact(attr_image); + mln_precondition(tree.f().domain() == a.domain()); + mln_precondition(a.is_valid()); + + p_array< mln_psite(A) > components = + internal::get_components<T, A>(tree, a, internal::pred_tree_leaves_last<T>(tree), true); + + trace::exiting("mln::morpho::tree::get_components"); + return components; + } + + + template <typename T, typename A> + p_array< mln_psite(A) > + get_components(const T& tree, const Image<A>& attr_image, unsigned n) + { + trace::entering("mln::morpho::tree::get_components"); + + const A& a = exact(attr_image); + mln_precondition(tree.f().domain() == a.domain()); + mln_precondition(a.is_valid()); + + p_array< mln_psite(A) > components; + + if (n > 0) + components = internal::get_components(tree, a, internal::pred_n_components(n), false); + + trace::exiting("mln::morpho::tree::get_components"); + return components; + } + + template <typename T, typename A, typename P2B> + p_array< mln_psite(A) > + get_components(const T& tree, const Image<A>& attr_image, const Function_p2b<P2B>& pred) + { + trace::entering("mln::morpho::tree::get_components"); + + const A& a = exact(attr_image); + const P2B& predicate = exact(pred); + mln_precondition(tree.f().domain() == a.domain()); + mln_precondition(a.is_valid()); + + p_array< mln_psite(A) > components = + internal::get_components(tree, a, internal::pred_p2b<P2B>(predicate), false); + + trace::exiting("mln::morpho::tree::get_components"); + return components; + } + + } // end of namespace mln::morpho::tree + } // end of namespace mln::morpho +} // end of namespace mln + +# endif /* !MLN_INCLUDE_ONLY */ + +#endif /* !MLN_MORPHO_TREE_COMPONENTS_HH_ */ Property changes on: trunk/milena/sandbox/edwin/mln/morpho/tree/components.hh ___________________________________________________________________ Added: svn:mergeinfo Index: trunk/milena/sandbox/edwin/mln/morpho/tree/filter.hh =================================================================== --- trunk/milena/sandbox/edwin/mln/morpho/tree/filter.hh (revision 0) +++ trunk/milena/sandbox/edwin/mln/morpho/tree/filter.hh (revision 3779) @@ -0,0 +1,205 @@ +// Copyright (C) 2009 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 MLN_MORPHO_TREE_FILTER_HH_ +# define MLN_MORPHO_TREE_FILTER_HH_ + +/** +** @file mln/morpho/tree/filter.hh +** +** @brief Methods to handle component tree filtering strategies with +** non-increasing attribute. Nevertheless, it works on increasing +** predicate as well. In this case, all strategies have the same +** result but min filter or direct filter should be used in term +** of performance. If a predicate test is not enough fast, then +** prefer the min filter that minimizes call to predicate. +*/ + +# include <mln/core/concept/function.hh> +# include <mln/morpho/tree/data.hh> +# include <mln/morpho/tree/propagate_if.hh> + +namespace mln { + namespace morpho { + namespace tree { + namespace filter { + + template <typename T, typename F, typename P2B> + inline + void + min(const T& tree, Image<F>& f_, const Function_p2b<P2B>& pred_); + + template <typename T, typename F, typename P2B> + inline + void + max(const T& tree, Image<F>& f_, const Function_p2b<P2B>& pred_); + + template <typename T, typename F, typename P2B> + inline + void + direct(const T& tree, Image<F>& f_, const Function_p2b<P2B>& pred_); + + template <typename T, typename F, typename P2B> + inline + void + subtractive(const T& tree, Image<F>& f_, const Function_p2b<P2B>& pred_); + + +# ifndef MLN_INCLUDE_ONLY + namespace internal + { + + template <typename P2B> + struct not_pred_ : Function_p2b< not_pred_<P2B> > + { + not_pred_(const Function_p2b<P2B>& f) : + f_ (exact(f)) + { + } + + template <typename P> + bool operator() (const P& p) const + { + return !(f_(p)); + } + + private: + const P2B& f_; + }; + + template <typename P2B> + inline + not_pred_<P2B> + not_pred(const Function_p2b<P2B>& f) + { + return not_pred_<P2B>(f); + } + + } + + + template <typename T, typename F, typename P2B> + inline + void + min(const T& tree, Image<F>& f_, const Function_p2b<P2B>& pred_) + { + F& f = exact(f_); + const P2B& pred = exact(pred_); + + //FIXME precondition + mln_ch_value(F, bool) mark; + initialize(mark, f); + mln::data::fill(mark, false); + + mln_dn_node_piter(T) n(tree); + for_all(n) + if (mark(tree.parent(n)) || !pred(n)) + { + f(n) = f(tree.parent(n)); + mark(n) = true; + } + //FIXME postcondition + } + + template <typename T, typename F, typename P2B> + inline + void + max(const T& tree, Image<F>& f_, const Function_p2b<P2B>& pred_) + { + F& f = exact(f_); + const P2B& pred = exact(pred_); + + //FIXME precondition + mln_ch_value(F, bool) mark; + initialize(mark, f); + mln::data::fill(mark, true); + + { + mln_up_node_piter(T) n(tree); + for_all(n) + if (!mark(n)) + mark(tree.parent(n)) = false; + else if (pred(n)) + { + mark(tree.parent(n)) = false; + mark(n) = false; + } + } + + { + mln_dn_node_piter(T) n(tree); + for_all(n) + if (mark(n)) + f(n) = f(tree.parent(n)); + } + } + + template <typename T, typename F, typename P2B> + inline + void + direct(const T& tree, Image<F>& f_, const Function_p2b<P2B>& pred_) + { + F& f = exact(f_); + const P2B& pred = exact(pred_); + + //FIXME precondition + mln_dn_node_piter(T) n(tree); + for_all(n) + if (!pred(n)) + f(n) = f(tree.parent(n)); + //FIXME postcondition + } + + template <typename T, typename F, typename P2B> + inline + void + subtractive(const T& tree, Image<F>& f_, const Function_p2b<P2B>& pred_) + { + F& f = exact(f_); + const P2B& pred = exact(pred_); + + //FIXME precondition + + morpho::tree::propagate_if(tree, f, morpho::tree::desc_propagation (), internal::not_pred(pred)); + + mln_up_node_piter(T) n(tree); + for_all(n) + if (!pred(n)) + f(n) = f(tree.parent(n)); + //FIXME postcondition + } + +# endif /* !MLN_INCLUDE_ONLY */ + + } // end of namespace mln::morpho::tree::filter + } // end of namespace mln::morpho::tree + } // end of namespace mln::morpho +} // end of namespace mln + + +#endif /* !MLN_MORPHO_TREE_FILTER_HH_ */ Index: trunk/milena/sandbox/edwin/mln/morpho/tree/propagate_node.hh =================================================================== --- trunk/milena/sandbox/edwin/mln/morpho/tree/propagate_node.hh (revision 0) +++ trunk/milena/sandbox/edwin/mln/morpho/tree/propagate_node.hh (revision 3779) @@ -0,0 +1,209 @@ +// Copyright (C) 2007, 2008, 2009 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 MLN_MORPHO_TREE_PROPAGATE_NODE_HH_ +# define MLN_MORPHO_TREE_PROPAGATE_NODE_HH_ + +# include <mln/core/concept/image.hh> +# include <mln/core/macros.hh> +# include <mln/morpho/tree/data.hh> + +/// \file mln/morpho/tree/propagate_node.hh +/// +/// Functions to propagate node in the tree. + +namespace mln { + namespace morpho { + namespace tree { + + /** + ** Propagate a value \p v from a node \p n to its descendants. + ** + ** @param n[in] Node to propagate. + ** @param t[in] Component tree used for propagation. + ** @param a_[in] Attribute image where values are propagated. + ** @param v[in] Value to propagate. + ** @param nb_leaves[out] Optional. Store the number of leaves in + ** the component. + */ + template <typename T, typename A> + void + propagate_node_to_descendants(mln_psite(A) n, + const T& t, + Image<A>& a_, + const mln_value(A)& v, + unsigned* nb_leaves = 0); + + /** + ** Propagate the node's value to its descendants. + ** + ** @param n[in] Node to propagate. + ** @param t[in] Component tree used for propagation. + ** @param a_[in] Attribute image where values are propagated. + ** @param nb_leaves[out] Optional. Store the number of leaves in + ** the component. + */ + template <typename T, typename A> + inline + void + propagate_node_to_descendants(mln_psite(A)& n, + const T& t, + Image<A>& a_, + unsigned* nb_leaves = 0); + + + /** + ** Propagate a value \v from a node \n to its ancestors. + ** + ** @param n Node to propagate. + ** @param t Component tree used for propagation. + ** @param a_ Attribute image where values are propagated. + ** @param v Value to propagate. + */ + template <typename T, typename A> + void + propagate_node_to_ancestors(mln_psite(A) n, + const T& t, + Image<A>& a_, + const mln_value(A)& v); + + /** + ** Propagate the node's value to its ancestors. + ** + ** @param n Node to propagate. + ** @param t Component tree used for propagation. + ** @param a_ Attribute image where values are propagated. + */ + template <typename T, typename A> + inline + void + propagate_node_to_ancestors(mln_psite(A) n, + const T& t, + Image<A>& a_); + + + # ifndef MLN_INCLUDE_ONLY + + /* Descendants propagation */ + + + template <typename T, typename A> + inline + void + propagate_node_to_descendants(mln_psite(A) n, + const T& t, + Image<A>& a_, + const mln_value(A)& v, + unsigned* nb_leaves = 0) + { + A& a = exact(a_); + mln_precondition(a.is_valid()); + mln_precondition(a.domain() == t.f().domain()); + mln_precondition(a.domain().has(n)); + + + if (!t.is_a_node(n)) // Get the representant. + n = t.parent(n); + mln_assertion(t.is_a_node(n)); + + typename T::preorder_piter pp(t, n); + + pp.start(); // We don't set n to v. + + if (nb_leaves) + *nb_leaves += t.is_a_leaf(pp); + + for (pp.next(); pp.is_valid(); pp.next()) + { + a(pp) = v; + if (nb_leaves && t.is_a_leaf(pp)) + ++(*nb_leaves); + } + } + + + template <typename T, typename A> + inline + void + propagate_node_to_descendants(mln_psite(A) n, + const T& t, + Image<A>& a_, + unsigned* nb_leaves = 0) + + { + A& a = exact(a_); + propagate_node_to_descendants(n, t, a, a(n), nb_leaves); + } + + + /* Ancestors propagation */ + + template <typename T, typename A> + void + propagate_node_to_ancestors(mln_psite(A) n, + const T& t, + Image<A>& a_, + const mln_value(A)& v) + { + A& a = exact(a_); + mln_precondition(a.is_valid()); + mln_precondition(a.domain() == t.f().domain()); + mln_precondition(a.domain().has(n)); + + if (!t.is_a_node(n)) // Get the representant. + n = t.parent(n); + mln_assertion(t.is_a_node(n)); + + if (t.is_root(n)) + return; + + do { + n = t.parent(n); + a(n) = v; + } while (!t.is_root(n)); + + } + + template <typename T, typename A> + inline + void + propagate_node_to_ancestors(mln_psite(A) n, + const T& t, + Image<A>& a_) + { + A& a = exact(a_); + propagate_node_to_ancestors(n, t, a, a(n)); + } + + # endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::morpho::tree + } // end of namespace mln::morpho +} // end of namespace mln + +#endif /* !MLN_MORPHO_TREE_PROPAGATE_NODE_HH_ */ Property changes on: trunk/milena/sandbox/edwin/mln/morpho/tree/propagate_node.hh ___________________________________________________________________ Added: svn:mergeinfo Index: trunk/milena/sandbox/edwin/mln/morpho/tree/propagate_if.hh =================================================================== --- trunk/milena/sandbox/edwin/mln/morpho/tree/propagate_if.hh (revision 0) +++ trunk/milena/sandbox/edwin/mln/morpho/tree/propagate_if.hh (revision 3779) @@ -0,0 +1,327 @@ +// Copyright (C) 2007, 2008, 2009 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 MLN_MORPHO_TREE_PROPAGATE_IF_HH_ +# define MLN_MORPHO_TREE_PROPAGATE_IF_HH_ + +/** +** @file mln/morpho/tree/propagate_if.hh +** +** @brief Methods to handle propagation startegies +** in component trees. +** +*/ + +# include <mln/morpho/tree/data.hh> +# include <mln/morpho/tree/propagate_node.hh> + +# include <mln/data/fill.hh> +# include <mln/pw/all.hh> + +namespace mln { + namespace morpho { + namespace tree { + + template <typename WP> + struct way_of_propagation : Object< WP > { protected: way_of_propagation() {}; }; + struct desc_propagation : way_of_propagation <desc_propagation> {}; + struct asc_propagation : way_of_propagation <asc_propagation> {}; + + /** + ** Propagate nodes checking the predicate \p pred in the way + ** defined by \p way_of_propagation. + ** + ** @param tree Component tree used for propagation. + ** @param a_ Attributed image where values are propagated. + ** @param way_of_propagation Propagate node in acsendant or + ** descendant way. + ** @param pred Predicate that node must check to be propagated. + ** @param v Value to be propagated. (By default \p v is the value + ** at the node being propagated). + */ + template <typename T, typename A, typename P2B, typename WP> + inline + void + propagate_if(const T& tree, + Image<A>& a_, + const way_of_propagation<WP>&, + const Function_p2b<P2B>& pred, + const mln_value(A)& v); + + template <typename T, typename A, typename P2B> + inline + void + propagate_if(const T& tree, + Image<A>& a_, + const desc_propagation&, + const Function_p2b<P2B>& pred); + + /** + ** Propagate nodes having the value v in the way + ** defined by \p way_of_propagation. + ** + ** @param tree Component tree used for propagation. + ** @param a_ Attributed image where values are propagated. + ** @param way_of_propagation Propagate node in acsendant or + ** descendant way. + ** @param v Value that node must have to be propagated. + ** @param v_prop Value to propagate (By default it is the value + ** at the node being propagated). + */ + template <typename T, typename A, typename WP> + inline + void + propagate_if_value(const T& tree, + Image<A>& a_, + const way_of_propagation<WP>&, + const mln_value(A)& v, + const mln_value(A)& v_prop); + + template <typename T, typename A, typename WP> + inline + void + propagate_if_value(const T& tree, + Image<A>& a_, + const way_of_propagation<WP>&, + const mln_value(A)& v); + + + + +# ifndef MLN_INCLUDE_ONLY + + namespace internal + { + template <typename T, typename A, typename P2B> + bool check_propagate_if(const T& t, + const A& a, + const asc_propagation& prop, + const P2B& pred, + const mln_value(A)& v) + { + (void) prop; + mln_node_piter(T) n(t); + for_all(n) + if (pred(n) && a(t.parent(n)) != v) + return false; + return true; + } + + template <typename T, typename A, typename P2B> + bool check_propagate_if(const T& t, + const A& a, + const desc_propagation& prop, + const P2B& pred, + const mln_value(A)& v) + { + (void) prop; + mln_node_piter(T) n(t); + for_all(n) + if (a(n) != v && pred(t.parent(n))) + return false; + return true; + } + + template <typename T, typename A, typename P2B> + bool check_propagate_if(const T& t, + const A& a, + const desc_propagation& prop, + const P2B& pred) + { + (void) prop; + mln_node_piter(T) n(t); + for_all(n) + if (a(n) != a(t.parent(n)) && pred(t.parent(n))) + return false; + return true; + } + + template <typename T, typename A, typename P2B> + inline + void + propagate_if(const T& tree, + A& a, + const desc_propagation& prop, + const P2B& pred, + const mln_value(A)& v) + { + (void) prop; + + mln_precondition(a.is_valid()); + mln_precondition(tree.f().domain() == a.domain()); + + mln_ch_value(typename T::function, bool) mark; + initialize(mark, tree.f()); + mln::data::fill(mark, false); + + mln_dn_node_piter(T) n(tree); + for_all(n) + if (mark(tree.parent(n))) + { + a(n) = v; + mark(n) = true; + } + else if (pred(n)) + mark(n) = true; + mln_postcondition(check_propagate_if(tree, a, prop, pred, v)); + } + + template <typename T, typename A, typename P2B> + inline + void + propagate_if(const T& tree, + A& a, + const desc_propagation& prop, + const P2B& pred) + { + (void) prop; + + mln_precondition(a.is_valid()); + mln_precondition(tree.f().domain() == a.domain()); + + mln_ch_value(typename T::function, bool) mark; + initialize(mark, tree.f()); + mln::data::fill(mark, false); + + mln_dn_node_piter(T) n(tree); + for_all(n) + if (mark(tree.parent(n))) + { + a(n) = a(tree.parent(n)); + mark(n) = true; + } + else if (pred(n)) + mark(n) = true; + mln_postcondition(check_propagate_if(tree, a, prop, pred)); + } + + + template <typename T, typename A, typename P2B> + inline + void + propagate_if(const T& tree, + A& a, + const asc_propagation& prop, + const P2B& pred, + const mln_value(A)& v) + { + (void) prop; + + mln_precondition(a.is_valid()); + mln_precondition(tree.f().domain() == a.domain()); + + mln_ch_value(typename T::function, bool) mark; + initialize(mark, tree.f()); + mln::data::fill(mark, false); + + mln_up_node_piter(T) n(tree); + for_all(n) + if (mark(n)) + { + a(n) = v; + mark(tree.parent(n)) = true; + } + else if (pred(n)) + mark(tree.parent(n)) = true; + + mln_postcondition(check_propagate_if(tree, a, prop, pred, v)); + } + + } // end of namespace mln::morpho::tree::internal + + + /* Facades */ + + template <typename T, typename A, typename WP> + inline + void + propagate_if_value(const T& tree, + Image<A>& a_, + const way_of_propagation<WP>& prop_, + const mln_value(A)& v, + const mln_value(A)& v_prop) + { + A& a = exact(a_); + const WP& prop = exact(prop_); + + internal::propagate_if(tree, a, prop, pw::value(a) == pw::cst(v), v_prop); + } + + + template <typename T, typename A, typename WP> + inline + void + propagate_if_value(const T& tree, + Image<A>& a_, + const way_of_propagation<WP>& prop_, + const mln_value(A)& v) + { + A& a = exact(a_); + const WP& prop = exact(prop_); + + internal::propagate_if(tree, a, prop, pw::value(a) == pw::cst(v), v); + } + + template <typename T, typename A, typename P2B, typename WP> + inline + void + propagate_if(const T& tree, + Image<A>& a_, + const way_of_propagation<WP>& prop_, + const Function_p2b<P2B>& pred_, + const mln_value(A)& v) + { + A& a = exact(a_); + const WP& prop = exact(prop_); + const P2B& pred = exact(pred_); + + internal::propagate_if(tree, a, prop, pred, v); + } + + template <typename T, typename A, typename P2B> + inline + void + propagate_if(const T& tree, + Image<A>& a_, + const desc_propagation& prop, + const Function_p2b<P2B>& pred_) + { + A& a = exact(a_); + const P2B& pred = exact(pred_); + + internal::propagate_if(tree, a, prop, pred); + } + +#endif /* !MLN_INCLUDE_ONLY */ + + + } // end of namespace mln::morpho::tree + } // end of namespace mln::morpho +} // end of namespace mln + +#endif /* !MLN_MORPHO_TREE_PROPAGATE_IF_HH_ */ Property changes on: trunk/milena/sandbox/edwin/mln/morpho/tree/propagate_if.hh ___________________________________________________________________ Added: svn:mergeinfo Index: trunk/milena/sandbox/edwin/mln/morpho/attribute/occupation.hh =================================================================== --- trunk/milena/sandbox/edwin/mln/morpho/attribute/occupation.hh (revision 0) +++ trunk/milena/sandbox/edwin/mln/morpho/attribute/occupation.hh (revision 3779) @@ -0,0 +1,252 @@ +// Copyright (C) 2007, 2008, 2009 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 MLN_MORPHO_ATTRIBUTE_OCCUPATION_HH +# define MLN_MORPHO_ATTRIBUTE_OCCUPATION_HH + +/// \file mln/morpho/attribute/occupation.hh +/// +/// Define an accumulator that computes the occupation of a +/// component. (occupation = volume of the component / volume of box bounding the component. + +# include <mln/core/concept/accumulator.hh> +# include <mln/accu/internal/base.hh> +# include <mln/morpho/attribute/volume.hh> + + +namespace mln +{ + + // Forward declaration. + namespace morpho { + namespace attribute { + template <typename I> + class occupation; + } + } + + + // Traits. + + namespace trait + { + + template <typename I> + struct accumulator_< morpho::attribute::occupation<I> > + { + typedef accumulator::has_untake::no has_untake; + typedef accumulator::has_set_value::no has_set_value; + typedef accumulator::has_stop::no has_stop; + typedef accumulator::when_pix::use_v when_pix; + }; + + } // end of namespace mln::trait + + + namespace morpho + { + + namespace attribute + { + + /// Occupation accumulator class. + /// + /// The parameter \p I is the image type on which the accumulator + /// of pixels is built. + template <typename I> + struct occupation + : public mln::accu::internal::base< double, occupation<I> > + { + typedef mln_value(I) argument; + + occupation(); + + /// Manipulators. + /// \{ + void init(); + + void take(const mln_value(I)& v); + void take(const util::pix<I>& px); + void take(const occupation<I>& other); + + void take_as_init(const mln_value(I)& v); + void take_as_init(const util::pix<I>& px); + /// \} + + /// Get the value of the accumulator. + double to_result() const; + + /// Check whether this accu is able to return a result. + /// Always true here. + bool is_valid() const; + + /// Give the area of the component. + unsigned area() const; + + /// Give the height. + unsigned height() const; + + /// Give the volume of the component. + unsigned volume() const; + + protected: + /// The min value taken. + unsigned min_; + /// The max value taken. + unsigned max_; + /// The volume of the component. + typename mln::morpho::attribute::volume<I> volume_; + }; + + + +# ifndef MLN_INCLUDE_ONLY + + template <typename I> + inline + occupation<I>::occupation() + { + init(); + } + + template <typename I> + inline + void + occupation<I>::init() + { + min_ = mln_max(unsigned); + max_ = mln_min(unsigned); + volume_.init(); + } + + template <typename I> + inline + void + occupation<I>::take(const mln_value(I)& v) + { + if (! is_valid()) + { + take_as_init(v); + return; + } + volume_.take(v); + if (v < min_) + min_ = v; + if (v > max_) + max_ = v; + } + + template <typename I> + inline + void + occupation<I>::take(const util::pix<I>& px) + { + take(px.v()); + } + + template <typename I> + inline + void + occupation<I>::take(const occupation<I>& other) + { + mln_invariant(is_valid()); + volume_.take(other.volume_); + if (other.min_ < min_) + min_ = other.min_; + if (other.max_ > max_) + max_ = other.max_; + } + + template <typename I> + inline + void + occupation<I>::take_as_init(const mln_value(I)& v) + { + volume_.take_as_init(v); + max_ = v; + min_ = v; + } + + template <typename I> + inline + void + occupation<I>::take_as_init(const util::pix<I>& px) + { + take_as_init(px.v()); + } + + template <typename I> + inline + double + occupation<I>::to_result() const + { + return (double)volume_.to_result() / (double)(volume_.area() * height()); + } + + template <typename I> + inline + unsigned + occupation<I>::area() const + { + return volume_.area(); + } + + template <typename I> + inline + unsigned + occupation<I>::volume() const + { + return volume_.to_result(); + } + + template <typename I> + inline + unsigned + occupation<I>::height() const + { + return (max_ - min_); + } + + template <typename I> + inline + bool + occupation<I>::is_valid() const + { + return volume_.is_valid(); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::morpho::attribute + + } // end of namespace mln::morpho + +} // end of namespace mln + + +#endif // ! MLN_MORPHO_ATTRIBUTE_OCCUPATION_HH Index: trunk/milena/sandbox/edwin/mln/morpho/attribute/bbox.hh =================================================================== --- trunk/milena/sandbox/edwin/mln/morpho/attribute/bbox.hh (revision 0) +++ trunk/milena/sandbox/edwin/mln/morpho/attribute/bbox.hh (revision 3779) @@ -0,0 +1,203 @@ +// Copyright (C) 2007, 2008, 2009 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 MLN_MORPHO_ATTRIBUTE_BBOX_HH_ +# define MLN_MORPHO_ATTRIBUTE_BBOX_HH_ + +/// \file mln/morpho/attribute/bbox.hh +/// +/// Define an accumulator that computes bounding box of a +/// component. + +# include <mln/core/concept/box.hh> + + +namespace mln +{ + + // Forward declaration. + + namespace morpho { + namespace attribute { + template <typename I> class bbox; + } + } + + // Traits. + + namespace trait + { + + template <typename I> + struct accumulator_< morpho::attribute::bbox<I> > + { + typedef accumulator::has_untake::no has_untake; + typedef accumulator::has_set_value::no has_set_value; + typedef accumulator::has_stop::no has_stop; + typedef accumulator::when_pix::use_p when_pix; + }; + + } // end of namespace mln::trait + + namespace morpho { + namespace attribute { + + template <typename I> + struct bbox : public mln::accu::internal::base<const box<mln_psite(I)>&, bbox<I> > + { + typedef mln::accu::internal::base<const box<mln_psite(I)>&, bbox<I> > super_; + + public: + typedef mln_psite(I) P; + typedef mln_psite(I) argument; + + bbox(); + + /// Manipulators. + /// \{ + void init(); + void take(const argument& p); + void take(const bbox<I>& other); + void take_as_init(const argument& p); + + /// \} + + /// Get the value of the accumulator. + const box<P>& to_result() const; + + + /// Check whether this accu is able to return a result. + /// Always true here. + bool is_valid() const; + + protected: + + bool is_valid_; + box<mln_psite(I)> b_; + }; + +# ifndef MLN_INCLUDE_ONLY + + template <typename I> + inline + bbox<I>::bbox() + { + init(); + } + + template <typename I> + inline + void + bbox<I>::init() + { + is_valid_ = false; + } + + template <typename I> + inline + void + bbox<I>::take_as_init(const mln_psite(I)& p) + { + b_.pmin() = p; + b_.pmax() = p; + is_valid_ = true; + } + + template <typename I> + inline + void + bbox<I>::take(const mln_psite(I)& p) + { + if (!is_valid_) + { + b_.pmin() = p; + b_.pmax() = p; + is_valid_ = true; + return; + } + for (unsigned i = 0; i < mln_psite_(I)::dim; ++i) + if (p[i] < b_.pmin()[i]) + b_.pmin()[i] = p[i]; + else if (p[i] > b_.pmax()[i]) + b_.pmax()[i] = p[i]; + } + + template <typename I> + inline + void + bbox<I>::take(const bbox<I>& other) + { + if (! other.is_valid_) + { + // no-op + return; + } + if (! this->is_valid_) + { + // 'other' makes '*this' valid + *this = other; + is_valid_ = true; + return; + } + // both are valids so: + const box<mln_psite(I)>& o_b = other.b_; + for (unsigned i = 0; i < mln_psite_(I)::dim; ++i) + { + if (o_b.pmin()[i] < b_.pmin()[i]) + b_.pmin()[i] = o_b.pmin()[i]; + if (o_b.pmax()[i] > b_.pmax()[i]) + b_.pmax()[i] = o_b.pmax()[i]; + } + } + + template <typename I> + inline + const box<mln_psite(I)>& + bbox<I>::to_result() const + { + mln_precondition(is_valid_); + return b_; + } + + template <typename I> + inline + bool + bbox<I>::is_valid() const + { + return is_valid_; + } + + +# endif /* !MLN_INCLUDE_ONLY */ + } // end of namespace mln::morpho::attribute + } // end of namespace mln::morpho +} // end of namespace mln + + + +#endif /* !MLN_MORPHO_ATTRIBUTE_BBOX_HH_ */ Property changes on: trunk/milena/sandbox/edwin/mln/morpho/attribute ___________________________________________________________________ Added: svn:mergeinfo
participants (1)
-
Edwin Carlinet