• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

slices_2d.hh

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

Generated on Thu Sep 8 2011 18:32:28 for Milena (Olena) by  doxygen 1.7.1