00001 #ifndef INCLUDED_Draw_h_
00002 #define INCLUDED_Draw_h_
00003
00004 #include "Shared/BoundingBox.h"
00005 #include "Planners/PlannerObstacles.h"
00006
00007 typedef struct _cairo cairo_t;
00008 typedef struct _cairo_surface cairo_surface_t;
00009
00010
00011
00012 class Draw {
00013 public:
00014
00015 Draw()
00016 : cr(NULL), surface(NULL), filename(), origin(), scale(1), surfaceWidth(), surfaceHeight(), addPath(false) {}
00017
00018
00019 Draw(const std::string& file, double width, double height)
00020 : cr(NULL), surface(NULL), filename(), origin(), scale(1), surfaceWidth(), surfaceHeight(), addPath(false) { setFile(file,width,height); }
00021
00022
00023 ~Draw() { teardown(); }
00024
00025
00026 Draw& setFile(const std::string& file, double width, double height);
00027
00028
00029 Draw& flushPage();
00030
00031
00032 Draw& setOrigin(const fmat::Column<2>& p) { origin=p; resetTransform(); return *this;}
00033
00034 Draw& setOrigin(double x, double y) { origin=fmat::pack(x,y); resetTransform(); return *this;}
00035
00036 Draw& centerView(const fmat::Column<2>& p) { return centerView(p[0],p[1]); }
00037
00038 Draw& centerView(double x, double y);
00039
00040
00041 Draw& setClip();
00042
00043 Draw& clearClip();
00044
00045
00046
00047 Draw& setScale(double x, bool scaleStroke=false);
00048
00049 double displayToScaled(double x) const { return x/scale; }
00050
00051 double scaledToDisplay(double x) const { return x*scale; }
00052
00053
00054 Draw& setRegion(const BoundingBox2D& region, bool scaleStroke=false);
00055
00056 Draw& drawGrid(double xRes, double yRes, double xOff=0, double yOff=0);
00057
00058 Draw& drawAxes();
00059
00060
00061 Draw& setStrokeWidth(double x);
00062
00063 Draw& setScaledStrokeWidth(double x);
00064
00065 double getStrokeWidth() const { return getScaledStrokeWidth()*scale; }
00066
00067 double getScaledStrokeWidth() const;
00068
00069
00070 enum Colors {
00071 BLACK, DK_GRAY, GRAY, LT_GRAY, WHITE, RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA
00072 };
00073
00074
00075 Draw& setColorRGB(double red, double green, double blue, double alpha=1);
00076
00077 Draw& setColorGray(double x, double alpha=1) { return setColorRGB(x,x,x,alpha); }
00078
00079 Draw& setColor(Colors c, double alpha=1);
00080
00081
00082 Draw& stroke();
00083
00084 Draw& stroke(Colors c, double alpha=1) { return setColor(c,alpha).stroke(); }
00085
00086 Draw& stroke(double red, double green, double blue, double alpha=1) { return setColorRGB(red,green,blue,alpha).stroke(); }
00087
00088 Draw& strokeLine(const fmat::Column<2>& p1, const fmat::Column<2>& p2) { return strokeLine(p1[0],p1[1],p2[0],p2[1]); }
00089
00090 Draw& strokeLine(double x1, double y1, double x2, double y2);
00091
00092
00093
00094 Draw& fill();
00095
00096 Draw& fill(Colors c, double alpha=1) { return setColor(c,alpha).fill(); }
00097
00098 Draw& fill(double red, double green, double blue, double alpha=1) { return setColorRGB(red,green,blue,alpha).fill(); }
00099
00100
00101
00102 enum PointStyles {
00103 CIRCLE, SQUARE, DIAMOND
00104 };
00105
00106
00107 Draw& point(const fmat::Column<2>& p, double size, PointStyles style=CIRCLE) { return point(p[0],p[1],size,style); }
00108
00109 Draw& point(double x, double y, double size, PointStyles style=CIRCLE);
00110
00111
00112 Draw& arc(const fmat::Column<2>& p, double r, double begin, double end) { return arc(p[0],p[1],r,begin,end); }
00113
00114 Draw& arc(double x, double y, double r, double begin, double end);
00115
00116
00117 Draw& circle(const fmat::Column<2>& c, double r) { return arc(c[0],c[1],r,0,2*M_PI); }
00118
00119 Draw& circle(double x, double y, double r) { return arc(x,y,r,0,2*M_PI); }
00120
00121
00122 Draw& ellipse(const fmat::Column<2>& c, double width, double height, double orientation=0);
00123
00124
00125 Draw& rect(const BoundingBox2D& r) { return rect(r.min,r.max); }
00126
00127 Draw& rect(const fmat::Column<2>& p1, const fmat::Column<2>& p2);
00128
00129
00130
00131
00132 Draw& arrow(const fmat::Column<2>& p1, const fmat::Column<2>& p2, double headWidth=2, double headLength=5);
00133
00134
00135 Draw& draw(const PlannerObstacle2D& o);
00136
00137 Draw& draw(const RectangularObstacle& o);
00138
00139 Draw& draw(const CircularObstacle& o);
00140
00141 Draw& draw(const EllipticalObstacle& o);
00142
00143 Draw& draw(const ConvexPolyObstacle& o);
00144
00145 Draw& draw(const HierarchicalObstacle& o);
00146
00147
00148 Draw& moveTo(const fmat::Column<2>& p) { return moveTo(p[0],p[1]); }
00149
00150
00151 Draw& moveTo(double x, double y);
00152
00153
00154 Draw& lineTo(const fmat::Column<2>& p) { return lineTo(p[0],p[1]); }
00155
00156 Draw& lineTo(double x, double y);
00157
00158
00159 Draw& arcTo(const fmat::Column<2>& p, double r, double begin, double end) { return arcTo(p[0],p[1],r,begin,end); }
00160
00161 Draw& arcTo(double x, double y, double r, double begin, double end);
00162
00163
00164 Draw& closePath();
00165
00166
00167 Draw& holdPath(bool continuePath=false) { if(!continuePath) clearPath(); addPath=true; return *this; }
00168
00169 Draw& releasePath() { addPath=false; return *this; }
00170
00171 bool getHoldMode() const { return addPath; }
00172
00173
00174 Draw& clearPath();
00175
00176 protected:
00177
00178 void teardown();
00179
00180 void resetTransform();
00181
00182 void checkSurfaceStatus(const std::string& msg);
00183
00184 void checkStatus(const std::string& msg);
00185
00186 cairo_t *cr;
00187 cairo_surface_t *surface;
00188 std::string filename;
00189
00190 fmat::Column<2> origin;
00191 double scale;
00192 double surfaceWidth;
00193 double surfaceHeight;
00194
00195
00196 bool addPath;
00197
00198 private:
00199 Draw(const Draw& o);
00200 Draw& operator=(const Draw& o);
00201 };
00202
00203
00204
00205
00206
00207
00208 #endif