aimstil  5.0.5
ConstImageLinearIterator.h
Go to the documentation of this file.
1 #ifndef TIL_CONSTIMAGECLINEARITERATOR_H
2 #define TIL_CONSTIMAGECLINEARITERATOR_H
3 
4 // includes from TIL library
5 #include "til/labels.h"
6 
7 // Namespace
8 namespace til
9 {
10 
11 
12 // Predeclaration
13 template < typename T > class ImageC;
14 
15 
16 // A class to iterate through all values of an image without having to
17 // know the current position (hence the fastest possible)
18 // Allows to read but not to write to image
19 
20 template < typename T >
21 class ConstLinearIterator<ImageC<T> > : public ImageIterator_label
22 {
23 
24 public: // typedefs
25 
26  typedef T value_type;
27  typedef ImageC<T> TImage;
28  typedef const T & reference;
29 
30 public: // constructors & destructor
31 
32  ConstLinearIterator<ImageC<T> >() { m_index = 0; };
33  ConstLinearIterator<ImageC<T> >(const ImageC<T> &im) { this->init(im); }
35 
36 public: // initialization
37 
38  // Initialize the iterator
39  void init(const ImageC<T> &im);
40 
41 public: // set & get
42 
43  void setEnd(const T * end)
44  {
45  if (end < m_index)
46  {
47  throw std::invalid_argument("end is after current position");
48  }
49  m_end = const_cast<T*>(end);
50  }
51 
52 public: // functions
53 
54  // NB: it appeared that _not_ being compliant with the standard, by returning
55  // nothing (as opposed to returning a reference on the object) is 50%
56  // faster (tested on release mode w/ profiling).
57  // It was assumed here that this performance was preferable to enabling
58  // such statement as iter1 = ++iter2;
59 
61  {
62  ++m_index;
63  }
64 
65  INLINE bool next() { if (m_index == m_end) return 0; ++m_index; return 1;}
66 
67  bool isAtEnd() const { return (m_index > m_end); }
68  reference operator*() const { return *m_index; }
69 
70 public:
71 //protected: // set & get
72 
73  // get pointer on the current voxel
74  T* getIndex() const { return m_index; }
75 
76 
77 private: // data
78 
79  // Pointer to current element
80  T* m_index;
81  // Pointer to last element
82  // NB: this is not the (last+1) element (like ::std), because we want this
83  // to work even for volumetric iterators. But, volumetric iterators can run
84  // along y and z axis. So end cannot be simply the 'next after the end' because
85  // the notion of 'next' depends on which direction we move on. So end
86  // has to be the real end element.
87  T* m_end;
88 };
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
124 //
125 // CODE
126 //
128 
129 
130 template < typename T >
132 {
133  if (!isAllocated(im))
134  {
135  m_index = 0;
136  m_end = 0;
137  return;
138  }
139 
140  m_index = const_cast<T*>(im.getPointer());
141  m_end = m_index + im.size() - 1;
142 }
143 
144 } // namespace
145 
146 #endif
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
Defines empty classes that serves as labels.
#define INLINE
Definition: til_common.h:26
INLINE bool isAllocated(const TImage &im)
Check whether smart pointer and image are allocated.
Definition: imageTools.h:36
T * getPointer()
Definition: ImageC.h:126
Image class using contiguous memory.
int size() const
Get total number of elements in image.
Definition: ImageBase.h:38