Loading [MathJax]/extensions/TeX/AMSsymbols.js
DGtal 2.0.0
FreemanChain.h
1
16
17#pragma once
18
38
39#if defined(FreemanChain_RECURSES)
40#error Recursive header files inclusion detected in FreemanChain.h
41#else // defined(FreemanChain_RECURSES)
43#define FreemanChain_RECURSES
44
45#if !defined FreemanChain_h
47#define FreemanChain_h
48
50// Inclusions
51
52#include <iostream>
53#include <sstream>
54#include <vector>
55#include <iterator>
56#include "DGtal/kernel/PointVector.h"
57#include "DGtal/base/OrderedAlphabet.h"
58#include "DGtal/base/Circulator.h"
59#include "DGtal/arithmetic/ModuloComputer.h"
60
61#include "DGtal/base/CConstSinglePassRange.h"
62#include "DGtal/topology/KhalimskySpaceND.h"
63#include "DGtal/base/ConstAlias.h"
64
65#include "boost/iterator/reverse_iterator.hpp"
66
67
69
70
71namespace DGtal
72{
73
75 // class FreemanChain
77
113
114 template <typename TInteger>
116 {
117
118 public :
119
121 typedef TInteger Integer;
123
126
127 typedef unsigned int Size;
128 typedef unsigned int Index;
129
130 // ------------------------- iterator ------------------------------
131 public:
132
134 // class FreemanChain::ConstIterator
136
137
142
144 {
145 public:
146
147 using iterator_category = std::bidirectional_iterator_tag;
149 using difference_type = int;
150 using pointer = Point*;
152
153 // ------------------------- Private data -----------------------
154
155 private:
156
159
162
165
166 // ------------------------- Standard services -----------------------
167 public:
168
174 : myFc( NULL ), myPos( 0 )
175 { }
176
185
186
199 : myFc( &aChain ), myPos( n ), myXY ( XY )
200 { }
201
206 ConstIterator( const ConstIterator & aOther )
207 : myFc( aOther.myFc ), myPos( aOther.myPos ), myXY( aOther.myXY )
208 { }
209
216
221 { }
222
223 // ------------------------- iteration services -------------------------
224 public:
225
226
230 const Point& operator*() const
231 {
232 return myXY;
233 }
234
238 const Point& get() const
239 {
240 return myXY;
241 }
242
248 {
249 this->next();
250 return *this;
251 }
252
258 {
259 ConstIterator tmp(*this);
260 this->next();
261 return tmp;
262 }
263
264
268 void next();
269
274
279 {
280 return myPos;
281 }
282
283
287 const FreemanChain * getChain() const
288 {
289 return myFc;
290 }
291
296 //unsigned int getCode() const
297 char getCode() const
298 {
299 ASSERT( myFc != 0 );
300 return myFc->code( myPos );
301 }
302
303
309 {
310 this->previous();
311 return *this;
312 }
313
319 {
320 ConstIterator tmp(*this);
321 this->previous();
322 return tmp;
323 }
324
325
329 void previous();
330
331
336
337
346 bool operator== ( const ConstIterator & aOther ) const
347 {
348 ASSERT( myFc == aOther.myFc );
349 return myPos == aOther.myPos;
350 }
351
352
361 bool operator!= ( const ConstIterator & aOther ) const
362 {
363 ASSERT( myFc == aOther.myFc );
364 return myPos != aOther.myPos;
365 }
366
376 bool operator< ( const ConstIterator & aOther ) const
377 {
378 ASSERT( myFc == aOther.myFc );
379 return myPos < aOther.myPos;
380 }
381
382 };
383
384
386// class CodesRange
388
396{
397
398 // ------------------------- inner types --------------------------------
399public:
400
401 typedef std::string::const_iterator ConstIterator;
402 typedef std::string::const_reverse_iterator ConstReverseIterator;
404 typedef boost::reverse_iterator<ConstCirculator> ConstReverseCirculator;
405
406 // ------------------------- standard services --------------------------------
407
412
416 CodesRange(const std::string& aChain ): myChain(aChain){}
417
422 CodesRange( const CodesRange & aOther )
423 : myChain( aOther.myChain ){}
424
431 {
432 if ( this != &other )
433 {
434 myChain = other.myChain;
435 }
436 return *this;
437 }
438
443
447 std::string::size_type size() const
448 {
449 return myChain.size();
450 }
451
456 bool isValid() const { return true; }
457
458 // ------------------------- display --------------------------------
463 void selfDisplay ( std::ostream & out ) const
464 {
466 out << "[FreemanChainCodes]" << std::endl;
467 out << "\t";
468 std::copy( this->begin(), this->end(), std::ostream_iterator<Value>(out, "") );
469 out << std::endl;
470 }
471
478 friend std::ostream& operator <<(std::ostream & out, const CodesRange & object)
479 {
480 object.selfDisplay( out );
481 return out;
482 }
483 // ------------------------- private data --------------------------------
484 private:
488 std::string myChain;
489 // ------------------------- iterator services --------------------------------
490public:
491
497 return myChain.begin();
498 }
499
505 return myChain.end();
506 }
507
513 return myChain.rbegin();
514 }
515
521 return myChain.rend();
522 }
523
529 return ConstCirculator( this->begin(), this->begin(), this->end() );
530 }
531
536 ConstReverseCirculator rc() const {
537 return ConstReverseCirculator( this->c() );
538 }
539};
540
542// end of class CodesRange
544
548 CodesRange getCodesRange()
549 {
550 return CodesRange(chain);
551 }
552
553
555 // ----------------------- Standard services ------------------------------
556
557 public:
558
563 { }
564
571 FreemanChain( const std::string & s = "", TInteger x = 0, TInteger y = 0 );
572
573
578 FreemanChain( const std::vector<Point>& vectPoints);
579
580
585 FreemanChain(std::istream & in );
586
587
592 FreemanChain( const FreemanChain & other );
593
594
601
602
608 bool operator==( const FreemanChain & other) const
609 {
610 return (chain == other.chain) && ( x0 == other.x0 ) && ( y0 == other.y0 )
611 && ( xn == other.xn ) && ( yn == other.yn );
612 }
613
619 bool operator!=( const FreemanChain & other) const
620 {
621 return !( (*this) == other );
622 }
623
624
631 //unsigned int code( Index pos ) const;
632 char code( Index pos ) const;
633
634
638 Size size() const;
639
640
652
653
663
664
674
675
684 void computeBoundingBox( TInteger & min_x, TInteger& min_y,
685 TInteger& max_x, TInteger& max_y ) const;
686
703 //BK
704 typename Self::ConstIterator
706
726 //BK
727 typename Self::ConstIterator
729
730
737 int isClosed() const ;
738
739
750 int ccwLoops() const ;
751
752
763 Point getPoint ( Index pos ) const;
764
765
770 {
771 return Point(x0,y0);
772 }
773
774
778 Point lastPoint ( ) const
779 {
780 return Point(xn,yn);
781 }
782
783
789 {
790 return lastPoint() - firstPoint();
791 }
792
793
799
805
806
807
808
809
810
811
813 // ----------------------- Iteration services ------------------------------
814
819 ConstIterator begin() const;
820
821
826 ConstIterator end() const;
827
835 Index next( Index pos ) const;
836
837
845 Index previous( Index pos ) const;
846
847
848 // ------------------------- Static services -----------------------
849
850 public:
851
857 static void write( std::ostream & out, const FreemanChain & c )
858 {
859 out << c.x0 << " " << c.y0 << " " << c.chain << std::endl;
860 }
861
862
868 static void read( std::istream & in, FreemanChain & c );
869
877 template<typename TConstIterator>
878 static void readFromPointsRange( const TConstIterator& itBegin, const TConstIterator& itEnd, FreemanChain & c );
879
886 template<typename TRange>
887 static void readFromPointsRange( const TRange& aRange, FreemanChain & c );
888
895 static void getContourPoints(const FreemanChain & fc,
896 std::vector<Point> & aVContour );
897
898
913
915 const FreemanChain & fc,
916 typename KhalimskySpaceND<2, TInteger>::SCellSet & aSCellContour,
917 bool aFlagForAppend=false);
918
919
920
921
928 //static void movePointFromFC(Point & aPoint, unsigned int aCode );
929 static void movePointFromFC(Point & aPoint, char aCode );
930
943 //static unsigned int movement( unsigned int aCode1, unsigned int aCode2,
944 // bool ccw = true );
945 static char movement( char aCode1, char aCode2,
946 bool ccw = true );
947
955 static char addToCode( char code, int n);
956
957
967 static short freemanCode4C(int dx, int dy);
968
969
970
978 // static void displacement( int & dx, int & dy, unsigned int aCode );
979 static void displacement( int & dx, int & dy, char aCode );
980
981
982
987 // static Point displacement( unsigned int aCode );
988 static Point displacement( char aCode );
989
990
1000 // static unsigned int turnedCode( unsigned int aCode, bool ccw = true );
1001 static char turnedCode( char aCode, bool ccw = true );
1002
1003
1025 static void pointel2pixel( FreemanChain & aPixChain,
1026 std::vector<unsigned int> & aPl2pix,
1027 std::vector<unsigned int> & aPix2pl,
1028 const FreemanChain & aPlChain )
1029 {
1030 innerContour( aPixChain, aPl2pix, aPix2pl, aPlChain, true );
1031 };
1032
1060 static void innerContour( FreemanChain & aInnerChain,
1061 std::vector<unsigned int> & aOuter2inner,
1062 std::vector<unsigned int> & aInner2outer,
1063 const FreemanChain & aOuterChain,
1064 bool ccw = true );
1065
1066
1089 static bool cleanOuterSpikes( FreemanChain & aCleanC,
1090 std::vector<unsigned int> & aC2clean,
1091 std::vector<unsigned int> & aClean2c,
1092 const FreemanChain & c,
1093 bool ccw = true );
1094
1095
1096
1097
1098 // ----------------------- Interface --------------------------------------
1099
1100 public:
1101
1106 void selfDisplay ( std::ostream & out ) const ;
1107
1108
1113 bool isValid() const ;
1114
1115
1116
1117 // ----------------------- Drawing services --------------------------------
1118
1119 public:
1120
1125 //DrawableWithBoard2D* defaultStyle( std::string mode = "" ) const;
1126
1127
1131 std::string className() const;
1132
1133 // ------------------------- Public Datas ------------------------------
1134
1135 public:
1139 std::string chain;
1140
1145
1150
1155
1160
1161
1162 // ------------------------- Internals ------------------------------------
1163
1164
1165 private:
1166
1171 // FreemanChain() {};
1172
1178
1179 public:
1180
1181
1182 }; // end of class FreemanChain
1183
1184
1185
1186
1193 template<typename TInteger>
1194 std::ostream&
1195 operator<< ( std::ostream & out, const FreemanChain<TInteger> & object );
1196
1197
1198} // namespace DGtal
1199
1200
1202// Includes inline functions/methods.
1203#include "DGtal/geometry/curves/FreemanChain.ih"
1204
1205// //
1207
1208#endif // !defined FreemanChain_h
1209
1210#undef FreemanChain_RECURSES
1211#endif // else defined(FreemanChain_RECURSES)
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition Circulator.h:86
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition ConstAlias.h:187
friend std::ostream & operator<<(std::ostream &out, const CodesRange &object)
ConstIterator end() const
void selfDisplay(std::ostream &out) const
Circulator< ConstIterator > ConstCirculator
CodesRange & operator=(const CodesRange &other)
CodesRange(const CodesRange &aOther)
boost::reverse_iterator< ConstCirculator > ConstReverseCirculator
ConstReverseIterator rend() const
ConstIterator begin() const
ConstCirculator c() const
ConstReverseCirculator rc() const
CodesRange(const std::string &aChain)
std::string::const_reverse_iterator ConstReverseIterator
ConstReverseIterator rbegin() const
std::string::size_type size() const
std::string::const_iterator ConstIterator
ConstIterator(const ConstIterator &aOther)
Index myPos
The current position in the word.
const FreemanChain * myFc
The Freeman chain visited by the iterator.
bool operator<(const ConstIterator &aOther) const
const FreemanChain * getChain() const
ConstIterator(ConstAlias< FreemanChain > aChain, Index n=0)
const Point & operator*() const
ConstIterator & operator=(const ConstIterator &other)
ConstIterator(ConstAlias< FreemanChain > aChain, Index n, const Point &XY)
bool operator==(const ConstIterator &aOther) const
Point myXY
The current coordinates of the iterator.
std::bidirectional_iterator_tag iterator_category
bool operator!=(const ConstIterator &aOther) const
void computeBoundingBox(TInteger &min_x, TInteger &min_y, TInteger &max_x, TInteger &max_y) const
FreemanChain(const std::vector< Point > &vectPoints)
PointVector< 2, Integer > Vector
char code(Index pos) const
Size size() const
FreemanChain & operator=(const FreemanChain &other)
int ccwLoops() const
Point lastPoint() const
FreemanChain(const std::string &s="", TInteger x=0, TInteger y=0)
static void write(std::ostream &out, const FreemanChain &c)
ConstIterator end() const
Point firstPoint() const
bool isValid() const
bool operator==(const FreemanChain &other) const
void selfDisplay(std::ostream &out) const
static void displacement(int &dx, int &dy, char aCode)
ConstIterator begin() const
CodesRange getCodesRange()
FreemanChain & operator+=(const FreemanChain &other)
static char turnedCode(char aCode, bool ccw=true)
PointVector< 2, Integer > Point
int isClosed() const
static char addToCode(char code, int n)
FreemanChain< Integer > Self
BOOST_CONCEPT_ASSERT((concepts::CInteger< TInteger >))
Index next(Index pos) const
static Point displacement(char aCode)
Vector totalDisplacement() const
static void innerContour(FreemanChain &aInnerChain, std::vector< unsigned int > &aOuter2inner, std::vector< unsigned int > &aInner2outer, const FreemanChain &aOuterChain, bool ccw=true)
static void getInterPixelLinels(const KhalimskySpaceND< 2, TInteger > &aKSpace, const FreemanChain &fc, typename KhalimskySpaceND< 2, TInteger >::SCellSet &aSCellContour, bool aFlagForAppend=false)
bool operator!=(const FreemanChain &other) const
Self::ConstIterator findQuadrantChange(OrderedAlphabet &A) const
FreemanChain & retract(Size n=1)
static void readFromPointsRange(const TRange &aRange, FreemanChain &c)
FreemanChain operator+(const FreemanChain &other) const
FreemanChain(std::istream &in)
Point getPoint(Index pos) const
FreemanChain(const FreemanChain &other)
static void read(std::istream &in, FreemanChain &c)
static void getContourPoints(const FreemanChain &fc, std::vector< Point > &aVContour)
static void readFromPointsRange(const TConstIterator &itBegin, const TConstIterator &itEnd, FreemanChain &c)
static void pointel2pixel(FreemanChain &aPixChain, std::vector< unsigned int > &aPl2pix, std::vector< unsigned int > &aPix2pl, const FreemanChain &aPlChain)
Self::ConstIterator findQuadrantChange4(OrderedAlphabet &A) const
FreemanChain & extend(char code)
static void movePointFromFC(Point &aPoint, char aCode)
static short freemanCode4C(int dx, int dy)
std::string className() const
Index previous(Index pos) const
FreemanChain subChain(Index pos, Size n) const
static char movement(char aCode1, char aCode2, bool ccw=true)
static bool cleanOuterSpikes(FreemanChain &aCleanC, std::vector< unsigned int > &aC2clean, std::vector< unsigned int > &aClean2c, const FreemanChain &c, bool ccw=true)
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
std::set< SCell > SCellSet
Preferred type for defining a set of SCell(s).
Aim: Describes an alphabet over an interval of (ascii) letters, where the lexicographic order can be ...
Aim: Implements basic operations that will be used in Point and Vector classes.
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
Aim: Concept checking for Integer Numbers. More precisely, this concept is a refinement of both CEucl...
Definition CInteger.h:88
const Point aPoint(3, 4)