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 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 }
00160
00161
00162 template <typename I>
00163 image2d<mln_value(I)>
00164 slices_2d(const Image<I>& input_,
00165 float ratio_hv,
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 }
00191
00192 }
00193
00194
00195 #endif // ! MLN_DEBUG_SLICES_2D_HH