v0.3.9b:
[mldemos:baraks-mldemos.git] / Core / 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 struct Pixmaps\r
32 {\r
33         int w, h;\r
34         QPixmap confidence;\r
35         QPixmap reward;\r
36         QPixmap model;\r
37         QPixmap info;\r
38         QPixmap grid;\r
39         QPixmap samples;\r
40         QPixmap trajectories;\r
41         QPixmap obstacles;\r
42         QPixmap timeseries;\r
43         void clear() {\r
44                 confidence = QPixmap();\r
45                 reward = QPixmap();\r
46                 model = QPixmap();\r
47                 info = QPixmap();\r
48                 grid = QPixmap();\r
49                 samples = QPixmap();\r
50                 trajectories = QPixmap();\r
51                 obstacles = QPixmap();\r
52                 timeseries = QPixmap();\r
53         }\r
54 };\r
55 \r
56 class Canvas : public QWidget\r
57 {\r
58         Q_OBJECT\r
59 \r
60 public:\r
61         Canvas(QWidget *parent);\r
62         ~Canvas();\r
63 \r
64         bool DeleteData(QPointF center, float radius);\r
65         static bool bCrossesAsDots;\r
66         void DrawSamples();\r
67         void DrawObstacles();\r
68         void DrawTrajectories();\r
69         void DrawTimeseries();\r
70         void DrawRewards();\r
71         void DrawObstacles(QPainter &painter);\r
72         void DrawTrajectories(QPainter &painter);\r
73         void DrawSamples(QPainter &painter);\r
74         void DrawTargets(QPainter &painter);\r
75         void DrawLiveTrajectory(QPainter &painter);\r
76         void ResetSamples(){drawnSamples = 0; drawnTrajectories = 0; drawnTimeseries = 0;}\r
77         void FitToData();\r
78         void DrawAxes(QPainter &painter);\r
79         void RedrawAxes();\r
80     void SetCanvasType(int);\r
81 \r
82     void PaintGaussian(QPointF position, double variance);\r
83         void PaintReward(fvec sample, float radius, float shift);\r
84         void PaintGradient(QPointF position);\r
85         bool bDrawing;\r
86         QPainterPath DrawObstacle(Obstacle o);\r
87         fvec center;\r
88         float zoom;\r
89         fvec zooms;\r
90         int xIndex, yIndex;\r
91         std::vector<fvec> targets;\r
92     int canvasType;\r
93 \r
94 protected:\r
95         void paintEvent(QPaintEvent *event);\r
96         void mousePressEvent(QMouseEvent *event);\r
97         void wheelEvent(QWheelEvent *event);\r
98         void mouseReleaseEvent(QMouseEvent *event);\r
99         void mouseMoveEvent(QMouseEvent *event);\r
100         void enterEvent(QEvent *event);\r
101         void focusOutEvent(QFocusEvent *event);\r
102         void leaveEvent(QEvent *event);\r
103         void dragEnterEvent(QDragEnterEvent *);\r
104         void dropEvent(QDropEvent *);\r
105 \r
106 public:\r
107         DatasetManager *data;\r
108         Pixmaps maps;\r
109         QPixmap& confidencePixmap(){return maps.confidence;};\r
110         QPixmap& rewardPixmap(){return maps.reward;};\r
111         QPixmap& modelPixmap(){return maps.model;};\r
112         QPixmap& infoPixmap(){return maps.info;};\r
113         QPixmap& gridPixmap(){return maps.grid;};\r
114         QPixmap& samplesPixmap(){return maps.samples;};\r
115         QPixmap& trajectoriesPixmap(){return maps.trajectories;};\r
116         QPixmap& obstaclesPixmap(){return maps.obstacles;};\r
117         QImage qimg;\r
118         QPainterPath crosshair;\r
119         bool bDisplayMap, bDisplayInfo, bDisplaySingle, bDisplaySamples;\r
120         bool bDisplayTrajectories, bDisplayLearned, bDisplayGrid, bDisplayTimeSeries;\r
121         bool bShowCrosshair, bNewCrosshair;\r
122         int trajectoryCenterType, trajectoryResampleType, trajectoryResampleCount;\r
123         QPoint mouse, mouseAnchor;\r
124         fvec fromCanvas(QPointF point);\r
125         fvec fromCanvas(float x, float y);\r
126         QPointF toCanvasCoords(float x, float y);\r
127         QPointF toCanvasCoords(fvec sample);\r
128         QPointF toCanvas(fVec sample);\r
129         fvec toSampleCoords(QPointF point);\r
130         fvec toSampleCoords(float x, float y);\r
131         fvec canvasTopLeft();\r
132         fvec canvasBottomRight();\r
133         QRectF canvasRect();\r
134         void SetZoom(float zoom);\r
135         void SetZoom(fvec zooms);\r
136         float GetZoom(){return zoom;}\r
137         void SetCenter(fvec center);\r
138         fvec GetCenter(){return center;}\r
139         void SetDim(int xIndex, int yIndex);\r
140 \r
141         std::map<int,fvec> centers;\r
142         int drawnSamples;\r
143         int drawnTrajectories;\r
144         int drawnTimeseries;\r
145         std::vector<fvec> liveTrajectory;\r
146 \r
147     void PaintStandard(QPainter &painter, bool bSvg=false);\r
148     void PaintMultivariate(QPainter &painter, int type);\r
149 \r
150         bool SaveScreenshot(QString filename);\r
151         QPixmap GetScreenshot();\r
152 \r
153 public slots:\r
154         void ResizeEvent();\r
155         void SetConfidenceMap(QImage image);\r
156         void SetModelImage(QImage image);\r
157 \r
158 signals:\r
159         void DrawCrosshair();\r
160         void Drawing(fvec sample, int label);\r
161         void CanvasMoveEvent();\r
162         void Released();\r
163         void Navigation(fvec sample);\r
164 \r
165 public:\r
166         /*\r
167  static QPixmap toPixmap(IplImage *src)\r
168  {\r
169   QPixmap pixmap;\r
170   if(src->nChannels == 4)\r
171   {\r
172    pixmap = QPixmap::fromImage(QImage((const unsigned char *)src->imageData,src->width, src->height, QImage::Format_RGB32)).copy();\r
173   }\r
174   else\r
175   {\r
176    IplImage *image = cvCreateImage(cvGetSize(src),8,4);\r
177    cvCvtColor(src, image, src->nChannels==1 ? CV_GRAY2BGRA : CV_BGR2BGRA);\r
178    QImage qimg = QImage((const unsigned char *)image->imageData, image->width, image->height, QImage::Format_RGB32);\r
179    pixmap = QPixmap::fromImage(qimg).copy();\r
180    cvReleaseImage(&image);\r
181   }\r
182   return pixmap;\r
183  }\r
184  */\r
185         static void drawCross(QPainter &painter, QPointF point, float radius, int label)\r
186         {\r
187                 float x = point.x();\r
188                 float y = point.y();\r
189 \r
190                 QColor c1 = SampleColor[label%SampleColorCnt];\r
191                 QColor c2 = Qt::black;\r
192                 if(label == 1)\r
193                 {\r
194                         c1 = Qt::black;\r
195                         c2 = Qt::white;\r
196                 }\r
197 \r
198                 QPen pen = painter.pen();\r
199                 pen.setColor(c1);pen.setWidth(3);\r
200                 painter.setPen(pen);\r
201                 painter.drawLine(QPointF(x-radius, y-radius), QPointF(x+radius, y+radius));\r
202                 painter.drawLine(QPointF(x+radius, y-radius), QPointF(x-radius, y+radius));\r
203                 pen.setColor(c2);pen.setWidth(1);\r
204                 painter.setPen(pen);\r
205                 painter.drawLine(QPointF(x-radius, y-radius), QPointF(x+radius, y+radius));\r
206                 painter.drawLine(QPointF(x+radius, y-radius), QPointF(x-radius, y+radius));\r
207         }\r
208 \r
209         static void drawSample(QPainter &painter, QPointF point, float radius, int label)\r
210         {\r
211                 float x = point.x();\r
212                 float y = point.y();\r
213 \r
214                 QColor color = SampleColor[label%SampleColorCnt];\r
215                 QColor edge = Qt::black;\r
216         //if(label == 1)\r
217         //{\r
218         //      color = Qt::black;\r
219         //      edge = Qt::white;\r
220         //}\r
221                 //              radius = 10;\r
222                 painter.setBrush(color);\r
223                 painter.setPen(edge);\r
224                 painter.drawEllipse(QRectF(x-radius/2.,y-radius/2.,radius,radius));\r
225 \r
226         }\r
227 };\r
228 \r
229 #endif // _CANVAS_H_\r