DGtal  0.9.3beta
TiledImage.h
1 
17 #pragma once
18 
31 #if defined(TiledImage_RECURSES)
32 #error Recursive header files inclusion detected in TiledImage.h
33 #else // defined(TiledImage_RECURSES)
34 
35 #define TiledImage_RECURSES
36 
37 #if !defined TiledImage_h
38 
39 #define TiledImage_h
40 
42 // Inclusions
43 #include <iostream>
44 #include "DGtal/base/Common.h"
45 #include "DGtal/base/ConceptUtils.h"
46 #include "DGtal/images/CImage.h"
47 #include "DGtal/images/CImageFactory.h"
48 #include "DGtal/images/CImageCacheReadPolicy.h"
49 #include "DGtal/images/CImageCacheWritePolicy.h"
50 #include "DGtal/base/Alias.h"
51 
52 #include "DGtal/images/ImageCache.h"
53 
54 #include "DGtal/base/TiledImageBidirectionalConstRangeFromPoint.h"
55 #include "DGtal/base/TiledImageBidirectionalRangeFromPoint.h"
57 
58 namespace DGtal
59 {
61  // Template class TiledImage
75  template <typename TImageContainer, typename TImageFactory, typename TImageCacheReadPolicy, typename TImageCacheWritePolicy>
76  class TiledImage
77  {
78 
79  // ----------------------- Types ------------------------------
80 
81  public:
83 
89 
91  typedef TImageContainer ImageContainer;
92  typedef typename ImageContainer::Domain Domain;
93  typedef typename ImageContainer::Point Point;
94  typedef typename ImageContainer::Value Value;
95 
96  typedef typename ImageContainer::Difference Difference;
97 
99  typedef TImageFactory ImageFactory;
100  typedef typename ImageFactory::OutputImage OutputImage;
101 
102  typedef TImageCacheReadPolicy ImageCacheReadPolicy;
103  typedef TImageCacheWritePolicy ImageCacheWritePolicy;
105 
106  // ----------------------- Standard services ------------------------------
107 
108  public:
109 
118  Alias<ImageCacheReadPolicy> aReadPolicy,
119  Alias<ImageCacheWritePolicy> aWritePolicy,
120  typename Domain::Integer N):
121  myN(N), myImageFactory(&anImageFactory), myReadPolicy(&aReadPolicy), myWritePolicy(&aWritePolicy)
122  {
124 
125  m_lowerBound = myImageFactory->domain().lowerBound();
126  m_upperBound = myImageFactory->domain().upperBound();
127 
128  for(typename DGtal::Dimension i=0; i<Domain::dimension; i++)
129  mySize[i] = (m_upperBound[i]-m_lowerBound[i]+1)/myN;
130  }
131 
136  {
137  delete myImageCache;
138  }
139 
140  private:
141 
142  public:
143 
148  TiledImage( const TiledImage &other )
149  {
150  myN = other.myN;
152  myReadPolicy = other.myReadPolicy;
154 
156 
157  m_lowerBound = myImageFactory->domain().lowerBound();
158  m_upperBound = myImageFactory->domain().upperBound();
159 
160  for(typename DGtal::Dimension i=0; i<Domain::dimension; i++)
161  mySize[i] = (m_upperBound[i]-m_lowerBound[i]+1)/myN;
162  }
163 
169  TiledImage & operator=( const TiledImage & other )
170  {
171  if ( this != &other )
172  {
173  myN = other.myN;
175  myReadPolicy = other.myReadPolicy;
177 
179 
180  m_lowerBound = myImageFactory->domain().lowerBound();
181  m_upperBound = myImageFactory->domain().upperBound();
182 
183  for(typename DGtal::Dimension i=0; i<Domain::dimension; i++)
184  mySize[i] = (m_upperBound[i]-m_lowerBound[i]+1)/myN;
185  }
186 
187  return *this;
188  }
189 
190  // ----------------------- Interface --------------------------------------
191  public:
192 
194 
200  const Domain & domain() const
201  {
202  return myImageFactory->domain();
203  }
204 
210  const Domain domainBlockCoords() const
211  {
212  Point lowerBoundCords, upperBoundCoords;
213 
214  for(typename DGtal::Dimension i=0; i<Domain::dimension; i++)
215  {
216  lowerBoundCords[i] = 0;
217  upperBoundCoords[i] = myN;
218 
219  if (((m_upperBound[i]-m_lowerBound[i]+1) % myN) == 0)
220  upperBoundCoords[i]--;
221  }
222 
223  return Domain(lowerBoundCords, upperBoundCoords);
224  }
225 
227 
232  class TiledIterator : public
233  std::iterator<std::bidirectional_iterator_tag, Value, ptrdiff_t, Value*, Value&>
234  {
235 
236  friend class TiledImage<ImageContainer, ImageFactory, ImageCacheReadPolicy, ImageCacheWritePolicy>;
237 
238  public:
239 
240  /*typedef std::bidirectional_iterator_tag iterator_category; // ???
241  typedef Value value_type;
242  typedef ptrdiff_t difference_type; // ???
243  typedef Value* pointer;
244  typedef Value& reference;*/
245 
246  typedef typename ImageContainer::Range::/*Output*/Iterator TiledRangeIterator;
248 
249  //TiledIterator();
250 
257  TiledIterator ( BlockCoordsIterator aBlockCoordsIterator,
258  const TiledImage<ImageContainer, ImageFactory,
259  ImageCacheReadPolicy, ImageCacheWritePolicy> *aTiledImage ) : myTiledImage ( aTiledImage ),
260  myBlockCoordsIterator ( aBlockCoordsIterator )
261  {
263  {
265  myTiledRangeIterator = myTile->range().begin();
266  }
267  }
268 
276  TiledIterator ( BlockCoordsIterator aBlockCoordsIterator,
277  const Point& aPoint,
278  const TiledImage<ImageContainer, ImageFactory,
279  ImageCacheReadPolicy, ImageCacheWritePolicy> *aTiledImage ) : myTiledImage ( aTiledImage ),
280  myBlockCoordsIterator ( aBlockCoordsIterator )
281  {
283  {
285  myTiledRangeIterator = myTile->range().begin(aPoint);
286  }
287  }
288 
294  inline
295  Value & operator*()
296  {
297  return (*myTiledRangeIterator);
298  }
299 
305  inline
306  bool operator== ( const TiledIterator &it ) const
307  {
308  return ( ( this->myBlockCoordsIterator == it.myBlockCoordsIterator ) && ( this->myTiledRangeIterator == it.myTiledRangeIterator ) );
309  }
310 
316  inline
317  bool operator!= ( const TiledIterator &it ) const
318  {
320  return false;
321 
322  return ( ( this->myBlockCoordsIterator != it.myBlockCoordsIterator ) || ( this->myTiledRangeIterator != it.myTiledRangeIterator ) );
323  }
324 
329  inline
331  {
333 
334  if ( myTiledRangeIterator != myTile->range().end() )
335  return;
336  else
337  {
339 
341  return;
342 
344  myTiledRangeIterator = myTile->range().begin();
345  }
346  }
347 
352  inline
354  {
356  return *this;
357  }
358 
363  inline
365  {
366  TiledIterator tmp = *this;
368  return tmp;
369  }
370 
375  inline
377  {
378  // -- IF we are at the end... (reverse, --)
380  {
382 
384 
385  myTiledRangeIterator = myTile->range().end();
387 
388  return;
389  }
390  // -- IF we are at the end... (reverse, --)
391 
392  // ---
393 
394  if ( myTiledRangeIterator != myTile->range().begin() )
395  {
397  return;
398  }
399  else
400  {
402  return;
403 
405 
407 
408  myTiledRangeIterator = myTile->range().end();
410  }
411  }
412 
417  inline
419  {
421  return *this;
422  }
423 
427  inline
429  {
430  TiledIterator tmp = *this;
432  return tmp;
433  }
434 
435  private:
438 
440  ImageContainer * myTile;
441 
443  TiledRangeIterator myTiledRangeIterator;
444 
446  BlockCoordsIterator myBlockCoordsIterator;
447  };
448 
449 
452 
453  typedef std::reverse_iterator<TiledIterator> ReverseTiledIterator;
454  typedef ReverseTiledIterator ConstReverseIterator;
455  typedef ReverseTiledIterator ReverseOutputIterator;
456 
457 
458  ConstIterator begin() const
459  {
460  return TiledIterator( this->domainBlockCoords().begin(), this );
461  }
462 
463  OutputIterator begin()
464  {
465  return TiledIterator( this->domainBlockCoords().begin(), this );
466  }
467 
468  ConstIterator begin(const Point& aPoint) const
469  {
470  Point coords = this->findBlockCoordsFromPoint(aPoint);
471  return TiledIterator( this->domainBlockCoords().begin(coords), aPoint, this );
472  }
473 
474  OutputIterator begin(const Point& aPoint)
475  {
476  Point coords = this->findBlockCoordsFromPoint(aPoint);
477  return TiledIterator( this->domainBlockCoords().begin(coords), aPoint, this );
478  }
479 
480  ConstIterator end() const
481  {
482  return TiledIterator( this->domainBlockCoords().end(), this );
483  }
484 
485  OutputIterator end()
486  {
487  return TiledIterator( this->domainBlockCoords().end(), this );
488  }
489 
490  ConstReverseIterator rbegin() const
491  {
492  return ReverseTiledIterator( end() );
493  }
494 
495  ReverseOutputIterator rbegin()
496  {
497  return ReverseTiledIterator( end() );
498  }
499 
500  ConstReverseIterator rbegin(const Point& aPoint) const
501  {
502  typename DGtal::Dimension i;
503 
504  Point point2;
505  for(i=0; i<Domain::dimension; i++)
506  point2[i]=(m_upperBound[i]-aPoint[i])+m_lowerBound[i];
507 
508  TiledIterator it( begin(point2) ); it++;
509  return ReverseTiledIterator(it);
510  }
511 
512  ReverseOutputIterator rbegin(const Point& aPoint)
513  {
514  typename DGtal::Dimension i;
515 
516  Point point2;
517  for(i=0; i<Domain::dimension; i++)
518  point2[i]=(m_upperBound[i]-aPoint[i])+m_lowerBound[i];
519 
520  TiledIterator it( begin(point2) ); it++;
521  return ReverseTiledIterator(it);
522  }
523 
524  ConstReverseIterator rend() const
525  {
526  return ReverseTiledIterator( begin() );
527  }
528 
529  ConstReverseIterator rend()
530  {
531  return ReverseTiledIterator( begin() );
532  }
533 
534  // ---
535 
536  OutputIterator outputIterator()
537  {
538  return OutputIterator( begin() );
539  }
540 
541  OutputIterator outputIterator(const Point& aPoint)
542  {
543  return OutputIterator( begin(aPoint) );
544  }
545 
546  ReverseOutputIterator routputIterator()
547  {
548  return ReverseOutputIterator( end() );
549  }
550 
551  ReverseOutputIterator routputIterator(const Point &aPoint)
552  {
553  return ReverseOutputIterator( rbegin(aPoint) );
554  }
555 
557 
560 
565  ConstRange constRange() const
566  {
567  return ConstRange( this );
568  }
569 
574  Range range()
575  {
576  return Range( this );
577  }
578 
580 
585  void selfDisplay ( std::ostream & out ) const;
586 
591  bool isValid() const
592  {
593  return (myImageFactory->isValid() && myImageCache->isValid());
594  }
595 
602  const Domain findSubDomain(const Point & aPoint) const
603  {
604  ASSERT(myImageFactory->domain().isInside(aPoint));
605 
606  typename DGtal::Dimension i;
607 
608  Point low;
609  for(i=0; i<Domain::dimension; i++)
610  {
611  low[i] = (aPoint[i]-m_lowerBound[i])/mySize[i];
612  }
613 
614  Point dMin, dMax;
615  for(i=0; i<Domain::dimension; i++)
616  {
617  dMin[i] = (low[i]*mySize[i])+m_lowerBound[i];
618  dMax[i] = dMin[i] + (mySize[i]-1);
619 
620  if (dMax[i] > m_upperBound[i]) // last tile
621  dMax[i] = m_upperBound[i];
622  }
623 
624  Domain di(dMin, dMax);
625  return di;
626  }
627 
634  const Point findBlockCoordsFromPoint(const Point & aPoint) const
635  {
636  ASSERT(myImageFactory->domain().isInside(aPoint));
637 
638  typename DGtal::Dimension i;
639 
640  Point low;
641  for(i=0; i<Domain::dimension; i++)
642  {
643  /*if ( (aPoint[i]-m_lowerBound[i]) < mySize[i] )
644  low[i] = 0;
645  else*/
646  low[i] = (aPoint[i]-m_lowerBound[i])/mySize[i];
647  }
648 
649  return low;
650  }
651 
658  const Domain findSubDomainFromBlockCoords(const Point & aCoord) const
659  {
660  ASSERT(domainBlockCoords().isInside(aCoord));
661 
662  typename DGtal::Dimension i;
663 
664  Point dMin, dMax;
665  for(i=0; i<Domain::dimension; i++)
666  {
667  dMin[i] = (aCoord[i]*mySize[i])+m_lowerBound[i];
668  dMax[i] = dMin[i] + (mySize[i]-1);
669 
670  if (dMax[i] > m_upperBound[i]) // last tile
671  dMax[i] = m_upperBound[i];
672  }
673 
674  Domain di(dMin, dMax);
675  return di;
676  }
677 
684  ImageContainer * findTileFromBlockCoords(const Point & aCoord) const
685  {
686  ASSERT(domainBlockCoords().isInside(aCoord));
687 
688  Domain d = findSubDomainFromBlockCoords( aCoord );
689  ImageContainer *tile = myImageCache->getPage(d);
690  if (!tile)
691  {
693  myImageCache->update(d);
694  tile = myImageCache->getPage(d);
695  }
696 
697  return tile;
698  }
699 
706  Value operator()(const Point & aPoint) const
707  {
708  ASSERT(myImageFactory->domain().isInside(aPoint));
709 
710  typename OutputImage::Value aValue;
711  bool res;
712 
713  res = myImageCache->read(aPoint, aValue);
714 
715  if (res)
716  return aValue;
717  else
718  {
720  Domain d;
721 #ifdef DEBUG_VERBOSE
722  trace.info()<<"+";
723 #endif
724  d = findSubDomain(aPoint);
725 
726  myImageCache->update(d);
727 
728  myImageCache->read(aPoint, aValue);
729 
730  return aValue;
731  }
732 
733  // Unspecified behavior, returning the default constructed value.
734  return aValue;
735  }
736 
743  void setValue(const Point &aPoint, const Value &aValue)
744  {
745  ASSERT(myImageFactory->domain().isInside(aPoint));
746 
747  if (myImageCache->write(aPoint, aValue))
748  return;
749  else
750  {
753  myImageCache->write(aPoint, aValue);
754  }
755  }
756 
760  unsigned int getCacheMissRead()
761  {
762  return myImageCache->getCacheMissRead();
763  }
764 
768  unsigned int getCacheMissWrite()
769  {
771  }
772 
777  {
779  }
780 
781  // ------------------------- Private Datas --------------------------------
782  protected:
783 
786 
788  Point mySize;
789 
791  ImageFactory *myImageFactory;
792 
794  MyImageCache *myImageCache;
795 
798 
800  TImageCacheReadPolicy *myReadPolicy;
801 
803  TImageCacheWritePolicy *myWritePolicy;
804 
805  // ------------------------- Internals ------------------------------------
806 
807  }; // end of class TiledImage
808 
809 
816  template <typename TImageContainer, typename TImageFactory, typename TImageCacheReadPolicy, typename TImageCacheWritePolicy>
817  std::ostream&
818  operator<< ( std::ostream & out, const TiledImage<TImageContainer, TImageFactory, TImageCacheReadPolicy, TImageCacheWritePolicy> & object );
819 
820 } // namespace DGtal
821 
822 
824 // Includes inline functions.
825 #include "DGtal/images/TiledImage.ih"
826 
827 // //
829 
830 #endif // !defined TiledImage_h
831 
832 #undef TiledImage_RECURSES
833 #endif // else defined(TiledImage_RECURSES)
TiledIterator(BlockCoordsIterator aBlockCoordsIterator, const Point &aPoint, const TiledImage< ImageContainer, ImageFactory, ImageCacheReadPolicy, ImageCacheWritePolicy > *aTiledImage)
Definition: TiledImage.h:276
const Domain domainBlockCoords() const
Definition: TiledImage.h:210
TiledIterator & operator++()
Definition: TiledImage.h:353
unsigned int getCacheMissWrite()
Definition: ImageCache.h:211
TiledImageBidirectionalRangeFromPoint< TiledImage > Range
Definition: TiledImage.h:559
ConstIterator begin(const Point &aPoint) const
Definition: TiledImage.h:468
const TiledImage * myTiledImage
TiledImage pointer.
Definition: TiledImage.h:437
Trace trace
Definition: Common.h:137
Domain::Iterator BlockCoordsIterator
Definition: TiledImage.h:247
const Point findBlockCoordsFromPoint(const Point &aPoint) const
Definition: TiledImage.h:634
TiledIterator(BlockCoordsIterator aBlockCoordsIterator, const TiledImage< ImageContainer, ImageFactory, ImageCacheReadPolicy, ImageCacheWritePolicy > *aTiledImage)
Definition: TiledImage.h:257
Point m_lowerBound
domain lower and upper bound
Definition: TiledImage.h:797
Aim: model of CConstBidirectionalRangeFromPoint that adapts a TiledImage range of elements bounded by...
Aim: model of CBidirectionalRangeFromPoint that adapts a TiledImage range of elements bounded by two ...
TImageContainer ImageContainer
Types copied from the container.
Definition: TiledImage.h:91
DGtal::uint32_t Dimension
Definition: Common.h:120
Domain::Integer myN
Number of tiles per dimension.
Definition: TiledImage.h:785
ConstReverseIterator rend()
Definition: TiledImage.h:529
OutputIterator begin()
Definition: TiledImage.h:463
std::reverse_iterator< TiledIterator > ReverseTiledIterator
Definition: TiledImage.h:453
TImageFactory ImageFactory
Types.
Definition: TiledImage.h:99
unsigned int getCacheMissRead()
Definition: ImageCache.h:203
bool isValid() const
Definition: ImageCache.h:156
ImageContainer * findTileFromBlockCoords(const Point &aCoord) const
Definition: TiledImage.h:684
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: Alias.h:182
bool read(const Point &aPoint, Value &aValue) const
TiledImage & operator=(const TiledImage &other)
Definition: TiledImage.h:169
BOOST_CONCEPT_ASSERT((concepts::CImage< TImageContainer >))
Checking concepts.
ImageFactory * myImageFactory
ImageFactory pointer.
Definition: TiledImage.h:791
TiledImage< TImageContainer, TImageFactory, TImageCacheReadPolicy, TImageCacheWritePolicy > Self
Definition: TiledImage.h:82
void incCacheMissRead()
Definition: ImageCache.h:219
TImageCacheReadPolicy * myReadPolicy
TImageCacheReadPolicy pointer.
Definition: TiledImage.h:800
ReverseTiledIterator ConstReverseIterator
Definition: TiledImage.h:454
const Domain findSubDomainFromBlockCoords(const Point &aCoord) const
Definition: TiledImage.h:658
ConstIterator begin() const
Definition: TiledImage.h:458
ImageContainer * myTile
Alias on the current tile.
Definition: TiledImage.h:440
void selfDisplay(std::ostream &out) const
void setValue(const Point &aPoint, const Value &aValue)
Definition: TiledImage.h:743
TImageCacheWritePolicy ImageCacheWritePolicy
Definition: TiledImage.h:103
ReverseTiledIterator ReverseOutputIterator
Definition: TiledImage.h:455
unsigned int getCacheMissRead()
Definition: TiledImage.h:760
OutputIterator end()
Definition: TiledImage.h:485
ReverseOutputIterator rbegin()
Definition: TiledImage.h:495
Aim: implements a tiled image from a "bigger/original" one from an ImageFactory.
Definition: TiledImage.h:76
ImageContainer::Point Point
Definition: TiledImage.h:93
ImageContainer * getPage(const Domain &aDomain) const
ConstReverseIterator rbegin(const Point &aPoint) const
Definition: TiledImage.h:500
Point mySize
Width of a tile (for each dimension)
Definition: TiledImage.h:788
ImageCache< OutputImage, ImageFactory, ImageCacheReadPolicy, ImageCacheWritePolicy > MyImageCache
Definition: TiledImage.h:104
void clearCacheAndResetCacheMisses()
Definition: TiledImage.h:776
ImageContainer::Difference Difference
Definition: TiledImage.h:96
bool operator!=(const TiledIterator &it) const
Definition: TiledImage.h:317
const Domain & domain() const
Definition: TiledImage.h:200
BlockCoordsIterator myBlockCoordsIterator
Current block coords iterator.
Definition: TiledImage.h:446
OutputIterator begin(const Point &aPoint)
Definition: TiledImage.h:474
TImageCacheWritePolicy * myWritePolicy
TImageCacheWritePolicy pointer.
Definition: TiledImage.h:803
Aim: Defines the concept describing an image factory.
Definition: CImageFactory.h:90
ImageContainer::Range::Iterator TiledRangeIterator
Definition: TiledImage.h:246
ConstIterator end() const
Definition: TiledImage.h:480
bool isValid() const
Definition: TiledImage.h:591
DGtal is the top-level namespace which contains all DGtal functions and types.
MyImageCache * myImageCache
ImageCache pointer.
Definition: TiledImage.h:794
Aim: implements an images cache with 'read and write' policies.
Definition: ImageCache.h:77
std::ostream & info()
TiledRangeIterator myTiledRangeIterator
Current tiled range iterator.
Definition: TiledImage.h:443
bool write(const Point &aPoint, const Value &aValue)
const Domain findSubDomain(const Point &aPoint) const
Definition: TiledImage.h:602
void update(const Domain &aDomain)
unsigned int getCacheMissWrite()
Definition: TiledImage.h:768
OutputIterator outputIterator()
Definition: TiledImage.h:536
Aim: Defines the concept describing a cache write policy.
ConstReverseIterator rend() const
Definition: TiledImage.h:524
void clearCacheAndResetCacheMisses()
Definition: ImageCache.h:235
bool operator==(const TiledIterator &it) const
Definition: TiledImage.h:306
OutputIterator outputIterator(const Point &aPoint)
Definition: TiledImage.h:541
ImageContainer::Domain Domain
Definition: TiledImage.h:92
TiledIterator ConstIterator
Definition: TiledImage.h:450
TiledImageBidirectionalConstRangeFromPoint< TiledImage > ConstRange
Definition: TiledImage.h:558
ConstRange constRange() const
Definition: TiledImage.h:565
ImageContainer::Value Value
Definition: TiledImage.h:94
TiledImage(const TiledImage &other)
Definition: TiledImage.h:148
TiledImage(Alias< ImageFactory > anImageFactory, Alias< ImageCacheReadPolicy > aReadPolicy, Alias< ImageCacheWritePolicy > aWritePolicy, typename Domain::Integer N)
Definition: TiledImage.h:117
void incCacheMissWrite()
Definition: ImageCache.h:227
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
TiledIterator OutputIterator
Definition: TiledImage.h:451
ImageFactory::OutputImage OutputImage
Definition: TiledImage.h:100
TiledIterator & operator--()
Definition: TiledImage.h:418
ConstReverseIterator rbegin() const
Definition: TiledImage.h:490
ReverseOutputIterator routputIterator(const Point &aPoint)
Definition: TiledImage.h:551
Value operator()(const Point &aPoint) const
Definition: TiledImage.h:706
ReverseOutputIterator routputIterator()
Definition: TiledImage.h:546
Aim: Defines the concept describing a cache read policy.
TImageCacheReadPolicy ImageCacheReadPolicy
Definition: TiledImage.h:102
ReverseOutputIterator rbegin(const Point &aPoint)
Definition: TiledImage.h:512