00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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,
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
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 unsigned round_up(float f)
00127 {
00128 unsigned n = static_cast<unsigned>(f + 0.499999f);
00129 if (n == 0u)
00130 ++n;
00131 if (float(n) < f)
00132 ++n;
00133 return n;
00134 }
00135
00136 void slices2d_helper(float nslis, float nrows, float ncols,
00137 float ratio_hv,
00138 unsigned& n_horizontal,
00139 unsigned& n_vertical)
00140 {
00141 if (ratio_hv > 1.f)
00142 {
00143 float n_v = std::sqrt(nslis * ncols / ratio_hv / nrows);
00144 n_vertical = internal::round_up(n_v);
00145 float n_h = nslis / float(n_vertical);
00146 n_horizontal = internal::round_up(n_h);
00147 }
00148 else
00149 {
00150 float n_h = std::sqrt(nrows * nslis * ratio_hv / ncols);
00151 n_horizontal = internal::round_up(n_h);
00152 float n_v = nslis / float(n_horizontal);
00153 n_vertical = internal::round_up(n_v);
00154 }
00155 }
00156
00157 }
00158
00159
00160 template <typename I>
00161 image2d<mln_value(I)>
00162 slices_2d(const Image<I>& input_,
00163 float ratio_hv,
00164 const mln_value(I)& bg)
00165 {
00166 trace::entering("debug::slices_2d");
00167 mlc_equal(mln_domain(I), box3d)::check();
00168
00169 const I& input = exact(input_);
00170 mln_precondition(input.is_valid());
00171 mln_precondition(ratio_hv > 0.f);
00172
00173 unsigned n_horizontal, n_vertical;
00174 internal::slices2d_helper(input.nslis(), input.nrows(), input.ncols(),
00175 ratio_hv,
00176 n_horizontal, n_vertical);
00177 mln_assertion(n_horizontal * n_vertical >= input.nslis());
00178
00179 image2d<mln_value(I)> output = slices_2d(input, n_horizontal, n_vertical, bg);
00180
00181 trace::exiting("debug::slices_2d");
00182 return output;
00183 }
00184
00185
00186 # endif // ! MLN_INCLUDE_ONLY
00187
00188 }
00189
00190 }
00191
00192
00193 #endif // ! MLN_DEBUG_SLICES_2D_HH