00001
00010 #ifndef _BOARD_BOARD_H_
00011 #define _BOARD_BOARD_H_
00012
00013 #include <ostream>
00014 #include <string>
00015 #include <vector>
00016
00017 #include "board/Point.h"
00018 #include "board/Path.h"
00019 #include "board/Shapes.h"
00020 #include "board/ShapeList.h"
00021
00022 namespace LibBoard {
00023
00029 class Board : public ShapeList {
00030
00031 public:
00032
00033 enum PageSize { BoundingBox, A4, Letter };
00034 enum Unit { PT, IN, CM, MM };
00035 static const double Degree;
00036
00042 Board( const Color & backgroundColor = Color::None );
00043
00049 Board( const Board & other );
00050
00051 ~Board();
00052
00060 Board & operator=( const Board & other );
00061
00062
00070 Board & operator<<( const Shape & shape );
00071
00079 Board & operator<<( Unit unit );
00080
00086 void clear( const Color & color = Color::None );
00087
00095 inline void clear( unsigned char red, unsigned char green, unsigned char blue );
00096
00097
00098
00099 Shape & rotate( double angle, const Point & center );
00100
00101 Shape & rotate( double angle );
00102
00103 Shape & translate( double dx, double dy );
00104
00105 Shape & scale( double sx, double sy );
00106
00107 Shape & scale( double s );
00108
00109 Board rotated( double angle, const Point & center );
00110
00111 Board rotated( double angle );
00112
00113 Board translated( double dx, double dy );
00114
00115 Board scaled( double sx, double sy );
00116
00117 Board scaled( double s );
00118
00119
00120
00126 void setUnit( Unit unit );
00127
00134 void setUnit( float factor, Unit unit );
00135
00143 void drawDot( float x, float y, int depth = -1 );
00144
00154 void drawLine( float x1, float y1, float x2, float y2,
00155 int depth = -1 );
00156
00167 void drawArrow( float x1, float y1, float x2, float y2,
00168 bool filled = true,
00169 int depth = -1 );
00170
00182 void drawTriangle( float x1, float y1,
00183 float x2, float y2,
00184 float x3, float y3,
00185 int depth = -1 );
00186
00195 void drawTriangle( const Point & p1,
00196 const Point & p2,
00197 const Point & p3,
00198 int depth = -1 );
00199
00211 void fillTriangle( float x1, float y1,
00212 float x2, float y2,
00213 float x3, float y3,
00214 int depth = -1 );
00215
00228 void fillGouraudTriangle( const Point & p1,
00229 const Color & color1,
00230 const Point & p2,
00231 const Color & color2,
00232 const Point & p3,
00233 const Color & color3,
00234 unsigned char divisions = 3,
00235 int depth = -1 );
00236
00252 inline void fillGouraudTriangle( const float x1, const float y1,
00253 const Color & color1,
00254 const float x2, const float y2,
00255 const Color & color2,
00256 const float x3, const float y3,
00257 const Color & color3,
00258 unsigned char divisions = 3,
00259 int depth = -1 );
00260
00273 void fillGouraudTriangle( const Point & p1,
00274 const float brightness1,
00275 const Point & p2,
00276 const float brightness2,
00277 const Point & p3,
00278 const float brightness3,
00279 unsigned char divisions = 3,
00280 int depth = -1 );
00281
00298 inline void fillGouraudTriangle( const float x1, const float y1,
00299 const float brightness1,
00300 const float x2, const float y2,
00301 const float brightness2,
00302 const float x3, const float y3,
00303 const float brightness3,
00304 unsigned char divisions = 3,
00305 int depth = -1 );
00306
00307
00316 void fillTriangle( const Point & p1,
00317 const Point & p2,
00318 const Point & p3,
00319 int depth = -1 );
00320
00330 void drawRectangle( float x, float y,
00331 float width, float height,
00332 int depth = -1 );
00333
00343 void fillRectangle( float x, float y,
00344 float width, float height,
00345 int depth = -1 );
00346
00355 void drawCircle( float x, float y, float radius,
00356 int depth = -1 );
00357
00366 void fillCircle( float x, float y, float radius,
00367 int depth = -1);
00368
00377 void drawEllipse( float x, float y,
00378 float xRadius, float yRadius,
00379 int depth = -1);
00380
00390 void fillEllipse( float x, float y,
00391 float xRadius, float yRadius,
00392 int depth = -1);
00393
00400 void drawPolyline( const std::vector<Point> & points,
00401 int depth = -1 );
00402
00409 void drawClosedPolyline( const std::vector<Point> & points,
00410 int depth = -1 );
00411
00418 void fillPolyline( const std::vector<Point> & points,
00419 int depth = -1 );
00420
00429 void drawText( float x, float y, const char * text,
00430 int depth = -1 );
00431
00440 void drawText( float x, float y, const std::string & str,
00441 int depth = -1 );
00442
00450 Board & setFont( const Fonts::Font font, float fontSize );
00451
00458 Board & setFontSize( float fontSize );
00459
00468 Board & setPenColorRGBi( unsigned char red,
00469 unsigned char green,
00470 unsigned char blue,
00471 unsigned char alpha = 255 );
00472
00482 Board & setPenColorRGBf( float red,
00483 float green,
00484 float blue,
00485 float alpha = 1.0f );
00486
00494 Board & setPenColor( const Color & color );
00495
00496
00506 Board & setFillColorRGBi( unsigned char red,
00507 unsigned char green,
00508 unsigned char blue,
00509 unsigned char alpha = 255 );
00510
00520 Board & setFillColorRGBf( float red, float green, float blue, float alpha = 1.0f );
00521
00529 Board & setFillColor( const Color & color );
00530
00537 Board & setLineWidth( float width );
00538
00547 inline Board & setLineCap( Shape::LineCap cap );
00548
00557 inline Board & setLineJoin( Shape::LineJoin join );
00558
00564 void backgroundColor( const Color & color );
00565
00571 void drawBoundingBox( int depth = -1 );
00572
00573
00582 void setClippingRectangle( float x, float y,
00583 float width, float height );
00584
00590 void setClippingPath( const std::vector<Point> & points );
00591
00597 void setClippingPath( const Path & path );
00598
00609 void addDuplicates( const Shape & shape,
00610 unsigned int times,
00611 double dx, double dy, double scale = 1.0 );
00612
00626 void addDuplicates( const Shape & shape,
00627 unsigned int times,
00628 double dx, double dy,
00629 double scaleX,
00630 double scaleY,
00631 double angle = 0.0 );
00632
00642 void save( const char * filename, PageSize size = Board::BoundingBox, float margin = 10.0 ) const;
00643
00654 void save( const char * filename, float pageWidth, float pageHeight, float margin = 10.0 ) const;
00655
00664 void saveEPS( const char * filename, PageSize size = Board::BoundingBox, float margin = 10.0 ) const ;
00665
00676 void saveEPS( const char * filename, float pageWidth, float pageHeight, float margin = 10.0 ) const ;
00677
00686 void saveFIG( const char * filename, PageSize size = Board::BoundingBox, float margin = 10.0 ) const;
00687
00698 void saveFIG( const char * filename, float pageWidth, float pageHeight, float margin = 10.0 ) const ;
00699
00708 void saveSVG( const char * filename, PageSize size = Board::BoundingBox, float margin = 10.0 ) const;
00709
00720 void saveSVG( const char * filename, float pageWidth, float pageHeight, float margin = 10.0 ) const ;
00721
00722 protected:
00723
00728 struct State {
00729 Color penColor;
00730 Color fillColor;
00731 float lineWidth;
00732 Shape::LineCap lineCap;
00733 Shape::LineJoin lineJoin;
00734 Fonts::Font font;
00735 float fontSize;
00736 float unitFactor;
00737 State();
00738 float unit( const float & x ) { return x * unitFactor; }
00739 Point unit( const Point & p ) { return Point( p.x * unitFactor, p.y * unitFactor); }
00740 void unit( Shape & shape ) { shape.scaleAll( unitFactor ); }
00741 };
00742 State _state;
00743 Color _backgroundColor;
00744 Path _clippingPath;
00745 };
00746
00747 inline void
00748 Board::clear( unsigned char red, unsigned char green, unsigned char blue )
00749 {
00750 clear( Color( red, green, blue ) );
00751 }
00752
00753 inline Board &
00754 Board::setLineCap( Shape::LineCap cap )
00755 {
00756 _state.lineCap = cap;
00757 return *this;
00758 }
00759
00760 inline Board &
00761 Board::setLineJoin( Shape::LineJoin join )
00762 {
00763 _state.lineJoin = join;
00764 return *this;
00765 }
00766
00767 inline void
00768 Board::fillGouraudTriangle( const float x1, const float y1,
00769 const Color & color1,
00770 const float x2, const float y2,
00771 const Color & color2,
00772 const float x3, const float y3,
00773 const Color & color3,
00774 unsigned char divisions,
00775 int depth )
00776 {
00777 fillGouraudTriangle( Point( x1, y1 ), color1,
00778 Point( x2, y2 ), color2,
00779 Point( x3, y3 ), color3,
00780 divisions, depth );
00781 }
00782
00783 void
00784 Board::fillGouraudTriangle( const float x1, const float y1,
00785 const float brightness1,
00786 const float x2, const float y2,
00787 const float brightness2,
00788 const float x3, const float y3,
00789 const float brightness3,
00790 unsigned char divisions,
00791 int depth )
00792 {
00793 fillGouraudTriangle( Point( x1, y1 ), brightness1,
00794 Point( x2, y2 ), brightness2,
00795 Point( x3, y3 ), brightness3,
00796 divisions, depth );
00797 }
00798
00799 }
00800
00801 #endif