DGtal  0.9.2
exampleIteratorCompletion.cpp
1 
30 #include <new>
32 #include <cstddef>
33 #include <algorithm>
34 #include <iostream>
35 
36 #include <DGtal/base/Common.h>
37 #include <DGtal/base/IteratorCompletion.h>
38 #include <DGtal/kernel/domains/HyperRectDomain.h>
39 #include <DGtal/kernel/domains/Linearizer.h>
40 #include <DGtal/kernel/SpaceND.h>
41 #include <DGtal/images/CImage.h>
43 
45 
46 // A minimal image from a C-style array with reverse-iterators and ranges.
47 template < typename T, DGtal::Dimension N >
48 class MyImage
49  : public DGtal::IteratorCompletion< MyImage<T, N> > // CRTP
50 {
51 public:
52 
53  // Typedefs
54  typedef MyImage<T, N> Self;
55  typedef typename DGtal::IteratorCompletionTraits<Self>::Iterator Iterator; // to be coherent with the traits that use IteratorCompletion
57 
58  typedef T Value; // The image values type
59  typedef DGtal::SpaceND<N> Space; // The image space type
60  typedef DGtal::HyperRectDomain<Space> Domain; // The image domain type
61  typedef typename Space::Point Point; // The associated point type
63 
64  // Contructor
65  MyImage( Domain const& aDomain = Domain() )
66  : myDomain(aDomain), myData( new T[ myDomain.size() ] )
67  {}
68 
69  // Destructor
70  ~MyImage()
71  {
72  delete[] myData;
73  }
74 
76 
78 
79  // Unary Functor
80  Value operator() ( Point const& aPoint ) const
81  {
82  return myData[ Linearizer::getIndex(aPoint, myDomain) ];
83  }
84 
85  // Getter
86  Value getValue( Point const& aPoint ) const
87  {
88  return operator()(aPoint);
89  }
90 
91  // Setter
92  void setValue( Point const& aPoint, Value const& aValue )
93  {
94  myData[ Linearizer::getIndex(aPoint, myDomain) ] = aValue;
95  }
96 
97  // Returns the associated domain
98  Domain const& domain() const
99  {
100  return myDomain;
101  }
102 
103  // Assignability (needed for CImage concept)
104  Self& operator= ( Self const& other )
105  {
106  if ( this != &other )
107  {
108  myDomain = other.domain();
109  delete[] myData;
110  myData = new Value[ myDomain.size() ];
111  std::copy( other.begin(), other.end(), begin() );
112  }
113 
114  return *this;
115  }
116 
118 
120 
121 // Begin mutable iterator
122  Iterator begin()
123  {
124  return myData;
125  }
126 
127  // Begin constant iterator
128  ConstIterator begin() const
129  {
130  return myData;
131  }
132 
133  // End mutable iterator
134  Iterator end()
135  {
136  return myData + myDomain.size();
137  }
138 
139  // End constant iterator
140  ConstIterator end() const
141  {
142  return myData + myDomain.size();
143  }
144 
146 
148 private:
149  Domain myDomain;
150  T* myData;
151 
152 };
154 
156 namespace DGtal
157 {
158  // Specialization of IteratorCompletionTraits for MyVector
159  template < typename T, Dimension N >
160  class IteratorCompletionTraits< MyImage<T, N> >
161  {
162  public:
163  typedef MyImage<T, N> Self;
164  typedef T* Iterator;
165  typedef T const* ConstIterator;
166 
167  class DistanceFunctor
168  {
169  public:
170  typedef typename Self::Domain Domain;
171  typedef typename Self::Point Point;
172  typedef typename Self::Difference Difference;
173 
174  DistanceFunctor( Self const* aVector )
175  : myDomain(aVector->domain())
176  {}
177 
178  Difference operator() ( Point const& aPoint ) const
179  {
180  return Self::Linearizer::getIndex( aPoint, myDomain );
181  }
182 
183  private:
184  Domain myDomain;
185  };
186 
187  };
188 }
190 
191 int main()
192 {
193 
195  using namespace std;
196 
197  // Typedefs
198  typedef MyImage<double, 2> My2DImage;
199  typedef My2DImage::Value Value;
200  typedef My2DImage::Domain Domain;
201  typedef My2DImage::Point Point;
202 
203  // Checks CImage concept
204  BOOST_CONCEPT_ASSERT( (DGtal::concepts::CImage<My2DImage>) );
205 
206  // Construction
207  My2DImage image( Domain( Point(0,0), Point(2,3) ) );
208 
209  // Filling
210  Value i = 0;
211  for ( My2DImage::Iterator it = image.begin(), it_end = image.end() ; it != it_end; ++it )
212  {
213  *it = ++i;
214  }
215 
216  // Modifying a value
217  image.setValue( Point(1,1), -1 );
218 
219  // Forward reading using range
220  My2DImage::ConstRange range = image.constRange();
221  copy( range.begin(), range.end(), ostream_iterator<Value>( cout, " " ) );
222  cout << endl;
223 
224  // Backward reading using reverse iterators
225  copy( image.rbegin(), image.rend(), ostream_iterator<Value>( cout, " " ) );
226  cout << endl;
227 
229 
230 }
Aim: Class that uses CRTP to add reverse iterators and ranges to a derived class. ...
IteratorCompletionTraits< TDerived >::ConstIterator ConstIterator
Constant iterator type.
Aim: SpaceND is a utility class that defines the fundamental structure of a Digital Space in ND...
Definition: SpaceND.h:95
Aim: Traits that must be specialized for each IteratorCompletion derived class.
STL namespace.
Aim: Linearization and de-linearization interface for domains.
Definition: Linearizer.h:78
IteratorCompletionTraits< TDerived >::Iterator Iterator
Mutable iterator type.
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102