DGtal  0.9.2
OneBalancedWordComputer.h
1 
17 #pragma once
18 
36 #if defined(OneBalancedWordComputer_RECURSES)
37 #error Recursive header files inclusion detected in OneBalancedWordComputer.h
38 #else // defined(OneBalancedWordComputer_RECURSES)
39 
40 #define OneBalancedWordComputer_RECURSES
41 
42 #if !defined OneBalancedWordComputer_h
43 
44 #define OneBalancedWordComputer_h
45 
47 // Inclusions
48 #include <iostream>
49 #include "DGtal/base/Common.h"
50 #include "DGtal/geometry/curves/FreemanChain.h"
51 #include "DGtal/base/OrderedAlphabet.h"
52 #include "DGtal/base/IteratorCirculatorTraits.h"
53 #include "DGtal/kernel/CInteger.h"
54 #include "DGtal/arithmetic/IntegerComputer.h"
55 #include "DGtal/geometry/curves/ArithmeticalDSL.h"
57 
58 namespace DGtal
59 {
60 
61 
62 
64  // template class OneBalancedWordComputer
90  template <typename TConstIterator, typename TInteger>
92  {
93 
94  // ----------------------- Types ------------------------------
95  public :
96  //Required type
98  typedef TConstIterator ConstIterator;
99  typedef TInteger Integer;
100 
101  //Required types
105 
106  //2D points and 2D vectors
109 
111  typedef int Size;
112  typedef int Index;
113 
114  //The basic steps associate to the codes are given by a function `f: Code -> Vector`
116 
117  //DSL
119 
120  private :
128  template < class TIterator, class iterator_type = typename IteratorCirculatorTraits <TIterator>::Category > class CodeHandler
129  {
130  public :
132  { }
133  void init( const TIterator & it )
134  {
135  myIter = it;
136  }
137 
138  Code getCode( Index n ) const
139  {
140  return myCodes[ n ];
141  }
142 
143  Code getCode( Index n )
144  {
145  while ( n >= myCodes.size() )
146  {
147  myCodes.push_back( *myIter );
148  ++myIter;
149  }
150  return myCodes[ n ];
151  }
152 
153  private :
154  std::vector<Code> myCodes;
155  TIterator myIter;
156  };
157 
162  template < class TIterator >
163  class CodeHandler< TIterator, BidirectionalCategory >
164  {
165  public :
167  {
168  }
169  void init( const TIterator & it )
170  {
171  myFirst = it;
172  myLast = it;
173  }
174 
175  Code getCode( Index n ) const
176  {
177  return ( n >= 0 ) ? myPosCodes[ n ] : myNegCodes[ 1-n ];
178  }
179 
180  Code getCode( Index n )
181  {
182  Code c;
183  if ( n<0 )
184  {
185  unsigned int i = 1-n;
186  while ( i >= myNegCodes.size() )
187  {
188  --myFirst;
189  myNegCodes.push_back( *myFirst );
190  }
191  c = myNegCodes[ n ];
192  }
193  else
194  {
195  unsigned int i = n;
196  while ( i >= myPosCodes.size() )
197  {
198  myPosCodes.push_back( *myLast );
199  ++myLast;
200  }
201  c = myPosCodes[ n ];
202  }
203  return c;
204  }
205 
206  private :
207  std::vector<Code> myPosCodes;
208  std::vector<Code> myNegCodes;
209  TIterator myFirst;
210  TIterator myLast;
211  };
212 
216  template < class TIterator>
218  {
219  public :
221  { }
222 
223  void init ( const TIterator & it )
224  {
225  myIter = it;
226  }
227 
228  Code getCode( Index n ) const
229  {
230  return myIter[ n ];
231  }
232 
233  Code getCode ( Index n )
234  {
235  return myIter[ n ];
236  }
237 
238  private :
239  TIterator myIter;
240  };
241 
242 
243  public :
244 
249  {
250 
252  typedef Point value_type;
253  typedef Index difference_type;
254  typedef Point * pointer;
255  typedef Point & reference;
256 
258  Index i;
259  Point p;
260 
265  {}
266 
273  ConstPointIterator( const OneBalancedWordComputer * dss, Index ind, Point pt ) :
274  myDSS(dss), i(ind), p(pt)
275  {}
276 
281 
285  bool operator==( const ConstPointIterator other) const
286  {
287  return i == other.i;
288  }
289  bool operator!=( const ConstPointIterator other) const
290  {
291  return i != other.i;
292  }
293 
294  Index operator-( const ConstPointIterator other) const
295  {
296  return i - other.i;
297  }
298 
299 
304  {
305  i = other.i;
306  myDSS = other.myDSS;
307  p = other.p;
308  return *this;
309  }
310 
311  Point operator*() const
312  {
313  return p;
314  }
315 
316  // pre-increment ++i
318  {
319  next();
320  return *this;
321  }
322 
323  // post-increment
325  {
326  ConstPointIterator it = *this;
327  next();
328  return it;
329  }
330 
331 
332  // pre-decrement --i
334  {
335  prev();
336  return *this;
337  }
338 
339  // post-decrement
341  {
342  ConstPointIterator it = *this;
343  prev();
344  return it;
345  }
346 
347  // Move to next position
348  void next()
349  {
350  p += myDSS->myDisplacements( myDSS->getCode( i ) );
351  ++i;
352  }
353 
354  // Move to previous position
355  void prev()
356  {
357  --i;
358  p -= myDSS->myDisplacements( myDSS->getCode( i ) );
359  }
360 
361 
362  // Get a reference to the current Combinatorial DSS
364  {
365  return myDSS;
366  }
367 
368 
369  // Get the index of the iterator
370  Index getIndex() const
371  {
372  return i;
373  }
374 
375  };
376 
377 
378 
379 
380  // ----------------------- Standard services ------------------------------
381  public:
382 
387 
392 
393 
406  void init( const ConstIterator & it,
407  const Point & start = Point(0,0),
408  Vector (*displacements) (Code) = defaultMoves );
409 
410 
411 
416  void init(const ConstPointIterator & i);
417 
426  void init(const FreemanChainCode & fc);
427 
436  void init(const typename FreemanChainCode::ConstIterator & it);
437 
442  OneBalancedWordComputer ( const Self & other );
443 
449  OneBalancedWordComputer & operator= ( const Self & other );
450 
454  Self getSelf( ) const;
455 
456 
457 
462  bool operator==( const Self & other ) const;
463 
470  bool operator!=( const Self & other ) const;
471 
472 
476  Reverse getReverse() const;
477 
478 
484  bool isExtendableFront();
485 
491  bool extendFront();
492 
498  bool extendBack();
499 
505  bool isExtendableBack();
506 
514  bool retractBack();
515 
523  bool retractFront();
524 
529  void setPosition( const Point & p );
530 
531 
536  void translate( const Vector & v );
537 
545  DSL getArithmeticalDescription() const;
546 
554  Integer getA() const;
555 
563  Integer getB() const;
564 
572  Integer getMu() const;
573 
581  Integer getOmega() const;
582 
583 
590  Integer remainder(const Point & aPoint) const;
591 
599  void computeLeaningPoints( Point & uf, Point & ul,
600  Point & lf, Point & ll ) const;
601 
602 
609  Point Uf() const;
610 
617  Point Ul() const;
618 
625  Point Lf() const;
626 
633  Point Ll() const;
634 
640  bool isValid() const;
641 
666  ConstIterator it,
667  const OrderedAlphabet & aOA);
668 
669 
670  // ----------------------- Accessors --------------------------------------
671 
672  public:
673 
678  Point back() const;
679 
684  Point front() const;
685 
690  ConstPointIterator pointBegin() const;
691 
696  ConstPointIterator pointEnd() const;
697 
701  ConstIterator begin() const;
702 
706  ConstIterator end() const;
707 
708 
709 
710 
711 
712 
713  // ----------------------- Interface --------------------------------------
714  public:
715 
720  void selfDisplay ( std::ostream & out ) const;
721 
722 
723  protected:
724  // ------------------------- Protected Datas ------------------------------
725 
730 
734  ConstIterator myBegin;
735  ConstIterator myEnd;
736 
742  Point myLastPoint;
743 
749 
750 
756  unsigned int myNbRepeat;
757 
764 
770 
777  Index myNextAfter;
778 
779 
784 
785 
786 
787  // ------------------------- Private Datas --------------------------------
788  private:
789 
790  // ------------------------- Hidden services ------------------------------
791  public:
792 
793 
798  Code getSmallLetter() const;
799 
804  Code getBigLetter() const;
805 
812  Code getCode(Index pos);
813 
819  Code getCode(Index pos) const;
820 
825  Size mainPatternLength() const;
826 
831  Vector mainPatternVector() const;
832 
838  Size suffixLength() const;
839 
845  Size prefixLength() const;
846 
857  bool isUL ( Index pos ) const;
858 
869  bool nextIsLL ( Index pos ) const;
870 
881  bool previousIsLL ( Index pos ) const;
882 
888  bool isTrivial() const;
889 
895  Vector displacement( Code c ) const;
896 
897 
898 
899 
900  private:
901  // ------------------------- Internals ------------------------------------
902  public :
903 
919  static Vector defaultMoves( Code c )
920  {
921  Integer x = ( c == '0' ) ? 1 : ( ( c == '2' ) ? -1 : 0 ) ;
922  Integer y = ( c == '1' ) ? 1 : ( ( c == '3' ) ? -1 : 0 ) ;
923  return Vector( x, y );
924  }
925 
926 
927 
928  }; // end of class OneBalancedWordComputer
929 
936  template < typename TConstIterator, typename TInteger >
937  std::ostream&
938  operator<< ( std::ostream & out,
939  const OneBalancedWordComputer<TConstIterator, TInteger> & object );
940 
941 
942 } // namespace DGtal
943 
944 
946 // Includes inline functions.
947 #include "DGtal/geometry/curves/OneBalancedWordComputer.ih"
948 // //
950 
951 #endif // !defined OneBalancedWordComputer_h
952 
953 #undef OneBalancedWordComputer_RECURSES
954 #endif // else defined(OneBalancedWordComputer_RECURSES)
Vector displacement(Code c) const
DSL getArithmeticalDescription() const
Vector mainPatternVector() const
bool operator==(const Self &other) const
bool nextIsLL(Index pos) const
FreemanChain< TInteger > FreemanChainCode
bool operator!=(const ConstPointIterator other) const
bool operator!=(const Self &other) const
Aim: Concept checking for Integer Numbers. More precisely, this concept is a refinement of both CEucl...
Definition: CInteger.h:87
ConstPointIterator(const OneBalancedWordComputer *dss, Index ind, Point pt)
IteratorCirculatorTraits< ConstIterator >::Value Code
ArithmeticalDSL< TInteger, TInteger, 4 > DSL
bool isUL(Index pos) const
const OneBalancedWordComputer * getDSS() const
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
ConstPointIterator pointEnd() const
void computeLeaningPoints(Point &uf, Point &ul, Point &lf, Point &ll) const
ConstPointIterator pointBegin() const
OneBalancedWordComputer & operator=(const Self &other)
BOOST_CONCEPT_ASSERT((concepts::CInteger< TInteger >))
void selfDisplay(std::ostream &out) const
OneBalancedWordComputer< ConstIterator, Integer > Reverse
DGtal is the top-level namespace which contains all DGtal functions and types.
bool previousIsLL(Index pos) const
ConstPointIterator & operator=(const ConstPointIterator &other)
bool longestChristoffelPrefix(ConstIterator it, const OrderedAlphabet &aOA)
Index operator-(const ConstPointIterator other) const
void setPosition(const Point &p)
CodeHandler< ConstIterator > myCodeHandler
Integer remainder(const Point &aPoint) const
Aim: This class represents a naive (resp. standard) digital straight line (DSL), ie. the set of digital points such that with , and (resp. ). Note that any DSL such that (resp. ) is simply 8-connected (resp. 4-connected).
bool operator==(const ConstPointIterator other) const
Aim: Describes an alphabet over an interval of (ascii) letters, where the lexicographic order can be ...
OneBalancedWordComputer< ConstIterator, Integer > Self
ConstIterator end() const
ConstIterator begin() const
DGtal::PointVector< 2, Integer > Vector
DGtal::PointVector< 2, Integer > Point
void translate(const Vector &v)
void init(const ConstIterator &it, const Point &start=Point(0, 0), Vector(*displacements)(Code)=defaultMoves)