DGtal  1.4.beta
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 
69 namespace 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 
142  {
143  public:
144 
145  using iterator_category = std::bidirectional_iterator_tag;
146  using value_type = Point;
147  using difference_type = int;
148  using pointer = Point*;
149  using reference = Point;
150 
151  // ------------------------- Private data -----------------------
152 
153  private:
154 
157 
160 
163 
164  // ------------------------- Standard services -----------------------
165  public:
166 
172  : myFc( NULL ), myPos( 0 )
173  { }
174 
183 
184 
197  : myFc( &aChain ), myPos( n ), myXY ( XY )
198  { }
199 
204  ConstIterator( const ConstIterator & aOther )
205  : myFc( aOther.myFc ), myPos( aOther.myPos ), myXY( aOther.myXY )
206  { }
207 
214 
219  { }
220 
221  // ------------------------- iteration services -------------------------
222  public:
223 
224 
228  const Point& operator*() const
229  {
230  return myXY;
231  }
232 
236  const Point& get() const
237  {
238  return myXY;
239  }
240 
246  {
247  this->next();
248  return *this;
249  }
250 
256  {
257  ConstIterator tmp(*this);
258  this->next();
259  return tmp;
260  }
261 
262 
266  void next();
267 
271  void nextInLoop();
272 
276  Index position() const
277  {
278  return myPos;
279  }
280 
281 
285  const FreemanChain * getChain() const
286  {
287  return myFc;
288  }
289 
294  //unsigned int getCode() const
295  char getCode() const
296  {
297  ASSERT( myFc != 0 );
298  return myFc->code( myPos );
299  }
300 
301 
307  {
308  this->previous();
309  return *this;
310  }
311 
317  {
318  ConstIterator tmp(*this);
319  this->previous();
320  return tmp;
321  }
322 
323 
327  void previous();
328 
329 
334 
335 
344  bool operator== ( const ConstIterator & aOther ) const
345  {
346  ASSERT( myFc == aOther.myFc );
347  return myPos == aOther.myPos;
348  }
349 
350 
359  bool operator!= ( const ConstIterator & aOther ) const
360  {
361  ASSERT( myFc == aOther.myFc );
362  return myPos != aOther.myPos;
363  }
364 
374  bool operator< ( const ConstIterator & aOther ) const
375  {
376  ASSERT( myFc == aOther.myFc );
377  return myPos < aOther.myPos;
378  }
379 
380  };
381 
382 
384 // class CodesRange
386 
394 {
395 
396  // ------------------------- inner types --------------------------------
397 public:
398 
399  typedef std::string::const_iterator ConstIterator;
400  typedef std::string::const_reverse_iterator ConstReverseIterator;
402  typedef std::reverse_iterator<ConstCirculator> ConstReverseCirculator;
403 
404  // ------------------------- standard services --------------------------------
405 
410 
414  CodesRange(const std::string& aChain ): myChain(aChain){}
415 
420  CodesRange( const CodesRange & aOther )
421  : myChain( aOther.myChain ){}
422 
428  CodesRange& operator= ( const CodesRange & other )
429  {
430  if ( this != &other )
431  {
432  myChain = other.myChain;
433  }
434  return *this;
435  }
436 
441 
445  std::string::size_type size() const
446  {
447  return myChain.size();
448  }
449 
454  bool isValid() const { return true; }
455 
456  // ------------------------- display --------------------------------
461  void selfDisplay ( std::ostream & out ) const
462  {
464  out << "[FreemanChainCodes]" << std::endl;
465  out << "\t";
466  std::copy( this->begin(), this->end(), std::ostream_iterator<Value>(out, "") );
467  out << std::endl;
468  }
469 
476  friend std::ostream& operator <<(std::ostream & out, const CodesRange & object)
477  {
478  object.selfDisplay( out );
479  return out;
480  }
481  // ------------------------- private data --------------------------------
482  private:
486  const std::string myChain;
487  // ------------------------- iterator services --------------------------------
488 public:
489 
495  return myChain.begin();
496  }
497 
502  ConstIterator end() const {
503  return myChain.end();
504  }
505 
511  return myChain.rbegin();
512  }
513 
519  return myChain.rend();
520  }
521 
526  ConstCirculator c() const {
527  return ConstCirculator( this->begin(), this->begin(), this->end() );
528  }
529 
534  ConstReverseCirculator rc() const {
535  return ConstReverseCirculator( this->c() );
536  }
537 };
538 
540 // end of class CodesRange
542 
547  {
548  return CodesRange(chain);
549  }
550 
551 
553  // ----------------------- Standard services ------------------------------
554 
555  public:
556 
561  { }
562 
569  FreemanChain( const std::string & s = "", TInteger x = 0, TInteger y = 0 );
570 
571 
576  FreemanChain( const std::vector<Point>& vectPoints);
577 
578 
583  FreemanChain(std::istream & in );
584 
585 
590  FreemanChain( const FreemanChain & other );
591 
592 
598  FreemanChain & operator=( const FreemanChain & other );
599 
600 
606  bool operator==( const FreemanChain & other) const
607  {
608  return (chain == other.chain) && ( x0 == other.x0 ) && ( y0 == other.y0 )
609  && ( xn == other.xn ) && ( yn == other.yn );
610  }
611 
617  bool operator!=( const FreemanChain & other) const
618  {
619  return !( (*this) == other );
620  }
621 
622 
629  //unsigned int code( Index pos ) const;
630  char code( Index pos ) const;
631 
632 
636  Size size() const;
637 
638 
649  FreemanChain subChain( Index pos, Size n ) const;
650 
651 
660  FreemanChain operator+(const FreemanChain& other) const;
661 
662 
672 
673 
682  void computeBoundingBox( TInteger & min_x, TInteger& min_y,
683  TInteger& max_x, TInteger& max_y ) const;
684 
701  //BK
702  typename Self::ConstIterator
704 
724  //BK
725  typename Self::ConstIterator
727 
728 
735  int isClosed() const ;
736 
737 
748  int ccwLoops() const ;
749 
750 
761  Point getPoint ( Index pos ) const;
762 
763 
767  Point firstPoint ( ) const
768  {
769  return Point(x0,y0);
770  }
771 
772 
776  Point lastPoint ( ) const
777  {
778  return Point(xn,yn);
779  }
780 
781 
787  {
788  return lastPoint() - firstPoint();
789  }
790 
791 
797 
803 
804 
805 
806 
807 
808 
809 
811  // ----------------------- Iteration services ------------------------------
812 
818 
819 
825 
833  Index next( Index pos ) const;
834 
835 
843  Index previous( Index pos ) const;
844 
845 
846  // ------------------------- Static services -----------------------
847 
848  public:
849 
855  static void write( std::ostream & out, const FreemanChain & c )
856  {
857  out << c.x0 << " " << c.y0 << " " << c.chain << std::endl;
858  }
859 
860 
866  static void read( std::istream & in, FreemanChain & c );
867 
875  template<typename TConstIterator>
876  static void readFromPointsRange( const TConstIterator& itBegin, const TConstIterator& itEnd, FreemanChain & c );
877 
884  template<typename TRange>
885  static void readFromPointsRange( const TRange& aRange, FreemanChain & c );
886 
893  static void getContourPoints(const FreemanChain & fc,
894  std::vector<Point> & aVContour );
895 
896 
913  const FreemanChain & fc,
914  typename KhalimskySpaceND<2, TInteger>::SCellSet & aSCellContour,
915  bool aFlagForAppend=false);
916 
917 
918 
919 
926  //static void movePointFromFC(Point & aPoint, unsigned int aCode );
927  static void movePointFromFC(Point & aPoint, char aCode );
928 
929 
930  // Deprecated
931  //
932  // /**
933  //  * @param aZero (returns) the '0' or 'x' letter for quadrant [quadrant].
934  //  * @param aOne (returns) the '1' or 'y' letter for quadrant [quadrant].
935  //  * @param aQuadrant the quadrant as any of '0', '1', '2', or '3'.
936  //  */
937  // static void alphabet( char & aZero, char & aOne, char aQuadrant )
938 
939 
952  //static unsigned int movement( unsigned int aCode1, unsigned int aCode2,
953  // bool ccw = true );
954  static char movement( char aCode1, char aCode2,
955  bool ccw = true );
956 
964  static char addToCode( char code, int n);
965 
966 
976  static short freemanCode4C(int dx, int dy);
977 
978 
979 
987  // static void displacement( int & dx, int & dy, unsigned int aCode );
988  static void displacement( int & dx, int & dy, char aCode );
989 
990 
991 
996  // static Point displacement( unsigned int aCode );
997  static Point displacement( char aCode );
998 
999 
1009  // static unsigned int turnedCode( unsigned int aCode, bool ccw = true );
1010  static char turnedCode( char aCode, bool ccw = true );
1011 
1012 
1034  static void pointel2pixel( FreemanChain & aPixChain,
1035  std::vector<unsigned int> & aPl2pix,
1036  std::vector<unsigned int> & aPix2pl,
1037  const FreemanChain & aPlChain )
1038  {
1039  innerContour( aPixChain, aPl2pix, aPix2pl, aPlChain, true );
1040  };
1041 
1069  static void innerContour( FreemanChain & aInnerChain,
1070  std::vector<unsigned int> & aOuter2inner,
1071  std::vector<unsigned int> & aInner2outer,
1072  const FreemanChain & aOuterChain,
1073  bool ccw = true );
1074 
1075 
1098  static bool cleanOuterSpikes( FreemanChain & aCleanC,
1099  std::vector<unsigned int> & aC2clean,
1100  std::vector<unsigned int> & aClean2c,
1101  const FreemanChain & c,
1102  bool ccw = true );
1103 
1104 
1105 
1106 
1107  // ----------------------- Interface --------------------------------------
1108 
1109  public:
1110 
1115  void selfDisplay ( std::ostream & out ) const ;
1116 
1117 
1122  bool isValid() const ;
1123 
1124 
1125 
1126  // ----------------------- Drawing services --------------------------------
1127 
1128  public:
1129 
1134  //DrawableWithBoard2D* defaultStyle( std::string mode = "" ) const;
1135 
1136 
1140  std::string className() const;
1141 
1142  // ------------------------- Public Datas ------------------------------
1143 
1144  public:
1148  std::string chain;
1149 
1154 
1159 
1164 
1169 
1170 
1171  // ------------------------- Internals ------------------------------------
1172 
1173 
1174  private:
1175 
1180  // FreemanChain() {};
1181 
1187 
1188  public:
1189 
1190 
1191  }; // end of class FreemanChain
1192 
1193 
1194 
1195 
1202  template<typename TInteger>
1203  std::ostream&
1204  operator<< ( std::ostream & out, const FreemanChain<TInteger> & object );
1205 
1206 
1207 } // namespace DGtal
1208 
1209 
1211 // Includes inline functions/methods.
1212 #include "DGtal/geometry/curves/FreemanChain.ih"
1213 
1214 // //
1216 
1217 #endif // !defined FreemanChain_h
1218 
1219 #undef FreemanChain_RECURSES
1220 #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:394
ConstIterator end() const
Definition: FreemanChain.h:502
void selfDisplay(std::ostream &out) const
Definition: FreemanChain.h:461
Circulator< ConstIterator > ConstCirculator
Definition: FreemanChain.h:401
CodesRange & operator=(const CodesRange &other)
Definition: FreemanChain.h:428
CodesRange(const CodesRange &aOther)
Definition: FreemanChain.h:420
ConstReverseIterator rend() const
Definition: FreemanChain.h:518
ConstIterator begin() const
Definition: FreemanChain.h:494
ConstCirculator c() const
Definition: FreemanChain.h:526
ConstReverseCirculator rc() const
Definition: FreemanChain.h:534
std::reverse_iterator< ConstCirculator > ConstReverseCirculator
Definition: FreemanChain.h:402
CodesRange(const std::string &aChain)
Definition: FreemanChain.h:414
std::string::const_reverse_iterator ConstReverseIterator
Definition: FreemanChain.h:400
ConstReverseIterator rbegin() const
Definition: FreemanChain.h:510
std::string::size_type size() const
Definition: FreemanChain.h:445
std::string::const_iterator ConstIterator
Definition: FreemanChain.h:399
friend std::ostream & operator<<(std::ostream &out, const CodesRange &object)
Definition: FreemanChain.h:476
ConstIterator(const ConstIterator &aOther)
Definition: FreemanChain.h:204
Index myPos
The current position in the word.
Definition: FreemanChain.h:159
ConstIterator & operator=(const ConstIterator &other)
const FreemanChain * myFc
The Freeman chain visited by the iterator.
Definition: FreemanChain.h:156
bool operator<(const ConstIterator &aOther) const
Definition: FreemanChain.h:374
const FreemanChain * getChain() const
Definition: FreemanChain.h:285
const Point & operator*() const
Definition: FreemanChain.h:228
ConstIterator(ConstAlias< FreemanChain > aChain, Index n=0)
ConstIterator(ConstAlias< FreemanChain > aChain, Index n, const Point &XY)
Definition: FreemanChain.h:196
bool operator==(const ConstIterator &aOther) const
Definition: FreemanChain.h:344
Point myXY
The current coordinates of the iterator.
Definition: FreemanChain.h:162
std::bidirectional_iterator_tag iterator_category
Definition: FreemanChain.h:145
bool operator!=(const ConstIterator &aOther) const
Definition: FreemanChain.h:359
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
unsigned int Size
Definition: FreemanChain.h:125
int ccwLoops() const
Point lastPoint() const
Definition: FreemanChain.h:776
FreemanChain(const std::string &s="", TInteger x=0, TInteger y=0)
static void write(std::ostream &out, const FreemanChain &c)
Definition: FreemanChain.h:855
ConstIterator end() const
Point firstPoint() const
Definition: FreemanChain.h:767
bool isValid() const
bool operator==(const FreemanChain &other) const
Definition: FreemanChain.h:606
void selfDisplay(std::ostream &out) const
FreemanChain & operator=(const FreemanChain &other)
static void displacement(int &dx, int &dy, char aCode)
ConstIterator begin() const
CodesRange getCodesRange()
Definition: FreemanChain.h:546
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 >))
FreemanChain & extend(char code)
Index next(Index pos) const
static Point displacement(char aCode)
Vector totalDisplacement() const
Definition: FreemanChain.h:786
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)
FreemanChain & retract(Size n=1)
bool operator!=(const FreemanChain &other) const
Definition: FreemanChain.h:617
Self::ConstIterator findQuadrantChange(OrderedAlphabet &A) const
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
static void movePointFromFC(Point &aPoint, char aCode)
static short freemanCode4C(int dx, int dy)
std::string className() const
Index previous(Index pos) const
FreemanChain & operator+=(const FreemanChain &other)
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 ...
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ATu0v1< TKSpace, TLinearAlgebra > &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)