Milena (Olena)  User documentation 2.0a Id
slices_2d.hh
00001 // Copyright (C) 2008, 2009, 2010, 2011 EPITA Research and Development
00002 // Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_DEBUG_SLICES_2D_HH
00028 # define MLN_DEBUG_SLICES_2D_HH
00029 
00033 
00034 # include <cmath>
00035 
00036 # include <mln/core/image/image2d.hh>
00037 
00038 # include <mln/core/image/image3d.hh>
00039 # include <mln/core/image/dmorph/slice_image.hh>
00040 
00041 # include <mln/core/image/dmorph/p2p_image.hh>
00042 # include <mln/fun/p2p/translation.hh>
00043 
00044 # include <mln/data/paste.hh>
00045 # include <mln/data/fill.hh>
00046 
00047 
00048 
00049 namespace mln
00050 {
00051 
00052   namespace debug
00053   {
00054 
00057     template <typename I>
00058     image2d<mln_value(I)>
00059     slices_2d(const Image<I>& input,
00060               unsigned n_horizontal, unsigned n_vertical,
00061               const mln_value(I)& bg);
00062 
00063 
00066     template <typename I>
00067     image2d<mln_value(I)>
00068     slices_2d(const Image<I>& input,
00069               float ratio_hv,           // horizontal / vertical
00070               const mln_value(I)& bg);
00071 
00072 
00073 
00074 # ifndef MLN_INCLUDE_ONLY
00075 
00076     template <typename I>
00077     inline
00078     image2d<mln_value(I)>
00079     slices_2d(const Image<I>& input_,
00080               unsigned n_horizontal, unsigned n_vertical,
00081               const mln_value(I)& bg)
00082     {
00083       trace::entering("debug::slices_2d");
00084       mlc_equal(mln_domain(I), box3d)::check();
00085 
00086       const I& input = exact(input_);
00087 
00088       mln_precondition(input.is_valid());
00089       mln_precondition(n_horizontal > 0 && n_vertical > 0);
00090       mln_precondition(input.nslis() <= n_horizontal * n_vertical);
00091 
00092       image2d<mln_value(I)> output(input.nrows() * n_vertical,
00093                                    input.ncols() * n_horizontal);
00094       if (input.nslis() != n_horizontal * n_vertical)
00095         data::fill(output, bg);
00096 
00097       const point3d& p_min = input.domain().pmin();
00098       def::coord
00099         sli = p_min.sli(),
00100         last_sli = input.domain().pmax().sli();
00101       for (unsigned i = 0; i < n_vertical; ++i)
00102         for (unsigned j = 0; j < n_horizontal; ++j)
00103           {
00104             dpoint2d dp(i * input.nrows() - p_min.row(),
00105                         j * input.ncols() - p_min.col());
00106             data::paste(apply_p2p(slice(input, sli),
00107                                   fun::p2p::translation(dp)),
00108                         output);
00109             if (++sli > last_sli)
00110               {
00111                 // Go out of loops.
00112                 i = n_vertical;
00113                 j = n_horizontal;
00114                 break;
00115               }
00116           }
00117 
00118       trace::exiting("debug::slices_2d");
00119       return output;
00120     }
00121 
00122 
00123     namespace internal
00124     {
00125 
00126       inline
00127       unsigned round_up(float f)
00128       {
00129         unsigned n = static_cast<unsigned>(f + 0.499999f);
00130         if (n == 0u)
00131           ++n;
00132         if (float(n) < f)
00133           ++n;
00134         return n;
00135       }
00136 
00137       inline
00138       void slices2d_helper(float nslis, float nrows, float ncols,
00139                            float ratio_hv,
00140                            unsigned& n_horizontal,
00141                            unsigned& n_vertical)
00142       {
00143         if (ratio_hv > 1.f)
00144           {
00145             float n_v = std::sqrt(nslis * ncols / ratio_hv / nrows);
00146             n_vertical = internal::round_up(n_v);
00147             float n_h = nslis / float(n_vertical);
00148             n_horizontal = internal::round_up(n_h);
00149           }
00150         else
00151           {
00152             float n_h = std::sqrt(nrows * nslis * ratio_hv / ncols);
00153             n_horizontal = internal::round_up(n_h);
00154             float n_v = nslis / float(n_horizontal);
00155             n_vertical = internal::round_up(n_v);
00156           }
00157       }
00158 
00159     } // end of namespace mln::debug::internal
00160 
00161 
00162     template <typename I>
00163     image2d<mln_value(I)>
00164     slices_2d(const Image<I>& input_,
00165               float ratio_hv,           // horizontal / vertical
00166               const mln_value(I)& bg)
00167     {
00168       trace::entering("debug::slices_2d");
00169       mlc_equal(mln_domain(I), box3d)::check();
00170 
00171       const I& input = exact(input_);
00172       mln_precondition(input.is_valid());
00173       mln_precondition(ratio_hv > 0.f);
00174 
00175       unsigned n_horizontal, n_vertical;
00176       internal::slices2d_helper(input.nslis(), input.nrows(), input.ncols(),
00177                                 ratio_hv,
00178                                 n_horizontal, n_vertical);
00179       mln_assertion(n_horizontal * n_vertical >= input.nslis());
00180 
00181       image2d<mln_value(I)> output = slices_2d(input, n_horizontal, n_vertical, bg);
00182 
00183       trace::exiting("debug::slices_2d");
00184       return output;
00185     }
00186 
00187 
00188 # endif // ! MLN_INCLUDE_ONLY
00189 
00190   } // end of namespace mln::debug
00191 
00192 } // end of namespace mln
00193 
00194 
00195 #endif // ! MLN_DEBUG_SLICES_2D_HH
 All Classes Namespaces Functions Variables Typedefs Enumerator