DGtal 0.3.0

Board/Shapes.h

Go to the documentation of this file.
00001 /* -*- mode: c++ -*- */
00009 /*
00010  * \@copyright This File is part of the Board library which is
00011  * licensed under the terms of the GNU Lesser General Public Licence.
00012  * See the LICENCE file for further details.
00013  */
00014 #ifndef _BOARD_SHAPES_H_
00015 #define _BOARD_SHAPES_H_
00016 
00017 #include "Board/Point.h"
00018 #include "Board/Rect.h"
00019 #include "Board/Path.h"
00020 #include "Board/Color.h"
00021 #include "Board/Transforms.h"
00022 #include "Board/PSFonts.h"
00023 #include "Board/Tools.h"
00024 #include <string>
00025 #include <vector>
00026 #include <iostream>
00027 #include <map>
00028 #include <cmath>
00029 
00030 #ifndef M_PI
00031 #define M_PI           3.14159265358979323846
00032 #endif
00033 
00034 #ifndef M_PI_2
00035 #define M_PI_2         1.57079632679489661923
00036 #endif
00037 
00038 namespace LibBoard {
00039 
00044 struct Shape {
00045 
00046   enum LineCap { ButtCap = 0, RoundCap, SquareCap };
00047   enum LineJoin { MiterJoin = 0, RoundJoin, BevelJoin };
00048   enum LineStyle { SolidStyle = 0,
00049                    DashStyle, 
00050                    DotStyle,
00051                    DashDotStyle,
00052                    DashDotDotStyle,
00053                    DashDotDotDotStyle };
00054 
00063   inline Shape( Color penColor, Color fillColor,
00064                 double lineWidth, 
00065                 LineStyle style,
00066                 const LineCap cap,
00067                 const LineJoin join,
00068                 int depth );
00069   
00073   virtual ~Shape() { }
00074 
00080   virtual const std::string & name() const;
00081 
00087   virtual Shape * clone() const = 0;
00088   
00094   inline bool filled() const { return _fillColor != Color::None; }
00095   
00101   virtual Point center() const = 0;
00102   
00111   virtual Shape & rotate( double angle, const Point & center ) = 0;
00112 
00120   virtual Shape & rotate( double angle ) = 0;
00121 
00130   inline Shape & rotateDeg( double angle, const Point & center );
00131   
00140   inline Shape & rotateDeg( double angle );
00141 
00150   virtual Shape & translate( double dx, double dy ) = 0;
00151 
00160   virtual Shape & scale( double sx, double sy ) = 0;
00161 
00169   virtual Shape & scale( double s ) = 0;
00170  
00176   virtual Rect boundingBox() const = 0;
00177 
00178   
00183   inline Rect bbox();
00184   
00185 
00191   inline Shape & operator--();
00192 
00198   inline Shape & operator++();
00199 
00200 
00207   virtual void scaleAll( double s ) = 0;
00208 
00209 
00217   virtual void flushPostscript( std::ostream & stream,
00218                                 const TransformEPS & transform ) const = 0;
00219 
00227   virtual void flushFIG( std::ostream & stream,
00228                          const TransformFIG & transform,
00229                          std::map<Color,int> & colormap ) const = 0;
00230 
00238   virtual void flushSVG( std::ostream & stream,
00239                          const TransformSVG & transform ) const = 0;
00240 
00241   inline int depth() const;
00242 
00243   virtual void depth( int );
00244 
00245   virtual void shiftDepth( int shift );
00246   
00247   inline const Color & penColor() const;
00248 
00249   inline const Color & fillColor() const;
00250 
00251 private:
00252   static const std::string _name; 
00254 protected:
00255 
00256   int _depth;                   
00257   Color _penColor;              
00258   Color _fillColor;             
00259   double _lineWidth;    
00260   LineStyle _lineStyle; 
00261   LineCap _lineCap;             
00262   LineJoin _lineJoin;   
00270   std::string svgProperties( const TransformSVG & transform ) const;
00271 
00272   
00278   std::string postscriptProperties() const;
00279 
00280 };
00281 
00282   
00283 inline Rect
00284 Shape::bbox()
00285 {
00286   return this->boundingBox();
00287 }
00288   
00289   
00290 inline Shape &
00291 Shape::operator++()
00292 {
00293   ++_depth;
00294   return *this;
00295 }
00296 
00297 inline Shape &
00298 Shape::operator--()
00299 {
00300   --_depth;
00301   return *this;
00302 }
00303 
00304 
00305 inline int
00306 Shape::depth() const
00307 {
00308   return _depth;
00309 }
00310 
00311 inline const Color &
00312 Shape::penColor() const
00313 {
00314   return _penColor;
00315 }
00316 
00317 const Color &
00318 Shape::fillColor() const
00319 {
00320   return _fillColor;
00321 }
00322 
00323 Shape &
00324 Shape::rotateDeg( double angle, const Point & aCenter )
00325 {
00326   return rotate( angle * ( M_PI / 180.0 ), aCenter ); 
00327 }
00328 
00329 Shape &
00330 Shape::rotateDeg( double angle )
00331 {
00332   return rotate( angle * ( M_PI / 180.0 ), center() ); 
00333 }
00334 
00343 struct Dot : public Shape { 
00344   
00345   inline Dot( double x, double y,
00346               Color color, 
00347               double lineWidth,
00348               int depth = -1 );
00349 
00355   const std::string & name() const;
00356   
00357   Point center() const;
00358 
00367   Dot & rotate( double angle, const Point & center );
00368 
00377   Dot rotated( double angle, const Point & center ) const;
00378 
00386   Dot & rotate( double angle );
00387 
00395   Dot rotated( double angle ) const;
00396 
00405   Dot & translate( double dx, double dy );
00406 
00415   Dot translated( double dx, double dy ) const;
00416 
00417   Shape & scale( double sx, double sy );
00418 
00419   Shape & scale( double s );
00420 
00431   Dot scaled( double sx, double sy ) const;
00432 
00433   Dot scaled( double s ) const;
00434 
00441   void scaleAll( double s );
00442 
00443   void flushPostscript( std::ostream & stream,
00444                         const TransformEPS & transform ) const;
00445   
00446   void flushFIG( std::ostream & stream,
00447                  const TransformFIG & transform,
00448                  std::map<Color,int> & colormap ) const;
00449 
00450   void flushSVG( std::ostream & stream,
00451                  const TransformSVG & transform ) const;
00452   
00453   Rect boundingBox() const;
00454 
00455   Dot * clone() const;
00456 
00457 private:
00458   static const std::string _name; 
00460 protected:
00461   double _x;                    
00462   double _y;                    
00463 };
00464 
00469 struct Line : public Shape { 
00470   
00482   inline Line( double x1, double y1, double x2, double y2, 
00483                Color color, 
00484                double lineWidth,
00485                const LineStyle style = SolidStyle,
00486                const LineCap cap = ButtCap,
00487                const LineJoin join = MiterJoin,
00488                int depth = -1 );
00489 
00495   const std::string & name() const;
00496 
00497   Point center() const;
00498 
00499   Line & rotate( double angle, const Point & center );
00500 
00509   Line rotated( double angle, const Point & center ) const;
00510 
00511   Line & rotate( double angle );
00512 
00520   Line rotated( double angle ) const;
00521 
00522   Line & translate( double dx, double dy );
00523 
00532   Line translated( double dx, double dy ) const;
00533 
00534   Shape & scale( double sx, double sy );
00535 
00536   Shape & scale( double s );
00537 
00546   Line scaled( double sx, double sy ) const;
00547 
00548   Line scaled( double s ) const;
00549   
00556   void scaleAll( double s );
00557 
00558   void flushPostscript( std::ostream & stream,
00559                         const TransformEPS & transform ) const;
00560   
00561   void flushFIG( std::ostream & stream,
00562                  const TransformFIG & transform,
00563                  std::map<Color,int> & colormap ) const;
00564 
00565   void flushSVG( std::ostream & stream,
00566                  const TransformSVG & transform ) const;
00567 
00568   Rect boundingBox() const;
00569 
00570   Line * clone() const;
00571 
00572 private:
00573   static const std::string _name; 
00575 protected:
00576   double _x1;                   
00577   double _y1;                   
00578   double _x2;                   
00579   double _y2;                   
00580 };
00581 
00582 
00583 
00584 
00589 struct Arrow : public Line { 
00590 
00603   inline Arrow( double x1, double y1, double x2, double y2,
00604                 Color penColor, Color fillColor,
00605                 double lineWidth, 
00606                 const LineStyle style = SolidStyle,
00607                 const LineCap cap = ButtCap,
00608                 const LineJoin join = MiterJoin,
00609                 int depth = -1 );
00610 
00616   const std::string & name() const;
00617   
00626   Arrow rotated( double angle, const Point & center ) const;
00627   
00635   Arrow rotated( double angle ) const;
00636 
00645   Arrow translated( double dx, double dy ) const;
00646 
00655   Arrow scaled( double sx, double sy ) const;
00656 
00657   Arrow scaled( double s ) const;
00658 
00659   void flushPostscript( std::ostream & stream,
00660                         const TransformEPS & transform ) const;
00661   
00662   void flushFIG( std::ostream & stream,
00663                  const TransformFIG & transform,
00664                  std::map<Color,int> & colormap ) const;
00665   void flushSVG( std::ostream & stream,
00666                  const TransformSVG & transform ) const;
00667 
00668   Arrow * clone() const;
00669 
00670 private:
00671   static const std::string _name; 
00672 };
00673 
00678 struct Polyline : public Shape { 
00679   inline Polyline( const std::vector<Point> & points, 
00680                    bool closed,
00681                    Color penColor, Color fillColor,
00682                    double lineWidth,
00683                    const LineStyle lineStyle = SolidStyle,
00684                    const LineCap cap = ButtCap,
00685                    const LineJoin join = MiterJoin,
00686                    int depth = -1 );
00687 
00688   inline Polyline( const Path & path, 
00689                    Color penColor, Color fillColor,
00690                    double lineWidth,
00691                    const LineStyle lineStyle = SolidStyle,
00692                    const LineCap cap = ButtCap,
00693                    const LineJoin join = MiterJoin,
00694                    int depth = -1 );
00695 
00696   inline Polyline( bool closed, Color penColor, Color fillColor,
00697                    double lineWidth,
00698                    const LineStyle lineStyle = SolidStyle,
00699                    const LineCap cap = ButtCap,
00700                    const LineJoin join = MiterJoin,
00701                    int depth = -1 );
00702             
00708   const std::string & name() const;
00709 
00710   Point center() const;
00711 
00719   Polyline & operator<<( const Point & p );
00720 
00728   Point & operator[]( const unsigned int n ) {
00729     return _path[ n ];
00730   }
00731 
00732 
00733   Polyline & rotate( double angle, const Point & center );
00734 
00743   Polyline rotated( double angle, const Point & center ) const;
00744 
00745   Polyline & rotate( double angle );
00746   
00754   Polyline rotated( double angle ) const;
00755 
00756   Polyline & translate( double dx, double dy );
00757   
00766   Polyline translated( double dx, double dy ) const;
00767 
00768   Shape & scale( double sx, double sy );
00769 
00770   Shape & scale( double s );
00771   
00780   Polyline scaled( double sx, double sy )  const;
00781 
00782   Polyline scaled( double s )  const;
00783 
00790   void scaleAll( double s );
00791 
00792   void flushPostscript( std::ostream & stream,
00793                         const TransformEPS & transform ) const;
00794 
00795   void flushFIG( std::ostream & stream,
00796                  const TransformFIG & transform,
00797                  std::map<Color,int> & colormap ) const;
00798 
00799   void flushSVG( std::ostream & stream,
00800                  const TransformSVG & transform ) const;
00801 
00802   Rect boundingBox() const;
00803 
00804   Polyline * clone() const;
00805 
00806 private:
00807   static const std::string _name; 
00809 protected:
00810   Path _path;
00811 };
00812 
00817 struct Rectangle : public Polyline {
00818 
00819   inline Rectangle( double x, double y, double width, double height,
00820              Color penColor, Color fillColor,
00821              double lineWidth, 
00822              const LineStyle style = SolidStyle,
00823              const LineCap cap = ButtCap,
00824              const LineJoin join = MiterJoin,
00825                     int depth = -1 );
00826 
00827   inline Rectangle( const Rect & rect,
00828                     Color penColor, Color fillColor,
00829                     double lineWidth,
00830                     const LineStyle style = SolidStyle,
00831                     const LineCap cap = ButtCap,
00832                     const LineJoin join = MiterJoin,
00833                     int depth = -1 );
00834 
00840   const std::string & name() const;
00841 
00842   double x() const { return _path[0].x; }
00843   double y() const { return _path[0].y; }
00844   double width() { return (_path[1] - _path[0]).norm(); }
00845   double height() { return (_path[0] - _path[3]).norm(); }
00846   Point topLeft() { return Point( _path[0].x, _path[0].y ); }
00847   Point topRight() { return Point( _path[1].x, _path[1].y ); }
00848   Point bottomLeft() { return Point( _path[3].x, _path[3].y ); }
00849   Point bottomRight() { return Point( _path[2].x, _path[2].y ); }
00850 
00851 
00860   Rectangle rotated( double angle, const Point & center )  const;
00861   
00869   Rectangle rotated( double angle ) const;
00870 
00879   Rectangle translated( double dx, double dy ) const;
00880 
00889   Rectangle scaled( double sx, double sy ) const;
00890 
00891   Rectangle scaled( double s ) const;
00892  
00899   void scaleAll( double s );
00900 
00901   void flushFIG( std::ostream & stream,
00902                  const TransformFIG & transform,
00903                  std::map<Color,int> & colormap ) const;
00904 
00905   void flushSVG( std::ostream & stream,
00906                  const TransformSVG & transform ) const;
00907 
00908   Rectangle * clone() const;
00909 
00910 private:
00911   static const std::string _name; 
00913 protected:
00914   bool _isRectangle;
00915 };
00916 
00917 
00918 
00923 struct Image : public Rectangle { 
00924   
00936   inline Image( double x0, double y0, double width, double height, 
00937                 std::string fileName, int depthValue );
00943   const std::string & name() const;
00944 
00945   Image * clone() const;
00946   
00947   void flushFIG( std::ostream & stream,
00948                  const TransformFIG & transform,
00949                  std::map<Color,int> & colormap ) const;
00950 
00951   void flushSVG( std::ostream & stream,
00952                  const TransformSVG & transform ) const;
00953 
00954 
00955 private:
00956   static const std::string _name; 
00958 protected:
00959   double _x0;                   
00960   double _y0;                   
00961   double _width;
00962   double _height;
00963   std::string _filename;
00964   
00965 };
00966 
00967 
00968 
00973 struct Triangle : public Polyline {
00974 
00975   Triangle( const Point & p1, const Point & p2, const Point & p3,
00976             Color pen,
00977             Color fill,
00978             double lineWidth,
00979             const LineStyle style = SolidStyle,
00980             const LineCap cap = ButtCap,
00981             const LineJoin join = MiterJoin,
00982             int depthValue = -1 )
00983     : Polyline( std::vector<Point>(), true, pen, fill, lineWidth, style, cap, join, depthValue ) {
00984     _path << p1;
00985     _path << p2;
00986     _path << p3;
00987   }
00988 
00989   Triangle( const double x1, const double y1, 
00990             const double x2, const double y2, 
00991             const double x3, const double y3,
00992             Color pen,
00993             Color fill,
00994             double lineWidth,
00995             const LineStyle style = SolidStyle,
00996             const LineCap cap = ButtCap,
00997             const LineJoin join = MiterJoin,
00998             int depthValue = -1 )
00999     : Polyline( std::vector<Point>(), true, pen, fill, lineWidth, style, cap, join, depthValue ) {
01000     _path << Point( x1, y1 );
01001     _path << Point( x2, y2 );
01002     _path << Point( x3, y3 );
01003   }
01004 
01010   const std::string & name() const;
01011 
01012   Triangle rotated( double angle ) const;
01013 
01022   Triangle translated( double dx, double dy ) const;
01023 
01032   Triangle scaled( double sx, double sy ) const;
01033 
01034   Triangle scaled( double s ) const;
01035 
01036   Triangle * clone() const;
01037   
01038 private:
01039   static const std::string _name; 
01041 protected:
01042 };
01043 
01044 
01049 struct GouraudTriangle : public Polyline {
01050 
01051 
01052   GouraudTriangle( const Point & p0, const Color & color0,
01053                    const Point & p1, const Color & color1,
01054                    const Point & p2, const Color & color2,
01055                    int subdivisions,
01056                    int depth = -1 );
01057 
01058   GouraudTriangle( const Point & p0, float brightness0,
01059                    const Point & p1, float brightness1,
01060                    const Point & p2, float brightness2,
01061                    const Color & fillColor,
01062                    int subdivisions,
01063                    int depth = -1 );
01064 
01070   const std::string & name() const;
01071 
01072   Point center() const;
01073 
01074   GouraudTriangle & rotate( double angle, const Point & center );  
01075 
01076   GouraudTriangle rotated( double angle, const Point & center ) const;  
01077 
01078   GouraudTriangle & rotate( double angle );
01079 
01080   GouraudTriangle rotated( double angle ) const;
01081 
01090   GouraudTriangle translated( double dx, double dy ) const;
01091 
01100   GouraudTriangle scaled( double sx, double sy ) const;
01101 
01102   GouraudTriangle scaled( double s ) const;
01103 
01104 
01111   void scaleAll( double s );
01112 
01119   void flushPostscript( std::ostream & stream,
01120                         const TransformEPS & transform ) const;
01121 
01134   void flushFIG( std::ostream & stream,
01135                  const TransformFIG & transform,
01136                  std::map<Color,int> & colormap ) const;
01137 
01138   void flushSVG( std::ostream & stream,
01139                  const TransformSVG & transform ) const;
01140 
01141   GouraudTriangle * clone() const;
01142 
01143 private:
01144   static const std::string _name; 
01146 protected:
01147   Color _color0;
01148   Color _color1;
01149   Color _color2;
01150   int _subdivisions;
01151 };
01152 
01157 struct Ellipse : public Shape {
01158   
01159   Ellipse( double x, double y, 
01160            double xRadius, double yRadius, 
01161            Color pen, Color fill,
01162            double lineWidth,
01163            const LineStyle lineStyle = SolidStyle,
01164            int depthValue = -1 )
01165     : Shape( pen, fill,
01166              lineWidth, lineStyle, ButtCap, MiterJoin, depthValue ),
01167       _center( x, y ), _xRadius( xRadius ), _yRadius( yRadius ),
01168       _angle( 0.0 ),
01169       _circle( false ) {
01170     while ( _angle > M_PI_2 ) _angle -= M_PI;
01171     while ( _angle < -M_PI_2 ) _angle += M_PI;
01172   }
01173 
01179   const std::string & name() const;
01180 
01181   Point center() const;
01182 
01183   Ellipse & rotate( double angle, const Point & center );
01184 
01193   Ellipse rotated( double angle, const Point & center ) const;
01194 
01195   Ellipse & rotate( double angle );
01196 
01204   Ellipse rotated( double angle ) const;
01205   
01206   Ellipse & translate( double dx, double dy );
01207 
01216   Ellipse translated( double dx, double dy ) const;
01217 
01218   Shape & scale( double sx, double sy );
01219 
01220   Shape & scale( double s );
01221 
01230   Ellipse scaled( double sx, double sy ) const;
01231 
01232   Ellipse scaled( double s ) const;
01233   
01240   void scaleAll( double s );
01241 
01242   void flushPostscript( std::ostream & stream,
01243                         const TransformEPS & transform ) const;
01244 
01245   void flushFIG( std::ostream & stream,
01246                  const TransformFIG & transform,
01247                  std::map<Color,int> & colormap ) const;
01248 
01249   void flushSVG( std::ostream & stream,
01250                  const TransformSVG & transform ) const;
01251 
01252   Rect boundingBox() const;
01253 
01254   Ellipse * clone() const;
01255 
01256 private:
01257   static const std::string _name; 
01259 protected:
01260   Point _center;
01261   double _xRadius; 
01262   double _yRadius;
01263   double _angle;
01264   bool _circle;
01265 };
01266 
01271 struct Circle : public Ellipse {
01272 
01273   Circle( double x, double y, double radius, 
01274           Color pen, Color fill,
01275           double lineWidth,
01276           const LineStyle style = SolidStyle,
01277           int depthValue = -1 )
01278     : Ellipse( x, y, radius, radius, pen, fill, lineWidth, style, depthValue )
01279   { _circle = true; }
01280 
01286   const std::string & name() const;
01287 
01288   Point center() const;
01289 
01290   Circle & rotate( double angle, const Point & center );
01291 
01292   Circle rotated( double angle, const Point & center ) const;
01293 
01294   Circle & rotate( double angle );
01295 
01296   Circle rotated( double angle ) const;
01297   
01298   Circle & translate( double dx, double dy );
01299 
01300   Circle translated( double dx, double dy ) const;
01301 
01302   Shape & scale( double sx, double sy );
01303 
01304   Shape & scale( double s );
01305 
01306   Circle scaled( double sx, double sy ) const;
01307 
01308   Circle scaled( double s ) const;
01309 
01316   void scaleAll( double s );
01317 
01318   void flushSVG( std::ostream & stream,
01319                  const TransformSVG & transform ) const;
01320 
01321   Circle * clone() const;
01322 
01323 private:
01324   static const std::string _name; 
01325 };
01326 
01331 struct Text : public Shape {
01332   
01346   Text( double x, double y,
01347         const std::string & text,
01348         const Fonts::Font font,
01349         double size,
01350         Color color = Color::Black,
01351         int depthValue = -1 )
01352     : Shape( color, Color::None, 1.0, SolidStyle, ButtCap, MiterJoin, depthValue ),
01353       _position( x, y ), _text( text ), _font( font ),
01354       _angle( 0.0 ), _size( size ),
01355       _xScale( 1.0 ), _yScale( 1.0 ) { }
01356 
01357   
01372   Text( double x, double y,
01373         const std::string & text,
01374         const Fonts::Font font,
01375         const std::string & svgFont,
01376         double size,
01377         Color color = Color::Black,
01378         int depthValue = -1 )
01379     : Shape( color, Color::None, 1.0, SolidStyle, ButtCap, MiterJoin, depthValue ),
01380       _position( x, y ),
01381       _text( text ), _font( font ), _svgFont( svgFont ),
01382       _angle( 0.0 ),
01383       _size( size ),
01384       _xScale( 1.0 ), _yScale( 1.0 ) { }
01385   
01391   const std::string & name() const;
01392 
01393   Point center() const;
01394 
01395   Text & rotate( double angle, const Point & center );
01396 
01397   Text rotated( double angle, const Point & center ) const;
01398 
01399   Text & rotate( double angle );
01400 
01401   Text rotated( double angle ) const;
01402   
01403   Text & translate( double dx, double dy );
01404 
01405   Text translated( double dx, double dy ) const;
01406 
01407   Shape & scale( double sx, double sy );
01408 
01409   Shape & scale( double s );
01410 
01411   Text scaled( double sx, double sy ) const;
01412  
01413   Text scaled( double s ) const;
01414   
01421   void scaleAll( double s );
01422 
01423   void flushPostscript( std::ostream & stream,
01424                         const TransformEPS & transform ) const;
01425 
01426   void flushFIG( std::ostream & stream,
01427                  const TransformFIG & transform,
01428                  std::map<Color,int> & colormap ) const;
01429 
01430   void flushSVG( std::ostream & stream,
01431                  const TransformSVG & transform ) const;
01432 
01433   Rect boundingBox() const;
01434 
01435   Text * clone() const;
01436 
01437 private:
01438   static const std::string _name; 
01440 protected:
01441   Point _position;
01442   std::string _text;
01443   Fonts::Font _font;
01444   std::string _svgFont;
01445   double _angle;
01446   double _size;
01447   double _xScale;
01448   double _yScale;
01449 };
01450 
01459 bool shapeGreaterDepth( const Shape *s1, const Shape *s2 );
01460 
01461 
01462 } // namespace LibBoard
01463 
01464 /*
01465  * Inline methods
01466  */
01467 #include "Shapes.ih"
01468 
01469 
01470 #endif /* _SHAPE_H_ */
01471 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines