$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sort_psites.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_DATA_SORT_PSITES_HH
27 # define MLN_DATA_SORT_PSITES_HH
28 
34 
35 # include <algorithm>
36 
37 # include <mln/core/concept/image.hh>
38 # include <mln/convert/to_p_array.hh>
39 # include <mln/histo/compute.hh>
40 # include <mln/util/ord.hh>
41 # include <mln/geom/nsites.hh>
42 
43 
44 namespace mln
45 {
46 
47  namespace data
48  {
49 
57  template <typename I>
58  p_array<mln_psite(I)> sort_psites_increasing(const Image<I>& input);
59 
67  template <typename I>
68  p_array<mln_psite(I)> sort_psites_decreasing(const Image<I>& input);
69 
70 
71 # ifndef MLN_INCLUDE_ONLY
72 
73  namespace impl
74  {
75 
76  // utility
77 
78  template <typename I>
79  struct value_psite_less_
80  {
81  const I& ima_;
82 
83  inline
84  value_psite_less_(const I& ima)
85  : ima_(ima)
86  {
87  }
88 
89  inline
90  bool operator()(const mln_psite(I)& lhs,
91  const mln_psite(I)& rhs) const
92  {
93  return util::ord_strict(ima_(lhs), ima_(rhs))
94  || (ima_(lhs) == ima_(rhs)
95  &&
96  util::ord_strict(lhs, rhs));
97  }
98  };
99 
100  template <typename I>
101  struct value_psite_greater_
102  {
103  const I& ima_;
104 
105  inline
106  value_psite_greater_(const I& ima)
107  : ima_(ima)
108  {
109  }
110 
111  inline
112  bool operator()(const mln_psite(I)& lhs,
113  const mln_psite(I)& rhs) const
114  {
115  return util::ord_strict(ima_(rhs), ima_(lhs))
116  || (ima_(lhs) == ima_(rhs)
117  &&
118  util::ord_strict(rhs, lhs));
119  }
120  };
121 
122 
123  // increasing
124 
125  template <typename I>
126  inline
127  p_array<mln_psite(I)>
128  sort_psites_increasing_(trait::image::quant::any, // general case
129  const I& input)
130  {
131  p_array<mln_psite(I)> v = mln::convert::to_p_array(input.domain());
132  std::sort(v.hook_std_vector_().begin(), v.hook_std_vector_().end(),
133  value_psite_less_<I>(input));
134  return v;
135  }
136 
137  template <typename I>
138  inline
139  p_array<mln_psite(I)>
140  sort_psites_increasing_(trait::image::quant::low, // low quantization
141  const I& input)
142  {
143  typedef mln_vset(I) S;
144  const S& vset = input.values_eligible();
145  const unsigned n = vset.nvalues();
146 
147  // h
148  histo::array<mln_value(I)> h = histo::compute(input);
149 
150  // preparing output data
151  std::vector<unsigned> loc(vset.nvalues());
152  loc[0] = 0;
153  for (unsigned i = 1; i != n; ++i)
154  loc[i] = loc[i-1] + h[i-1];
155 
156  // computing output data
157  std::vector<mln_psite(I)> vec(geom::nsites(input));
158  mln_fwd_piter(I) p(input.domain());
159  for_all(p)
160  vec[loc[vset.index_of(input(p))]++] = p;
161 
162  p_array<mln_psite(I)> v(vec);
163  return v;
164  }
165 
166 
167  // decreasing
168 
169  template <typename I>
170  inline
171  p_array<mln_psite(I)>
172  sort_psites_decreasing_(trait::image::quant::any, // general case
173  const I& input)
174  {
175  p_array<mln_psite(I)> v = mln::convert::to_p_array(input.domain());
176  std::sort(v.hook_std_vector_().begin(), v.hook_std_vector_().end(),
177  value_psite_greater_<I>(input));
178  return v;
179  }
180 
181  template <typename I>
182  inline
183  p_array<mln_psite(I)>
184  sort_psites_decreasing_(trait::image::quant::low, // low quantization
185  const I& input)
186  {
187  typedef mln_vset(I) S;
188  const S& vset = input.values_eligible();
189  const unsigned n = vset.nvalues();
190 
191  // h
192  histo::array<mln_value(I)> h = histo::compute(input);
193 
194  // preparing output data
195  std::vector<unsigned> loc(vset.nvalues());
196  loc[n-1] = 0;
197  for (int i = n - 2; i >= 0; --i)
198  loc[i] = loc[i+1] + h[i+1];
199 
200  // computing output data
201  std::vector<mln_psite(I)> vec(geom::nsites(input));
202  mln_fwd_piter(I) p(input.domain());
203  for_all(p)
204  vec[loc[vset.index_of(input(p))]++] = p;
205 
206  p_array<mln_psite(I)> v(vec);
207  return v;
208  }
209 
210 
211  } // end of namespace mln::data::impl
212 
213 
214  // Facades.
215 
216  template <typename I>
217  inline
218  p_array<mln_psite(I)>
219  sort_psites_increasing(const Image<I>& input)
220  {
221  mln_precondition(exact(input).is_valid());
222  return impl::sort_psites_increasing_(mln_trait_image_quant(I)(),
223  exact(input));
224  }
225 
226  template <typename I>
227  inline
228  p_array<mln_psite(I)>
229  sort_psites_decreasing(const Image<I>& input)
230  {
231  mln_precondition(exact(input).is_valid());
232  return impl::sort_psites_decreasing_(mln_trait_image_quant(I)(),
233  exact(input));
234  }
235 
236 # endif // ! MLN_INCLUDE_ONLY
237 
238  } // end of namespace mln::data
239 
240 } // end of namespace mln
241 
242 
243 #endif // ! MLN_DATA_SORT_PSITES_HH