$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
window.hh
1 // Copyright (C) 2007, 2008, 2009, 2012, 2013 EPITA Research and
2 // Development 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_CORE_WINDOW_HH
28 # define MLN_CORE_WINDOW_HH
29 
44 # include <mln/core/internal/window_base.hh>
45 # include <mln/core/concept/gdpoint.hh>
46 
47 # include <mln/metal/is_a.hh>
48 # include <mln/util/set.hh>
49 # include <mln/fun/i2v/all_to.hh>
50 # include <mln/norm/linfty.hh>
51 # include <mln/literal/zero.hh>
52 
53 
54 namespace mln
55 {
56 
57  // Forward declarations.
58  template <typename D> class window;
59  template <typename V> class dpsites_fwd_piter;
60  template <typename V> class dpsites_bkd_piter;
61 
62 
63 
64  namespace trait
65  {
66 
67  template <typename D>
68  struct window_< mln::window<D> >
69  {
70  typedef trait::window::size::fixed size;
71  typedef trait::window::support::regular support;
72  typedef trait::window::definition::unique definition;
73  };
74 
75  } // end of namespace mln::trait
76 
77 
78 
86  template <typename D>
87  class window : public internal::window_base< D, window<D> >
88  {
89  public:
90 
92  typedef window<D> regular;
93 
94 
99  window();
100 
105  bool is_centered() const;
106 
112  bool is_symmetric() const;
113 
115  void sym();
116 
117 
122 
127 
131  typedef fwd_qiter qiter;
132 
133 
135  unsigned size() const;
136 
139  bool is_empty() const;
140 
142  void clear();
143 
147  unsigned delta() const;
148 
150  const D& dp(unsigned i) const;
151 
153  bool has(const D& dp) const;
154 
156  window<D>& insert(const D& dp);
157 
159  template <typename W>
160  window<D>& insert(const Window<W>& win);
161 
164  window<D>& insert(const mln_coord(D)& dind); // For 1D or index access.
165 
166  window<D>& insert(const mln_coord(D)& drow,
167  const mln_coord(D)& dcol); // For 2D.
168 
169  window<D>& insert(const mln_coord(D)& dsli,
170  const mln_coord(D)& drow,
171  const mln_coord(D)& dcol); // For 3D.
173 
174 
176  const std::vector<D>& std_vector() const;
177 
180  const mln::util::set<D>& dps_hook_() const;
182 
184  void print(std::ostream& ostr) const;
185 
186  private:
187 
188  util::set<D> dps_;
189 
190  unsigned delta_(int i) const; // For indices.
191  unsigned delta_(const Gdpoint<D>& dp) const; // For grids delta-points.
192  };
193 
194 
195 
200  template <typename D>
201  bool operator==(const window<D>& lhs, const window<D>& rhs);
202 
203 
204 
205 # ifndef MLN_INCLUDE_ONLY
206 
207  // window<D>
208 
209  template <typename D>
210  inline
212  {
213  // FIXME HERE: Was: mln::metal::is_a<D, Dpoint>::check();
214  // mln::metal::is_a<D, Delta_Point_Site>::check();
215  }
216 
217  template <typename D>
218  inline
219  bool
221  {
222  window<D> cpy = *this;
223  cpy.sym();
224  return cpy == *this;
225  }
226 
227  template <typename D>
228  inline
229  bool
230  window<D>::is_centered() const
231  {
232  return this->dps_.has(literal::zero);
233  }
234 
235  template <typename D>
236  inline
237  void
239  {
240  window<D> tmp;
241  const unsigned n = size();
242  for (unsigned i = 0; i < n; ++i)
243  tmp.insert(- this->dp(i));
244  *this = tmp;
245  }
246 
247  template <typename D>
248  inline
249  bool
250  window<D>::is_empty() const
251  {
252  return dps_.is_empty();
253  }
254 
255  template <typename D>
256  inline
257  void
259  {
260  dps_.clear();
261  }
262 
263  template <typename D>
264  inline
265  unsigned
266  window<D>::delta() const
267  {
268  unsigned d = 0;
269  const unsigned n = size();
270  for (unsigned i = 0; i < n; ++i)
271  {
272  unsigned dd = delta_(dp(i));
273  if (dd > d)
274  d = dd;
275  }
276  return d;
277  }
278 
279  template <typename D>
280  inline
281  unsigned
282  window<D>::delta_(int i) const
283  {
284  return i;
285  }
286 
287  template <typename D>
288  inline
289  unsigned
290  window<D>::delta_(const Gdpoint<D>& dp) const
291  {
292  return norm::linfty(exact(dp).to_vec());
293  }
294 
295  template <typename D>
296  inline
297  unsigned
298  window<D>::size() const
299  {
300  return dps_.nelements();
301  }
302 
303  template <typename D>
304  inline
305  const D&
306  window<D>::dp(unsigned i) const
307  {
308  mln_precondition(i < size());
309  return dps_[i];
310  }
311 
312  template <typename D>
313  inline
314  bool
315  window<D>::has(const D& dp) const
316  {
317  return dps_.has(dp);
318  }
319 
320  template <typename D>
321  inline
322  const std::vector<D>&
323  window<D>::std_vector() const
324  {
325  return dps_.std_vector();
326  }
327 
328  template <typename D>
329  inline
330  window<D>&
331  window<D>::insert(const D& dp)
332  {
333  dps_.insert(dp);
334  return *this;
335  }
336 
337  template <typename D>
338  template <typename W>
339  inline
340  window<D>&
341  window<D>::insert(const Window<W>& win_)
342  {
343  const W& win = exact(win_);
344  const unsigned n = win.size();
345  for (unsigned i = 0; i < n; ++i)
346  dps_.insert(win.dp(i));
347  return *this;
348  }
349 
350  template <typename D>
351  inline
352  window<D>&
353  window<D>::insert(const mln_coord(D)& dind)
354  {
355  mlc_bool(D::dim == 1)::check();
356  D dp(dind);
357  return insert(dp);
358  }
359 
360  template <typename D>
361  inline
362  window<D>&
363  window<D>::insert(const mln_coord(D)& drow,
364  const mln_coord(D)& dcol)
365  {
366  mlc_bool(D::dim == 2)::check();
367  D dp(drow, dcol);
368  return insert(dp);
369  }
370 
371  template <typename D>
372  inline
373  window<D>&
374  window<D>::insert(const mln_coord(D)& dsli,
375  const mln_coord(D)& drow,
376  const mln_coord(D)& dcol)
377  {
378  mlc_bool(D::dim == 3)::check();
379  D dp(dsli, drow, dcol);
380  return insert(dp);
381  }
382 
383  template <typename D>
384  inline
385  const util::set<D>&
386  window<D>::dps_hook_() const
387  {
388  return dps_;
389  }
390 
391  template <typename D>
392  inline
393  void
394  window<D>::print(std::ostream& ostr) const
395  {
396  ostr << dps_;
397  }
398 
399 
400  // Operators.
401 
402  template <typename D>
403  bool
404  operator==(const window<D>& lhs, const window<D>& rhs)
405  {
406  return lhs.dps_hook_() == rhs.dps_hook_();
407  }
408 
409 # endif // ! MLN_INCLUDE_ONLY
410 
411 } // end of namespace mln
412 
413 
414 # include <mln/core/dpsites_piter.hh>
415 
416 
417 #endif // ! MLN_CORE_WINDOW_HH