Commit 2df3dd07 authored by Thierry Geraud's avatar Thierry Geraud
Browse files

Add int_s, an histogram class, and distributed sort.

	* tests/int_s8.cc: New.
	* tests/int_u8.cc: Augment.
	* mln/histo/sort.hh: New.
	* mln/histo/data.hh: New.
	* mln/histo/compute.hh: New.
	* mln/accu/histo.hh: Fix doc.
	* mln/accu/median.hh: Fix doc.
	* mln/value/int_s8.hh: New.
	* mln/value/int_u16.hh: New.
	* mln/value/int_u8.hh: New.
	* mln/value/int_s.hh: New.
	* mln/value/int_u.hh (zero, one): New.
	(encoding_): Move to...
	* mln/value/internal/encoding.hh: ...this new file.
	* tests/median.cc,
	* tests/fast_median.cc,
	* tests/main.cc,
	* tests/hmedian.cc,
	* tests/erosion.cc,
	* tests/naive_median.cc,
	* tests/vset.cc,
	* tests/histo.cc,
	* mln/io/save_pgm.hh,
	* mln/io/load_pgm.hh: Update.


git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@1037 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 8b9996bc
2007-07-24 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Add int_s, an histogram class, and distributed sort.
* tests/int_s8.cc: New.
* tests/int_u8.cc: Augment.
* mln/histo/sort.hh: New.
* mln/histo/data.hh: New.
* mln/histo/compute.hh: New.
* mln/accu/histo.hh: Fix doc.
* mln/accu/median.hh: Fix doc.
* mln/value/int_s8.hh: New.
* mln/value/int_u16.hh: New.
* mln/value/int_u8.hh: New.
* mln/value/int_s.hh: New.
* mln/value/int_u.hh (zero, one): New.
(encoding_): Move to...
* mln/value/internal/encoding.hh: ...this new file.
* tests/median.cc,
* tests/fast_median.cc,
* tests/main.cc,
* tests/hmedian.cc,
* tests/erosion.cc,
* tests/naive_median.cc,
* tests/vset.cc,
* tests/histo.cc,
* mln/io/save_pgm.hh,
* mln/io/load_pgm.hh: Update.
2007-07-24 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Make value::set works for any type.
......
......@@ -30,7 +30,7 @@
/*! \file mln/accu/histo.hh
*
* \brief Define a couple of generic histogram classes.
* \brief Define a generic histogram accumulator class.
*/
# include <vector>
......
......@@ -30,7 +30,7 @@
/*! \file mln/accu/median.hh
*
* \brief Define FIXME
* \brief Define a generic median accumulator class.
*/
# include <mln/core/concept/accumulator.hh>
......
// 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_HISTO_COMPUTE_HH
# define MLN_HISTO_COMPUTE_HH
/*! \file mln/histo/compute.hh
*
* \brief Routine to compute an histogram.
*/
# include <mln/core/concept/image.hh>
# include <mln/histo/data.hh>
namespace mln
{
namespace histo
{
/// Compute the histogram of image \p input.
template <typename I>
data<mln_vset(I)> compute(const Image<I>& input);
# ifndef MLN_INCLUDE_ONLY
namespace impl
{
template <typename I>
data<mln_vset(I)> compute(const Image<I>& input_)
{
const I& input = exact(input_);
data<mln_vset(I)> h(input.values());
mln_piter(I) p(input.domain());
for_all(p)
++h(input(p));
return h;
}
template <typename I>
data<mln_vset(I)> compute(const Fast_Image<I>& input_)
{
const I& input = exact(input_);
data<mln_vset(I)> h(input.values());
mln_pixter(const I) p(input);
for_all(p)
++h(*p);
return h;
}
} // end of namespace mln::histo::impl
template <typename I>
data<mln_vset(I)> compute(const Image<I>& input)
{
mln_precondition(exact(input).has_data());
return impl::compute(exact(input));
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::histo
} // end of namespace mln
#endif // ! MLN_HISTO_COMPUTE_HH
// 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_HISTO_DATA_HH
# define MLN_HISTO_DATA_HH
/*! \file mln/histo/data.hh
*
* \brief Define a generic histogram class.
*/
# include <vector>
# include <algorithm>
# include <mln/core/concept/value_set.hh>
namespace mln
{
namespace histo
{
/*! Generic histogram class over a value set with type \c S.
*/
template <typename S>
struct data
{
typedef mln_value(S) value;
data(const Value_Set<S>& s);
void clear();
std::size_t operator()(const value& v) const;
std::size_t& operator()(const value& v);
const std::vector<std::size_t>& vec() const;
const S& vset() const;
std::size_t operator[](unsigned i) const;
protected:
const S& s_;
std::vector<std::size_t> h_;
};
template <typename S>
std::ostream& operator<<(std::ostream& ostr, const data<S>& h);
# ifndef MLN_INCLUDE_ONLY
template <typename S>
data<S>::data(const Value_Set<S>& s)
: s_(exact(s)),
h_(s_.nvalues(), 0)
{
clear();
}
template <typename S>
void
data<S>::clear()
{
std::fill(h_.begin(), h_.end(), 0);
}
template <typename S>
std::size_t
data<S>::operator()(const value& v) const
{
return h_[s_.index_of(v)];
}
template <typename S>
std::size_t&
data<S>::operator()(const value& v)
{
return h_[s_.index_of(v)];
}
template <typename S>
const S&
data<S>::vset() const
{
return s_;
}
template <typename S>
std::size_t
data<S>::operator[](unsigned i) const
{
mln_precondition(i < s_.nvalues());
return h_[i];
}
template <typename S>
const std::vector<std::size_t>&
data<S>::vec() const
{
return h_;
}
template <typename S>
std::ostream& operator<<(std::ostream& ostr, const data<S>& h)
{
mln_viter(S) v(h.vset());
for_all(v)
if (h(v) != 0)
ostr << v << ':' << h(v) << ' ';
return ostr;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::histo
} // end of namespace mln
#endif // ! MLN_HISTO_DATA_HH
// 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_HISTO_SORT_HH
# define MLN_HISTO_SORT_HH
/*! \file mln/histo/sort.hh
*
* \brief Routine to sort an histogram.
*/
# include <vector>
# include <mln/core/concept/image.hh>
# include <mln/histo/compute.hh>
namespace mln
{
namespace histo
{
/// Sort points of image \p input by decreasing values.
template <typename I>
std::vector<mln_point(I)> sort_points_decreasing(const Image<I>& input);
/// Sort points of image \p input by increasing values.
template <typename I>
std::vector<mln_point(I)> sort_points_increasing(const Image<I>& input);
# ifndef MLN_INCLUDE_ONLY
namespace impl
{
template <typename I>
std::vector<mln_point(I)>
sort_points_decreasing(const Image<I>& input_)
{
const I& input = exact(input_);
typedef mln_vset(I) S;
const S& vset = input.values();
const unsigned n = vset.nvalues();
// h
histo::data<S> h = compute(input);
// preparing output data
std::vector<unsigned> loc(vset.nvalues());
loc[n-1] = 0;
for (unsigned i = n - 2; i != 0; --i)
loc[i] = loc[i+1] + h[i+1];
// computing output data
std::vector<mln_point(I)> vec(input.npoints());
mln_fwd_piter(I) p(input.domain());
for_all(p)
vec[loc[vset.index_of(input(p))]++] = p;
return vec;
}
template <typename I>
std::vector<mln_point(I)>
sort_points_increasing(const Image<I>& input_)
{
const I& input = exact(input_);
typedef mln_vset(I) S;
const S& vset = input.values();
const unsigned n = vset.nvalues();
// h
histo::data<S> h = compute(input);
// preparing output data
std::vector<unsigned> loc(vset.nvalues());
loc[0] = 0;
for (unsigned i = 1; i != n; ++i)
loc[i] = loc[i-1] + h[i-1];
// computing output data
std::vector<mln_point(I)> vec(input.npoints());
mln_fwd_piter(I) p(input.domain());
for_all(p)
vec[loc[vset.index_of(input(p))]++] = p;
return vec;
}
} // end of namespace mln::histo::impl
template <typename I>
std::vector<mln_point(I)>
sort_points_decreasing(const Image<I>& input)
{
mln_precondition(exact(input).has_data());
mln_precondition(exact(input).values().nvalues() > 1); // FIXME
return impl::sort_points_decreasing(exact(input));
}
template <typename I>
std::vector<mln_point(I)>
sort_points_increasing(const Image<I>& input)
{
mln_precondition(exact(input).has_data());
mln_precondition(exact(input).values().nvalues() > 1); // FIXME
return impl::sort_points_increasing(exact(input));
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::histo
} // end of namespace mln
#endif // ! MLN_HISTO_SORT_HH
......@@ -34,7 +34,7 @@
# include <string>
# include <mln/core/image2d_b.hh>
# include <mln/value/int_u.hh>
# include <mln/value/int_u8.hh>
namespace mln
......
......@@ -33,7 +33,7 @@
# include <fstream>
# include <mln/core/image2d_b.hh>
# include <mln/value/int_u.hh>
# include <mln/value/int_u8.hh>
namespace mln
......
// 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_VALUE_INT_S_HH
# define MLN_VALUE_INT_S_HH
/*! \file mln/value/int_s.hh
*
* \brief Define a generic class for signed integers.
*/
# include <mln/metal/math.hh>
# include <mln/value/internal/value_like.hh>
# include <mln/value/internal/encoding.hh>
# include <mln/value/props.hh>
# include <mln/value/set.hh>
# include <mln/debug/format.hh>
namespace mln
{
namespace value
{
/*! \brief Signed integer value class.
*
* The parameter is \c n the number of encoding bits.
*/
template <unsigned n>
struct int_s
: public internal::value_like_< typename internal::encoding_int_s_<n>::ret,
int_s<n> >
{
protected:
typedef internal::value_like_< typename internal::encoding_int_s_<n>::ret,
int_s<n> > super;
public:
/// Encoding associated type.
typedef typename super::enc enc;
/// Constructor without argument.
int_s();
/// Constructor from an integer.
int_s(int i);
/// Negation.
int_s<n> operator-() const;
/// Zero value.
static const int_s<n> zero;
/// Unit value.
static const int_s<n> one;
};
template <unsigned n>
struct props< int_s<n> >
{
static const int_s<n> max; // = 2^(n-1) - 1
static const int_s<n> min; // = - max
static const std::size_t card = metal::pow<2, n>::value;
static const unsigned nbits = n;
typedef data_kind kind;
};
// Safety.
template <> struct int_s<0>;
template <> struct int_s<1>;
/*! \brief Print an signed integer \p i into the output stream \p ostr.
*
* \param[in,out] ostr An output stream.
* \param[in] i An signed integer.
*
* \return The modified output stream \p ostr.
*/
template <unsigned n>
std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i);
# ifndef MLN_INCLUDE_ONLY
template <unsigned n>
int_s<n>::int_s()
{
}
template <unsigned n>
int_s<n>::int_s(int i)
{
mln_precondition(i >= mln_min(enc));
mln_precondition(i <= mln_max(enc));
this->v_ = enc(i);
}
template <unsigned n>
int_s<n> int_s<n>::operator-() const
{
return - this->v_;
}
template <unsigned n>
const int_s<n> int_s<n>::zero = 0;
template <unsigned n>
const int_s<n> int_s<n>::one = 1;
template <unsigned n>
const int_s<n>
props< int_s<n> >::min = 1 - metal::pow<2, n - 1>::value;
template <unsigned n>
const int_s<n>
props< int_s<n> >::max = metal::pow<2, n - 1>::value - 1;
template <unsigned n>
std::ostream& operator<<(std::ostream& ostr, const int_s<n>& i)
{
return ostr << debug::format(i.to_equiv());
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::value
} // end of namespace mln