DGtal 1.4.0
Loading...
Searching...
No Matches
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)
35#define TiledImage_RECURSES
36
37#if !defined TiledImage_h
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
58namespace DGtal
59{
61 // Template class TiledImage
75 template <typename TImageContainer, typename TImageFactory, typename TImageCacheReadPolicy, typename TImageCacheWritePolicy>
77 {
78
79 // ----------------------- Types ------------------------------
80
81 public:
83
89
93 typedef typename ImageContainer::Point Point;
94 typedef typename ImageContainer::Value Value;
95
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;
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;
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
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
233 {
234
236
237 public:
238
239 using iterator_category = std::bidirectional_iterator_tag;
241 using difference_type = ptrdiff_t;
242 using pointer = Value*;
243 using reference = Value&;
244
245 typedef typename ImageContainer::Range::/*Output*/Iterator TiledRangeIterator;
247
248 //TiledIterator();
249
256 TiledIterator ( BlockCoordsIterator aBlockCoordsIterator,
258 ImageCacheReadPolicy, ImageCacheWritePolicy> *aTiledImage ) : myTiledImage ( aTiledImage ),
259 myBlockCoordsIterator ( aBlockCoordsIterator )
260 {
262 {
264 myTiledRangeIterator = myTile->range().begin();
265 }
266 }
267
275 TiledIterator ( BlockCoordsIterator aBlockCoordsIterator,
276 const Point& aPoint,
278 ImageCacheReadPolicy, ImageCacheWritePolicy> *aTiledImage ) : myTiledImage ( aTiledImage ),
279 myBlockCoordsIterator ( aBlockCoordsIterator )
280 {
282 {
284 myTiledRangeIterator = myTile->range().begin(aPoint);
285 }
286 }
287
293 inline
295 {
296 return (*myTiledRangeIterator);
297 }
298
304 inline
305 bool operator== ( const TiledIterator &it ) const
306 {
307 return ( ( this->myBlockCoordsIterator == it.myBlockCoordsIterator ) && ( this->myTiledRangeIterator == it.myTiledRangeIterator ) );
308 }
309
315 inline
316 bool operator!= ( const TiledIterator &it ) const
317 {
319 return false;
320
321 return ( ( this->myBlockCoordsIterator != it.myBlockCoordsIterator ) || ( this->myTiledRangeIterator != it.myTiledRangeIterator ) );
322 }
323
328 inline
330 {
332
333 if ( myTiledRangeIterator != myTile->range().end() )
334 return;
335 else
336 {
338
340 return;
341
343 myTiledRangeIterator = myTile->range().begin();
344 }
345 }
346
351 inline
353 {
355 return *this;
356 }
357
362 inline
364 {
365 TiledIterator tmp = *this;
367 return tmp;
368 }
369
374 inline
376 {
377 // -- IF we are at the end... (reverse, --)
379 {
381
383
384 myTiledRangeIterator = myTile->range().end();
386
387 return;
388 }
389 // -- IF we are at the end... (reverse, --)
390
391 // ---
392
393 if ( myTiledRangeIterator != myTile->range().begin() )
394 {
396 return;
397 }
398 else
399 {
401 return;
402
404
406
407 myTiledRangeIterator = myTile->range().end();
409 }
410 }
411
416 inline
418 {
420 return *this;
421 }
422
426 inline
428 {
429 TiledIterator tmp = *this;
431 return tmp;
432 }
433
434 private:
437
440
443
446 };
447
448
451
452 typedef std::reverse_iterator<TiledIterator> ReverseTiledIterator;
455
456
458 {
459 return TiledIterator( this->domainBlockCoords().begin(), this );
460 }
461
462 OutputIterator begin()
463 {
464 return TiledIterator( this->domainBlockCoords().begin(), this );
465 }
466
467 ConstIterator begin(const Point& aPoint) const
468 {
469 Point coords = this->findBlockCoordsFromPoint(aPoint);
470 return TiledIterator( this->domainBlockCoords().begin(coords), aPoint, this );
471 }
472
474 {
475 Point coords = this->findBlockCoordsFromPoint(aPoint);
476 return TiledIterator( this->domainBlockCoords().begin(coords), aPoint, this );
477 }
478
480 {
481 return TiledIterator( this->domainBlockCoords().end(), this );
482 }
483
484 OutputIterator end()
485 {
486 return TiledIterator( this->domainBlockCoords().end(), this );
487 }
488
489 ConstReverseIterator rbegin() const
490 {
491 return ReverseTiledIterator( end() );
492 }
493
495 {
496 return ReverseTiledIterator( end() );
497 }
498
500 {
501 typename DGtal::Dimension i;
502
503 Point point2;
504 for(i=0; i<Domain::dimension; i++)
505 point2[i]=(m_upperBound[i]-aPoint[i])+m_lowerBound[i];
506
507 TiledIterator it( begin(point2) ); it++;
508 return ReverseTiledIterator(it);
509 }
510
512 {
513 typename DGtal::Dimension i;
514
515 Point point2;
516 for(i=0; i<Domain::dimension; i++)
517 point2[i]=(m_upperBound[i]-aPoint[i])+m_lowerBound[i];
518
519 TiledIterator it( begin(point2) ); it++;
520 return ReverseTiledIterator(it);
521 }
522
524 {
525 return ReverseTiledIterator( begin() );
526 }
527
529 {
530 return ReverseTiledIterator( begin() );
531 }
532
533 // ---
534
536 {
537 return OutputIterator( begin() );
538 }
539
544
549
554
556
559
565 {
566 return ConstRange( this );
567 }
568
574 {
575 return Range( this );
576 }
577
579
584 void selfDisplay ( std::ostream & out ) const;
585
590 bool isValid() const
591 {
592 return (myImageFactory->isValid() && myImageCache->isValid());
593 }
594
601 const Domain findSubDomain(const Point & aPoint) const
602 {
603 ASSERT(myImageFactory->domain().isInside(aPoint));
604
605 typename DGtal::Dimension i;
606
607 Point low;
608 for(i=0; i<Domain::dimension; i++)
609 {
610 low[i] = (aPoint[i]-m_lowerBound[i])/mySize[i];
611 }
612
613 Point dMin, dMax;
614 for(i=0; i<Domain::dimension; i++)
615 {
616 dMin[i] = (low[i]*mySize[i])+m_lowerBound[i];
617 dMax[i] = dMin[i] + (mySize[i]-1);
618
619 if (dMax[i] > m_upperBound[i]) // last tile
620 dMax[i] = m_upperBound[i];
621 }
622
623 Domain di(dMin, dMax);
624 return di;
625 }
626
634 {
635 ASSERT(myImageFactory->domain().isInside(aPoint));
636
637 typename DGtal::Dimension i;
638
639 Point low;
640 for(i=0; i<Domain::dimension; i++)
641 {
642 /*if ( (aPoint[i]-m_lowerBound[i]) < mySize[i] )
643 low[i] = 0;
644 else*/
645 low[i] = (aPoint[i]-m_lowerBound[i])/mySize[i];
646 }
647
648 return low;
649 }
650
657 const Domain findSubDomainFromBlockCoords(const Point & aCoord) const
658 {
659 ASSERT(domainBlockCoords().isInside(aCoord));
660
661 typename DGtal::Dimension i;
662
663 Point dMin, dMax;
664 for(i=0; i<Domain::dimension; i++)
665 {
666 dMin[i] = (aCoord[i]*mySize[i])+m_lowerBound[i];
667 dMax[i] = dMin[i] + (mySize[i]-1);
668
669 if (dMax[i] > m_upperBound[i]) // last tile
670 dMax[i] = m_upperBound[i];
671 }
672
673 Domain di(dMin, dMax);
674 return di;
675 }
676
684 {
685 ASSERT(domainBlockCoords().isInside(aCoord));
686
689 if (!tile)
690 {
693 tile = myImageCache->getPage(d);
694 }
695
696 return tile;
697 }
698
706 {
707 ASSERT(myImageFactory->domain().isInside(aPoint));
708
709 typename OutputImage::Value aValue;
710 bool res;
711
712 res = myImageCache->read(aPoint, aValue);
713
714 if (res)
715 return aValue;
716 else
717 {
719 Domain d;
720#ifdef DEBUG_VERBOSE
721 trace.info()<<"+";
722#endif
724
726
727 myImageCache->read(aPoint, aValue);
728
729 return aValue;
730 }
731
732 // Unspecified behavior, returning the default constructed value.
733 return aValue;
734 }
735
742 void setValue(const Point &aPoint, const Value &aValue)
743 {
744 ASSERT(myImageFactory->domain().isInside(aPoint));
745
746 if (myImageCache->write(aPoint, aValue))
747 return;
748 else
749 {
752 myImageCache->write(aPoint, aValue);
753 }
754 }
755
759 unsigned int getCacheMissRead()
760 {
762 }
763
767 unsigned int getCacheMissWrite()
768 {
770 }
771
779
780 // ------------------------- Private Datas --------------------------------
781 protected:
782
785
788
791
794
797
799 TImageCacheReadPolicy *myReadPolicy;
800
802 TImageCacheWritePolicy *myWritePolicy;
803
804 // ------------------------- Internals ------------------------------------
805
806 }; // end of class TiledImage
807
808
815 template <typename TImageContainer, typename TImageFactory, typename TImageCacheReadPolicy, typename TImageCacheWritePolicy>
816 std::ostream&
818
819} // namespace DGtal
820
821
823// Includes inline functions.
824#include "DGtal/images/TiledImage.ih"
825
826// //
828
829#endif // !defined TiledImage_h
830
831#undef TiledImage_RECURSES
832#endif // else defined(TiledImage_RECURSES)
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition Alias.h:183
Aim: implements an images cache with 'read and write' policies.
Definition ImageCache.h:78
void clearCacheAndResetCacheMisses()
Definition ImageCache.h:235
bool read(const Point &aPoint, Value &aValue) const
bool isValid() const
Definition ImageCache.h:156
unsigned int getCacheMissRead()
Definition ImageCache.h:203
void update(const Domain &aDomain)
bool write(const Point &aPoint, const Value &aValue)
void incCacheMissWrite()
Definition ImageCache.h:227
ImageContainer * getPage(const Domain &aDomain) const
void incCacheMissRead()
Definition ImageCache.h:219
unsigned int getCacheMissWrite()
Definition ImageCache.h:211
std::vector< Value >::difference_type Difference
SimpleRandomAccessRangeFromPoint< ConstIterator, Iterator, DistanceFunctorFromPoint< Self > > Range
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 ...
Domain::Iterator BlockCoordsIterator
Definition TiledImage.h:246
std::bidirectional_iterator_tag iterator_category
Definition TiledImage.h:239
bool operator!=(const TiledIterator &it) const
Definition TiledImage.h:316
bool operator==(const TiledIterator &it) const
Definition TiledImage.h:305
TiledIterator(BlockCoordsIterator aBlockCoordsIterator, const Point &aPoint, const TiledImage< ImageContainer, ImageFactory, ImageCacheReadPolicy, ImageCacheWritePolicy > *aTiledImage)
Definition TiledImage.h:275
ImageContainer * myTile
Alias on the current tile.
Definition TiledImage.h:439
BlockCoordsIterator myBlockCoordsIterator
Current block coords iterator.
Definition TiledImage.h:445
const TiledImage * myTiledImage
TiledImage pointer.
Definition TiledImage.h:436
TiledIterator(BlockCoordsIterator aBlockCoordsIterator, const TiledImage< ImageContainer, ImageFactory, ImageCacheReadPolicy, ImageCacheWritePolicy > *aTiledImage)
Definition TiledImage.h:256
ImageContainer::Range::Iterator TiledRangeIterator
Definition TiledImage.h:245
TiledRangeIterator myTiledRangeIterator
Current tiled range iterator.
Definition TiledImage.h:442
Aim: implements a tiled image from a "bigger/original" one from an ImageFactory.
Definition TiledImage.h:77
ConstIterator begin() const
Definition TiledImage.h:457
OutputIterator begin()
Definition TiledImage.h:462
TiledIterator ConstIterator
Definition TiledImage.h:449
ImageContainer * findTileFromBlockCoords(const Point &aCoord) const
Definition TiledImage.h:683
TImageCacheReadPolicy ImageCacheReadPolicy
Definition TiledImage.h:102
std::reverse_iterator< TiledIterator > ReverseTiledIterator
Definition TiledImage.h:452
TImageCacheWritePolicy ImageCacheWritePolicy
Definition TiledImage.h:103
bool isValid() const
Definition TiledImage.h:590
TImageCacheReadPolicy * myReadPolicy
TImageCacheReadPolicy pointer.
Definition TiledImage.h:799
ImageContainer::Point Point
Definition TiledImage.h:93
ReverseTiledIterator ReverseOutputIterator
Definition TiledImage.h:454
ReverseTiledIterator ConstReverseIterator
Definition TiledImage.h:453
BOOST_CONCEPT_ASSERT((concepts::CImageCacheReadPolicy< TImageCacheReadPolicy >))
ConstReverseIterator rend() const
Definition TiledImage.h:523
unsigned int getCacheMissRead()
Definition TiledImage.h:759
TiledImage< TImageContainer, TImageFactory, TImageCacheReadPolicy, TImageCacheWritePolicy > Self
Definition TiledImage.h:82
const Domain findSubDomain(const Point &aPoint) const
Definition TiledImage.h:601
ImageFactory * myImageFactory
ImageFactory pointer.
Definition TiledImage.h:790
const Domain domainBlockCoords() const
Definition TiledImage.h:210
ConstReverseIterator rend()
Definition TiledImage.h:528
TiledImageBidirectionalConstRangeFromPoint< TiledImage > ConstRange
Definition TiledImage.h:557
const Domain findSubDomainFromBlockCoords(const Point &aCoord) const
Definition TiledImage.h:657
BOOST_CONCEPT_ASSERT((concepts::CImageFactory< TImageFactory >))
ImageContainer::Difference Difference
Definition TiledImage.h:96
ImageFactory::OutputImage OutputImage
Definition TiledImage.h:100
void selfDisplay(std::ostream &out) const
ReverseOutputIterator routputIterator(const Point &aPoint)
Definition TiledImage.h:550
MyImageCache * myImageCache
ImageCache pointer.
Definition TiledImage.h:793
OutputIterator end()
Definition TiledImage.h:484
ReverseOutputIterator routputIterator()
Definition TiledImage.h:545
Domain::Integer myN
Number of tiles per dimension.
Definition TiledImage.h:784
ImageContainer::Value Value
Definition TiledImage.h:94
TiledImageBidirectionalRangeFromPoint< TiledImage > Range
Definition TiledImage.h:558
const Domain & domain() const
Definition TiledImage.h:200
ConstIterator begin(const Point &aPoint) const
Definition TiledImage.h:467
TiledImage(const TiledImage &other)
Definition TiledImage.h:148
OutputIterator outputIterator()
Definition TiledImage.h:535
OutputIterator outputIterator(const Point &aPoint)
Definition TiledImage.h:540
void setValue(const Point &aPoint, const Value &aValue)
Definition TiledImage.h:742
TImageCacheWritePolicy * myWritePolicy
TImageCacheWritePolicy pointer.
Definition TiledImage.h:802
ConstRange constRange() const
Definition TiledImage.h:564
ReverseOutputIterator rbegin(const Point &aPoint)
Definition TiledImage.h:511
ConstReverseIterator rbegin(const Point &aPoint) const
Definition TiledImage.h:499
ImageCache< OutputImage, ImageFactory, ImageCacheReadPolicy, ImageCacheWritePolicy > MyImageCache
Definition TiledImage.h:104
TiledImage & operator=(const TiledImage &other)
Definition TiledImage.h:169
TImageContainer ImageContainer
Types copied from the container.
Definition TiledImage.h:91
Point mySize
Width of a tile (for each dimension)
Definition TiledImage.h:787
ReverseOutputIterator rbegin()
Definition TiledImage.h:494
TiledIterator OutputIterator
Definition TiledImage.h:450
ConstIterator end() const
Definition TiledImage.h:479
ConstReverseIterator rbegin() const
Definition TiledImage.h:489
Point m_lowerBound
domain lower and upper bound
Definition TiledImage.h:796
TiledImage(Alias< ImageFactory > anImageFactory, Alias< ImageCacheReadPolicy > aReadPolicy, Alias< ImageCacheWritePolicy > aWritePolicy, typename Domain::Integer N)
Definition TiledImage.h:117
void clearCacheAndResetCacheMisses()
Definition TiledImage.h:775
ImageContainer::Domain Domain
Definition TiledImage.h:92
BOOST_CONCEPT_ASSERT((concepts::CImage< TImageContainer >))
Checking concepts.
OutputIterator begin(const Point &aPoint)
Definition TiledImage.h:473
Value operator()(const Point &aPoint) const
Definition TiledImage.h:705
TImageFactory ImageFactory
Types.
Definition TiledImage.h:99
BOOST_CONCEPT_ASSERT((concepts::CImageCacheWritePolicy< TImageCacheWritePolicy >))
unsigned int getCacheMissWrite()
Definition TiledImage.h:767
const Point findBlockCoordsFromPoint(const Point &aPoint) const
Definition TiledImage.h:633
std::ostream & info()
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
DGtal::uint32_t Dimension
Definition Common.h:136
Trace trace
Definition Common.h:153
Aim: Defines the concept describing a cache read policy.
Aim: Defines the concept describing a cache write policy.
Aim: Defines the concept describing an image factory.
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition CImage.h:103
const Point aPoint(3, 4)
ImageContainerBySTLVector< HyperRectDomain< Z2i::Space >, std::unordered_set< Z2i::Point > > TImageContainer