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
00028 #ifndef OLENA_LRDE_UFMT_UTILS_HH
00029 # define OLENA_LRDE_UFMT_UTILS_HH
00030
00031 # include <cstdlib>
00032 # include <vector>
00033
00034 # include <ntg/real/int_u.hh>
00035 # include <oln/core/image1d.hh>
00036 # include <oln/core/image2d.hh>
00037 # include <oln/core/image3d.hh>
00038
00039
00040
00041 # define oln_neighborhood_type(I) \
00042 typename oln::lrde::ufmt::internal::neighborhood<I>::ret
00043
00044
00045
00046 namespace oln
00047 {
00048
00049
00050
00051 struct neighborhood1d;
00052 struct neighborhood2d;
00053 struct neighborhood3d;
00054
00055
00056
00057
00058 namespace lrde
00059 {
00060
00061 namespace ufmt
00062 {
00063
00064
00065
00066
00067
00068
00069 template <class I>
00070 size_t uint_nvalues(const abstract::image<I>& input)
00071 {
00072 typedef oln_value_type(I) T;
00073 typedef typename ntg_is_a(T, ntg::unsigned_integer)::ensure_type ensure_type;
00074 return size_t(ntg_max_val(T)) + 1;
00075 }
00076
00077 template <class T>
00078 size_t uint_nvalues()
00079 {
00080 typedef typename ntg_is_a(T, ntg::unsigned_integer)::ensure_type ensure_type;
00081 return size_t(ntg_max_val(T)) + 1;
00082 }
00083
00084
00085
00086
00087
00088 template <class I>
00089 std::vector<size_t> histogram(const abstract::image<I>& input,
00090 size_t N)
00091 {
00092 unsigned nvalues = uint_nvalues(input);
00093 std::vector<size_t> H(nvalues, 0);
00094 size_t count = 0;
00095 oln_iter_type(I) p(input);
00096 for_all(p)
00097 {
00098 ++H[input[p]];
00099 if (++count == N)
00100 return H;
00101 }
00102 return H;
00103 }
00104
00105
00106
00107
00108 template <class I>
00109 std::vector<size_t> histogram(const abstract::image<I>& input)
00110 {
00111 return histogram(input, input.npoints());
00112 }
00113
00114
00115
00116
00117 template <class I>
00118 std::vector<oln_point_type(I)>
00119 histogram_reverse_sort_p(const abstract::image<I>& input,
00120 size_t N,
00121 std::vector<size_t>& H)
00122 {
00123 H = histogram(input, N);
00124
00125
00126
00127 unsigned nvalues = uint_nvalues(input);
00128 std::vector<int> loc(nvalues);
00129 loc[nvalues - 1] = 0;
00130 for (int l = int(nvalues) - 2; l >= 0; --l)
00131 loc[l] = loc[l+1] + H[l+1];
00132
00133 std::vector<oln_point_type(I)> vec(N);
00134
00135
00136
00137 oln_iter_type(I) p(input);
00138 size_t count = 0;
00139 for_all(p)
00140 {
00141 vec[loc[input[p]]++] = p;
00142 if (++count == N)
00143 return vec;
00144 }
00145 return vec;
00146 }
00147
00148
00149
00150 template <class I>
00151 std::vector<oln_point_type(I)>
00152 histogram_reverse_sort_p(const abstract::image<I>& input,
00153 std::vector<size_t>& H)
00154 {
00155 return histogram_reverse_sort_p(input, input.npoints(), H);
00156 }
00157
00158
00159 template <class I>
00160 std::vector<oln_point_type(I)>
00161 histogram_reverse_sort_p(const abstract::image<I>& input,
00162 size_t N)
00163 {
00164 std::vector<size_t> H;
00165 return histogram_reverse_sort_p(input, N, H);
00166 }
00167
00168
00169 template <class I>
00170 std::vector<oln_point_type(I)>
00171 histogram_reverse_sort_p(const abstract::image<I>& input)
00172 {
00173 std::vector<size_t> H;
00174 return histogram_reverse_sort_p(input, input.npoints(), H);
00175 }
00176
00177
00178
00179
00180 template <class I>
00181 std::vector<int>
00182 histogram_reverse_sort_i(const abstract::image<I>& input,
00183 size_t N,
00184 std::vector<size_t>& H)
00185 {
00186 H = histogram(input, N);
00187
00188
00189
00190 unsigned nvalues = uint_nvalues(input);
00191 std::vector<int> loc(nvalues);
00192 loc[nvalues - 1] = 0;
00193 for (int l = int(nvalues) - 2; l >= 0; --l)
00194 loc[l] = loc[l+1] + H[l+1];
00195
00196 std::vector<int> vec(N);
00197
00198
00199
00200 typedef oln_value_type(I) value_t;
00201 typedef oln_point_type(I) point_t;
00202
00203 I& input_ = const_cast<I&>(input.exact());
00204 value_t* orig = &(input_[point_t()]);
00205 oln_iter_type(I) p(input);
00206 size_t count = 0;
00207 for_all(p)
00208 {
00209 vec[loc[input[p]]++] = int(&(input_[p]) - orig);
00210 if (++count == N)
00211 return vec;
00212 }
00213
00214 return vec;
00215 }
00216
00217
00218 template <class I>
00219 std::vector<int>
00220 histogram_reverse_sort_i(const abstract::image<I>& input,
00221 std::vector<size_t>& H)
00222 {
00223 return histogram_reverse_sort_i(input, input.npoints(), H);
00224 }
00225
00226
00227
00228
00229
00230
00231 namespace internal {
00232
00233 template <class I>
00234 struct neighborhood;
00235
00236 template <class T>
00237 struct neighborhood < image1d<T> > { typedef neighborhood1d ret; };
00238 template <class T>
00239 struct neighborhood < image2d<T> > { typedef neighborhood2d ret; };
00240 template <class T>
00241 struct neighborhood < image3d<T> > { typedef neighborhood3d ret; };
00242
00243 }
00244
00245
00246
00247 template <class I>
00248 unsigned pre(const oln_neighborhood_type(I)& nbh,
00249 std::vector<oln_dpoint_type(I)>& pre)
00250 {
00251 for (unsigned i = 0; i < nbh.card(); ++i)
00252 {
00253 unsigned d;
00254 for (d = 0; d < I::dim; ++d)
00255 if (nbh.dp(i).nth(d) < 0) {
00256 pre.push_back(nbh.dp(i));
00257 break;
00258 }
00259 else if (nbh.dp(i).nth(d) > 0)
00260 break;
00261 }
00262 assert(pre.size() == (nbh.card() / 2));
00263 return pre.size();
00264 }
00265
00266
00267 template <class I>
00268 unsigned split(const oln_neighborhood_type(I)& nbh,
00269 std::vector<oln_dpoint_type(I)>& pre,
00270 std::vector<oln_dpoint_type(I)>& post)
00271 {
00272 for (unsigned i = 0; i < nbh.card(); ++i)
00273 {
00274 unsigned d;
00275 bool is_post = true;
00276 for (d = 0; d < I::dim; ++d)
00277 if (nbh.dp(i).nth(d) < 0) {
00278 pre.push_back(nbh.dp(i));
00279 is_post = false;
00280 break;
00281 }
00282 else if (nbh.dp(i).nth(d) > 0)
00283 break;
00284 if (is_post)
00285 post.push_back(nbh.dp(i));
00286 }
00287 if (not ((pre.size() + post.size()) == nbh.card())
00288 or not (pre.size() == post.size()))
00289 {
00290 std::cerr << "pb in oln::lrde::ufmt::split (utils.hh) so abort!" << std::endl;
00291 abort();
00292 }
00293 assert((pre.size() + post.size()) == nbh.card());
00294 assert(pre.size() == post.size());
00295 return pre.size();
00296 }
00297
00298
00299
00300 template <class I>
00301 typename mute<I, float>::ret
00302 to_float_with_noise(const abstract::image<I>& input)
00303 {
00304 const float max = float(RAND_MAX);
00305 typename mute<I, float>::ret output(input.size());
00306 oln_iter_type(I) p(input);
00307 for_all(p)
00308 output[p] = float(input[p]) + float(rand()) / max;
00309 return output;
00310 }
00311
00312
00313 template <typename I>
00314 struct rev_sort
00315 {
00316 typedef oln_point_type(I) point;
00317 const I& f;
00318
00319 rev_sort(const I& f) :
00320 f(f)
00321 {}
00322
00323 bool operator()(const point& lhs, const point& rhs) const
00324 {
00325 return f[lhs] > f[rhs];
00326 }
00327 };
00328
00329
00330 }
00331
00332 }
00333
00334 }
00335
00336
00337
00338
00339 #endif // ! OLENA_LRDE_UFMT_UTILS_HH