
* mln/accu/math/span.hh, * mln/fun/vv2v/span.hh, * mln/fun/vvvv2v/span.hh: Do not force the use of value::interval. * mln/world/kn/fill_0_1_faces_internal_border.hh: Rely on mln::inner_border::fill. * mln/inner_border/internal/on_frontiere.hh: Rename as... * mln/inner_border/internal/is_on_frontiere.hh: ... this. * mln/data/compute_in_inner_border.hh, * mln/inner_border/fill.hh: Rename on_frontiere as is_on_frontiere. * mln/value/interval.hh: Relax type of constructor arguments. * mln/world/k2/is_primary_2_face.hh: Fix invalid test. * mln/world/kn/internal/fill_primary_2_faces_from_input.hh, * mln/world/kn/un_immerse.hh: Make use of safe_cast_to. * mln/world/kn/safe_cast.hh: Remove safe_cast() function. --- milena/ChangeLog | 27 ++++++ milena/mln/accu/math/span.hh | 43 +++++----- milena/mln/data/compute_in_inner_border.hh | 6 +- milena/mln/fun/vv2v/span.hh | 19 ++--- milena/mln/fun/vvvv2v/span.hh | 27 +++---- milena/mln/inner_border/fill.hh | 4 +- .../{on_frontiere.hh => is_on_frontiere.hh} | 45 +++++++---- milena/mln/value/interval.hh | 20 +++-- milena/mln/world/k2/is_primary_2_face.hh | 2 +- .../mln/world/kn/fill_0_1_faces_internal_border.hh | 22 ++---- .../kn/internal/fill_primary_2_faces_from_input.hh | 3 +- milena/mln/world/kn/safe_cast.hh | 87 ++++++++++---------- milena/mln/world/kn/un_immerse.hh | 4 +- 13 files changed, 171 insertions(+), 138 deletions(-) rename milena/mln/inner_border/internal/{on_frontiere.hh => is_on_frontiere.hh} (68%) diff --git a/milena/ChangeLog b/milena/ChangeLog index 2a3885d..9c48f48 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,3 +1,30 @@ +2012-10-25 Guillaume Lazzara <z@lrde.epita.fr> + + Various fixes. + + * mln/accu/math/span.hh, + * mln/fun/vv2v/span.hh, + * mln/fun/vvvv2v/span.hh: Do not force the use of value::interval. + + * mln/world/kn/fill_0_1_faces_internal_border.hh: Rely on + mln::inner_border::fill. + + * mln/inner_border/internal/on_frontiere.hh: Rename as... + * mln/inner_border/internal/is_on_frontiere.hh: ... this. + + * mln/data/compute_in_inner_border.hh, + * mln/inner_border/fill.hh: Rename on_frontiere as + is_on_frontiere. + + * mln/value/interval.hh: Relax type of constructor arguments. + + * mln/world/k2/is_primary_2_face.hh: Fix invalid test. + + * mln/world/kn/internal/fill_primary_2_faces_from_input.hh, + * mln/world/kn/un_immerse.hh: Make use of safe_cast_to. + + * mln/world/kn/safe_cast.hh: Remove safe_cast() function. + 2012-10-24 Guillaume Lazzara <z@lrde.epita.fr> Some fixes. diff --git a/milena/mln/accu/math/span.hh b/milena/mln/accu/math/span.hh index eda4e85..839dbcc 100644 --- a/milena/mln/accu/math/span.hh +++ b/milena/mln/accu/math/span.hh @@ -31,7 +31,6 @@ /// Define an accumulator that computes a span. # include <mln/accu/internal/base.hh> -# include <mln/value/interval.hh> namespace mln { @@ -50,10 +49,10 @@ namespace mln /// /// \ingroup modaccuvalues // - template <typename T> - struct span : public mln::accu::internal::base< const value::interval<T>&, span<T> > + template <typename T, typename R = T> + struct span : public mln::accu::internal::base< const R&, span<T,R> > { - typedef value::interval<T> argument; + typedef T argument; span(); @@ -62,11 +61,11 @@ namespace mln void init(); void take_as_init_(const argument& t); void take(const argument& t); - void take(const span<T>& other); + void take(const span<T,R>& other); /// \} /// Get the value of the accumulator. - const value::interval<T>& to_result() const; + const R& to_result() const; /// Check whether this accu is able to return a result. /// Always true here. @@ -74,7 +73,7 @@ namespace mln protected: - value::interval<T> t_; + R t_; bool is_valid_; }; @@ -88,32 +87,32 @@ namespace mln namespace math { - template <typename T> + template <typename T, typename R> inline - span<T>::span() + span<T,R>::span() { init(); } - template <typename T> + template <typename T, typename R> inline void - span<T>::init() + span<T,R>::init() { is_valid_ = false; } - template <typename T> + template <typename T, typename R> inline - void span<T>::take_as_init_(const argument& t) + void span<T,R>::take_as_init_(const argument& t) { t_ = t; is_valid_ = true; } - template <typename T> + template <typename T, typename R> inline - void span<T>::take(const argument& t) + void span<T,R>::take(const argument& t) { if (is_valid_) this->t_ = value::span(this->t_, t); @@ -125,10 +124,10 @@ namespace mln } } - template <typename T> + template <typename T, typename R> inline void - span<T>::take(const span<T>& other) + span<T,R>::take(const span<T,R>& other) { mln_precondition(other.is_valid()); @@ -141,18 +140,18 @@ namespace mln } } - template <typename T> + template <typename T, typename R> inline - const value::interval<T>& - span<T>::to_result() const + const R& + span<T,R>::to_result() const { return t_; } - template <typename T> + template <typename T, typename R> inline bool - span<T>::is_valid() const + span<T,R>::is_valid() const { return is_valid_; } diff --git a/milena/mln/data/compute_in_inner_border.hh b/milena/mln/data/compute_in_inner_border.hh index 917373e..780aa63 100644 --- a/milena/mln/data/compute_in_inner_border.hh +++ b/milena/mln/data/compute_in_inner_border.hh @@ -32,7 +32,7 @@ # include <mln/core/concept/image.hh> # include <mln/core/concept/accumulator.hh> -# include <mln/inner_border/internal/on_frontiere.hh> +# include <mln/inner_border/internal/is_on_frontiere.hh> # include <mln/geom/nrows.hh> # include <mln/geom/ncols.hh> @@ -126,8 +126,8 @@ namespace mln mln_piter(I) p(input.domain()); for_all(p) - if (inner_border::internal::on_frontiere(p, input.domain(), - inner_border_thickness)) + if (inner_border::internal::is_on_frontiere(p, input.domain(), + inner_border_thickness)) a.take(input(p)); trace::exiting("mln::data::compute_in_inner_border"); diff --git a/milena/mln/fun/vv2v/span.hh b/milena/mln/fun/vv2v/span.hh index e9cd002..3e5a367 100644 --- a/milena/mln/fun/vv2v/span.hh +++ b/milena/mln/fun/vv2v/span.hh @@ -31,7 +31,6 @@ /// Functor that computes the spanimum of two values. # include <mln/core/concept/function.hh> -# include <mln/value/interval.hh> namespace mln @@ -46,24 +45,22 @@ namespace mln // FIXME: Doc. /// \brief A functor computing the span of two interval values. - template <typename T> - struct span : public Function_vv2v< span<T> > + template <typename T, typename R = T> + struct span : public Function_vv2v< span<T,R> > { - typedef value::interval<T> result; + typedef R result; - value::interval<T> operator()(const value::interval<T>& v1, - const value::interval<T>& v2) const; + R operator()(const T& v1, const T& v2) const; }; # ifndef MLN_INCLUDE_ONLY - template <typename T> - value::interval<T> - span<T>::operator()(const value::interval<T>& v1, - const value::interval<T>& v2) const + template <typename T, typename R> + R + span<T,R>::operator()(const T& v1, const T& v2) const { - return value::span(v1, v2); + return R(value::span(v1, v2)); } # endif // ! MLN_INCLUDE_ONLY diff --git a/milena/mln/fun/vvvv2v/span.hh b/milena/mln/fun/vvvv2v/span.hh index 8c99bd1..3ce0987 100644 --- a/milena/mln/fun/vvvv2v/span.hh +++ b/milena/mln/fun/vvvv2v/span.hh @@ -31,7 +31,6 @@ /// Functor that computes the spanimum of two values. # include <mln/core/concept/function.hh> -# include <mln/value/interval.hh> namespace mln @@ -46,28 +45,26 @@ namespace mln // FIXME: Doc. /// \brief A functor computing the span of two interval values. - template <typename T> - struct span : public Function_vvvv2v< span<T> > + template <typename T, typename R = T> + struct span : public Function_vvvv2v< span<T,R> > { - typedef value::interval<T> result; + typedef R result; - value::interval<T> operator()(const value::interval<T>& v1, - const value::interval<T>& v2, - const value::interval<T>& v3, - const value::interval<T>& v4) const; + R operator()(const T& v1, + const T& v2, + const T& v3, + const T& v4) const; }; # ifndef MLN_INCLUDE_ONLY - template <typename T> - value::interval<T> - span<T>::operator()(const value::interval<T>& v1, - const value::interval<T>& v2, - const value::interval<T>& v3, - const value::interval<T>& v4) const + template <typename T, typename R> + R + span<T,R>::operator()(const T& v1, const T& v2, + const T& v3, const T& v4) const { - return value::span(v1, v2, v3, v4); + return R(value::span(v1, v2, v3, v4)); } # endif // ! MLN_INCLUDE_ONLY diff --git a/milena/mln/inner_border/fill.hh b/milena/mln/inner_border/fill.hh index ccee289..8f669df 100644 --- a/milena/mln/inner_border/fill.hh +++ b/milena/mln/inner_border/fill.hh @@ -32,7 +32,7 @@ # include <mln/core/image/image2d.hh> # include <mln/data/paste.hh> -# include <mln/inner_border/internal/on_frontiere.hh> +# include <mln/inner_border/internal/is_on_frontiere.hh> namespace mln { @@ -67,7 +67,7 @@ namespace mln mln_piter(I) p(input.domain()); for_all(p) - if (internal::on_frontiere(p, input.domain(), border_size)) + if (internal::is_on_frontiere(p, input.domain(), border_size)) input(p) = value; trace::exiting("mln::inner_border::fill"); diff --git a/milena/mln/inner_border/internal/on_frontiere.hh b/milena/mln/inner_border/internal/is_on_frontiere.hh similarity index 68% rename from milena/mln/inner_border/internal/on_frontiere.hh rename to milena/mln/inner_border/internal/is_on_frontiere.hh index 49430a8..33d8cb9 100644 --- a/milena/mln/inner_border/internal/on_frontiere.hh +++ b/milena/mln/inner_border/internal/is_on_frontiere.hh @@ -23,8 +23,8 @@ // exception does not however invalidate any other reasons why the // executable file might be covered by the GNU General Public License. -#ifndef MLN_INNER_BORDER_ON_FRONTIERE_HH -# define MLN_INNER_BORDER_ON_FRONTIERE_HH +#ifndef MLN_INNER_BORDER_IS_ON_FRONTIERE_HH +# define MLN_INNER_BORDER_IS_ON_FRONTIERE_HH /// \file /// @@ -48,8 +48,14 @@ namespace mln /// border of the image. template <typename P> bool - on_frontiere(const mln_site(box<P>)& p, const box<P>& b, - def::coord inner_border_size); + is_on_frontiere(const mln_site(box<P>)& p, const box<P>& b, + def::coord inner_border_size); + + /// \overload + /// inner_border_size is set to 1. + template <typename P> + bool + is_on_frontiere(const mln_site(box<P>)& p, const box<P>& b); # ifndef MLN_INCLUDE_ONLY @@ -58,8 +64,8 @@ namespace mln inline bool - on_frontiere_2d(const point2d& p, const box2d& b, - def::coord inner_border_size) + is_on_frontiere_2d(const point2d& p, const box2d& b, + def::coord inner_border_size) { for (int d = 0; d < 2; ++d) if ((p[d] >= b.pmin()[d] @@ -75,30 +81,37 @@ namespace mln inline bool - on_frontiere_dispatch(const point2d& p, const box2d& b, - def::coord inner_border_size) + is_on_frontiere_dispatch(const point2d& p, const box2d& b, + def::coord inner_border_size) { - return on_frontiere_2d(p, b, inner_border_size); + return is_on_frontiere_2d(p, b, inner_border_size); } template <typename P> bool - on_frontiere_dispatch(const mln_site(box<P>)& p, const box<P>& b, - def::coord inner_border_size) + is_on_frontiere_dispatch(const mln_site(box<P>)& p, const box<P>& b, + def::coord inner_border_size) { mlc_abort(P)::check(); // Not implemented. return false; } - // Facade + // Facades + + template <typename P> + bool + is_on_frontiere(const mln_site(box<P>)& p, const box<P>& b, + def::coord inner_border_size) + { + return is_on_frontiere_dispatch(p, b, inner_border_size); + } template <typename P> bool - on_frontiere(const mln_site(box<P>)& p, const box<P>& b, - def::coord inner_border_size) + is_on_frontiere(const mln_site(box<P>)& p, const box<P>& b) { - return on_frontiere_dispatch(p, b, inner_border_size); + return is_on_frontiere(p, b, 1); } # endif // ! MLN_INCLUDE_ONLY @@ -109,5 +122,5 @@ namespace mln } // end of namespace mln -#endif // ! MLN_INNER_BORDER_ON_FRONTIERE_HH +#endif // ! MLN_INNER_BORDER_IS_ON_FRONTIERE_HH diff --git a/milena/mln/value/interval.hh b/milena/mln/value/interval.hh index bdae72f..b40f989 100644 --- a/milena/mln/value/interval.hh +++ b/milena/mln/value/interval.hh @@ -53,8 +53,12 @@ namespace mln typedef T equiv; interval(); - interval(T single); - interval(T first, T last); + + template <typename U> + interval(U single); + + template <typename U> + interval(U first, U last); interval& operator=(const interval& rhs); @@ -223,8 +227,10 @@ namespace mln } template <typename T> - interval<T>::interval(T single) + template <typename U> + interval<T>::interval(U single) { + mlc_converts_to(U,T)::check(); first_ = single; last_ = single; @@ -233,8 +239,10 @@ namespace mln template <typename T> - interval<T>::interval(T first, T last) + template <typename U> + interval<T>::interval(U first, U last) { + mlc_converts_to(U,T)::check(); mln_precondition(last >= first); first_ = first; last_ = last; @@ -245,7 +253,7 @@ namespace mln if (first != last) { for (T v = value::succ(first_); v <= last_ && v > first_; value::inc(v)) - ++nelements_; // FIXME: overflow with unsigned char + [0,255] + ++nelements_; } } @@ -321,7 +329,7 @@ namespace mln interval<T>::self_open() { if (is_degenerated()) - abort(); + std::abort(); mln_precondition(nelements_ > 2); first += iota<T>::value(); diff --git a/milena/mln/world/k2/is_primary_2_face.hh b/milena/mln/world/k2/is_primary_2_face.hh index cd90bac..442a8f5 100644 --- a/milena/mln/world/k2/is_primary_2_face.hh +++ b/milena/mln/world/k2/is_primary_2_face.hh @@ -66,7 +66,7 @@ namespace mln bool is_primary_2_face(const mln::def::coord& row, const mln::def::coord& col) { - return !((row % 4) + (col % 4)); + return row % 4 == 0 && col % 4 == 0; } diff --git a/milena/mln/world/kn/fill_0_1_faces_internal_border.hh b/milena/mln/world/kn/fill_0_1_faces_internal_border.hh index 7324a34..915a69a 100644 --- a/milena/mln/world/kn/fill_0_1_faces_internal_border.hh +++ b/milena/mln/world/kn/fill_0_1_faces_internal_border.hh @@ -30,7 +30,7 @@ #ifndef MLN_WORLD_KN_FILL_0_1_FACES_INTERNAL_BORDER_HH # define MLN_WORLD_KN_FILL_0_1_FACES_INTERNAL_BORDER_HH -# include <mln/core/alias/point2d.hh> +# include <mln/inner_border/fill.hh> namespace mln @@ -57,7 +57,8 @@ namespace mln */ template <typename I> - void fill_0_1_faces_internal_border(Image<I>& inout, const mln_value(I)& v); + void fill_0_1_faces_internal_border(Image<I>& inout, + const mln_value(I)& v); @@ -68,26 +69,15 @@ namespace mln template <typename I> - void fill_0_1_faces_internal_border(Image<I>& inout_, const mln_value(I)& v) + void fill_0_1_faces_internal_border(Image<I>& inout_, + const mln_value(I)& v) { trace::entering("mln::world::kn::fill_0_1_faces_internal_border"); mln_precondition(exact(inout_).is_valid()); I& inout = exact(inout_); - // Horizontal borders - for (mln::def::coord col = geom::min_col(inout); col <= geom::max_col(inout); ++col) - { - inout.at_(geom::min_row(inout), col) = v; - inout.at_(geom::max_row(inout), col) = v; - } - - // Vertical borders - for (mln::def::coord row = geom::min_row(inout); row <= geom::max_row(inout); ++row) - { - inout.at_(row, geom::min_col(inout)) = v; - inout.at_(row, geom::max_col(inout)) = v; - } + inner_border::fill(inout, v); trace::exiting("mln::world::kn::fill_0_1_faces_internal_border"); } diff --git a/milena/mln/world/kn/internal/fill_primary_2_faces_from_input.hh b/milena/mln/world/kn/internal/fill_primary_2_faces_from_input.hh index 5884771..04baddc 100644 --- a/milena/mln/world/kn/internal/fill_primary_2_faces_from_input.hh +++ b/milena/mln/world/kn/internal/fill_primary_2_faces_from_input.hh @@ -83,11 +83,12 @@ namespace mln const J& ima = exact(ima_); // Filling Primary 2-Faces + typedef mln_value(I) V; mln_piter(J) p(ima.domain()); for_all(p) { mln_site(I) pout = internal::immerse_point(p, n, inner_border_thickness); - ima_kn(pout) = safe_cast(ima(p)); + ima_kn(pout) = safe_cast_to<V>(ima(p)); } trace::exiting("mln::world::kn::internal::fill_primary_2_faces_from_input"); diff --git a/milena/mln/world/kn/safe_cast.hh b/milena/mln/world/kn/safe_cast.hh index 31fe3cd..d4e4f5c 100644 --- a/milena/mln/world/kn/safe_cast.hh +++ b/milena/mln/world/kn/safe_cast.hh @@ -46,17 +46,6 @@ namespace mln using namespace mln::value; - // Forward declaration. - namespace internal { - template <typename Tsrc> struct to_be_casted_t; - } - - - /// \brief Safe convertion function for manipulating Kn-immersed - /// images. - template <typename Tsrc> - internal::to_be_casted_t<Tsrc> safe_cast(const Tsrc& src); - /// \brief Safe convertion function for manipulating Kn-immersed /// images. template <typename Tdest, typename Tsrc> @@ -85,19 +74,47 @@ namespace mln to = intsub<n>(from.to_interop()); } + inline + void safe_cast_(const int_u8& from, interval<int_u8>& to) + { + to = from; + } + + inline + void safe_cast_(const int_u8& from, int& to) + { + to = from.to_interop(); + } + template <unsigned n> - void safe_cast_(const interval<intsub<n> >& from, value::int_u8& to) + void safe_cast_(const interval<intsub<n> >& from, int_u8& to) { if (!from.is_degenerated()) - abort(); + std::abort(); to = intsub<n>(from.first()); } + inline + void safe_cast_(const interval<int_u8>& from, int_u8& to) + { + if (!from.is_degenerated()) + std::abort(); + to = from.first(); + } + + template <unsigned n> + void safe_cast_(const interval<int_u8>& from, intsub<n>& to) + { + if (!from.is_degenerated()) + std::abort(); + to = from.first().to_interop(); + } + template <unsigned n> void safe_cast_(const interval<intsub<n> >& from, int& to) { if (!from.is_degenerated()) - abort(); + std::abort(); to = from.first(); } @@ -105,7 +122,7 @@ namespace mln void safe_cast_(const interval<intsub<n> >& from, intsub<n>& to) { if (!from.is_degenerated()) - abort(); + std::abort(); to = from.first(); } @@ -115,6 +132,12 @@ namespace mln to = interval<intsub<n> >(from); } + inline + void safe_cast_(const int& from, int_u8& to) + { + to = from; + } + template <unsigned n> void safe_cast_(const intsub<n>& from, intsub<2*n>& to) { @@ -122,6 +145,12 @@ namespace mln } template <unsigned n> + void safe_cast_(const intsub<n>& from, intsub<n/2>& to) + { + to = static_cast<intsub<n/2> >(from.to_int()); + } + + template <unsigned n> void safe_cast_(const intsub<n>& from, int_u8& to) { to = from.to_interop(); @@ -152,34 +181,6 @@ namespace mln namespace kn { - namespace internal - { - - template <typename Tsrc> - struct to_be_casted_t - { - to_be_casted_t(const Tsrc& src) : src(src) {} - Tsrc src; - - template <typename Tdest> - operator Tdest const() - { - Tdest dest; - safe_cast_(src, dest); - return dest; - } - }; - - } // end of namespace mln::world::kn::internal - - - template <typename Tsrc> - internal::to_be_casted_t<Tsrc> safe_cast(const Tsrc& src) - { - internal::to_be_casted_t<Tsrc> tmp(src); - return tmp; - } - template <typename Tdest, typename Tsrc> Tdest safe_cast_to(const Tsrc& src) { diff --git a/milena/mln/world/kn/un_immerse.hh b/milena/mln/world/kn/un_immerse.hh index 5159d86..ead7132 100644 --- a/milena/mln/world/kn/un_immerse.hh +++ b/milena/mln/world/kn/un_immerse.hh @@ -113,11 +113,11 @@ namespace mln const I& ima = exact(ima_); (void) new_value_type; - mln_concrete(I) output(internal::domain_K0_from_Kn(ima.domain(), n)); + mln_ch_value(I,V) output(internal::domain_K0_from_Kn(ima.domain(), n)); mln_piter(I) p(output.domain()); for_all(p) - output(p) = safe_cast(ima(internal::immerse_point(p, n))); + output(p) = safe_cast_to<V>(ima(internal::immerse_point(p, n))); trace::exiting("mln::world::kn::un_immerse"); return output; -- 1.7.2.5