- Integrated the file import into the main interface (not as a plugin anymore)
[mldemos:mldemos.git] / _AlgorithmsPlugins / GMM / interfaceGMMDynamic.cpp
1 /*********************************************************************
2 MLDemos: A User-Friendly visualization toolkit for machine learning
3 Copyright (C) 2010  Basilio Noris
4 Contact: mldemos@b4silio.com
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License,
8 version 3 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *********************************************************************/
19 #include "interfaceGMMDynamic.h"
20 #include "drawUtils.h"
21 #include <QPixmap>
22 #include <QBitmap>
23 #include <QPainter>
24
25 using namespace std;
26
27 DynamicGMM::DynamicGMM()
28 {
29         params = new Ui::ParametersGMMDynamic();
30         params->setupUi(widget = new QWidget());
31 }
32
33 void DynamicGMM::SetParams(Dynamical *dynamical)
34 {
35         if(!dynamical) return;
36         int clusters = params->gmmCount->value();
37         int covType = params->gmmCovarianceCombo->currentIndex();
38         int initType = params->gmmInitCombo->currentIndex();
39
40         ((DynamicalGMR *)dynamical)->SetParams(clusters, covType, initType);
41 }
42
43 Dynamical *DynamicGMM::GetDynamical()
44 {
45         DynamicalGMR *dynamical = new DynamicalGMR();
46         SetParams(dynamical);
47         return dynamical;
48 }
49
50 void DynamicGMM::DrawInfo(Canvas *canvas, QPainter &painter, Dynamical *dynamical)
51 {
52         if(!canvas || !dynamical) return;
53         painter.setRenderHint(QPainter::Antialiasing);
54
55     Gmm *gmm = ((DynamicalGMR*)dynamical)->gmm;
56     int xIndex = canvas->xIndex;
57     int yIndex = canvas->yIndex;
58     int dim = gmm->dim;
59     float mean[2];
60     float sigma[3];
61     painter.setBrush(Qt::NoBrush);
62     FOR(i, gmm->nstates)
63     {
64         float* bigSigma = new float[dim*dim];
65         float* bigMean = new float[dim];
66         gmm->getCovariance(i, bigSigma, false);
67         sigma[0] = bigSigma[xIndex*dim + xIndex];
68         sigma[1] = bigSigma[yIndex*dim + xIndex];
69         sigma[2] = bigSigma[yIndex*dim + yIndex];
70         gmm->getMean(i, bigMean);
71         mean[0] = bigMean[xIndex];
72         mean[1] = bigMean[yIndex];
73         delete [] bigSigma;
74         delete [] bigMean;
75         //FOR(j,4) sigma[j] = sqrt(sigma[j]);
76         painter.setPen(QPen(Qt::black, 1));
77         DrawEllipse(mean, sigma, 1, &painter, canvas);
78         painter.setPen(QPen(Qt::black, 0.5));
79         DrawEllipse(mean, sigma, 2, &painter, canvas);
80         QPointF point = canvas->toCanvasCoords(mean[0],mean[1]);
81         painter.setPen(QPen(Qt::black, 4));
82         painter.drawEllipse(point, 2, 2);
83         painter.setPen(QPen(Qt::white, 2));
84         painter.drawEllipse(point, 2, 2);
85     }
86 }
87
88 void DynamicGMM::DrawModel(Canvas *canvas, QPainter &painter, Dynamical *dynamical)
89 {
90         if(!canvas || !dynamical) return;
91 }
92
93 void DynamicGMM::SaveOptions(QSettings &settings)
94 {
95         settings.setValue("gmmCount", params->gmmCount->value());
96         settings.setValue("gmmCovariance", params->gmmCovarianceCombo->currentIndex());
97         settings.setValue("gmmInit", params->gmmInitCombo->currentIndex());
98 }
99
100 bool DynamicGMM::LoadOptions(QSettings &settings)
101 {
102         if(settings.contains("gmmCount")) params->gmmCount->setValue(settings.value("gmmCount").toFloat());
103         if(settings.contains("gmmCovariance")) params->gmmCovarianceCombo->setCurrentIndex(settings.value("gmmCovariance").toInt());
104         if(settings.contains("gmmInit")) params->gmmInitCombo->setCurrentIndex(settings.value("gmmInit").toInt());
105         return true;
106 }
107
108 void DynamicGMM::SaveParams(QTextStream &file)
109 {
110         file << "dynamicalOptions" << ":" << "gmmCount" << " " << params->gmmCount->value() << "\n";
111         file << "dynamicalOptions" << ":" << "gmmCovariance" << " " << params->gmmCovarianceCombo->currentIndex() << "\n";
112         file << "dynamicalOptions" << ":" << "gmmInit" << " " << params->gmmInitCombo->currentIndex() << "\n";
113 }
114
115 bool DynamicGMM::LoadParams(QString name, float value)
116 {
117         if(name.endsWith("gmmCount")) params->gmmCount->setValue((int)value);
118         if(name.endsWith("gmmCovariance")) params->gmmCovarianceCombo->setCurrentIndex((int)value);
119         if(name.endsWith("gmmInit")) params->gmmInitCombo->setCurrentIndex((int)value);
120         return true;
121 }