DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ArithmeticalDSS.h
1 
17 #pragma once
18 
35 #if defined(ArithmeticalDSS_RECURSES)
36 #error Recursive header files inclusion detected in ArithmeticalDSS.h
37 #else // defined(ArithmeticalDSS_RECURSES)
38 
39 #define ArithmeticalDSS_RECURSES
40 
41 #if !defined ArithmeticalDSS_h
42 
43 #define ArithmeticalDSS_h
44 
46 // Inclusions
47 #include <iostream>
48 #include <list>
49 #include "DGtal/base/Exceptions.h"
50 #include "DGtal/base/Common.h"
51 #include "DGtal/kernel/PointVector.h"
52 #include "DGtal/kernel/CInteger.h"
53 #include "DGtal/base/ReverseIterator.h"
54 #include "DGtal/io/Color.h"
55 
56 #include "DGtal/geometry/curves/SegmentComputerUtils.h"
57 
59 
60 
61 namespace DGtal
62 {
63 
64 
66  // class ArithmeticalDSS
113  template <typename TIterator,
114  typename TInteger = typename IteratorCirculatorTraits<TIterator>::Value::Coordinate,
115  int connectivity = 8>
117  {
118 
119 
120 
122  // generic class for computing the norm of (a,b)
123  // max(a,b) for 8-connectivity
124  // |a|+|b| for 4-connectivity
125 
126  //default (for 8-connectivity)
127  template <typename TInt, int c>
128  struct Tools
129  {
130  static TInt norm(const TInt& a, const TInt& b)
131  {
132  TInt x;
133  if (a>=0) x = a;
134  else x = -a;
135  TInt y;
136  if (b>=0) y = b;
137  else y = -b;
138  return (x>=y)?x:y;
139  }
140  static TInt dualNorm(const TInt& a, const TInt& b)
141  {
142  if (a > 0) {
143  if (b > 0) {
144  return (a+b);
145  } else {
146  return (a-b);
147  }
148  } else {
149  if (b > 0) {
150  return (-a+b);
151  } else {
152  return (-a-b);
153  }
154  }
155  }
156  };
157 
158  //specialisation for 4-connectivity
159  template <typename TInt>
160  struct Tools<TInt,4>
161  {
162  static TInt norm(const TInt& a, const TInt& b)
163  {
164  if (a > 0) {
165  if (b > 0) {
166  return (a+b);
167  } else {
168  return (a-b);
169  }
170  } else {
171  if (b > 0) {
172  return (-a+b);
173  } else {
174  return (-a-b);
175  }
176  }
177  }
178 
179  static TInt dualNorm(const TInt& a, const TInt& b)
180  {
181  TInt x;
182  if (a>=0) x = a;
183  else x = -a;
184  TInt y;
185  if (b>=0) y = b;
186  else y = -b;
187  return (x>=y)?x:y;
188  }
189 
190  };
191 
192 
193  // ----------------------- inner types ------------------------------
194  public:
195 
196  //entier
198  typedef TInteger Integer;
199 
200  //requiered types
201  typedef TIterator ConstIterator;
204 
205  //2D point and 2D vector
208 
209  //Point should be 2D Point
210  //uncomment if CPointVector is written
211  //BOOST_CONCEPT_ASSERT(( CPointVector<Point> ));
212  BOOST_STATIC_ASSERT(( Point::dimension == 2 ));
213 
215 
216 
217  // ----------------------- Standard services ------------------------------
218  public:
219 
220 
225  ArithmeticalDSS();
226 
232  ArithmeticalDSS(const ConstIterator& it);
233 
238  void init(const ConstIterator& it);
239 
240 
245  ArithmeticalDSS ( const Self & other );
246 
252  Self& operator= ( const Self & other );
253 
257  Self getSelf() const;
258 
262  Reverse getReverse() const;
263 
264 
273  bool operator==( const Self & other ) const;
274 
281  bool operator!=( const Self & other ) const;
282 
287 
288  // ----------------------- Interface --------------------------------------
289  public:
290 
302  bool isExtendableForward(const ConstIterator & itf);
303 
309  bool isExtendableForward();
310 
316  bool isExtendableBackward();
317 
331  bool extendForward(const ConstIterator & itf);
332 
346  bool extendBackward(const ConstIterator & itb);
347 
353  bool extendForward();
354 
360  bool extendBackward();
361 
367  bool retractForward();
368 
374  bool retractBackward();
375 
376 
377 
378 
379 
380  // ------------------------- Accessors ------------------------------
384  Integer getA() const;
388  Integer getB() const;
392  Integer getMu() const;
396  Integer getOmega() const;
401  Point getUf() const;
406  Point getUl() const;
411  Point getLf() const;
416  Point getLl() const;
422  Point getBackPoint() const;
428  Point getFrontPoint() const;
433  Point getFirstPoint() const;
438  Point getLastPoint() const;
446  ConstIterator getBack() const;
454  ConstIterator getFront() const;
459  ConstIterator begin() const;
463  ConstIterator end() const;
464 
465 
470  bool isValid() const;
471 
472 
473  // ------------------ Useful tools -----------------------------------
474 
481  Integer getRemainder(const ConstIterator & it) const;
482 
489  Integer getRemainder( const Point& aPoint ) const;
490 
497  Integer getPosition(const ConstIterator & it) const;
498 
505  Integer getPosition( const Point& aPoint ) const;
506 
513  bool isInDSL( const Point& aPoint ) const;
514 
521  bool isInDSL(const ConstIterator & it) const;
522 
528  bool isInDSS( const Point& aPoint ) const;
529 
535  bool isInDSS(const ConstIterator & it) const;
536 
537 
538  // ------------------------- Hidden services ------------------------------
539  private:
540 
552  bool isExtendableForward( const Point & lastPoint,
553  const Vector & lastMove );
554 
571  bool extendForward( const ConstIterator & it,
572  ConstIterator & lastIt,
573  const Vector & lastMove,
574  Point & Uf, Point & Ul,
575  Point & Lf, Point & Ll );
576 
593  bool retractForward( ConstIterator & firstIt,
594  ConstIterator & lastIt,
595  ConstIterator & nextIt,
596  Point & Uf, Point & Ul,
597  Point & Lf, Point & Ll,
598  const Integer& s );
599 
600 
609  bool hasLessThanTwoSteps(const Vector& aStep) const;
610 
611 
619  Vector vectorFrom0ToOmega() const;
620 
621 
622 
623  // ------------------------- Protected Datas ------------------------------
624  protected:
625 
626  //parameters of the DSS
643 
644  //number of upper and lower patterns
653 
654 
658  std::vector<Vector> mySteps;
659 
660  // ------------------------- Private Datas --------------------------------
661 
662  private:
663 
664 
665  // ------------------ Display ------------------------------------------
666 
667  public:
668  //leaning points (here because Display2DFactory, todo: accessors)
685 
686  //Iterators to the first (at the back) and last (at the front)
687  //points of the DSS (here because Display2DFactory, todo: accessors)
696 
703  PointD project( const Point & m ) const;
704 
715  PointD project( const Point & m, double r ) const;
716 
726  PointD project( const Point & m, const Point & p ) const;
727 
728 
732  double projectedSegmentLength() const;
733 
738  void selfDisplay ( std::ostream & out ) ;
739 
740 
741  // ------------------------- Private Datas --------------------------------
742  private:
743 
744 
745 
746  // --------------- CDrawableWithBoard2D realization --------------------
747  public:
748 
754  //DrawableWithBoard2D* defaultStyle( std::string mode = "" ) const;
755 
759  std::string className() const;
760 
761 
762  }; // end of class ArithmeticalDSS
763 
764 
771 template <typename TIterator, typename TInteger, int connectivity>
772 std::ostream&
773 operator<< ( std::ostream & out, ArithmeticalDSS<TIterator,TInteger,connectivity> & object )
774 {
775  object.selfDisplay( out);
776  return out;
777 }
778 
779 
780 } // namespace DGtal
781 
782 
783 
785 // Includes inline functions/methods.
786 #include "DGtal/geometry/curves/ArithmeticalDSS.ih"
787 
788 // //
790 
791 #endif // !defined ArithmeticalDSS_h
792 
793 #undef ArithmeticalDSS_RECURSES
794 #endif // else defined(ArithmeticalDSS_RECURSES)