milena r1388: Add tree_fast_to_image

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2007-10-25 Guillaume Duhamel <guillaume.duhamel@lrde.epita.fr> Add tree_fast_to_image. * mln/util/tree_fast_to_image.hh: New function for tree_fast. * tests/tree_fast_to_image.cc: New test for this function. * mln/util/tree_fast.hh, * tests/tree_fast.cc: Update. --- mln/util/tree_fast.hh | 39 ++++++------ mln/util/tree_fast_to_image.hh | 85 +++++++++++++++++++++++++++ tests/tree_fast.cc | 26 ++++---- tests/tree_fast_to_image.cc | 127 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+), 32 deletions(-) Index: trunk/milena/tests/tree_fast.cc =================================================================== --- trunk/milena/tests/tree_fast.cc (revision 1387) +++ trunk/milena/tests/tree_fast.cc (revision 1388) @@ -47,17 +47,17 @@ unsigned elt5 = 5; unsigned elt6= 42; - util::tree<unsigned> tree(elt1); - mln_assertion(tree.has (elt1)); - tree.add_child(tree.search(elt1), elt2); - mln_assertion(tree.has (elt2)); - tree.add_child(tree.search(elt1), elt3); - mln_assertion(tree.has (elt3)); - tree.add_child(tree.search(elt2), elt4); - mln_assertion(tree.has (elt4)); - tree.add_child(tree.search(elt2), elt5); - mln_assertion(tree.has (elt5)); - tree.add_parent(elt6); - mln_assertion(tree.has (elt6)); - mln_assertion(tree.search(elt6) == tree.root_); + util::tree_fast<unsigned> tree_fast(elt1); + mln_assertion(tree_fast.has (elt1)); + tree_fast.add_child(tree_fast.search(elt1), elt2); + mln_assertion(tree_fast.has (elt2)); + tree_fast.add_child(tree_fast.search(elt1), elt3); + mln_assertion(tree_fast.has (elt3)); + tree_fast.add_child(tree_fast.search(elt2), elt4); + mln_assertion(tree_fast.has (elt4)); + tree_fast.add_child(tree_fast.search(elt2), elt5); + mln_assertion(tree_fast.has (elt5)); + tree_fast.add_parent(elt6); + mln_assertion(tree_fast.has (elt6)); + mln_assertion(tree_fast.search(elt6) == tree_fast.root_); } Index: trunk/milena/tests/tree_fast_to_image.cc =================================================================== --- trunk/milena/tests/tree_fast_to_image.cc (revision 0) +++ trunk/milena/tests/tree_fast_to_image.cc (revision 1388) @@ -0,0 +1,127 @@ +// Copyright (C) 2007 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. + +/*! + * \file tests/tree_to_image.cc + * + * \brief test of mln::util::tree_to_image + * + */ + +#include <mln/core/contract.hh> +#include <mln/core/image2d.hh> +#include <mln/core/set_p.hh> +#include <mln/value/int_u8.hh> +#include <mln/level/stretch.hh> +#include <mln/io/pgm/save.hh> +#include <vector> +#include <mln/util/tree_fast_to_image.hh> +#include <mln/util/tree_fast.hh> + +template <typename P, typename V> +struct fllt_node +{ + V value; + mln::set_p<P> points; + mln::set_p<P> holes; +}; + +template <typename P, typename V> +bool operator==(const struct fllt_node<P,V>& lhs, const struct fllt_node<P,V>& rhs) +{ + if (lhs.value != rhs.value) + return false; + + /// FIXME + +// if (lhs.points != rhs.points) +// return false; + +// if (lhs.holes != rhs.holes) +// return false; + + return true; +} + +int main (void) +{ + using namespace mln; + using value::int_u8; + + typedef set_p<point2d > I; + typedef fllt_node<point2d, int_u8> T; + + T s1; + T s2; + T s3; + T s4; + T s5; + T s6; + T s7; + + for (int i = 0; i < 100; ++i) + for (int j = 0; j < 100; ++j) + s1.points.insert(point2d(i, j)); + s1.value = 60; + for (int i = 200; i < 300; ++i) + for (int j = 0; j < 100; ++j) + s2.points.insert(point2d(i, j)); + s2.value = 100; + for (int i = 0; i < 100; ++i) + for (int j = 0; j < 100; ++j) + s3.points.insert(point2d(i, j)); + s3.value = 110; + for (int i = 260; i < 290; ++i) + for (int j = 0; j < 50; ++j) + s4.points.insert(point2d(i, j)); + s4.value = 170; + for (int i = 200; i < 210; ++i) + for (int j = 0; j < 50; ++j) + s5.points.insert(point2d(i, j)); + s5.value = 180; + for (int i = 270; i < 280; ++i) + for (int j = 50; j < 60; ++j) + s6.points.insert(point2d(i, j)); + s6.value = 210; + for (int i = 0; i < 300; ++i) + for (int j = 0; j < 200; ++j) + s7.points.insert(point2d(i, j)); + s7.value = 10; + + util::tree_fast<T> tree(s1); + tree.add_child(tree.search(s1), s2); + tree.add_child(tree.search(s1), s3); + tree.add_child(tree.search(s2), s4); + tree.add_child(tree.search(s2), s5); + tree.add_child(tree.search(s4), s6); + tree.add_parent(s7); + image2d<int_u8> out (300,300); + util::tree_fast_to_image(tree, out); + io::pgm::save(out, "out.pgm"); + std::cout << "out.pgm generate" + << std::endl; +} Index: trunk/milena/mln/util/tree_fast.hh =================================================================== --- trunk/milena/mln/util/tree_fast.hh (revision 1387) +++ trunk/milena/mln/util/tree_fast.hh (revision 1388) @@ -25,8 +25,8 @@ // reasons why the executable file might be covered by the GNU General // Public License. -#ifndef MLN_UTIL_TREE_HH -# define MLN_UTIL_TREE_HH +#ifndef MLN_UTIL_TREE_FAST_HH +# define MLN_UTIL_TREE_FAST_HH # include <vector> # include <mln/core/contract.hh> @@ -34,7 +34,7 @@ /*! * \file mln/util/tree_fast.hh * - * \brief Definition of a fast generic general tree. + * \brief Definition of a fast generic general fast tree. * */ @@ -45,10 +45,10 @@ { template <typename T> - struct tree + struct tree_fast { - tree(); - tree(T& elt); +// tree_fast(); + tree_fast(T& elt); const unsigned size() const; bool has (T& elt) const; @@ -65,13 +65,13 @@ # ifndef MLN_INCLUDE_ONLY - template <typename T> - tree<T>::tree() - { - } +// template <typename T> +// tree_fast<T>::tree_fast() +// { +// } template <typename T> - tree<T>::tree(T& elt) + tree_fast<T>::tree_fast(T& elt) { std::vector<unsigned> v; data_.push_back(elt); @@ -82,7 +82,7 @@ template <typename T> const unsigned - tree<T>::size() const + tree_fast<T>::size() const { return (data_.size ()); } @@ -90,7 +90,7 @@ template <typename T> bool - tree<T>::has (T& elt) const + tree_fast<T>::has (T& elt) const { for (unsigned i = 0; i < data_.size (); ++i) if (data_[i] == elt) @@ -101,25 +101,26 @@ template <typename T> unsigned - tree<T>::search (T& elt) const + tree_fast<T>::search (T& elt) const { for (unsigned i = 0; i < data_.size (); ++i) if (data_[i] == elt) return i; - + std::cerr << "BUGG !!!!" + << std::endl; return (unsigned)(-1); } template <typename T> bool - tree<T>::is_root (unsigned i) const + tree_fast<T>::is_root (unsigned i) const { return (root_ == i); } template <typename T> void - tree<T>::add_child (unsigned i, T& elt) + tree_fast<T>::add_child (unsigned i, T& elt) { mln_assertion (i < data_.size ()); std::vector<unsigned> v; @@ -131,7 +132,7 @@ template <typename T> void - tree<T>::add_parent (T& elt) + tree_fast<T>::add_parent (T& elt) { std::vector<unsigned> v; data_.push_back(elt); @@ -153,4 +154,4 @@ } // end of namespace mln -#endif // !MLN_UTIL_TREE_HH +#endif // !MLN_UTIL_TREE_FAST_HH Index: trunk/milena/mln/util/tree_fast_to_image.hh =================================================================== --- trunk/milena/mln/util/tree_fast_to_image.hh (revision 0) +++ trunk/milena/mln/util/tree_fast_to_image.hh (revision 1388) @@ -0,0 +1,85 @@ +// Copyright (C) 2007 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_TREE_FAST_TO_IMAGE_HH +# define MLN_UTIL_TREE_FAST_TO_IMAGE_HH + +/*! + * \file mln/util/tree_fast_to_image.hh + * + * \brief Definition of function which transform a tree_fast into an + * image. + * + */ + +# include <mln/util/tree_fast.hh> +# include <mln/core/set_p.hh> +# include <list> + +namespace mln +{ + + namespace util + { + + template <typename T, typename I> + void + tree_fast_to_image (tree_fast<T>& tree, Image<I>& output_); + +# ifndef MLN_INCLUDE_ONLY + + template <typename T, typename I> + void + tree_fast_to_image (tree_fast<T>& tree, Image<I>& output_) + { + I& output = exact(output_); + std::list<unsigned> q; + + q.push_back (tree.root_); + while (!(q.empty ())) + { + unsigned current = q.front (); + for (unsigned i = 0; i < tree.child_[current].size (); ++i) + q.push_back (tree.child_[current][i]); + + mln_piter(set_p<point2d>) p(tree.data_[current].points); + + for_all(p) + { + output(p) = tree.data_[current].value; + } + q.pop_front (); + } + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::util + +} // end of namespace mln + +#endif // !MLN_UTIL_TREE_FAST_TO_IMAGE_HH
participants (1)
-
Guillaume Duhamel