- Introduced SVG export, which has some issues with dynamical systems (too many paths...
[mldemos:mldemos.git] / MLDemos / canvas.h
1 /*********************************************************************\r
2 MLDemos: A User-Friendly visualization toolkit for machine learning\r
3 Copyright (C) 2010  Basilio Noris\r
4 Contact: mldemos@b4silio.com\r
5 \r
6 This library is free software; you can redistribute it and/or\r
7 modify it under the terms of the GNU Lesser General Public License,\r
8 version 3 as published by the Free Software Foundation.\r
9 \r
10 This library is distributed in the hope that it will be useful, but\r
11 WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
13 Lesser General Public License for more details.\r
14 \r
15 You should have received a copy of the GNU Lesser General Public\r
16 License along with this library; if not, write to the Free\r
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
18 *********************************************************************/\r
19 #ifndef _CANVAS_H_\r
20 #define _CANVAS_H_\r
21 \r
22 #include "datasetManager.h"\r
23 #include "mymaths.h"\r
24 #include <QWidget>\r
25 #include <map>\r
26 #include <QMouseEvent>\r
27 #include <QWheelEvent>\r
28 #include <QPainter>\r
29 #include <QPainterPath>\r
30 \r
31 class Canvas : public QWidget\r
32 {\r
33         Q_OBJECT\r
34 \r
35 public:\r
36         Canvas(QWidget *parent);\r
37         ~Canvas();\r
38 \r
39         bool DeleteData(QPointF center, float radius);\r
40         static bool bCrossesAsDots;\r
41         void DrawSamples();\r
42         void DrawObstacles();\r
43         void DrawTrajectories();\r
44         void DrawRewards();\r
45         void DrawObstacles(QPainter &painter);\r
46         void DrawTrajectories(QPainter &painter);\r
47         void DrawSamples(QPainter &painter);\r
48         void DrawTargets(QPainter &painter);\r
49         void DrawLiveTrajectory(QPainter &painter);\r
50         void ResetSamples(){drawnSamples = 0; drawnTrajectories = 0;};\r
51         void FitToData();\r
52         void DrawAxes(QPainter &painter);\r
53         void RedrawAxes();\r
54         void PaintGaussian(QPointF position, double variance);\r
55         void PaintReward(fvec sample, float radius, float shift);\r
56         void PaintGradient(QPointF position);\r
57         bool bDrawing;\r
58         QPainterPath DrawObstacle(Obstacle o);\r
59         fVec center;\r
60         float zoom;\r
61         std::vector<fvec> targets;\r
62 \r
63 protected:\r
64         void paintEvent(QPaintEvent *event);\r
65         void mousePressEvent(QMouseEvent *event);\r
66         void wheelEvent(QWheelEvent *event);\r
67         void mouseReleaseEvent(QMouseEvent *event);\r
68         void mouseMoveEvent(QMouseEvent *event);\r
69         void enterEvent(QEvent *event);\r
70         void focusOutEvent(QFocusEvent *event);\r
71         void leaveEvent(QEvent *event);\r
72         void dragEnterEvent(QDragEnterEvent *);\r
73         void dropEvent(QDropEvent *);\r
74 \r
75 public:\r
76         DatasetManager *data;\r
77         QPixmap confidencePixmap;\r
78         QPixmap rewardPixmap;\r
79         QPixmap modelPixmap;\r
80         QPixmap infoPixmap;\r
81         QPixmap gridPixmap;\r
82         QPainterPath crosshair;\r
83         QPixmap samplesPixmap;\r
84         QPixmap trajectoriesPixmap;\r
85         QPixmap obstaclesPixmap;\r
86         QImage qimg;\r
87         bool bDisplayMap, bDisplayInfo, bDisplaySingle, bDisplaySamples, bDisplayTrajectories, bDisplayLearned, bDisplayGrid;\r
88         bool bShowCrosshair, bNewCrosshair;\r
89         int trajectoryCenterType, trajectoryResampleType, trajectoryResampleCount;\r
90         QPoint mouse, mouseAnchor;\r
91         fVec fromCanvas(QPointF point);\r
92         fVec fromCanvas(float x, float y);\r
93         QPointF toCanvasCoords(float x, float y);\r
94         QPointF toCanvasCoords(fvec sample);\r
95         QPointF toCanvas(fVec sample);\r
96         fvec toSampleCoords(QPointF point);\r
97         fvec toSampleCoords(float x, float y);\r
98         fvec canvasTopLeft();\r
99         fvec canvasBottomRight();\r
100         QRectF canvasRect();\r
101         void SetZoom(float zoom);\r
102         float GetZoom(){return zoom;};\r
103         void SetCenter(fVec center);\r
104         fvec GetCenter(){return center;};\r
105 \r
106         std::map<int,fvec> centers;\r
107         int drawnSamples;\r
108         int drawnTrajectories;\r
109         std::vector<fvec> liveTrajectory;\r
110 \r
111         void Paint(QPainter &painter, bool bSvg=false);\r
112 \r
113         bool SaveScreenshot(QString filename);\r
114         QPixmap GetScreenshot();\r
115 \r
116 public slots:\r
117         void ResizeEvent();\r
118         void SetConfidenceMap(QImage image);\r
119         void SetModelImage(QImage image);\r
120 \r
121 signals:\r
122         void DrawCrosshair();\r
123         void Drawing(fvec sample, int label);\r
124         void CanvasMoveEvent();\r
125         void Released();\r
126         void Navigation(fvec sample);\r
127 \r
128 public:\r
129         /*\r
130  static QPixmap toPixmap(IplImage *src)\r
131  {\r
132   QPixmap pixmap;\r
133   if(src->nChannels == 4)\r
134   {\r
135    pixmap = QPixmap::fromImage(QImage((const unsigned char *)src->imageData,src->width, src->height, QImage::Format_RGB32)).copy();\r
136   }\r
137   else\r
138   {\r
139    IplImage *image = cvCreateImage(cvGetSize(src),8,4);\r
140    cvCvtColor(src, image, src->nChannels==1 ? CV_GRAY2BGRA : CV_BGR2BGRA);\r
141    QImage qimg = QImage((const unsigned char *)image->imageData, image->width, image->height, QImage::Format_RGB32);\r
142    pixmap = QPixmap::fromImage(qimg).copy();\r
143    cvReleaseImage(&image);\r
144   }\r
145   return pixmap;\r
146  }\r
147  */\r
148         static void drawCross(QPainter &painter, QPointF point, float radius, int label)\r
149         {\r
150                 float x = point.x();\r
151                 float y = point.y();\r
152                 QColor c1 = Qt::white;\r
153                 QColor c2 = Qt::black;\r
154 \r
155                 switch(label)\r
156                 {\r
157                 case 0:\r
158                         c1 = Qt::white;\r
159                         c2 = Qt::black;\r
160                         break;\r
161                 case 1:\r
162                         c1 = Qt::black;\r
163                         c2 = Qt::white;\r
164                         break;\r
165                 case 2:\r
166                         c1 = Qt::red;\r
167                         c2 = Qt::black;\r
168                         break;\r
169                 case 3:\r
170                         c1 = Qt::blue;\r
171                         c2 = Qt::white;\r
172                         break;\r
173                 default:\r
174                         srand(label);\r
175                         c1 = QColor(rand()%255,rand()%255,rand()%255);\r
176                         c2 = Qt::black;\r
177                         break;\r
178                 }\r
179 \r
180                 QPen pen = painter.pen();\r
181                 pen.setColor(c1);pen.setWidth(3);\r
182                 painter.setPen(pen);\r
183                 painter.drawLine(QPointF(x-radius, y-radius), QPointF(x+radius, y+radius));\r
184                 painter.drawLine(QPointF(x+radius, y-radius), QPointF(x-radius, y+radius));\r
185                 pen.setColor(c2);pen.setWidth(1);\r
186                 painter.setPen(pen);\r
187                 painter.drawLine(QPointF(x-radius, y-radius), QPointF(x+radius, y+radius));\r
188                 painter.drawLine(QPointF(x+radius, y-radius), QPointF(x-radius, y+radius));\r
189         }\r
190 \r
191         static void drawSample(QPainter &painter, QPointF point, float radius, int label)\r
192         {\r
193                 float x = point.x();\r
194                 float y = point.y();\r
195 \r
196                 if(label == 0)\r
197                 {\r
198                         //              radius = 10;\r
199                         painter.setBrush(Qt::white);\r
200                         painter.setPen(Qt::black);\r
201                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
202                 }\r
203                 else if(label == 1)\r
204                 {\r
205                         //              radius = 10;\r
206                         painter.setBrush(Qt::black);\r
207                         painter.setPen(Qt::white);\r
208                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
209                 }\r
210                 else if(label == 2)\r
211                 {\r
212                         //              radius = 10;\r
213                         painter.setBrush(Qt::red);\r
214                         painter.setPen(Qt::black);\r
215                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
216                 }\r
217                 else if(label == 3)\r
218                 {\r
219                         //              radius = 10;\r
220                         painter.setBrush(Qt::blue);\r
221                         painter.setPen(Qt::white);\r
222                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
223                 }\r
224                 else\r
225                 {\r
226                         //              radius = 10;\r
227                         srand(label);\r
228                         painter.setBrush(QColor(rand()%255,rand()%255,rand()%255));\r
229                         painter.setPen(Qt::black);\r
230                         painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
231                 }\r
232         }\r
233 };\r
234 \r
235 #endif // _CANVAS_H_\r