1 2 3 4 5 6 7 8 9 10 11 12 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
|
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2014 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#include <pml/PhysicalModel.h>
#include "LoadsSimulation.h"
#include "LoadsSimulationDriver.h"
#include "AnimationMotorAddon.h"
#include "LoadsManager.h"
#include "PMManagerDC.h"
// camiTK
#include <Application.h>
#include <MainWindow.h>
#include <InteractiveViewer.h>
using namespace camitk;
// qt
#include <QToolBar>
#include <QSettings>
#include <QTime>
#include <QTextStream>
#include <QMessageBox>
#include <QFileDialog>
#include <QLibrary>
// icons
#include "player_end.xpm"
#include "player_pause.xpm"
#include "player_play.xpm"
#include "player_rew.xpm"
#include "player_record.xpm"
//--------------- Constructor ---------------------------------
LoadsSimulation::LoadsSimulation(LoadsManager * myLoadsManager, QWidget* parent) : QDialog(parent) {
ui.setupUi(this);
myLM = myLoadsManager;
myMotor = NULL;
simulationToolBar = NULL;
// online recording
video = false;
output = false;
imageId = 0;
// create the load simulator driver and connect it
simDriver = new LoadsSimulationDriver(myLM, NULL);
simDriver->setTime(0.0);
connect(simDriver, SIGNAL(doOneStep()), this, SLOT(doOneStep()));
// get the normal bg color
bgColor = ui.tLineEdit->palette().color(QPalette::Base);
simDriver->resetTMaxToDefault();
// create the tool bar actions
rewindToolbar = new QAction(QIcon(player_rew_xpm), "rewind", this);
connect(rewindToolbar, SIGNAL(triggered()), this, SLOT(rewind()));
playToolbar = new QAction(QIcon(player_play_xpm), "simulate", this);
connect(playToolbar, SIGNAL(triggered()), this, SLOT(simulate()));
playOneStepToolbar = new QAction(QIcon(player_end_xpm), "step", this);
connect(playOneStepToolbar, SIGNAL(triggered()), this, SLOT(simulateOneStep()));
pauseToolbar = new QAction(QIcon(player_pause_xpm), "pause", this);
connect(pauseToolbar, SIGNAL(triggered()), this, SLOT(pause()));
tLineEditToolbar = new QLineEdit("time", this);
tLineEditToolbar->setReadOnly(true);
tLineEditToolbar->setText("0.0");
tLineEditToolbar->setMaximumWidth(110);
// create a new tool bar
simulationToolBar = Application::getMainWindow()->addToolBar("Simulation toolbar");
simulationToolBar->setFixedWidth(250);
simulationToolBar->addAction(rewindToolbar);
simulationToolBar->addAction(playToolbar);
simulationToolBar->addAction(playOneStepToolbar);
simulationToolBar->addAction(pauseToolbar);
simulationToolBar->addWidget(tLineEditToolbar);
// fix libpng problem: reload pixmap
ui.playPushButton->setIcon(QPixmap(player_play_xpm));
ui.pausePushButton->setIcon(QPixmap(player_pause_xpm));
ui.playOneStepPushButton->setIcon(QPixmap(player_end_xpm));
ui.rewindPushButton->setIcon(QPixmap(player_rew_xpm));
ui.recordPushButton->setIcon(QPixmap(player_record_xpm));
// get default (settings) values
QSettings & settings = Application::getSettings();
settings.beginGroup("physicalModelComponent");
QString tmax(settings.value("animationMotorAddOn/tMax").toString());
if (tmax != "") {
ui.tMaxLineEdit->setText(tmax);
simDriver->setTMax(tmax.toDouble());
}
else
ui.tMaxLineEdit->setText(QString::number(simDriver->getTMax()));
QString dt(settings.value("animationMotorAddOn/dt").toString());
if (dt != "") {
ui.dtLineEdit->setText(dt);
simDriver->setDt(dt.toDouble());
}
else
ui.dtLineEdit->setText(QString::number(simDriver->getDt()));
QString refreshdt(settings.value("animationMotorAddOn/refreshDt").toString());
if (refreshdt != "") {
ui.refreshDtLineEdit->setText(refreshdt);
simDriver->setRefreshDt(refreshdt.toDouble());
}
else
ui.refreshDtLineEdit->setText(QString::number(simDriver->getRefreshDt()));
settings.endGroup();
// update the display
updateDisplay(true);
// reset the bg color
QPalette palette;
palette.setColor(QPalette::Base, bgColor);
ui.tLineEdit->setPalette(palette); // bgColor
ui.tMaxLineEdit->setPalette(palette); // bgColor
ui.dtLineEdit->setPalette(palette); // bgColor
ui.refreshDtLineEdit->setPalette(palette); // bgColor
// set the focus to tLineEdit
ui.dtLineEdit->setFocus();
// reset elapsed
elapsed = 0.0;
// connect ui
connect(ui.tMaxLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(tMaxModified()));
connect(ui.tMaxLineEdit, SIGNAL(returnPressed()), this, SLOT(tMaxChanged()));
connect(ui.dtLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(dtModified()));
connect(ui.dtLineEdit, SIGNAL(returnPressed()), this, SLOT(dtChanged()));
connect(ui.rewindPushButton, SIGNAL(clicked()), this, SLOT(rewind()));
connect(ui.pausePushButton, SIGNAL(clicked()), this, SLOT(pause()));
connect(ui.refreshDtLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(refreshDtModified()));
connect(ui.refreshDtLineEdit, SIGNAL(returnPressed()), this, SLOT(refreshDtChanged()));
connect(ui.playOneStepPushButton, SIGNAL(clicked()), this, SLOT(simulateOneStep()));
connect(ui.addonLocationPushButton, SIGNAL(clicked()), this, SLOT(addAnimationMotorAddon()));
connect(ui.playPushButton, SIGNAL(clicked()), this, SLOT(simulate()));
connect(ui.outputPushButton, SIGNAL(clicked()), this, SLOT(chooseOutputDir()));
connect(ui.recordPushButton, SIGNAL(toggled(bool)), this, SLOT(videoToggled(bool)));
iterationCount = 0.0;
}
//--------------- Destructor ---------------------------------
LoadsSimulation::~LoadsSimulation() {
if (simDriver)
delete simDriver;
simDriver = NULL;
if (myMotor)
delete myMotor;
myMotor = NULL;
if (simulationToolBar)
delete simulationToolBar;
simulationToolBar = NULL;
video = false;
output = false;
}
//--------------- getTime ---------------------------------
double LoadsSimulation::getTime() const {
if (simDriver)
return simDriver->getTime();
else
return 0.0;
}
//--------------- init ---------------------------------
void LoadsSimulation::init() {
// try to find an add-on
if (myLM->getAnimationMotorAddonLocation() != "") {
// a location is already there...
loadAddon(myLM->getAnimationMotorAddonLocation());
}
else {
// get default (settings) values
QSettings & settings = Application::getSettings();
settings.beginGroup("physicalModelComponent");
// get the add-on if found in the settings
QString addonFile(settings.value("animationMotorAddOn/location").toString());
settings.endGroup();
if (addonFile != "")
loadAddon(addonFile);
}
// set things available or not
enableButtons(myMotor != NULL);
// update the display
updateDisplay(true);
}
//--------------- rewind -----------------------
void LoadsSimulation::rewind() {
pause();
simDriver->setLoads(NULL);
simDriver->setTime(0.0);
myMotor->restart();
updateDisplay(true);
Application::getMainWindow()->getProgressBar()->setValue(0);
imageId = 0;
ui.videoInfoLabel->setText("");
// reset elapsed
elapsed = 0.0;
iterationCount = 0.0;
ui.simulFreq->setText("0.0");
}
//--------------- pause -----------------------
void LoadsSimulation::pause() {
// stop the timer
simDriver->stopTimer();
// force display the movements and time display
double refreshDt = simDriver->getRefreshDt();
simDriver->setRefreshDt(-1);
updateDisplay(true);
simDriver->setRefreshDt(refreshDt);
}
//--------------- doOneStep -----------------------
bool LoadsSimulation::doOneStep() {
// As this method is called by a timer, it could happen that the motor
// has not finished one step of computation when the timer starts
// the method again. This results in a strange state.
// waitingForFinish act as a flag to say "wait, wait, we are
// already computing a step, we can't do many at the same time,
// take your chance next time".
// That allows the timer to be regulated by the motor computation time.
static bool waitingToFinish = false;
if (waitingToFinish) {
simDriver->slower();
return false; // bye bye, we are too busy at the moment, we haven't done anything
}
waitingToFinish = true;
// if t is ok, play it
if (simDriver->getTime() <= simDriver->getTMax()) {
// update cursor
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
// show the progress
Application::getMainWindow()->getProgressBar()->setValue((int)(simDriver->getTime() *100.0 / simDriver->getTMax()));
// keep what is the atom display state (just in case)
LoadsManager::AtomDataDisplayType previousDisplayType = myLM->getAtomDataDisplay();
// start timer
QTime chrono;
chrono.start();
// do simulation steps until the next refresh time is reached
Loads * newLoads = myMotor->doMove(simDriver->getDt(), simDriver->getNextRefreshTime());
elapsed += chrono.elapsed() / 1000.0; // elapsed is in seconds
iterationCount += simDriver->getRefreshDt() / simDriver->getDt();
double freq = iterationCount / elapsed;
ui.simulFreq->setText(QString::number(freq, 'f', 2));
simDriver->setLoads(newLoads);
// restore the atom display state
if (previousDisplayType != LoadsManager::ADD_ON)
myLM->setAtomDataDisplay(previousDisplayType);
// update time and 3D display the movements and time display
updateDisplay(false);
// record video if needed
if (video) {
// make the video
QString fn, ff;
double time = myMotor->getTime();
QTextStream fnTS(&fn);
fnTS << outputDir << "/" << myLM->getPMManagerDC()->getPhysicalModel()->getName().c_str();
// set the equivalent of sprintf(%08.3f",time)
fnTS.setRealNumberNotation(QTextStream::FixedNotation);
fnTS.setRealNumberPrecision(3);
fnTS.setFieldWidth(8);
fnTS.setPadChar('0');
fnTS << time;
fnTS << ".bmp";
// ff is just the file name w/o the directory TODO use QFileInfo
ff = fn.right(fn.lastIndexOf("/"));
InteractiveViewer::get3DViewer()->screenshot(fn);
ui.videoInfoLabel->setText(ff + " saved in " + outputDir);
qApp->processEvents();
// next one
imageId++;
}
// update cursor
QApplication::restoreOverrideCursor();
}
else {
if (simDriver->isTimerActive()) {
pause();
Application::getMainWindow()->getProgressBar()->setValue(100);
}
// recording video: tell the user what happened
if (imageId > 0) {
QMessageBox::information(NULL, "Video Images Saved",
QString("Took %1").arg(imageId) + " screenshots\nAll saved in " +
outputDir);
}
}
// release the flag, so that next time this method is called,
// a new step could be computed
waitingToFinish = false;
return true; // bye bye, we have done one step
}
//--------------- simulate -----------------------
void LoadsSimulation::simulate() {
simDriver->maxSpeed();
if (!simDriver->isTimerActive()) {
// timer wasn't active, action to be done = launch the timer
simDriver->startTimer();
} else {
// timer is active, action to be done = play one step
doOneStep();
}
}
//--------------- simulateOneStep -----------------------
void LoadsSimulation::simulateOneStep() {
// force one step to be done
while (!doOneStep())
;
}
//--------------- updateTime -----------------------
void LoadsSimulation::updateTime(bool getMotorTime) {
double t;
if (getMotorTime && myMotor != NULL) {
// get the animation motor add-on time
t = myMotor->getTime();
// force the simDriver time using t
simDriver->setTime(t);
}
else {
// get the simulation driver time
t = simDriver->getTime();
}
// refresh the time display
// tLineEdit
ui.tLineEdit->setText(QString::number(t));
tLineEditToolbar->setText(QString::number(t));
ui.tLineEdit->update();
tLineEditToolbar->update();
}
//--------------- updateDisplay -----------------------
void LoadsSimulation::updateDisplay(bool force) {
// update the 3D display
simDriver->updateDisplay(force);
// update time display
updateTime();
}
//--------------- dtChanged -----------------------
void LoadsSimulation::dtChanged() {
simDriver->setDt(ui.dtLineEdit->text().toDouble());
// reset the bgd color
QPalette palette;
palette.setColor(QPalette::Base, bgColor);
ui.dtLineEdit->setPalette(palette); // bgColor
// update the settings
QSettings & settings = Application::getSettings();
settings.beginGroup("physicalModelComponent");
settings.setValue("animationMotorAddOn/dt", ui.dtLineEdit->text());
settings.endGroup();
}
//--------------- dtModified -----------------------
void LoadsSimulation::dtModified() {
QPalette palette;
palette.setColor(QPalette::Base, QColor(255, 220, 168));
ui.dtLineEdit->setPalette(palette); // QColor(255, 220, 168)
}
//--------------- tMaxChanged -----------------------
void LoadsSimulation::tMaxChanged() {
simDriver->setTMax(ui.tMaxLineEdit->text().toDouble());
// reset the bgd color
QPalette palette;
palette.setColor(QPalette::Base, bgColor);
ui.tMaxLineEdit->setPalette(palette); // bgColor
// update the settings
QSettings & settings = Application::getSettings();
settings.beginGroup("physicalModelComponent");
settings.setValue("animationMotorAddOn/tMax", ui.tMaxLineEdit->text());
settings.endGroup();
}
//--------------- tMaxModified -----------------------
void LoadsSimulation::tMaxModified() {
QPalette palette;
palette.setColor(QPalette::Base, QColor(255, 220, 168));
ui.tMaxLineEdit->setPalette(palette); // QColor(255, 220, 168)
}
//--------------- refreshDtChanged -----------------------
void LoadsSimulation::refreshDtChanged() {
simDriver->setRefreshDt(ui.refreshDtLineEdit->text().toDouble());
// reset the bgd color
QPalette palette;
palette.setColor(QPalette::Base, bgColor);
ui.refreshDtLineEdit->setPalette(palette); // bgColor
// update the settings
QSettings & settings = Application::getSettings();
settings.beginGroup("physicalModelComponent");
settings.setValue("animationMotorAddOn/refreshDt", ui.refreshDtLineEdit->text());
settings.endGroup();
}
//--------------- refreshDtModified -----------------------
void LoadsSimulation::refreshDtModified() {
QPalette palette;
palette.setColor(QPalette::Base, QColor(255, 220, 168));
ui.refreshDtLineEdit->setPalette(palette); // QColor(255, 220, 168)
}
//--------------- addAnimationMotorAddon -----------------------
void LoadsSimulation::addAnimationMotorAddon() {
QSettings & settings = Application::getSettings();
settings.beginGroup("physicalModelComponent");
QString lastOpened = QFileInfo(settings.value("animationMotorAddOn/location").toString()).absoluteDir().absolutePath();
settings.endGroup();
QString addonFile = QFileDialog::getOpenFileName(NULL, "Open an Animation Motor Add-on", lastOpened, "(*.so *.dll *.dylib)");
if (!addonFile.isNull()) {
loadAddon(addonFile);
}
}
//--------------- loadAddon -----------------------
void LoadsSimulation::loadAddon(QString addonFile) {
PtrToAnimationMotorAddon motorAddonFunction;
addonFilename = addonFile;
// try to load the add-on: first, look for the name
motorAddonFunction = NULL;
motorAddonFunction = (PtrToAnimationMotorAddon) QLibrary::resolve(addonFile, "getAnimationMotorAddon");
// if the function is found, get the motor!
if (motorAddonFunction) {
// remove all tabs but the first one
QWidget * motorWidget = ui.simulationTabs->widget(2);
while (motorWidget != NULL) {
ui.simulationTabs->removeTab(ui.simulationTabs->indexOf(motorWidget));
delete motorWidget;
motorWidget = ui.simulationTabs->widget(2);
}
// delete the previous motor
if (myMotor != NULL) {
delete myMotor;
myMotor = NULL;
}
myMotor = motorAddonFunction(myLM);
// set the name in the loads
myLM->setAnimationMotorAddonLocation(addonFile);
// update the settings
QSettings & settings = Application::getSettings();
settings.beginGroup("physicalModelComponent");
settings.setValue("animationMotorAddOn/location", addonFile);
settings.endGroup();
// update the display
ui.addonLocationLineEdit->setText(addonFile);
QString ss(myMotor->getDescription());
ui.descriptionLabel->setText(ss);
// get the add-on widget and add it if non null
if (myMotor != NULL) {
motorWidget = myMotor->getMotorWidget();
if (motorWidget) {
addTab(motorWidget);
}
adjustSize();
}
// the action buttons
enableButtons(myMotor != NULL);
}
else {
QMessageBox::warning(NULL, "Animation Motor Add-on",
QString("File ") + addonFile + QString("\nis not a valid animation motor add-on."));
show();
}
}
//--------------- reject -----------------------
void LoadsSimulation::reject() {
simDriver->stopTimer();
Application::getMainWindow()->getProgressBar()->setValue(0);
// uic_LoadsSimulation::reject();
}
//--------------- enableButtons -----------------------
void LoadsSimulation::enableButtons(bool enable) {
// window buttons
ui.playPushButton->setEnabled(enable);
ui.rewindPushButton->setEnabled(enable);
ui.pausePushButton->setEnabled(enable);
ui.playOneStepPushButton->setEnabled(enable);
ui.outputPushButton->setEnabled(enable);
// toolbar buttons
tLineEditToolbar->setEnabled(enable);
rewindToolbar->setEnabled(enable);
playToolbar->setEnabled(enable);
playOneStepToolbar->setEnabled(enable);
pauseToolbar->setEnabled(enable);
}
//--------------- addTab -----------------------
void LoadsSimulation::addTab(QWidget *w) {
ui.simulationTabs->addTab(w, w->objectName());
}
//--------------- chooseOutputDir -----------------------
void LoadsSimulation::chooseOutputDir() {
//QFileDialog* fd = new QFileDialog( this, "Output directory for screenshots", TRUE );
//fd->setMode(QFileDialog::Directory);
QString dir = QFileDialog::getExistingDirectory(this, tr("Output Directory For Dcreenshots"), ui.outputLineEdit->text());
if (!dir.isNull()) {
outputDir = dir;
ui.outputLineEdit->setText(outputDir);
output = true;
}
}
//--------------- videoToggled -----------------------
void LoadsSimulation::videoToggled(bool state) {
video = state;
if (video) {
if (output) {
// get the value
outputDir = ui.outputLineEdit->text();
}
else {
// tell the user what happened
QMessageBox::warning(NULL, "Missing output directory!",
QString("Could not record: choose an output directory first!"));
ui.recordPushButton->setChecked(false);
}
}
}
|