DGtal  0.9.2
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)
42 
43 #define FreemanChain_RECURSES
44 
45 #if !defined FreemanChain_h
46 
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 //#include "DGtal/io/boards/Board2D.h"
61 #include "DGtal/base/CConstSinglePassRange.h"
62 #include "DGtal/topology/KhalimskySpaceND.h"
63 #include "DGtal/base/ConstAlias.h"
64 
66 
67 
68 namespace DGtal
69 {
70 
72  // class FreemanChain
74 
111  template <typename TInteger>
113  {
114 
115  public :
116 
118  typedef TInteger Integer;
120 
123 
124  typedef unsigned int Size;
125  typedef unsigned int Index;
126 
127  // ------------------------- iterator ------------------------------
128  public:
129 
131  // class FreemanChain::ConstIterator
133 
134 
140  class ConstIterator : public
141  std::iterator<std::bidirectional_iterator_tag, Point, int, Point*, Point>
142  {
143 
144  // ------------------------- Private data -----------------------
145 
146  private:
147 
150 
152  Index myPos;
153 
155  Point myXY;
156 
157  // ------------------------- Standard services -----------------------
158  public:
159 
165  : myFc( NULL ), myPos( 0 )
166  { }
167 
175  ConstIterator( ConstAlias<FreemanChain> aChain, Index n =0);
176 
177 
189  ConstIterator( ConstAlias<FreemanChain> aChain, Index n, const Point & XY)
190  : myFc( &aChain ), myPos( n ), myXY ( XY )
191  { }
192 
197  ConstIterator( const ConstIterator & aOther )
198  : myFc( aOther.myFc ), myPos( aOther.myPos ), myXY( aOther.myXY )
199  { }
200 
206  ConstIterator& operator= ( const ConstIterator & other );
207 
212  { }
213 
214  // ------------------------- iteration services -------------------------
215  public:
216 
217 
221  const Point& operator*() const
222  {
223  return myXY;
224  }
225 
229  const Point& get() const
230  {
231  return myXY;
232  }
233 
239  {
240  this->next();
241  return *this;
242  }
243 
249  {
250  ConstIterator tmp(*this);
251  this->next();
252  return tmp;
253  }
254 
255 
259  void next();
260 
264  void nextInLoop();
265 
269  Index position() const
270  {
271  return myPos;
272  }
273 
274 
278  const FreemanChain * getChain() const
279  {
280  return myFc;
281  }
282 
287  //unsigned int getCode() const
288  char getCode() const
289  {
290  ASSERT( myFc != 0 );
291  return myFc->code( myPos );
292  }
293 
294 
300  {
301  this->previous();
302  return *this;
303  }
304 
310  {
311  ConstIterator tmp(*this);
312  this->previous();
313  return tmp;
314  }
315 
316 
320  void previous();
321 
322 
326  void previousInLoop();
327 
328 
337  bool operator== ( const ConstIterator & aOther ) const
338  {
339  ASSERT( myFc == aOther.myFc );
340  return myPos == aOther.myPos;
341  }
342 
343 
352  bool operator!= ( const ConstIterator & aOther ) const
353  {
354  ASSERT( myFc == aOther.myFc );
355  return myPos != aOther.myPos;
356  }
357 
367  bool operator< ( const ConstIterator & aOther ) const
368  {
369  ASSERT( myFc == aOther.myFc );
370  return myPos < aOther.myPos;
371  }
372 
373  };
374 
375 
377 // class CodesRange
379 
387 {
388 
389  // ------------------------- inner types --------------------------------
390 public:
391 
392  typedef std::string::const_iterator ConstIterator;
393  typedef std::string::const_reverse_iterator ConstReverseIterator;
395  typedef std::reverse_iterator<ConstCirculator> ConstReverseCirculator;
396 
397  // ------------------------- standard services --------------------------------
398 
403 
407  CodesRange(const std::string& aChain ): myChain(aChain){}
408 
413  CodesRange( const CodesRange & aOther )
414  : myChain( aOther.myChain ){}
415 
421  CodesRange& operator= ( const CodesRange & other )
422  {
423  if ( this != &other )
424  {
425  myChain = other.myChain;
426  }
427  return *this;
428  }
429 
434 
438  std::string::size_type size() const
439  {
440  return myChain.size();
441  }
442 
447  bool isValid() const { return true; }
448 
449  // ------------------------- display --------------------------------
454  void selfDisplay ( std::ostream & out ) const
455  {
456  typedef typename IteratorCirculatorTraits<ConstIterator>::Value Value;
457  out << "[FreemanChainCodes]" << std::endl;
458  out << "\t";
459  std::copy( this->begin(), this->end(), std::ostream_iterator<Value>(out, "") );
460  out << std::endl;
461  }
462 
469  friend std::ostream& operator <<(std::ostream & out, const CodesRange & object)
470  {
471  object.selfDisplay( out );
472  return out;
473  }
474  // ------------------------- private data --------------------------------
475  private:
479  const std::string myChain;
480  // ------------------------- iterator services --------------------------------
481 public:
482 
487  ConstIterator begin() const {
488  return myChain.begin();
489  }
490 
495  ConstIterator end() const {
496  return myChain.end();
497  }
498 
503  ConstReverseIterator rbegin() const {
504  return myChain.rbegin();
505  }
506 
511  ConstReverseIterator rend() const {
512  return myChain.rend();
513  }
514 
519  ConstCirculator c() const {
520  return ConstCirculator( this->begin(), this->begin(), this->end() );
521  }
522 
527  ConstReverseCirculator rc() const {
528  return ConstReverseCirculator( this->c() );
529  }
530 };
531 
533 // end of class CodesRange
535 
539  CodesRange getCodesRange()
540  {
541  return CodesRange(chain);
542  }
543 
544 
546  // ----------------------- Standard services ------------------------------
547 
548  public:
549 
554  { }
555 
562  FreemanChain( const std::string & s = "", TInteger x = 0, TInteger y = 0 );
563 
564 
569  FreemanChain( const std::vector<Point>& vectPoints);
570 
571 
576  FreemanChain(std::istream & in );
577 
578 
583  FreemanChain( const FreemanChain & other );
584 
585 
591  FreemanChain & operator=( const FreemanChain & other );
592 
593 
599  bool operator==( const FreemanChain & other) const
600  {
601  return (chain == other.chain) && ( x0 == other.x0 ) && ( y0 == other.y0 )
602  && ( xn == other.xn ) && ( yn == other.yn );
603  }
604 
610  bool operator!=( const FreemanChain & other) const
611  {
612  return !( (*this) == other );
613  }
614 
615 
622  //unsigned int code( Index pos ) const;
623  char code( Index pos ) const;
624 
625 
629  Size size() const;
630 
631 
642  FreemanChain subChain( Index pos, Size n ) const;
643 
644 
653  FreemanChain operator+(const FreemanChain& other) const;
654 
655 
664  FreemanChain& operator+=(const FreemanChain& other);
665 
666 
675  void computeBoundingBox( TInteger & min_x, TInteger& min_y,
676  TInteger& max_x, TInteger& max_y ) const;
677 
694  //BK
695  typename Self::ConstIterator
696  findQuadrantChange( OrderedAlphabet & A ) const;
697 
717  //BK
718  typename Self::ConstIterator
719  findQuadrantChange4( OrderedAlphabet & A ) const ;
720 
721 
728  int isClosed() const ;
729 
730 
741  int ccwLoops() const ;
742 
743 
754  Point getPoint ( Index pos ) const;
755 
756 
760  Point firstPoint ( ) const
761  {
762  return Point(x0,y0);
763  }
764 
765 
769  Point lastPoint ( ) const
770  {
771  return Point(xn,yn);
772  }
773 
774 
779  Vector totalDisplacement() const
780  {
781  return lastPoint() - firstPoint();
782  }
783 
784 
789  FreemanChain & extend(char code);
790 
795  FreemanChain & retract(Size n = 1);
796 
797 
798 
799 
800 
801 
802 
804  // ----------------------- Iteration services ------------------------------
805 
810  ConstIterator begin() const;
811 
812 
817  ConstIterator end() const;
818 
826  Index next( Index pos ) const;
827 
828 
836  Index previous( Index pos ) const;
837 
838 
839  // ------------------------- Static services -----------------------
840 
841  public:
842 
848  static void write( std::ostream & out, const FreemanChain & c )
849  {
850  out << c.x0 << " " << c.y0 << " " << c.chain << std::endl;
851  }
852 
853 
859  static void read( std::istream & in, FreemanChain & c );
860 
868  template<typename TConstIterator>
869  static void readFromPointsRange( const TConstIterator& itBegin, const TConstIterator& itEnd, FreemanChain & c );
870 
877  template<typename TRange>
878  static void readFromPointsRange( const TRange& aRange, FreemanChain & c );
879 
886  static void getContourPoints(const FreemanChain & fc,
887  std::vector<Point> & aVContour );
888 
889 
905  static void getInterPixelLinels(const KhalimskySpaceND<2, TInteger> & aKSpace,
906  const FreemanChain & fc,
907  typename KhalimskySpaceND<2, TInteger>::SCellSet & aSCellContour,
908  bool aFlagForAppend=false);
909 
910 
911 
912 
919  //static void movePointFromFC(Point & aPoint, unsigned int aCode );
920  static void movePointFromFC(Point & aPoint, char aCode );
921 
922 
923  // Deprecated
924  //
925  // /**
926  //  * @param aZero (returns) the '0' or 'x' letter for quadrant [quadrant].
927  //  * @param aOne (returns) the '1' or 'y' letter for quadrant [quadrant].
928  //  * @param aQuadrant the quadrant as any of '0', '1', '2', or '3'.
929  //  */
930  // static void alphabet( char & aZero, char & aOne, char aQuadrant )
931 
932 
945  //static unsigned int movement( unsigned int aCode1, unsigned int aCode2,
946  // bool ccw = true );
947  static char movement( char aCode1, char aCode2,
948  bool ccw = true );
949 
957  static char addToCode( char code, int n);
958 
959 
960 
968  // static void displacement( int & dx, int & dy, unsigned int aCode );
969  static void displacement( int & dx, int & dy, char aCode );
970 
971 
976  // static Point displacement( unsigned int aCode );
977  static Point displacement( char aCode );
978 
979 
989  // static unsigned int turnedCode( unsigned int aCode, bool ccw = true );
990  static char turnedCode( char aCode, bool ccw = true );
991 
992 
1014  static void pointel2pixel( FreemanChain & aPixChain,
1015  std::vector<unsigned int> & aPl2pix,
1016  std::vector<unsigned int> & aPix2pl,
1017  const FreemanChain & aPlChain )
1018  {
1019  innerContour( aPixChain, aPl2pix, aPix2pl, aPlChain, true );
1020  };
1021 
1049  static void innerContour( FreemanChain & aInnerChain,
1050  std::vector<unsigned int> & aOuter2inner,
1051  std::vector<unsigned int> & aInner2outer,
1052  const FreemanChain & aOuterChain,
1053  bool ccw = true );
1054 
1055 
1078  static bool cleanOuterSpikes( FreemanChain & aCleanC,
1079  std::vector<unsigned int> & aC2clean,
1080  std::vector<unsigned int> & aClean2c,
1081  const FreemanChain & c,
1082  bool ccw = true );
1083 
1084 
1085 
1086 
1087  // ----------------------- Interface --------------------------------------
1088 
1089  public:
1090 
1095  void selfDisplay ( std::ostream & out ) const ;
1096 
1097 
1102  bool isValid() const ;
1103 
1104 
1105 
1106  // ----------------------- Drawing services --------------------------------
1107 
1108  public:
1109 
1114  //DrawableWithBoard2D* defaultStyle( std::string mode = "" ) const;
1115 
1116 
1120  std::string className() const;
1121 
1122  // ------------------------- Public Datas ------------------------------
1123 
1124  public:
1128  std::string chain;
1129 
1133  Integer x0;
1134 
1138  Integer y0;
1139 
1143  Integer xn;
1144 
1148  Integer yn;
1149 
1150 
1151  // ------------------------- Internals ------------------------------------
1152 
1153 
1154  private:
1155 
1160  // FreemanChain() {};
1161 
1166  void computeLastPoint();
1167 
1168  public:
1169 
1170 
1171  }; // end of class FreemanChain
1172 
1173 
1174 
1175 
1182  template<typename TInteger>
1183  std::ostream&
1184  operator<< ( std::ostream & out, const FreemanChain<TInteger> & object );
1185 
1186 
1187 } // namespace DGtal
1188 
1189 
1191 // Includes inline functions/methods.
1192 #include "DGtal/geometry/curves/FreemanChain.ih"
1193 
1194 // //
1196 
1197 #endif // !defined FreemanChain_h
1198 
1199 #undef FreemanChain_RECURSES
1200 #endif // else defined(FreemanChain_RECURSES)
Point getPoint(Index pos) const
ConstIterator(const ConstIterator &aOther)
Definition: FreemanChain.h:197
Aim: model of CRange that provides services to (circularly)iterate over the letters of the freeman ch...
Definition: FreemanChain.h:386
static void pointel2pixel(FreemanChain &aPixChain, std::vector< unsigned int > &aPl2pix, std::vector< unsigned int > &aPix2pl, const FreemanChain &aPlChain)
std::set< SCell > SCellSet
Preferred type for defining a set of SCell(s).
FreemanChain & extend(char code)
Self::ConstIterator findQuadrantChange4(OrderedAlphabet &A) const
int ccwLoops() const
Point lastPoint() const
Definition: FreemanChain.h:769
bool operator==(const FreemanChain &other) const
Definition: FreemanChain.h:599
FreemanChain subChain(Index pos, Size n) const
char code(Index pos) const
static bool cleanOuterSpikes(FreemanChain &aCleanC, std::vector< unsigned int > &aC2clean, std::vector< unsigned int > &aClean2c, const FreemanChain &c, bool ccw=true)
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: ConstAlias.h:186
ConstReverseIterator rend() const
Definition: FreemanChain.h:511
static char addToCode(char code, int n)
FreemanChain & operator=(const FreemanChain &other)
FreemanChain operator+(const FreemanChain &other) const
static void movePointFromFC(Point &aPoint, char aCode)
BOOST_CONCEPT_ASSERT((concepts::CInteger< TInteger >))
static void write(std::ostream &out, const FreemanChain &c)
Definition: FreemanChain.h:848
unsigned int Index
Definition: FreemanChain.h:125
static void getInterPixelLinels(const KhalimskySpaceND< 2, TInteger > &aKSpace, const FreemanChain &fc, typename KhalimskySpaceND< 2, TInteger >::SCellSet &aSCellContour, bool aFlagForAppend=false)
ConstIterator begin() const
std::string::const_iterator ConstIterator
Definition: FreemanChain.h:392
std::string::size_type size() const
Definition: FreemanChain.h:438
void selfDisplay(std::ostream &out) const
Definition: FreemanChain.h:454
ConstIterator end() const
Definition: FreemanChain.h:495
void selfDisplay(std::ostream &out) const
void computeBoundingBox(TInteger &min_x, TInteger &min_y, TInteger &max_x, TInteger &max_y) const
Aim: Concept checking for Integer Numbers. More precisely, this concept is a refinement of both CEucl...
Definition: CInteger.h:87
ConstIterator end() const
CodesRange(const CodesRange &aOther)
Definition: FreemanChain.h:413
ConstIterator & operator=(const ConstIterator &other)
const FreemanChain * myFc
The Freeman chain visited by the iterator.
Definition: FreemanChain.h:149
static void read(std::istream &in, FreemanChain &c)
FreemanChain & operator+=(const FreemanChain &other)
static char turnedCode(char aCode, bool ccw=true)
bool operator!=(const ConstIterator &aOther) const
Definition: FreemanChain.h:352
static void innerContour(FreemanChain &aInnerChain, std::vector< unsigned int > &aOuter2inner, std::vector< unsigned int > &aInner2outer, const FreemanChain &aOuterChain, bool ccw=true)
std::string::const_reverse_iterator ConstReverseIterator
Definition: FreemanChain.h:393
static void displacement(int &dx, int &dy, char aCode)
PointVector< 2, Integer > Vector
Definition: FreemanChain.h:122
ConstIterator(ConstAlias< FreemanChain > aChain, Index n, const Point &XY)
Definition: FreemanChain.h:189
Self::ConstIterator findQuadrantChange(OrderedAlphabet &A) const
ConstReverseIterator rbegin() const
Definition: FreemanChain.h:503
int isClosed() const
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:85
Point myXY
The current coordinates of the iterator.
Definition: FreemanChain.h:155
DGtal is the top-level namespace which contains all DGtal functions and types.
ConstIterator begin() const
Definition: FreemanChain.h:487
Point firstPoint() const
Definition: FreemanChain.h:760
friend std::ostream & operator<<(std::ostream &out, const CodesRange &object)
Definition: FreemanChain.h:469
ConstReverseCirculator rc() const
Definition: FreemanChain.h:527
PointVector< 2, Integer > Point
Definition: FreemanChain.h:121
std::reverse_iterator< ConstCirculator > ConstReverseCirculator
Definition: FreemanChain.h:395
Index previous(Index pos) const
Index myPos
The current position in the word.
Definition: FreemanChain.h:152
CodesRange getCodesRange()
Definition: FreemanChain.h:539
Index next(Index pos) const
static void readFromPointsRange(const TConstIterator &itBegin, const TConstIterator &itEnd, FreemanChain &c)
Vector totalDisplacement() const
Definition: FreemanChain.h:779
bool isValid() const
ConstCirculator c() const
Definition: FreemanChain.h:519
FreemanChain< Integer > Self
Definition: FreemanChain.h:119
bool operator<(const ConstIterator &aOther) const
Definition: FreemanChain.h:367
Circulator< ConstIterator > ConstCirculator
Definition: FreemanChain.h:394
FreemanChain & retract(Size n=1)
bool operator!=(const FreemanChain &other) const
Definition: FreemanChain.h:610
Aim: Describes an alphabet over an interval of (ascii) letters, where the lexicographic order can be ...
FreemanChain(const std::string &s="", TInteger x=0, TInteger y=0)
CodesRange(const std::string &aChain)
Definition: FreemanChain.h:407
bool operator==(const ConstIterator &aOther) const
Definition: FreemanChain.h:337
const FreemanChain * getChain() const
Definition: FreemanChain.h:278
std::string className() const
static void getContourPoints(const FreemanChain &fc, std::vector< Point > &aVContour)
unsigned int Size
Definition: FreemanChain.h:124
const Point & operator*() const
Definition: FreemanChain.h:221
Size size() const
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
static char movement(char aCode1, char aCode2, bool ccw=true)
CodesRange & operator=(const CodesRange &other)
Definition: FreemanChain.h:421