v0.3.9b:
[mldemos:baraks-mldemos.git] / Core / drawSVG.cpp
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\r
8 License as published by the Free Software Foundation; either\r
9 version 2.1 of the License, or (at your option) any later version.\r
10 \r
11 This library is distributed in the hope that it will be useful,\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
14 Library General Public License for more details.\r
15 \r
16 You should have received a copy of the GNU Lesser General Public\r
17 License along with this library; if not, write to the Free\r
18 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
19 *********************************************************************/\r
20 #include <QtSvg>\r
21 #include <QtGui>\r
22 #include <QWidget>\r
23 #include <QSize>\r
24 #include <QPixmap>\r
25 #include <QDebug>\r
26 #include <QMutexLocker>\r
27 \r
28 #include "public.h"\r
29 #include "basicMath.h"\r
30 #include "drawSVG.h"\r
31 \r
32 using namespace std;\r
33 \r
34 DrawSVG::DrawSVG(Canvas *canvas, QMutex *mutex)\r
35         : canvas(canvas),\r
36           classifier(0), regressor(0), dynamical(0), clusterer(0),\r
37           drawClass(0), drawRegr(0), drawDyn(0), drawClust(0),\r
38           mutex(mutex),\r
39           perm(0), w(0), h(0)\r
40 {\r
41 }\r
42 \r
43 DrawSVG::~DrawSVG()\r
44 {\r
45 \r
46 }\r
47 \r
48 void DrawSVG::Write(QString filename)\r
49 {\r
50         if(!canvas) return;\r
51         QSvgGenerator generator;\r
52         generator.setFileName(filename);\r
53         generator.setSize(QSize(canvas->width(), canvas->height()));\r
54         generator.setTitle("MLDemos screenshot");\r
55         generator.setDescription("Generated with MLDemos");\r
56         QPainter painter;\r
57         painter.begin(&generator);\r
58         // we need to paint the different layers:\r
59         // confidence map\r
60         // samples + trajectories + reward\r
61         canvas->PaintStandard(painter, true);\r
62 \r
63         if(canvas->bDisplayLearned)\r
64         {\r
65                 // learned model\r
66                 if(classifier) drawClass->DrawModel(canvas, painter, classifier);\r
67                 if(regressor) drawRegr->DrawModel(canvas, painter, regressor);\r
68                 if(dynamical) drawDyn->DrawModel(canvas, painter, dynamical);\r
69                 if(clusterer) drawClust->DrawModel(canvas, painter, clusterer);\r
70                 if(dynamical)\r
71                 {\r
72                         int cnt = 10000; // not too many or it will make unreadable files\r
73                         int steps = 8;\r
74                         VectorsFast(cnt, steps, painter);\r
75                 }\r
76                 if(maximizer)\r
77                 {\r
78                         Maximization(painter);\r
79                 }\r
80         }\r
81 \r
82         if(canvas->bDisplayInfo)\r
83         {\r
84                 // model info\r
85                 if(classifier) drawClass->DrawInfo(canvas, painter, classifier);\r
86                 if(regressor) drawRegr->DrawInfo(canvas, painter, regressor);\r
87                 if(dynamical) drawDyn->DrawInfo(canvas, painter, dynamical);\r
88                 if(clusterer) drawClust->DrawInfo(canvas, painter, clusterer);\r
89         }\r
90         painter.end();\r
91 }\r
92 \r
93 void DrawSVG::Vectors(int count, int steps, QPainter &painter)\r
94 {\r
95         if(!dynamical) return;\r
96         float dT = dynamical->dT;// * (dynamical->count/100.f);\r
97         //float dT = 0.02f;\r
98         fvec sample;\r
99         sample.resize(2,0);\r
100         int w = canvas->width();\r
101         int h = canvas->height();\r
102 \r
103         painter.setRenderHint(QPainter::Antialiasing, true);\r
104         painter.setRenderHint(QPainter::HighQualityAntialiasing, true);\r
105         vector<Obstacle> obstacles = canvas->data->GetObstacles();\r
106 \r
107         QPointF oldPoint(-FLT_MAX,-FLT_MAX);\r
108         QPointF oldPointUp(-FLT_MAX,-FLT_MAX);\r
109         QPointF oldPointDown(-FLT_MAX,-FLT_MAX);\r
110         FOR(i, count)\r
111         {\r
112                 QPointF samplePre(rand()/(float)RAND_MAX * w, rand()/(float)RAND_MAX * h);\r
113                 sample = canvas->toSampleCoords(samplePre);\r
114                 float color = (rand()/(float)RAND_MAX*0.7f)*255.f;\r
115                 color = 0;\r
116                 QPointF oldPoint = canvas->toCanvasCoords(sample);\r
117                 FOR(j, steps)\r
118                 {\r
119                         fvec res = dynamical->Test(sample);\r
120                         if(dynamical->avoid)\r
121                         {\r
122                                 dynamical->avoid->SetObstacles(obstacles);\r
123                                 fvec newRes = dynamical->avoid->Avoid(sample, res);\r
124                                 res = newRes;\r
125                         }\r
126                         sample += res*dT;\r
127                         float speed = sqrtf(res[0]*res[0] + res[1]*res[1]);\r
128                         QPointF point = canvas->toCanvasCoords(sample);\r
129                         painter.setOpacity(speed);\r
130                         QColor c(color,color,color);\r
131                         painter.setPen(QPen(c, 0.25));\r
132                         painter.drawLine(point, oldPoint);\r
133                         oldPoint = point;\r
134                 }\r
135         }\r
136 }\r
137 \r
138 void DrawSVG::Maximization(QPainter &painter)\r
139 {\r
140         if(!maximizer) return;\r
141         painter.setRenderHint(QPainter::Antialiasing, true);\r
142         painter.setRenderHint(QPainter::HighQualityAntialiasing, true);\r
143         maximizer->Draw(painter);\r
144 }\r
145 \r
146 void DrawSVG::VectorsFast(int count, int steps, QPainter &painter)\r
147 {\r
148         if(!dynamical) return;\r
149         QPointF oldPoint(-FLT_MAX,-FLT_MAX);\r
150         QPointF oldPointUp(-FLT_MAX,-FLT_MAX);\r
151         QPointF oldPointDown(-FLT_MAX,-FLT_MAX);\r
152         float dT = dynamical->dT;// * (dynamical->count/100.f);\r
153         //float dT = 0.02f;\r
154         fvec sample;\r
155         sample.resize(2,0);\r
156         int w = canvas->width();\r
157         int h = canvas->height();\r
158 \r
159         painter.setRenderHint(QPainter::Antialiasing, true);\r
160         painter.setRenderHint(QPainter::HighQualityAntialiasing, true);\r
161         vector<Obstacle> obstacles = canvas->data->GetObstacles();\r
162         FOR(i, count)\r
163         {\r
164                 QPointF samplePre(rand()/(float)RAND_MAX * w, rand()/(float)RAND_MAX * h);\r
165                 sample = canvas->toSampleCoords(samplePre);\r
166                 float color = (rand()/(float)RAND_MAX*0.7f)*255.f;\r
167                 color = 0;\r
168                 QPointF oldPoint = canvas->toCanvasCoords(sample);\r
169                 FOR(j, steps)\r
170                 {\r
171                         fvec res = dynamical->Test(sample);\r
172                         if(dynamical->avoid)\r
173                         {\r
174                                 dynamical->avoid->SetObstacles(obstacles);\r
175                                 fvec newRes = dynamical->avoid->Avoid(sample, res);\r
176                                 res = newRes;\r
177                         }\r
178                         sample += res*dT;\r
179                         float speed = sqrtf(res[0]*res[0] + res[1]*res[1]);\r
180                         QPointF point = canvas->toCanvasCoords(sample);\r
181                         painter.setOpacity(speed);\r
182                         QColor c(color,color,color);\r
183                         painter.setPen(QPen(c, 0.25));\r
184                         painter.drawLine(point, oldPoint);\r
185                         oldPoint = point;\r
186                 }\r
187         }\r
188 }\r