DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
144  static const typename Domain::Dimension dimension = Domain::dimension;
145 
147  BOOST_STATIC_ASSERT ( ( boost::is_same< Domain,
149 
152  typedef TValue Value;
153  // obsolete
154  // typedef SimpleConstRange<typename vector<Value>::const_iterator > ConstRange;
155 
157 
158 private:
159 
162 
165 
167 
168 public:
169 
176  ImageContainerBySTLVector ( const Domain &aDomain );
177 
185 
194 
200 
201 
203 
204 
214  Value operator() ( const Point & aPoint ) const;
215 
224  void setValue ( const Point &aPoint, const Value &aValue );
225 
229  const Domain &domain() const;
230 
231 
235  Vector extent() const;
236 
241  void translateDomain ( const Vector& aShift );
242 
247  void selfDisplay ( std::ostream & out ) const;
248 
249 
253  bool isValid() const;
254 
255  // ------------- realization CDrawableWithBoard2D --------------------
256 
260  std::string className() const;
261 
262 
264  // built-in iterators
265  typedef typename vector<Value>::iterator Iterator;
266  typedef typename vector<Value>::const_iterator ConstIterator;
267  typedef typename vector<Value>::reverse_iterator ReverseIterator;
268  typedef typename vector<Value>::const_reverse_iterator ConstReverseIterator;
269  typedef typename vector<Value>::difference_type Difference;
270 
271  typedef typename vector<Value>::iterator OutputIterator;
272  typedef typename vector<Value>::reverse_iterator ReverseOutputIterator;
273 
277 
282  ConstRange constRange() const;
283 
288  Range range();
289 
290 
292 
300  {
301 
303 
304  public:
305 
307  typedef std::bidirectional_iterator_tag iterator_category;
308  typedef Value value_type;
309  typedef ptrdiff_t difference_type;
310  typedef Value* pointer;
311  typedef Value& reference;
312 
320  SpanIterator ( const Point & p ,
321  const Dimension aDim ,
322  ImageContainerBySTLVector<Domain, Value> *aMap ) : myMap ( aMap ), myDimension ( aDim )
323  {
324  myPos = aMap->linearized ( p );
325 
326  //We compute the myShift quantity
327  myShift = 1;
328 
329  for ( Dimension k = 0; k < myDimension ; k++ )
330  myShift *= aMap->myExtent[k];
331  }
332 
333 
339  inline
340  void setValue ( const Value aVal )
341  {
342  ( *myMap ) [ myPos ] = aVal;
343  }
344 
350  inline
351  const Value & operator*()
352  {
353  return ( *myMap ) [ myPos ];
354  }
355 
361  inline
362  bool operator== ( const SpanIterator &it ) const
363  {
364  return ( myPos == it.myPos );
365  }
366 
372  inline
373  bool operator!= ( const SpanIterator &it ) const
374  {
375  return ( myPos != it.myPos );
376  }
377 
382  inline
383  void next()
384  {
385  myPos += myShift;
386  }
387 
392  inline
393  void prev()
394  {
395  ASSERT ( ( long int ) myPos - myShift > 0 );
396  myPos -= myShift;
397  }
398 
403  inline
405  {
406  this->next();
407  return *this;
408  }
409 
414  inline
416  {
417  SpanIterator tmp = *this;
418  ++*this;
419  return tmp;
420  }
421 
426  inline
428  {
429  this->prev();
430  return *this;
431  }
432 
437  inline
439  {
440  SpanIterator tmp = *this;
441  --*this;
442  return tmp;
443  }
444 
445  private:
448 
451 
454 
457 
458  };
459 
466  void setValue ( SpanIterator &it, const Value &aValue )
467  {
468  it.setValue ( aValue );
469  }
470 
471 
481  SpanIterator spanBegin ( const Point &aPoint, const Dimension aDimension )
482  {
483  return SpanIterator ( aPoint, aDimension, this );
484  }
485 
496  SpanIterator spanEnd ( const Point &aPoint, const Dimension aDimension )
497  {
498  Point tmp = aPoint;
499  tmp[ aDimension ] = myDomain.upperBound() [ aDimension ] + 1;
500  return SpanIterator ( tmp, aDimension, this );
501  }
502 
509  Value getValue ( SpanIterator &it )
510  {
511  return ( *it );
512  };
513 
514 
515 
516 
522  Size linearized ( const Point &aPoint ) const;
523 
524 
525 
526 };
527 
534 template <typename Domain, typename V>
535 inline
536 std::ostream&
537 operator<< ( std::ostream & out, const ImageContainerBySTLVector<Domain, V> & object )
538 {
539  object.selfDisplay ( out );
540  return out;
541 }
542 
543 } // namespace DGtal
544 
545 
547 // Includes inline functions
548 #include "DGtal/images/ImageContainerBySTLVector.ih"
549 
550 // //
552 
553 #endif // !defined ImageContainerBySTLVector_h
554 
555 #undef ImageContainerBySTLVector_RECURSES
556 #endif // else defined(ImageContainerBySTLVector_RECURSES)