$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends 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 
35 
36 # include <cmath>
37 
38 # include <mln/core/image/image2d.hh>
39 
40 # include <mln/core/image/image3d.hh>
41 # include <mln/core/image/dmorph/slice_image.hh>
42 
43 # include <mln/core/image/dmorph/p2p_image.hh>
44 # include <mln/fun/p2p/translation.hh>
45 
46 # include <mln/data/paste.hh>
47 # include <mln/data/fill.hh>
48 
49 
50 
51 namespace mln
52 {
53 
54  namespace debug
55  {
56 
61  template <typename I>
62  image2d<mln_value(I)>
63  slices_2d(const Image<I>& input,
64  unsigned n_horizontal, unsigned n_vertical,
65  const mln_value(I)& bg);
66 
67 
72  template <typename I>
73  image2d<mln_value(I)>
74  slices_2d(const Image<I>& input,
75  float ratio_hv, // horizontal / vertical
76  const mln_value(I)& bg);
77 
78 
79 
80 # ifndef MLN_INCLUDE_ONLY
81 
82  template <typename I>
83  inline
84  image2d<mln_value(I)>
85  slices_2d(const Image<I>& input_,
86  unsigned n_horizontal, unsigned n_vertical,
87  const mln_value(I)& bg)
88  {
89  mln_trace("debug::slices_2d");
90  mlc_equal(mln_domain(I), box3d)::check();
91 
92  const I& input = exact(input_);
93 
94  mln_precondition(input.is_valid());
95  mln_precondition(n_horizontal > 0 && n_vertical > 0);
96  mln_precondition(input.nslis() <= n_horizontal * n_vertical);
97 
98  image2d<mln_value(I)> output(input.nrows() * n_vertical,
99  input.ncols() * n_horizontal);
100  if (input.nslis() != n_horizontal * n_vertical)
101  data::fill(output, bg);
102 
103  const point3d& p_min = input.domain().pmin();
104  def::coord
105  sli = p_min.sli(),
106  last_sli = input.domain().pmax().sli();
107  for (unsigned i = 0; i < n_vertical; ++i)
108  for (unsigned j = 0; j < n_horizontal; ++j)
109  {
110  dpoint2d dp(i * input.nrows() - p_min.row(),
111  j * input.ncols() - p_min.col());
112  data::paste(apply_p2p(slice(input, sli),
114  output);
115  if (++sli > last_sli)
116  {
117  // Go out of loops.
118  i = n_vertical;
119  j = n_horizontal;
120  break;
121  }
122  }
123 
124  return output;
125  }
126 
127 
128  namespace internal
129  {
130 
131  inline
132  unsigned round_up(float f)
133  {
134  unsigned n = static_cast<unsigned>(f + 0.499999f);
135  if (n == 0u)
136  ++n;
137  if (float(n) < f)
138  ++n;
139  return n;
140  }
141 
142  inline
143  void slices2d_helper(float nslis, float nrows, float ncols,
144  float ratio_hv,
145  unsigned& n_horizontal,
146  unsigned& n_vertical)
147  {
148  if (ratio_hv > 1.f)
149  {
150  float n_v = std::sqrt(nslis * ncols / ratio_hv / nrows);
151  n_vertical = internal::round_up(n_v);
152  float n_h = nslis / float(n_vertical);
153  n_horizontal = internal::round_up(n_h);
154  }
155  else
156  {
157  float n_h = std::sqrt(nrows * nslis * ratio_hv / ncols);
158  n_horizontal = internal::round_up(n_h);
159  float n_v = nslis / float(n_horizontal);
160  n_vertical = internal::round_up(n_v);
161  }
162  }
163 
164  } // end of namespace mln::debug::internal
165 
166 
167  template <typename I>
168  image2d<mln_value(I)>
169  slices_2d(const Image<I>& input_,
170  float ratio_hv, // horizontal / vertical
171  const mln_value(I)& bg)
172  {
173  mln_trace("debug::slices_2d");
174  mlc_equal(mln_domain(I), box3d)::check();
175 
176  const I& input = exact(input_);
177  mln_precondition(input.is_valid());
178  mln_precondition(ratio_hv > 0.f);
179 
180  unsigned n_horizontal, n_vertical;
181  internal::slices2d_helper(input.nslis(), input.nrows(), input.ncols(),
182  ratio_hv,
183  n_horizontal, n_vertical);
184  mln_assertion(n_horizontal * n_vertical >= input.nslis());
185 
186  image2d<mln_value(I)> output = slices_2d(input, n_horizontal, n_vertical, bg);
187 
188  return output;
189  }
190 
191 
192 # endif // ! MLN_INCLUDE_ONLY
193 
194  } // end of namespace mln::debug
195 
196 } // end of namespace mln
197 
198 
199 #endif // ! MLN_DEBUG_SLICES_2D_HH