$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dpoints_pixter.hh
1 // Copyright (C) 2007, 2008, 2009, 2011, 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_DPOINTS_PIXTER_HH
28 # define MLN_CORE_DPOINTS_PIXTER_HH
29 
36 
37 # include <cassert>
38 # include <vector>
39 
40 # include <mln/core/concept/proxy.hh>
41 # include <mln/core/concept/pixel_iterator.hh>
42 # include <mln/core/internal/pixel_impl.hh>
43 # include <mln/metal/converts_to.hh>
44 
45 
46 namespace mln
47 {
48 
49  /*------------------------.
50  | dpoints_fwd_pixter<I>. |
51  `------------------------*/
52 
60  template <typename I>
62  : public Pixel_Iterator< dpoints_fwd_pixter<I> >,
63  public internal::pixel_impl_< I, dpoints_fwd_pixter<I> >
64  {
66 
67  public:
75  template <typename Dps, typename Pref>
77  const Dps& dps,
78  const Pref& p_ref);
79 
85  template <typename Dps, typename Pref>
87  const Dps& dps);
88 
92  void start();
93 
96  void next_();
98 
100  void invalidate();
102  bool is_valid() const;
103 
106  void update();
108 
110  const mln_value(I)& center_val() const;
111 
113  private:
114  template <typename Dps>
115  void init_(const Dps& dps);
116 
117  private:
123  std::vector<int> offset_;
125  typename std::vector<int>::const_iterator i_;
126 
132  mln_qlf_value(I)** value_ref_;
134  const mln_psite(I)* p_ref_;
136 
138 
139  };
140 
141 
142  /*------------------------.
143  | dpoints_bkd_pixter<I>. |
144  `------------------------*/
145 
153  template <typename I>
155  : public Pixel_Iterator< dpoints_bkd_pixter<I> >,
156  public internal::pixel_impl_< I, dpoints_bkd_pixter<I> >
157  {
159 
160  public:
168  template <typename Dps, typename Pref>
170  const Dps& dps,
171  const Pref& p_ref);
172 
178  template <typename Dps, typename Pref>
180  const Dps& dps);
181 
185  void start();
186 
189  void next_();
191 
193  void invalidate();
195  bool is_valid() const;
196 
199  void update();
201 
203  const mln_value(I)& center_val() const;
204 
206 
207  private:
208  template <typename Dps>
209  void init_(const Dps& dps);
210 
211  private:
217  std::vector<int> offset_;
219  typename std::vector<int>::const_reverse_iterator i_;
220 
226  mln_qlf_value(I)** value_ref_;
228  const mln_psite(I)* p_ref_;
230 
232  };
233 
234 
235 
236 #ifndef MLN_INCLUDE_ONLY
237 
238  /*------------------------.
239  | dpoints_fwd_pixter<I>. |
240  `------------------------*/
241 
242  template <typename I>
243  template <typename Dps, typename Pref>
244  inline
246  const Dps& dps,
247  const Pref& p_ref)
248  : super_(image)
249  {
250  mln_precondition(image.is_valid());
251 
252  mlc_converts_to(Pref, const mln_psite(I)&)::check();
253  p_ref_ = & static_cast< const mln_psite(I)& >(p_ref);
254 
255  value_ref_ = 0;
256  init_(dps);
257  }
258 
259  template <typename I>
260  template <typename Dps, typename Pref>
261  inline
262  dpoints_fwd_pixter<I>::dpoints_fwd_pixter(const Generalized_Pixel<Pref>& pxl_ref_,
263  const Dps& dps)
264  : super_(internal::force_exact<Pref>(pxl_ref_).ima())
265  {
266  const Pref& pxl_ref = internal::force_exact<Pref>(pxl_ref_);
267  mln_precondition(pxl_ref.ima().is_valid());
268  p_ref_ = 0;
269  // Potential promotion from (T**) to (const T**) shall be forced.
270  value_ref_ = const_cast<mln_qlf_value(I)**>(pxl_ref.address_());
271  init_(dps);
272  }
273 
274  template <typename I>
275  inline
276  const mln_value(I)&
278  {
279  mln_invariant(value_ref_ != 0 || p_ref_ != 0);
280  if (p_ref_)
281  return this->image_(*p_ref_);
282  else
283  return **value_ref_;
284  }
285 
286  template <typename I>
287  template <typename Dps>
288  inline
289  void
290  dpoints_fwd_pixter<I>::init_(const Dps& dps)
291  {
292  for (unsigned i = 0; i < dps.size(); ++i)
293  offset_.push_back(this->image_.delta_offset(dps.dp(i)));
294  // offset_[0] is absolute
295  // other offsets are relative:
296  if (dps.size() > 1)
297  for (unsigned i = dps.size() - 1; i > 0; --i)
298  offset_[i] -= offset_[i - 1];
299  invalidate();
300  }
301 
302  template <typename I>
303  inline
304  void
306  {
307  if (is_valid())
308  {
309  if (p_ref_)
310  this->value_ptr_ = & this->image_(*p_ref_) + *i_;
311  else
312  this->value_ptr_ = * value_ref_ + *i_;
313  }
314  }
315 
316  template <typename I>
317  inline
318  void
320  {
321  i_ = offset_.begin();
322  update();
323  }
324 
325  template <typename I>
326  inline
327  void
329  {
330  ++i_;
331  if (is_valid())
332  this->value_ptr_ += *i_;
333  }
334 
335  template <typename I>
336  inline
337  bool
339  {
340  return i_ != offset_.end();
341  }
342 
343  template <typename I>
344  inline
345  void
347  {
348  i_ = offset_.end();
349  }
350 
351 
352  /*------------------------.
353  | dpoints_bkd_pixter<I>. |
354  `------------------------*/
355 
356  template <typename I>
357  template <typename Dps, typename Pref>
358  inline
360  const Dps& dps,
361  const Pref& p_ref)
362  : super_(image)
363  {
364  mln_precondition(image.is_valid());
365  internal::get_adr(p_ref_, p_ref);
366  value_ref_ = 0;
367  init_(dps);
368  }
369 
370  template <typename I>
371  template <typename Dps, typename Pref>
372  inline
373  dpoints_bkd_pixter<I>::dpoints_bkd_pixter(const Generalized_Pixel<Pref>& pxl_ref_,
374  const Dps& dps)
375  : super_(internal::force_exact<Pref>(pxl_ref_).ima())
376  {
377  const Pref& pxl_ref = internal::force_exact<Pref>(pxl_ref_);
378  mln_precondition(pxl_ref.ima().is_valid());
379  p_ref_ = 0;
380  // Potential promotion from (T**) to (const T**) shall be forced.
381  value_ref_ = const_cast<mln_qlf_value(I)**>(pxl_ref.address_());
382  init_(dps);
383  }
384 
385  template <typename I>
386  inline
387  const mln_value(I)&
389  {
390  mln_invariant(value_ref_ != 0 || p_ref_ != 0);
391  if (p_ref_)
392  return this->image_(*p_ref_);
393  else
394  return **value_ref_;
395  }
396 
397  template <typename I>
398  template <typename Dps>
399  inline
400  void
401  dpoints_bkd_pixter<I>::init_(const Dps& dps)
402  {
403  for (unsigned i = 0; i < dps.size(); ++i)
404  offset_.push_back(this->image_.delta_offset(dps.dp(i)));
405  // offset_[size() - 1] is absolute
406  // other offsets are relative:
407  if (dps.size() > 1)
408  for (unsigned i = 0; i < dps.size() - 1; ++i)
409  offset_[i] -= offset_[i + 1];
410  invalidate();
411  }
412 
413  template <typename I>
414  inline
415  void
417  {
418  if (is_valid())
419  {
420  if (p_ref_)
421  this->value_ptr_ = & this->image_(*p_ref_) + *i_;
422  else
423  this->value_ptr_ = * value_ref_ + *i_;
424  }
425  }
426 
427  template <typename I>
428  inline
429  void
431  {
432  i_ = offset_.rbegin();
433  update();
434  }
435 
436  template <typename I>
437  inline
438  void
440  {
441  ++i_;
442  if (is_valid())
443  this->value_ptr_ += *i_;
444  }
445 
446  template <typename I>
447  inline
448  bool
450  {
451  return i_ != offset_.rend();
452  }
453 
454  template <typename I>
455  inline
456  void
458  {
459  i_ = offset_.rend();
460  }
461 
462 #endif // ! MLN_INCLUDE_ONLY
463 
464 } // end of namespace mln
465 
466 
467 #endif // ! MLN_CORE_DPOINTS_PIXTER_HH