DGtal  1.0.beta
ImageContainerBySTLVector.h
1 
17 #pragma once
18 
36 #if defined(ImageContainerBySTLVector_RECURSES)
37 #error Recursive header files inclusion detected in ImageContainerBySTLVector.h
38 #else // defined(ImageContainerBySTLVector_RECURSES)
39 
40 #define ImageContainerBySTLVector_RECURSES
41 
42 #if !defined ImageContainerBySTLVector_h
43 
44 #define ImageContainerBySTLVector_h
45 
47 // Inclusions
48 #include <iostream>
49 #include <vector>
50 #include "DGtal/base/Common.h"
51 #include "DGtal/base/SimpleRandomAccessConstRangeFromPoint.h"
52 #include "DGtal/base/SimpleRandomAccessRangeFromPoint.h"
53 #include "DGtal/base/CLabel.h"
54 #include "DGtal/kernel/domains/CDomain.h"
55 #include "DGtal/kernel/SpaceND.h"
56 #include "DGtal/kernel/domains/HyperRectDomain.h"
57 #include "DGtal/kernel/NumberTraits.h"
58 
60 
61 namespace DGtal
62 {
63 
64 
72  template<typename TImage>
74  {
75 
76  public:
77 
78  typedef typename TImage::Point Point;
79  typedef typename TImage::Difference Difference;
80 
81  public:
82 
83  DistanceFunctorFromPoint( const TImage *aImagePtr): myImagePtr(aImagePtr)
84  {};
85 
86  Difference operator() ( const Point &aPoint ) const
87  {
88  return Difference ( myImagePtr->linearized ( aPoint ) );
89  }
90  private:
91  const TImage *myImagePtr;
92 
93  };
94 
96  // class ImageContainerBySTLVector
97 
124  template <typename TDomain, typename TValue>
125 
126  class ImageContainerBySTLVector: public std::vector<TValue>
127  {
128 
129  public:
130 
132 
135  typedef TDomain Domain;
136  typedef typename Domain::Point Point;
137  typedef typename Domain::Vector Vector;
138  typedef typename Domain::Integer Integer;
139  typedef typename Domain::Size Size;
140  typedef typename Domain::Dimension Dimension;
141  typedef Point Vertex;
142 
143  BOOST_STATIC_CONSTANT( Dimension, dimension = Domain::Space::dimension );
144 
146  BOOST_STATIC_ASSERT ( ( boost::is_same< Domain,
148 
151  typedef TValue Value;
152 
154 
155  private:
156 
158  Domain myDomain;
159 
161  Vector myExtent;
162 
164 
165  public:
166 
173  ImageContainerBySTLVector ( const Domain &aDomain );
174 
182 
191 
197 
198 
200 
201 
211  Value operator() ( const Point & aPoint ) const;
212 
221  void setValue ( const Point &aPoint, const Value &aValue );
222 
226  const Domain &domain() const;
227 
228 
232  Vector extent() const;
233 
238  void translateDomain ( const Vector& aShift );
239 
244  void selfDisplay ( std::ostream & out ) const;
245 
246 
250  bool isValid() const;
251 
252  // ------------- realization CDrawableWithBoard2D --------------------
253 
257  std::string className() const;
258 
259 
261  // built-in iterators
262  typedef typename std::vector<Value>::iterator Iterator;
263  typedef typename std::vector<Value>::const_iterator ConstIterator;
264  typedef typename std::vector<Value>::reverse_iterator ReverseIterator;
265  typedef typename std::vector<Value>::const_reverse_iterator ConstReverseIterator;
266  typedef typename std::vector<Value>::difference_type Difference;
267 
268  typedef typename std::vector<Value>::iterator OutputIterator;
269  typedef typename std::vector<Value>::reverse_iterator ReverseOutputIterator;
270 
274 
279  ConstRange constRange() const;
280 
285  Range range();
286 
287 
289 
295  {
296 
297  friend class ImageContainerBySTLVector<Domain, Value>;
298 
299  public:
300 
302  typedef std::bidirectional_iterator_tag iterator_category;
303  typedef Value value_type;
304  typedef ptrdiff_t difference_type;
305  typedef Value* pointer;
306  typedef Value& reference;
307 
315  SpanIterator ( const Point & p ,
316  const Dimension aDim ,
317  ImageContainerBySTLVector<Domain, Value> *aMap ) : myMap ( aMap ), myDimension ( aDim )
318  {
319  myPos = aMap->linearized ( p );
320 
321  //We compute the myShift quantity
322  myShift = 1;
323 
324  for ( Dimension k = 0; k < myDimension ; k++ )
325  myShift *= aMap->myExtent[k];
326  }
327 
328 
334  inline
335  void setValue ( const Value aVal )
336  {
337  ( *myMap ) [ myPos ] = aVal;
338  }
339 
345  inline
346  const Value & operator*()
347  {
348  return ( *myMap ) [ myPos ];
349  }
350 
356  inline
357  bool operator== ( const SpanIterator &it ) const
358  {
359  return ( myPos == it.myPos );
360  }
361 
367  inline
368  bool operator!= ( const SpanIterator &it ) const
369  {
370  return ( myPos != it.myPos );
371  }
372 
377  inline
378  void next()
379  {
380  myPos += myShift;
381  }
382 
387  inline
388  void prev()
389  {
390  ASSERT ( ( long int ) myPos - myShift > 0 );
391  myPos -= myShift;
392  }
393 
398  inline
400  {
401  this->next();
402  return *this;
403  }
404 
409  inline
411  {
412  SpanIterator tmp = *this;
413  ++*this;
414  return tmp;
415  }
416 
421  inline
423  {
424  this->prev();
425  return *this;
426  }
427 
432  inline
434  {
435  SpanIterator tmp = *this;
436  --*this;
437  return tmp;
438  }
439 
440  private:
442  Size myPos;
443 
446 
448  Dimension myDimension;
449 
451  Size myShift;
452 
453  };
454 
461  void setValue ( SpanIterator &it, const Value &aValue )
462  {
463  it.setValue ( aValue );
464  }
465 
466 
476  SpanIterator spanBegin ( const Point &aPoint, const Dimension aDimension )
477  {
478  return SpanIterator ( aPoint, aDimension, this );
479  }
480 
491  SpanIterator spanEnd ( const Point &aPoint, const Dimension aDimension )
492  {
493  Point tmp = aPoint;
494  tmp[ aDimension ] = myDomain.upperBound() [ aDimension ] + 1;
495  return SpanIterator ( tmp, aDimension, this );
496  }
497 
504  Value getValue ( SpanIterator &it )
505  {
506  return ( *it );
507  };
508 
509 
510 
511 
517  Size linearized ( const Point &aPoint ) const;
518 
519 
520 
521  };
522 
529  template <typename Domain, typename V>
530  inline
531  std::ostream&
532  operator<< ( std::ostream & out, const ImageContainerBySTLVector<Domain, V> & object )
533  {
534  object.selfDisplay ( out );
535  return out;
536  }
537 
538 } // namespace DGtal
539 
540 
542 // Includes inline functions
543 #include "DGtal/images/ImageContainerBySTLVector.ih"
544 
545 // //
547 
548 #endif // !defined ImageContainerBySTLVector_h
549 
550 #undef ImageContainerBySTLVector_RECURSES
551 #endif // else defined(ImageContainerBySTLVector_RECURSES)
std::vector< Value >::iterator Iterator
Size linearized(const Point &aPoint) const
Aim: Define the concept of DGtal labels. Models of CLabel can be default-constructible, assignable and equality comparable.
Definition: CLabel.h:91
BOOST_STATIC_ASSERT((boost::is_same< Domain, HyperRectDomain< typename Domain::Space > >::value))
domain should be rectangular
std::string className() const
SpanIterator spanEnd(const Point &aPoint, const Dimension aDimension)
Aim: Parallelepidec region of a digital space, model of a 'CDomain'.
ImageContainerBySTLVector< TDomain, TValue > Self
std::vector< Value >::reverse_iterator ReverseOutputIterator
ImageContainerBySTLVector & operator=(const ImageContainerBySTLVector &other)
void setValue(SpanIterator &it, const Value &aValue)
ConstRange constRange() const
SpanIterator(const Point &p, const Dimension aDim, ImageContainerBySTLVector< Domain, Value > *aMap)
void selfDisplay(std::ostream &out) const
Aim: model of CBidirectionalRangeFromPoint that adapts any range of elements bounded by two iterators...
Aim: model of CConstBidirectionalRangeFromPoint that adapts any range of elements bounded by two iter...
std::vector< Value >::const_reverse_iterator ConstReverseIterator
Aim: This concept represents a digital domain, i.e. a non mutable subset of points of the given digit...
Definition: CDomain.h:129
SimpleRandomAccessRangeFromPoint< ConstIterator, Iterator, DistanceFunctorFromPoint< Self > > Range
std::vector< Value >::difference_type Difference
Dimension myDimension
Dimension on which the iterator must iterate.
ImageContainerBySTLVector< Domain, Value > * myMap
Copy of the underlying images.
std::vector< Value >::reverse_iterator ReverseIterator
DistanceFunctorFromPoint(const TImage *aImagePtr)
Vector myExtent
Domain extent (stored for linearization efficiency)
Value operator()(const Point &aPoint) const
BOOST_STATIC_CONSTANT(Dimension, dimension=Domain::Space::dimension)
DGtal is the top-level namespace which contains all DGtal functions and types.
SpanIterator spanBegin(const Point &aPoint, const Dimension aDimension)
const Domain & domain() const
ImageContainerBySTLVector(const Domain &aDomain)
std::vector< Value >::const_iterator ConstIterator
std::vector< Value >::iterator OutputIterator
BOOST_CONCEPT_ASSERT((concepts::CDomain< TDomain >))
domain
void translateDomain(const Vector &aShift)
void setValue(const Point &aPoint, const Value &aValue)
SimpleRandomAccessConstRangeFromPoint< ConstIterator, DistanceFunctorFromPoint< Self > > ConstRange
Difference operator()(const Point &aPoint) const