Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
window.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development
2 // 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 
84  template <typename D>
85  class window : public internal::window_base< D, window<D> >
86  {
87  public:
88 
90  typedef window<D> regular;
91 
92 
97  window();
98 
103  bool is_centered() const;
104 
110  bool is_symmetric() const;
111 
113  void sym();
114 
115 
120 
125 
129  typedef fwd_qiter qiter;
130 
131 
133  unsigned size() const;
134 
137  bool is_empty() const;
138 
140  void clear();
141 
145  unsigned delta() const;
146 
148  const D& dp(unsigned i) const;
149 
151  bool has(const D& dp) const;
152 
154  window<D>& insert(const D& dp);
155 
157  template <typename W>
158  window<D>& insert(const Window<W>& win);
159 
162  window<D>& insert(const mln_coord(D)& dind); // For 1D or index access.
163 
164  window<D>& insert(const mln_coord(D)& drow,
165  const mln_coord(D)& dcol); // For 2D.
166 
167  window<D>& insert(const mln_coord(D)& dsli,
168  const mln_coord(D)& drow,
169  const mln_coord(D)& dcol); // For 3D.
171 
172 
174  const std::vector<D>& std_vector() const;
175 
177  const mln::util::set<D>& dps_hook_() const;
178 
180  void print(std::ostream& ostr) const;
181 
182  private:
183 
184  util::set<D> dps_;
185 
186  unsigned delta_(int i) const; // For indices.
187  unsigned delta_(const Gdpoint<D>& dp) const; // For grids delta-points.
188  };
189 
190 
191 
196  template <typename D>
197  bool operator==(const window<D>& lhs, const window<D>& rhs);
198 
199 
200 
201 # ifndef MLN_INCLUDE_ONLY
202 
203  // window<D>
204 
205  template <typename D>
206  inline
208  {
209  // FIXME HERE: Was: mln::metal::is_a<D, Dpoint>::check();
210  // mln::metal::is_a<D, Delta_Point_Site>::check();
211  }
212 
213  template <typename D>
214  inline
215  bool
217  {
218  window<D> cpy = *this;
219  cpy.sym();
220  return cpy == *this;
221  }
222 
223  template <typename D>
224  inline
225  bool
227  {
228  return this->dps_.has(literal::zero);
229  }
230 
231  template <typename D>
232  inline
233  void
235  {
236  window<D> tmp;
237  const unsigned n = size();
238  for (unsigned i = 0; i < n; ++i)
239  tmp.insert(- this->dp(i));
240  *this = tmp;
241  }
242 
243  template <typename D>
244  inline
245  bool
247  {
248  return dps_.is_empty();
249  }
250 
251  template <typename D>
252  inline
253  void
255  {
256  dps_.clear();
257  }
258 
259  template <typename D>
260  inline
261  unsigned
263  {
264  unsigned d = 0;
265  const unsigned n = size();
266  for (unsigned i = 0; i < n; ++i)
267  {
268  unsigned dd = delta_(dp(i));
269  if (dd > d)
270  d = dd;
271  }
272  return d;
273  }
274 
275  template <typename D>
276  inline
277  unsigned
278  window<D>::delta_(int i) const
279  {
280  return i;
281  }
282 
283  template <typename D>
284  inline
285  unsigned
286  window<D>::delta_(const Gdpoint<D>& dp) const
287  {
288  return norm::linfty(exact(dp).to_vec());
289  }
290 
291  template <typename D>
292  inline
293  unsigned
295  {
296  return dps_.nelements();
297  }
298 
299  template <typename D>
300  inline
301  const D&
302  window<D>::dp(unsigned i) const
303  {
304  mln_precondition(i < size());
305  return dps_[i];
306  }
307 
308  template <typename D>
309  inline
310  bool
311  window<D>::has(const D& dp) const
312  {
313  return dps_.has(dp);
314  }
315 
316  template <typename D>
317  inline
318  const std::vector<D>&
320  {
321  return dps_.std_vector();
322  }
323 
324  template <typename D>
325  inline
326  window<D>&
327  window<D>::insert(const D& dp)
328  {
329  dps_.insert(dp);
330  return *this;
331  }
332 
333  template <typename D>
334  template <typename W>
335  inline
336  window<D>&
338  {
339  const W& win = exact(win_);
340  const unsigned n = win.size();
341  for (unsigned i = 0; i < n; ++i)
342  dps_.insert(win.dp(i));
343  return *this;
344  }
345 
346  template <typename D>
347  inline
348  window<D>&
349  window<D>::insert(const mln_coord(D)& dind)
350  {
351  mlc_bool(D::dim == 1)::check();
352  D dp(dind);
353  return insert(dp);
354  }
355 
356  template <typename D>
357  inline
358  window<D>&
359  window<D>::insert(const mln_coord(D)& drow,
360  const mln_coord(D)& dcol)
361  {
362  mlc_bool(D::dim == 2)::check();
363  D dp(drow, dcol);
364  return insert(dp);
365  }
366 
367  template <typename D>
368  inline
369  window<D>&
370  window<D>::insert(const mln_coord(D)& dsli,
371  const mln_coord(D)& drow,
372  const mln_coord(D)& dcol)
373  {
374  mlc_bool(D::dim == 3)::check();
375  D dp(dsli, drow, dcol);
376  return insert(dp);
377  }
378 
379  template <typename D>
380  inline
381  const util::set<D>&
382  window<D>::dps_hook_() const
383  {
384  return dps_;
385  }
386 
387  template <typename D>
388  inline
389  void
390  window<D>::print(std::ostream& ostr) const
391  {
392  ostr << dps_;
393  }
394 
395 
396  // Operators.
397 
398  template <typename D>
399  bool
400  operator==(const window<D>& lhs, const window<D>& rhs)
401  {
402  return lhs.dps_hook_() == rhs.dps_hook_();
403  }
404 
405 # endif // ! MLN_INCLUDE_ONLY
406 
407 } // end of namespace mln
408 
409 
410 # include <mln/core/dpsites_piter.hh>
411 
412 
413 #endif // ! MLN_CORE_WINDOW_HH