DGtal 0.3.0

ArithmeticalDSS.h

Go to the documentation of this file.
00001 
00017 #pragma once
00018 
00032 #if defined(ArithmeticalDSS_RECURSES)
00033 #error Recursive header files inclusion detected in ArithmeticalDSS.h
00034 #else // defined(ArithmeticalDSS_RECURSES)
00035 
00036 #define ArithmeticalDSS_RECURSES
00037 
00038 #if !defined ArithmeticalDSS_h
00039 
00040 #define ArithmeticalDSS_h
00041 
00043 // Inclusions
00044 #include <iostream>
00045 #include "DGtal/base/Exceptions.h"
00046 #include "DGtal/base/Common.h"
00047 #include "DGtal/kernel/PointVector.h"
00048 #include "DGtal/kernel/CInteger.h"
00049 #include "DGtal/io/DGtalBoard.h"
00051 
00052 
00053 namespace DGtal
00054 {
00055 
00056 
00058   // class ArithmeticalDSS
00125   template <typename TIterator, typename TInteger, int connectivity>
00126   class ArithmeticalDSS
00127   {
00128 
00129 
00130     // ----------------------- Types ------------------------------
00131   public:
00132 
00133 
00134     typedef TIterator Iterator;
00135 
00136     //entier
00137     BOOST_CONCEPT_ASSERT(( CInteger<TInteger> ) );
00138     typedef TInteger Integer;
00139 
00140     //2D point and 2D vector
00141     typedef DGtal::PointVector<2,Integer> Point;
00142     typedef DGtal::PointVector<2,Integer> Vector;
00143   
00144     typedef DGtal::PointVector<2,double> PointD;
00145   
00146 
00148     // generic class for computing the norm of (a,b)
00149     // max(a,b) for 8-connectivity 
00150     // |a|+|b| for 4-connectivity
00151 
00152     //default (for 8-connectivity) 
00153     template <typename TInt, int c>
00154     struct Tools
00155     {
00156       static TInt norm(const TInt& a, const TInt& b) 
00157       {
00158         TInt x;
00159         if (a>=0) x = a;
00160         else x = -a;
00161         TInt y;
00162         if (b>=0) y = b;
00163         else y = -b;
00164         return (x>=y)?x:y;
00165       }
00166       static TInt dualNorm(const TInt& a, const TInt& b) 
00167       {
00168         if (a > 0) {
00169           if (b > 0) {
00170             return (a+b);
00171           } else {
00172             return (a-b);               
00173           }
00174         } else {
00175           if (b > 0) {
00176             return (-a+b);                                              
00177           } else {
00178             return (-a-b);              
00179           }
00180         }
00181       }
00182     };
00183 
00184     //specialisation for 4-connectivity
00185     template <typename TInt>
00186     struct Tools<TInt,4> 
00187     {
00188       static TInt norm(const TInt& a, const TInt& b) 
00189       {
00190         if (a > 0) {
00191           if (b > 0) {
00192             return (a+b);
00193           } else {
00194             return (a-b);               
00195           }
00196         } else {
00197           if (b > 0) {
00198             return (-a+b);                                              
00199           } else {
00200             return (-a-b);              
00201           }
00202         }
00203       }
00204 
00205       static TInt dualNorm(const TInt& a, const TInt& b) 
00206       {
00207         TInt x;
00208         if (a>=0) x = a;
00209         else x = -a;
00210         TInt y;
00211         if (b>=0) y = b;
00212         else y = -b;
00213         return (x>=y)?x:y;
00214       }
00215 
00216     };
00217 
00218     // ----------------------- Standard services ------------------------------
00219   public:
00220 
00221 
00226     ArithmeticalDSS();
00227 
00232     ArithmeticalDSS(const Iterator& it);
00233 
00238     void init(const Iterator& it);
00239 
00240 
00245     ArithmeticalDSS ( const ArithmeticalDSS & other );
00246 
00252     ArithmeticalDSS & operator= ( const ArithmeticalDSS & other );
00253 
00262     bool operator==( const ArithmeticalDSS & other ) const;
00263 
00270     bool operator!=( const ArithmeticalDSS & other ) const;
00271 
00275     ~ArithmeticalDSS(){};
00276 
00277     // ----------------------- Interface --------------------------------------
00278   public:
00279      
00288     bool isExtendable(const Iterator & itf);
00289 
00300     bool extend(const Iterator & itf);
00301 
00312     bool extendOppositeEnd(const Iterator & itb);
00313 
00321     bool retract();
00322 
00330     bool retractOppositeEnd();
00331 
00332 
00333 
00340     Integer getRemainder(const Iterator & it) const;
00341 
00348     Integer getRemainder( const Point& aPoint ) const;
00349 
00356     bool isInDSL( const Point& aPoint ) const;
00357 
00364     bool isInDSL(const Iterator & it) const;
00365 
00371     bool isInDSS( const Point& aPoint ) const;
00372 
00378     bool isInDSS(const Iterator & it) const;
00379 
00380     // ------------------------- Accessors ------------------------------
00385     Integer getA() const;
00390     Integer getB() const;
00395     Integer getMu() const;
00400     Integer getOmega() const;
00405     Point getUf() const;
00410     Point getUl() const;
00415     Point getLf() const;
00420     Point getLl() const;
00425     Point getBackPoint() const;
00430     Point getFrontPoint() const;
00431 
00436     bool isValid() const;
00437 
00438     // ------------------ Display ------------------------------------------
00439 
00440   public:
00446     PointD project( const Point & m ) const;
00447 
00458     PointD project( const Point & m, double r ) const;
00459     
00471     PointD project( const Point & m, const Point & p ) const;
00472   
00473 
00477     double projectedSegmentLength() const;
00478     
00479 
00484     void selfDisplay ( std::ostream & out ) ;
00485 
00486     
00494     void selfDrawAsDigitalPoints( DGtalBoard & board ) const;
00495     
00496     
00502     void selfDrawAsBoundingBox( DGtalBoard & board ) const;
00503     
00504     
00505     
00506     // ------------------------- Private Datas --------------------------------
00507   private:
00508 
00512     struct DefaultDrawStyleBB : public DrawableWithDGtalBoard
00513     {
00514       virtual void selfDraw(DGtalBoard & aBoard) const
00515       {
00516         // Set board style
00517         aBoard.setLineStyle(DGtalBoard::Shape::SolidStyle);
00518         aBoard.setPenColor(DGtalBoard::Color::Red);
00519         aBoard.setLineWidth(1);
00520         aBoard.setFillColor(DGtalBoard::Color::None);
00521       }
00522     };
00523     
00527     struct DefaultDrawStylePoints : public DrawableWithDGtalBoard
00528     {
00529       virtual void selfDraw(DGtalBoard & aBoard) const
00530       {
00531         // Set board style
00532         aBoard.setLineStyle(DGtalBoard::Shape::SolidStyle);
00533         aBoard.setPenColor(DGtalBoard::Color::Black);
00534         aBoard.setLineWidth(2);
00535         aBoard.setFillColor(DGtalBoard::Color::None);
00536       }
00537     };
00538 
00539     // --------------- CDrawableWithDGtalBoard realization --------------------
00540   public:
00541     
00546     DrawableWithDGtalBoard* defaultStyle( std::string mode = "" ) const;
00547     
00551     std::string styleName() const;
00552 
00553 
00554 
00555 
00556 
00565     void selfDraw(DGtalBoard & board ) const;
00566     
00567     
00568 
00569 
00570 
00571 
00572     // ------------------------- Protected Datas ------------------------------
00573   protected:
00574 
00575     //parameters of the DSS
00576     Integer myA, myB, myMu, myOmega;
00577     //number of upper and lower patterns
00578     Integer myNbUpPat, myNbLowPat; 
00579 
00580     //leaning points
00581     Point myUf, myUl, myLf, myLl;
00582 
00583     //first (at the front) and last (at the back) points of the DSS
00584     Iterator myF, myL;
00585 
00586     //steps of the DSS 
00587     //e.g. right and up in the first octant
00588     std::vector<Vector> step;
00589 
00590     // ------------------------- Private Datas --------------------------------
00591         
00592   private:
00593 
00594     // ------------------------- Hidden services ------------------------------
00595   private:
00596 
00611     bool extend( const Iterator & it, 
00612                  Iterator & lastIt, 
00613                  const Vector & lastMove,
00614                  Point & Uf,    Point & Ul,
00615                  Point & Lf,    Point & Ll );
00616 
00630     bool retract( Iterator & firstIt,
00631                   Iterator & lastIt,
00632                   Iterator & nextIt,              
00633                   Point & Uf,   Point & Ul,
00634                   Point & Lf,   Point & Ll,
00635                   const Integer& s );
00636 
00637 
00646     bool hasLessThanTwoSteps(const Vector& aStep) const;
00647 
00648 
00656     Vector vectorFrom0ToOmega() const;
00657 
00658 
00659 
00660   }; // end of class ArithmeticalDSS
00661 
00662 
00667   struct DrawDSSBoundingBox : public DrawWithBoardModifier {
00668     void selfDraw( DGtalBoard & board ) const
00669     {
00670       board.myModes[ "ArithmeticalDSS" ] = "BoundingBox";
00671     }
00672   };
00673  
00678   struct DrawDSSPoints : public DrawWithBoardModifier {
00679     void selfDraw( DGtalBoard & board ) const
00680     {
00681       board.myModes[ "ArithmeticalDSS" ] = "Points";
00682     }
00683   };
00684 
00691   template <typename TIterator, typename TInteger, int connectivity>
00692   std::ostream&
00693   operator<< ( std::ostream & out,  ArithmeticalDSS<TIterator,TInteger,connectivity> & object )
00694   {
00695     object.selfDisplay( out);
00696     return out;
00697   }
00698 
00699 
00700 } // namespace DGtal
00701 
00702 
00703 
00705 // Includes inline functions/methods if necessary.
00706 #if defined(INLINE)
00707 #include "DGtal/geometry/2d/ArithmeticalDSS.ih"
00708 #endif
00709 
00710 //                                                                           //
00712 
00713 #endif // !defined ArithmeticalDSS_h
00714 
00715 #undef ArithmeticalDSS_RECURSES
00716 #endif // else defined(ArithmeticalDSS_RECURSES)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines