DGtal 1.3.0
Loading...
Searching...
No Matches
FreemanChain.h
1
17#pragma once
18
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
67
68
69namespace DGtal
70{
71
73 // class FreemanChain
75
112 template <typename TInteger>
114 {
115
116 public :
117
119 typedef TInteger Integer;
121
124
125 typedef unsigned int Size;
126 typedef unsigned int Index;
127
128 // ------------------------- iterator ------------------------------
129 public:
130
132 // class FreemanChain::ConstIterator
134
135
141 class ConstIterator : public
142 std::iterator<std::bidirectional_iterator_tag, Point, int, Point*, Point>
143 {
144
145 // ------------------------- Private data -----------------------
146
147 private:
148
151
154
157
158 // ------------------------- Standard services -----------------------
159 public:
160
166 : myFc( NULL ), myPos( 0 )
167 { }
168
177
178
191 : myFc( &aChain ), myPos( n ), myXY ( XY )
192 { }
193
198 ConstIterator( const ConstIterator & aOther )
199 : myFc( aOther.myFc ), myPos( aOther.myPos ), myXY( aOther.myXY )
200 { }
201
208
213 { }
214
215 // ------------------------- iteration services -------------------------
216 public:
217
218
222 const Point& operator*() const
223 {
224 return myXY;
225 }
226
230 const Point& get() const
231 {
232 return myXY;
233 }
234
240 {
241 this->next();
242 return *this;
243 }
244
250 {
251 ConstIterator tmp(*this);
252 this->next();
253 return tmp;
254 }
255
256
260 void next();
261
266
271 {
272 return myPos;
273 }
274
275
279 const FreemanChain * getChain() const
280 {
281 return myFc;
282 }
283
288 //unsigned int getCode() const
289 char getCode() const
290 {
291 ASSERT( myFc != 0 );
292 return myFc->code( myPos );
293 }
294
295
301 {
302 this->previous();
303 return *this;
304 }
305
311 {
312 ConstIterator tmp(*this);
313 this->previous();
314 return tmp;
315 }
316
317
321 void previous();
322
323
328
329
338 bool operator== ( const ConstIterator & aOther ) const
339 {
340 ASSERT( myFc == aOther.myFc );
341 return myPos == aOther.myPos;
342 }
343
344
353 bool operator!= ( const ConstIterator & aOther ) const
354 {
355 ASSERT( myFc == aOther.myFc );
356 return myPos != aOther.myPos;
357 }
358
368 bool operator< ( const ConstIterator & aOther ) const
369 {
370 ASSERT( myFc == aOther.myFc );
371 return myPos < aOther.myPos;
372 }
373
374 };
375
376
378// class CodesRange
380
388{
389
390 // ------------------------- inner types --------------------------------
391public:
392
393 typedef std::string::const_iterator ConstIterator;
394 typedef std::string::const_reverse_iterator ConstReverseIterator;
396 typedef std::reverse_iterator<ConstCirculator> ConstReverseCirculator;
397
398 // ------------------------- standard services --------------------------------
399
404
408 CodesRange(const std::string& aChain ): myChain(aChain){}
409
414 CodesRange( const CodesRange & aOther )
415 : myChain( aOther.myChain ){}
416
423 {
424 if ( this != &other )
425 {
426 myChain = other.myChain;
427 }
428 return *this;
429 }
430
435
439 std::string::size_type size() const
440 {
441 return myChain.size();
442 }
443
448 bool isValid() const { return true; }
449
450 // ------------------------- display --------------------------------
455 void selfDisplay ( std::ostream & out ) const
456 {
458 out << "[FreemanChainCodes]" << std::endl;
459 out << "\t";
460 std::copy( this->begin(), this->end(), std::ostream_iterator<Value>(out, "") );
461 out << std::endl;
462 }
463
470 friend std::ostream& operator <<(std::ostream & out, const CodesRange & object)
471 {
472 object.selfDisplay( out );
473 return out;
474 }
475 // ------------------------- private data --------------------------------
476 private:
480 const std::string myChain;
481 // ------------------------- iterator services --------------------------------
482public:
483
489 return myChain.begin();
490 }
491
497 return myChain.end();
498 }
499
505 return myChain.rbegin();
506 }
507
513 return myChain.rend();
514 }
515
521 return ConstCirculator( this->begin(), this->begin(), this->end() );
522 }
523
528 ConstReverseCirculator rc() const {
529 return ConstReverseCirculator( this->c() );
530 }
531};
532
534// end of class CodesRange
536
541 {
542 return CodesRange(chain);
543 }
544
545
547 // ----------------------- Standard services ------------------------------
548
549 public:
550
555 { }
556
563 FreemanChain( const std::string & s = "", TInteger x = 0, TInteger y = 0 );
564
565
570 FreemanChain( const std::vector<Point>& vectPoints);
571
572
577 FreemanChain(std::istream & in );
578
579
584 FreemanChain( const FreemanChain & other );
585
586
593
594
600 bool operator==( const FreemanChain & other) const
601 {
602 return (chain == other.chain) && ( x0 == other.x0 ) && ( y0 == other.y0 )
603 && ( xn == other.xn ) && ( yn == other.yn );
604 }
605
611 bool operator!=( const FreemanChain & other) const
612 {
613 return !( (*this) == other );
614 }
615
616
623 //unsigned int code( Index pos ) const;
624 char code( Index pos ) const;
625
626
630 Size size() const;
631
632
644
645
655
656
666
667
676 void computeBoundingBox( TInteger & min_x, TInteger& min_y,
677 TInteger& max_x, TInteger& max_y ) const;
678
695 //BK
696 typename Self::ConstIterator
698
718 //BK
719 typename Self::ConstIterator
721
722
729 int isClosed() const ;
730
731
742 int ccwLoops() const ;
743
744
755 Point getPoint ( Index pos ) const;
756
757
762 {
763 return Point(x0,y0);
764 }
765
766
770 Point lastPoint ( ) const
771 {
772 return Point(xn,yn);
773 }
774
775
781 {
782 return lastPoint() - firstPoint();
783 }
784
785
791
797
798
799
800
801
802
803
805 // ----------------------- Iteration services ------------------------------
806
812
813
819
827 Index next( Index pos ) const;
828
829
837 Index previous( Index pos ) const;
838
839
840 // ------------------------- Static services -----------------------
841
842 public:
843
849 static void write( std::ostream & out, const FreemanChain & c )
850 {
851 out << c.x0 << " " << c.y0 << " " << c.chain << std::endl;
852 }
853
854
860 static void read( std::istream & in, FreemanChain & c );
861
869 template<typename TConstIterator>
870 static void readFromPointsRange( const TConstIterator& itBegin, const TConstIterator& itEnd, FreemanChain & c );
871
878 template<typename TRange>
879 static void readFromPointsRange( const TRange& aRange, FreemanChain & c );
880
887 static void getContourPoints(const FreemanChain & fc,
888 std::vector<Point> & aVContour );
889
890
907 const FreemanChain & fc,
908 typename KhalimskySpaceND<2, TInteger>::SCellSet & aSCellContour,
909 bool aFlagForAppend=false);
910
911
912
913
920 //static void movePointFromFC(Point & aPoint, unsigned int aCode );
921 static void movePointFromFC(Point & aPoint, char aCode );
922
923
924 // Deprecated
925 //
926 // /**
927 //  * @param aZero (returns) the '0' or 'x' letter for quadrant [quadrant].
928 //  * @param aOne (returns) the '1' or 'y' letter for quadrant [quadrant].
929 //  * @param aQuadrant the quadrant as any of '0', '1', '2', or '3'.
930 //  */
931 // static void alphabet( char & aZero, char & aOne, char aQuadrant )
932
933
946 //static unsigned int movement( unsigned int aCode1, unsigned int aCode2,
947 // bool ccw = true );
948 static char movement( char aCode1, char aCode2,
949 bool ccw = true );
950
958 static char addToCode( char code, int n);
959
960
970 static short freemanCode4C(int dx, int dy);
971
972
973
981 // static void displacement( int & dx, int & dy, unsigned int aCode );
982 static void displacement( int & dx, int & dy, char aCode );
983
984
985
990 // static Point displacement( unsigned int aCode );
991 static Point displacement( char aCode );
992
993
1003 // static unsigned int turnedCode( unsigned int aCode, bool ccw = true );
1004 static char turnedCode( char aCode, bool ccw = true );
1005
1006
1028 static void pointel2pixel( FreemanChain & aPixChain,
1029 std::vector<unsigned int> & aPl2pix,
1030 std::vector<unsigned int> & aPix2pl,
1031 const FreemanChain & aPlChain )
1032 {
1033 innerContour( aPixChain, aPl2pix, aPix2pl, aPlChain, true );
1034 };
1035
1063 static void innerContour( FreemanChain & aInnerChain,
1064 std::vector<unsigned int> & aOuter2inner,
1065 std::vector<unsigned int> & aInner2outer,
1066 const FreemanChain & aOuterChain,
1067 bool ccw = true );
1068
1069
1092 static bool cleanOuterSpikes( FreemanChain & aCleanC,
1093 std::vector<unsigned int> & aC2clean,
1094 std::vector<unsigned int> & aClean2c,
1095 const FreemanChain & c,
1096 bool ccw = true );
1097
1098
1099
1100
1101 // ----------------------- Interface --------------------------------------
1102
1103 public:
1104
1109 void selfDisplay ( std::ostream & out ) const ;
1110
1111
1116 bool isValid() const ;
1117
1118
1119
1120 // ----------------------- Drawing services --------------------------------
1121
1122 public:
1123
1128 //DrawableWithBoard2D* defaultStyle( std::string mode = "" ) const;
1129
1130
1134 std::string className() const;
1135
1136 // ------------------------- Public Datas ------------------------------
1137
1138 public:
1142 std::string chain;
1143
1148
1153
1158
1163
1164
1165 // ------------------------- Internals ------------------------------------
1166
1167
1168 private:
1169
1174 // FreemanChain() {};
1175
1181
1182 public:
1183
1184
1185 }; // end of class FreemanChain
1186
1187
1188
1189
1196 template<typename TInteger>
1197 std::ostream&
1198 operator<< ( std::ostream & out, const FreemanChain<TInteger> & object );
1199
1200
1201} // namespace DGtal
1202
1203
1205// Includes inline functions/methods.
1206#include "DGtal/geometry/curves/FreemanChain.ih"
1207
1208// //
1210
1211#endif // !defined FreemanChain_h
1212
1213#undef FreemanChain_RECURSES
1214#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
Aim: model of CRange that provides services to (circularly)iterate over the letters of the freeman ch...
Definition: FreemanChain.h:388
friend std::ostream & operator<<(std::ostream &out, const CodesRange &object)
Definition: FreemanChain.h:470
ConstIterator end() const
Definition: FreemanChain.h:496
void selfDisplay(std::ostream &out) const
Definition: FreemanChain.h:455
Circulator< ConstIterator > ConstCirculator
Definition: FreemanChain.h:395
CodesRange & operator=(const CodesRange &other)
Definition: FreemanChain.h:422
CodesRange(const CodesRange &aOther)
Definition: FreemanChain.h:414
ConstReverseIterator rend() const
Definition: FreemanChain.h:512
ConstIterator begin() const
Definition: FreemanChain.h:488
ConstCirculator c() const
Definition: FreemanChain.h:520
ConstReverseCirculator rc() const
Definition: FreemanChain.h:528
std::reverse_iterator< ConstCirculator > ConstReverseCirculator
Definition: FreemanChain.h:396
CodesRange(const std::string &aChain)
Definition: FreemanChain.h:408
std::string::const_reverse_iterator ConstReverseIterator
Definition: FreemanChain.h:394
ConstReverseIterator rbegin() const
Definition: FreemanChain.h:504
std::string::size_type size() const
Definition: FreemanChain.h:439
std::string::const_iterator ConstIterator
Definition: FreemanChain.h:393
ConstIterator(const ConstIterator &aOther)
Definition: FreemanChain.h:198
Index myPos
The current position in the word.
Definition: FreemanChain.h:153
const FreemanChain * myFc
The Freeman chain visited by the iterator.
Definition: FreemanChain.h:150
bool operator<(const ConstIterator &aOther) const
Definition: FreemanChain.h:368
const FreemanChain * getChain() const
Definition: FreemanChain.h:279
ConstIterator(ConstAlias< FreemanChain > aChain, Index n=0)
const Point & operator*() const
Definition: FreemanChain.h:222
ConstIterator & operator=(const ConstIterator &other)
ConstIterator(ConstAlias< FreemanChain > aChain, Index n, const Point &XY)
Definition: FreemanChain.h:190
bool operator==(const ConstIterator &aOther) const
Definition: FreemanChain.h:338
Point myXY
The current coordinates of the iterator.
Definition: FreemanChain.h:156
bool operator!=(const ConstIterator &aOther) const
Definition: FreemanChain.h:353
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
Definition: FreemanChain.h:123
char code(Index pos) const
Size size() const
FreemanChain & operator=(const FreemanChain &other)
unsigned int Size
Definition: FreemanChain.h:125
int ccwLoops() const
Point lastPoint() const
Definition: FreemanChain.h:770
FreemanChain(const std::string &s="", TInteger x=0, TInteger y=0)
static void write(std::ostream &out, const FreemanChain &c)
Definition: FreemanChain.h:849
ConstIterator end() const
Point firstPoint() const
Definition: FreemanChain.h:761
bool isValid() const
bool operator==(const FreemanChain &other) const
Definition: FreemanChain.h:600
void selfDisplay(std::ostream &out) const
static void displacement(int &dx, int &dy, char aCode)
ConstIterator begin() const
CodesRange getCodesRange()
Definition: FreemanChain.h:540
FreemanChain & operator+=(const FreemanChain &other)
static char turnedCode(char aCode, bool ccw=true)
PointVector< 2, Integer > Point
Definition: FreemanChain.h:122
int isClosed() const
static char addToCode(char code, int n)
FreemanChain< Integer > Self
Definition: FreemanChain.h:120
BOOST_CONCEPT_ASSERT((concepts::CInteger< TInteger >))
Index next(Index pos) const
static Point displacement(char aCode)
Vector totalDisplacement() const
Definition: FreemanChain.h:780
unsigned int Index
Definition: FreemanChain.h:126
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
Definition: FreemanChain.h:611
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.
Definition: PointVector.h:593
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)