DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CombinatorialDSS.h
1 
17 #pragma once
18 
36 #if defined(CombinatorialDSS_RECURSES)
37 #error Recursive header files inclusion detected in CombinatorialDSS.h
38 #else // defined(CombinatorialDSS_RECURSES)
39 
40 #define CombinatorialDSS_RECURSES
41 
42 #if !defined CombinatorialDSS_h
43 
44 #define CombinatorialDSS_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"
53 
54 namespace DGtal
55 {
56 
57 
58 
60  // template class CombinatorialDSS
86  template <typename TConstIterator, typename TInteger>
88  {
89 
90  // ----------------------- Types ------------------------------
91  public :
92  //Required type
94  typedef TConstIterator ConstIterator;
95  typedef TInteger Integer;
96 
97  //Required types
101 
102  //2D points and 2D vectors
105 
106 
107  typedef typename iterator_traits<ConstIterator>::value_type Code;
108  typedef int Size;
109  typedef int Index;
110 
111  //The basic steps associate to the codes are given by a function `f: Code -> Vector`
113 
114 
115  private :
122  template < class TIterator, class iterator_type = typename iterator_traits<TIterator>::iterator_category >
124  {
125  public :
127  { }
128  void init( const TIterator & it )
129  {
130  myIter = it;
131  }
132 
133  Code getCode( Index n ) const
134  {
135  return myCodes[ n ];
136  }
137 
139  {
140  while ( n >= myCodes.size() )
141  {
142  myCodes.push_back( *myIter );
143  ++myIter;
144  }
145  return myCodes[ n ];
146  }
147 
148  private :
149  vector<Code> myCodes;
150  TIterator myIter;
151  };
152 
157  template < class TIterator >
158  class CodeHandler< TIterator, bidirectional_iterator_tag >
159  {
160  public :
162  {
163  }
164  void init( const TIterator & it )
165  {
166  myFirst = it;
167  myLast = it;
168  }
169 
170  Code getCode( Index n ) const
171  {
172  return ( n >= 0 ) ? myPosCodes[ n ] : myNegCodes[ 1-n ];
173  }
174 
176  {
177  Code c;
178  if ( n<0 )
179  {
180  unsigned int i = 1-n;
181  while ( i >= myNegCodes.size() )
182  {
183  --myFirst;
184  myNegCodes.push_back( *myFirst );
185  }
186  c = myNegCodes[ n ];
187  }
188  else
189  {
190  unsigned int i = n;
191  while ( i >= myPosCodes.size() )
192  {
193  myPosCodes.push_back( *myLast );
194  ++myLast;
195  }
196  c = myPosCodes[ n ];
197  }
198  return c;
199  }
200 
201  private :
202  vector<Code> myPosCodes;
203  vector<Code> myNegCodes;
204  TIterator myFirst;
205  TIterator myLast;
206  };
207 
211  template < class TIterator>
212  class CodeHandler<TIterator, random_access_iterator_tag >
213  {
214  public :
216  { }
217 
218  void init ( const TIterator & it )
219  {
220  myIter = it;
221  }
222 
223  Code getCode( Index n ) const
224  {
225  return myIter[ n ];
226  }
227 
229  {
230  return myIter[ n ];
231  }
232 
233  private :
234  TIterator myIter;
235  };
236 
237 
238  public :
239 
244  {
245 
246  typedef bidirectional_iterator_tag iterator_category;
247  typedef Point value_type;
249  typedef Point * pointer;
250  typedef Point & reference;
251 
255 
260  {}
261 
268  ConstPointIterator( const CombinatorialDSS * dss, Index ind, Point pt ) :
269  myDSS(dss), i(ind), p(pt)
270  {}
271 
276 
280  bool operator==( const ConstPointIterator other) const
281  {
282  return i == other.i;
283  }
284  bool operator!=( const ConstPointIterator other) const
285  {
286  return i != other.i;
287  }
288 
289  Index operator-( const ConstPointIterator other) const
290  {
291  return i - other.i;
292  }
293 
294 
299  {
300  i = other.i;
301  myDSS = other.myDSS;
302  p = other.p;
303  return *this;
304  }
305 
306  Point operator*() const
307  {
308  return p;
309  }
310 
311  // pre-increment ++i
313  {
314  next();
315  return *this;
316  }
317 
318  // post-increment
320  {
321  ConstPointIterator it = *this;
322  next();
323  return it;
324  }
325 
326 
327  // pre-decrement --i
329  {
330  prev();
331  return *this;
332  }
333 
334  // post-decrement
336  {
337  ConstPointIterator it = *this;
338  prev();
339  return it;
340  }
341 
342  // Move to next position
343  void next()
344  {
345  p += myDSS->myDisplacements( myDSS->getCode( i ) );
346  ++i;
347  }
348 
349  // Move to previous position
350  void prev()
351  {
352  --i;
353  p -= myDSS->myDisplacements( myDSS->getCode( i ) );
354  }
355 
356 
357  // Get a reference to the current Combinatorial DSS
358  const CombinatorialDSS * getDSS() const
359  {
360  return myDSS;
361  }
362 
363 
364  // Get the index of the iterator
365  Index getIndex() const
366  {
367  return i;
368  }
369 
370  };
371 
372 
373 
374 
375  // ----------------------- Standard services ------------------------------
376  public:
377 
382 
387 
388 
401  void init( const ConstIterator & it,
402  const Point & start = Point(0,0),
403  Vector (*displacements) (Code) = defaultMoves );
404 
405 
406 
411  void init(const ConstPointIterator & i);
412 
421  void init(const FreemanChainCode & fc);
422 
431  void init(const typename FreemanChainCode::ConstIterator & it);
432 
434  // * Initialize from an array of codes.
435  // * @param a pointer to an array of codes.
436  // * @param the length of the code array.
437  // * @param the start position in 'theCode'.
438  // * @param the position of the starting point of the first code.
439  // * @param a function that returns the displacement vector of the codes.
440  // */
441  //void init(const Code * theCode, Size codeLength, Index firstLetter, const
442  // Point & startPoint, Vector (*displacement) (Code) );
443 
448  CombinatorialDSS ( const Self & other );
449 
455  CombinatorialDSS & operator= ( const Self & other );
456 
460  Self getSelf( ) const;
461 
462 
463 
468  bool operator==( const Self & other ) const;
469 
476  bool operator!=( const Self & other ) const;
477 
478 
483  Reverse getReverse() const;
484 
485 
491  bool isExtendableForward();
492 
498  bool extendForward();
499 
505  bool extendBackward();
506 
512  bool isExtendableBackward();
513 
521  bool retractForward();
522 
530  bool retractBackward();
531 
536  void setPosition( const Point & p );
537 
538 
543  void translate( const Vector & v );
544 
554  &mu, Integer &omega) const;
555 
563  Integer getA() const;
564 
572  Integer getB() const;
573 
581  Integer getMu() const;
582 
590  Integer getOmega() const;
591 
592 
599  Integer getRemainder(const Point & aPoint) const;
600 
608  void computeLeaningPoints( Point & uf, Point & ul,
609  Point & lf, Point & ll ) const;
610 
611 
618  Point getUf() const;
619 
626  Point getUl() const;
627 
634  Point getLf() const;
635 
642  Point getLl() const;
643 
649  bool isValid() const;
650 
682  ConstIterator it,
683  const OrderedAlphabet & aOA);
684 
685 
686  // ----------------------- Accessors --------------------------------------
687 
688  public:
689 
694  Point getFirstPoint() const;
695 
700  Point getLastPoint() const;
701 
706  ConstPointIterator pointBegin() const;
707 
712  ConstPointIterator pointEnd() const;
713 
717  ConstIterator begin() const;
718 
722  ConstIterator end() const;
723 
724 
725 
726 
727 
728 
729  // ----------------------- Interface --------------------------------------
730  public:
731 
736  void selfDisplay ( std::ostream & out ) const;
737 
738 
739  protected:
740  // ------------------------- Protected Datas ------------------------------
741 
746  //const FreemanChainCode * myFC;
747 
752 
758 
765 
771 
772 
778  unsigned int myNbRepeat;
779 
786 
792 
800 
801 
806 
807 
808 
809  // ------------------------- Private Datas --------------------------------
810  private:
811 
812  // ------------------------- Hidden services ------------------------------
813  public:
814 
815 
820  Code getSmallLetter() const;
821 
826  Code getBigLetter() const;
827 
834  Code getCode(Index pos);
835 
841  Code getCode(Index pos) const;
842 
847  Size mainPatternLength() const;
848 
853  Vector mainPatternVector() const;
854 
860  Size suffixLength() const;
861 
867  Size prefixLength() const;
868 
879  bool isUL ( Index pos ) const;
880 
891  bool nextIsLL ( Index pos ) const;
892 
903  bool previousIsLL ( Index pos ) const;
904 
910  bool isTrivial() const;
911 
917  Vector displacement( Code c ) const;
918 
919 
920 
921 
922  private:
923  // ------------------------- Internals ------------------------------------
924  public :
925 
941  {
942  Integer x = ( c == '0' ) ? 1 : ( ( c == '2' ) ? -1 : 0 ) ;
943  Integer y = ( c == '1' ) ? 1 : ( ( c == '3' ) ? -1 : 0 ) ;
944  return Vector( x, y );
945  }
946 
947 
948 
949  }; // end of class CombinatorialDSS
950 
951 
958  template <typename T1, typename T2>
959  std::ostream&
960  operator<< ( std::ostream & out, const CombinatorialDSS<T1,T2> & object );
961 
962 
963 
964 } // namespace DGtal
965 
966 
968 // Includes inline functions.
969 #include "DGtal/geometry/curves/CombinatorialDSS.ih"
970 // //
972 
973 #endif // !defined CombinatorialDSS_h
974 
975 #undef CombinatorialDSS_RECURSES
976 #endif // else defined(CombinatorialDSS_RECURSES)