Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
slices_2d.hh
1 // Copyright (C) 2008, 2009, 2010, 2011 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_DEBUG_SLICES_2D_HH
28 # define MLN_DEBUG_SLICES_2D_HH
29 
33 
34 # include <cmath>
35 
36 # include <mln/core/image/image2d.hh>
37 
38 # include <mln/core/image/image3d.hh>
39 # include <mln/core/image/dmorph/slice_image.hh>
40 
41 # include <mln/core/image/dmorph/p2p_image.hh>
42 # include <mln/fun/p2p/translation.hh>
43 
44 # include <mln/data/paste.hh>
45 # include <mln/data/fill.hh>
46 
47 
48 
49 namespace mln
50 {
51 
52  namespace debug
53  {
54 
57  template <typename I>
58  image2d<mln_value(I)>
59  slices_2d(const Image<I>& input,
60  unsigned n_horizontal, unsigned n_vertical,
61  const mln_value(I)& bg);
62 
63 
66  template <typename I>
67  image2d<mln_value(I)>
68  slices_2d(const Image<I>& input,
69  float ratio_hv, // horizontal / vertical
70  const mln_value(I)& bg);
71 
72 
73 
74 # ifndef MLN_INCLUDE_ONLY
75 
76  template <typename I>
77  inline
78  image2d<mln_value(I)>
79  slices_2d(const Image<I>& input_,
80  unsigned n_horizontal, unsigned n_vertical,
81  const mln_value(I)& bg)
82  {
83  trace::entering("debug::slices_2d");
84  mlc_equal(mln_domain(I), box3d)::check();
85 
86  const I& input = exact(input_);
87 
88  mln_precondition(input.is_valid());
89  mln_precondition(n_horizontal > 0 && n_vertical > 0);
90  mln_precondition(input.nslis() <= n_horizontal * n_vertical);
91 
92  image2d<mln_value(I)> output(input.nrows() * n_vertical,
93  input.ncols() * n_horizontal);
94  if (input.nslis() != n_horizontal * n_vertical)
95  data::fill(output, bg);
96 
97  const point3d& p_min = input.domain().pmin();
99  sli = p_min.sli(),
100  last_sli = input.domain().pmax().sli();
101  for (unsigned i = 0; i < n_vertical; ++i)
102  for (unsigned j = 0; j < n_horizontal; ++j)
103  {
104  dpoint2d dp(i * input.nrows() - p_min.row(),
105  j * input.ncols() - p_min.col());
106  data::paste(apply_p2p(slice(input, sli),
107  fun::p2p::translation(dp)),
108  output);
109  if (++sli > last_sli)
110  {
111  // Go out of loops.
112  i = n_vertical;
113  j = n_horizontal;
114  break;
115  }
116  }
117 
118  trace::exiting("debug::slices_2d");
119  return output;
120  }
121 
122 
123  namespace internal
124  {
125 
126  inline
127  unsigned round_up(float f)
128  {
129  unsigned n = static_cast<unsigned>(f + 0.499999f);
130  if (n == 0u)
131  ++n;
132  if (float(n) < f)
133  ++n;
134  return n;
135  }
136 
137  inline
138  void slices2d_helper(float nslis, float nrows, float ncols,
139  float ratio_hv,
140  unsigned& n_horizontal,
141  unsigned& n_vertical)
142  {
143  if (ratio_hv > 1.f)
144  {
145  float n_v = std::sqrt(nslis * ncols / ratio_hv / nrows);
146  n_vertical = internal::round_up(n_v);
147  float n_h = nslis / float(n_vertical);
148  n_horizontal = internal::round_up(n_h);
149  }
150  else
151  {
152  float n_h = std::sqrt(nrows * nslis * ratio_hv / ncols);
153  n_horizontal = internal::round_up(n_h);
154  float n_v = nslis / float(n_horizontal);
155  n_vertical = internal::round_up(n_v);
156  }
157  }
158 
159  } // end of namespace mln::debug::internal
160 
161 
162  template <typename I>
163  image2d<mln_value(I)>
164  slices_2d(const Image<I>& input_,
165  float ratio_hv, // horizontal / vertical
166  const mln_value(I)& bg)
167  {
168  trace::entering("debug::slices_2d");
169  mlc_equal(mln_domain(I), box3d)::check();
170 
171  const I& input = exact(input_);
172  mln_precondition(input.is_valid());
173  mln_precondition(ratio_hv > 0.f);
174 
175  unsigned n_horizontal, n_vertical;
176  internal::slices2d_helper(input.nslis(), input.nrows(), input.ncols(),
177  ratio_hv,
178  n_horizontal, n_vertical);
179  mln_assertion(n_horizontal * n_vertical >= input.nslis());
180 
181  image2d<mln_value(I)> output = slices_2d(input, n_horizontal, n_vertical, bg);
182 
183  trace::exiting("debug::slices_2d");
184  return output;
185  }
186 
187 
188 # endif // ! MLN_INCLUDE_ONLY
189 
190  } // end of namespace mln::debug
191 
192 } // end of namespace mln
193 
194 
195 #endif // ! MLN_DEBUG_SLICES_2D_HH