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