pathplayer.cc 11.2 KB
Newer Older
Joseph Mirabel's avatar
Joseph Mirabel committed
1
#include "hppwidgetsplugin/pathplayer.hh"
Joseph Mirabel's avatar
Joseph Mirabel committed
2
3
4
5

#include <hpp/corbaserver/common.hh>
#include <hpp/corbaserver/client.hh>

6
7
8
#include "gepetto/gui/mainwindow.hh"
#include <gepetto/gui/windows-manager.hh>
#include <gepetto/gui/osgwidget.hh>
Joseph Mirabel's avatar
Joseph Mirabel committed
9

10
#include "hppwidgetsplugin/conversions.hh"
11
#include "hppwidgetsplugin/ui_pathplayerwidget.h"
12

Joseph Mirabel's avatar
Joseph Mirabel committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace hpp {
  namespace gui {
    PathPlayer::PathPlayer (HppWidgetsPlugin *plugin, QWidget *parent) :
      QWidget (parent)
      , ui_ (new ::Ui::PathPlayerWidget)
      , frameRate_ (25)
      , process_ (new QProcess (this))
      , showPOutput_ (new QDialog (this, Qt::Dialog | Qt::WindowCloseButtonHint | Qt::WindowMinMaxButtonsHint))
      , pOutput_ (new QTextBrowser())
      , plugin_ (plugin)
    {
      ui_->setupUi (this);
      pathIndex()->setMaximum(0);
      connect (pathSlider(), SIGNAL (sliderMoved (int)), this, SLOT (pathSliderChanged(int)));
      connect (pathIndex(), SIGNAL (valueChanged(int)), this, SLOT (pathIndexChanged(int)));
      connect (playPause(), SIGNAL (toggled (bool)), this, SLOT (playPauseToggled(bool)));
      connect (stop(), SIGNAL (clicked()), this, SLOT (stopClicked()));
      connect (record(), SIGNAL (toggled(bool)), this, SLOT (recordToggled(bool)));
      connect (ui_->refreshButton_path, SIGNAL (clicked()), this, SLOT (update()));

      process_->setProcessChannelMode(QProcess::MergedChannels);
      connect (process_, SIGNAL (readyReadStandardOutput ()), SLOT (readyReadProcessOutput()));

      showPOutput_->setModal(false);
      showPOutput_->setLayout(new QHBoxLayout ());
      showPOutput_->layout()->addWidget(pOutput_);
Joseph Mirabel's avatar
Joseph Mirabel committed
39
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
40
41
42
43
44
45
46
47
48

    PathPlayer::~PathPlayer()
    {
      if (process_ != NULL) {
        if (process_->state() == QProcess::Running)
          process_->kill();
        delete process_;
      }
      delete ui_;
Joseph Mirabel's avatar
Joseph Mirabel committed
49
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
50
51
52

    void PathPlayer::displayWaypointsOfPath(const std::string jointName)
    {
53
      gepetto::gui::MainWindow* main = gepetto::gui::MainWindow::instance();
Joseph Mirabel's avatar
Joseph Mirabel committed
54
55
56
57
58
59
60
61
62
      if (!pathIndex()->isEnabled()) {
        main->logError("There is no path. Did you solve a problem ?");
        return;
      }
      int pid = pathIndex()->value();
      std::stringstream ss; ss << "path" << pid << "_" << jointName;
      std::string pn = ss.str();
      float colorN[] = {0.f, 0.f, 1.f, 1.f};
      float colorE[] = {1.f, 0.f, 0.f, 1.f};
63
      gepetto::gui::WindowsManagerPtr_t wsm = main->osg();
Joseph Mirabel's avatar
Joseph Mirabel committed
64
65
66
67
68
69
70
71
72
      HppWidgetsPlugin::HppClient* hpp = plugin_->client();
      hpp::floatSeqSeq_var waypoints = hpp->problem()->getWaypoints((CORBA::UShort)pid);
      wsm->createScene (pn.c_str());
      hpp::floatSeq_var curCfg = hpp->robot()->getCurrentConfig();
      for (unsigned int i = 0; i < waypoints->length(); ++i) {
        float pos[7];
        float pos1[3], pos2[3];
        hpp->robot()->setCurrentConfig(waypoints[i]);
        hpp::Transform__var t = hpp->robot()->getLinkPosition(jointName.c_str());
73
        fromHPP(t, pos);
Joseph Mirabel's avatar
Joseph Mirabel committed
74
        for (int j = 0; j < 3; ++j) { pos1[j] = pos2[j]; }
75
        for (int j = 0; j < 3; ++j) { pos2[j] = pos[j]; }
Joseph Mirabel's avatar
Joseph Mirabel committed
76
77
78
79
        QString xyzName = QString::fromStdString(pn).append("/node%1").arg (i);
        wsm->addXYZaxis(xyzName.toLocal8Bit().data(), colorN, 0.01f, 1.f);
        wsm->applyConfiguration(xyzName.toLocal8Bit().data(), pos);
        if  (i > 0) {
80
81
82
          QString lineName = QString::fromStdString(pn).append("/edge%1").arg (i);
          wsm->addLine(lineName.toLocal8Bit().data(), pos1, pos2, colorE);
        }
Joseph Mirabel's avatar
Joseph Mirabel committed
83
84
85
86
      }
      hpp->robot()->setCurrentConfig(curCfg.in());
      wsm->addToGroup(pn.c_str(), "hpp-gui");
      wsm->refresh();
87
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
88
89
90
91
92
93
94
95
96
97
98
99

    void PathPlayer::displayPath(const std::string jointName)
    {
      QFutureWatcher <void>* fw = new QFutureWatcher<void>(this);
      QProgressDialog* pd = new QProgressDialog ("Computing curved path. Please wait...", "Cancel", 0, 100, this);
      connect(this, SIGNAL(displayPath_status(int)), pd, SLOT (setValue(int)));
      pd->setCancelButton(0);
      pd->setRange(0, 100);
      pd->show();
      fw->setFuture(QtConcurrent::run (this, &PathPlayer::displayPath_impl, jointName));
      connect (fw, SIGNAL (finished()), pd, SLOT (deleteLater()));
      connect (fw, SIGNAL (finished()), fw, SLOT (deleteLater()));
Joseph Mirabel's avatar
Joseph Mirabel committed
100
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
101
102
103

    void PathPlayer::displayPath_impl(const std::string jointName)
    {
104
      gepetto::gui::MainWindow* main = gepetto::gui::MainWindow::instance();
Joseph Mirabel's avatar
Joseph Mirabel committed
105
106
107
108
109
110
111
112
113
      if (!pathIndex()->isEnabled()) {
        main->logError("There is no path. Did you solve a problem ?");
        emit displayPath_status(100);
        return;
      }
      CORBA::UShort pid = (CORBA::UShort) pathIndex()->value();
      std::stringstream ss; ss << "curvedpath_" << pid << "_" << jointName;
      std::string pn = ss.str();
      float colorE[] = {1.f, 0.f, 0.f, 1.f};
114
      gepetto::gui::WindowsManagerPtr_t wsm = main->osg();
Joseph Mirabel's avatar
Joseph Mirabel committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
      HppWidgetsPlugin::HppClient* hpp = plugin_->client();
      hpp::floatSeq_var curCfg = hpp->robot()->getCurrentConfig();
      CORBA::Double length = hpp->problem()->pathLength(pid);
      double dt = lengthBetweenRefresh();
      std::size_t nbPos = (std::size_t)(length / dt) + 1;
      gepetto::corbaserver::PositionSeq_var posSeq = new gepetto::corbaserver::PositionSeq (nbPos);
      posSeq->length(nbPos);
      float statusStep = 100.f / (float) nbPos;
      emit displayPath_status(0);
      for (std::size_t i = 0; i < nbPos; ++i) {
        double t = std::min (i * dt, length);
        hpp::floatSeq_var q = hpp->problem()->configAtParam(pid, t);
        hpp->robot()->setCurrentConfig(q);
        hpp::Transform__var transform = hpp->robot()->getLinkPosition(jointName.c_str());
        for (int j = 0; j < 3; ++j) { posSeq[i][j] = (float)transform.in()[j]; }
        emit displayPath_status(qFloor ((float)i * statusStep));
      }
      wsm->addCurve(pn.c_str(), posSeq.in(), colorE);
      hpp->robot()->setCurrentConfig(curCfg.in());
      wsm->addToGroup(pn.c_str(), "hpp-gui");
      wsm->refresh();
      emit displayPath_status (100);
Joseph Mirabel's avatar
Joseph Mirabel committed
137
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
138
139

    void PathPlayer::update ()
Joseph Mirabel's avatar
Joseph Mirabel committed
140
    {
Joseph Mirabel's avatar
Joseph Mirabel committed
141
142
143
144
145
146
147
148
      CORBA::Short nbPath = plugin_->client()->problem ()->numberPaths ();
      if (nbPath > 0)
      {
        pathIndex()->setEnabled(true);
        pathSlider()->setEnabled(true);
        playPause()->setEnabled(true);
        stop()->setEnabled(true);
        if (pathIndex()->maximum() == 0) {
Joseph Mirabel's avatar
Joseph Mirabel committed
149
150
151
          // If path index value is 0, no signal valueChanged will
          // be emitted. Force a value changed.
          if (pathIndex()->value() == 0)
152
            pathIndexChanged(0);
Joseph Mirabel's avatar
Joseph Mirabel committed
153
        }
Joseph Mirabel's avatar
Joseph Mirabel committed
154
        pathIndex()->setMaximum(nbPath - 1);
155
	pathIndex()->setValue(nbPath - 1);
Joseph Mirabel's avatar
Joseph Mirabel committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
      }
      else {
        pathIndex()->setEnabled(false);
        pathSlider()->setEnabled(false);
        playPause()->setEnabled(false);
        stop()->setEnabled(false);
      }
    }

    void PathPlayer::pathIndexChanged(int i)
    {
      assert (i >= 0);
      pathLength_ = plugin_->client()->problem()->pathLength ((short unsigned int)i);
      currentParam_= 0;
    }

    void PathPlayer::pathSliderChanged(int value)
    {
      // The user moved the slider manually.
      currentParam_ = sliderToLength(value);
      updateConfiguration();
Joseph Mirabel's avatar
Joseph Mirabel committed
177
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
178
179
180
181
182

    void PathPlayer::playPauseToggled(bool toggled)
    {
      if (toggled) timerId_ = startTimer(0);
      else killTimer(timerId_);
183
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
184
185
186
187
188
189
190
191
192
193
194
195
196

    void PathPlayer::stopClicked()
    {
      // Pause
      playPause()->setChecked(false);
      // Reset current position.
      currentParam_ = 0;
      pathSlider()->setSliderPosition(0);
      updateConfiguration();
    }

    void PathPlayer::recordToggled(bool toggled)
    {
197
      gepetto::gui::MainWindow* main = gepetto::gui::MainWindow::instance();
Joseph Mirabel's avatar
Joseph Mirabel committed
198
199
200
201
      if (toggled) {
        QDir tmp ("/tmp");
        tmp.mkpath ("hpp-gui/record"); tmp.cd("hpp-gui/record");
        foreach (QString f, tmp.entryList(QStringList() << "img_0_*.jpeg", QDir::Files))
Joseph Mirabel's avatar
Joseph Mirabel committed
202
          tmp.remove(f);
Joseph Mirabel's avatar
Joseph Mirabel committed
203
204
205
        QString path = tmp.absoluteFilePath("img");
        QString ext = "jpeg";
        main->osg ()->startCapture(
Joseph Mirabel's avatar
Joseph Mirabel committed
206
207
208
            main->centralWidget()->windowID(),
            path.toLocal8Bit().data(),
            ext.toLocal8Bit().data());
Joseph Mirabel's avatar
Joseph Mirabel committed
209
210
211
212
213
        main->log("Saving images to " + path + "_*." + ext);
      } else {
        main->osg()->stopCapture(main->centralWidget()->windowID());
        QString outputFile = QFileDialog::getSaveFileName(this, tr("Save video to"), "untitled.mp4");
        if (!outputFile.isNull()) {
Joseph Mirabel's avatar
Joseph Mirabel committed
214
          if (QFile::exists(outputFile))
Joseph Mirabel's avatar
Joseph Mirabel committed
215
            QFile::remove(outputFile);
Joseph Mirabel's avatar
Joseph Mirabel committed
216
217
218
219
220
          QString avconv = "avconv";

          QStringList args;
          QString input = "/tmp/hpp-gui/record/img_0_%d.jpeg";
          args << "-r" << "50"
Joseph Mirabel's avatar
Joseph Mirabel committed
221
222
223
224
225
            << "-i" << input
            << "-vf" << "scale=trunc(iw/2)*2:trunc(ih/2)*2"
            << "-r" << "25"
            << "-vcodec" << "libx264"
            << outputFile;
Joseph Mirabel's avatar
Joseph Mirabel committed
226
227
228
229
230
231
232
233
          qDebug () << args;

          showPOutput_->setWindowTitle(avconv + " " + args.join(" "));
          pOutput_->clear();
          showPOutput_->resize(main->size() / 2);
          showPOutput_->show();
          process_->start(avconv, args);
        }
Joseph Mirabel's avatar
Joseph Mirabel committed
234
      }
Joseph Mirabel's avatar
Joseph Mirabel committed
235
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
236
237
238
239
240
241
242

    void PathPlayer::pathPulse()
    {
      if (playPause()->isChecked()) {
        // Stil playing
        currentParam_ += lengthBetweenRefresh();
        if (currentParam_ > pathLength_) {
Joseph Mirabel's avatar
Joseph Mirabel committed
243
          currentParam_ = pathLength_;
Joseph Mirabel's avatar
Joseph Mirabel committed
244
245
          playPause()->setChecked(false);
        }
Joseph Mirabel's avatar
Joseph Mirabel committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
        else
          timerId_ = startTimer(timeBetweenRefresh());
        pathSlider()->setSliderPosition(lengthToSlider(currentParam_));
        updateConfiguration();
      }
    }

    void PathPlayer::timerEvent(QTimerEvent *event)
    {
      if (timerId_ == event->timerId()) {
        pathPulse();
      }
    }

    void PathPlayer::readyReadProcessOutput()
    {
      pOutput_->append(process_->readAll());
Joseph Mirabel's avatar
Joseph Mirabel committed
263
264
    }

Joseph Mirabel's avatar
Joseph Mirabel committed
265
266
267
268
269
    void PathPlayer::updateConfiguration ()
    {
      hpp::floatSeq_var config =
        plugin_->client()->problem()->configAtParam ((short unsigned int)pathIndex()->value(),currentParam_);
      plugin_->client()->robot()->setCurrentConfig (config.in());
270
      gepetto::gui::MainWindow::instance()->requestApplyCurrentConfiguration();
Joseph Mirabel's avatar
Joseph Mirabel committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    }

    inline double PathPlayer::sliderToLength(int v) const
    {
      return ((double)v / (double)pathSlider()->maximum()) * pathLength_;
    }

    int PathPlayer::lengthToSlider(double l) const
    {
      return (int) (pathSlider()->maximum() * l / pathLength_);
    }

    inline double PathPlayer::timeToLength(double time) const
    {
      return time * pathLength_ / timeSpinBox()->value();
    }

    inline int PathPlayer::timeBetweenRefresh() const
    {
      return 1000/frameRate_;
    }

    double PathPlayer::lengthBetweenRefresh() const
    {
      return pathLength_ / (timeSpinBox()->value() * frameRate_);
    }

    QDoubleSpinBox *PathPlayer::timeSpinBox() const
    {
      return ui_->timeSpinBox;
    }

    QSpinBox *PathPlayer::pathIndex() const
    {
      return ui_->pathIndexSpin;
    }

    QSlider *PathPlayer::pathSlider() const
    {
      return ui_->pathSlider;
    }

    QPushButton *PathPlayer::playPause() const
    {
      return ui_->playPauseButton;
    }

    QPushButton *PathPlayer::stop() const
    {
      return ui_->stopButton;
    }

    QPushButton *PathPlayer::record() const
    {
      return ui_->recordButton;
Joseph Mirabel's avatar
Joseph Mirabel committed
326
    }
327
328
329
330
331

    int	PathPlayer::getCurrentPath() const
    {
      return pathIndex()->value();
    }
Joseph Mirabel's avatar
Joseph Mirabel committed
332
333
  } // namespace gui
} // namespace hpp