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
|
/*****************************************************************************
* $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$
****************************************************************************/
// PML/LML
#include <pml/PhysicalModel.h>
#include <pml/MultiComponent.h>
#include "LoadsManager.h"
#include "LoadsSimulation.h"
#include "LoadsMovie.h"
#include "LoadsEditor.h"
#include "AtomDecoration.h"
#include "PMManagerDC.h"
#include "AtomDC.h"
#include <lml/XMLLoads.h>
// camiTK
#include <Application.h>
#include <MainWindow.h>
#include <InteractiveViewer.h>
using namespace camitk;
// Qt
#include <QDockWidget>
#include <QMessageBox>
#include <QFileDialog>
// other
#include <math.h>
#include <limits>
//----------------- constructor ----------------------------
LoadsManager::LoadsManager(PMManagerDC *pmManagerDC) {
fileName = "";
motorAddonLocation = "";
myPMManagerDC = pmManagerDC;
myLoads = NULL;
previewDialog = NULL;
loadsEditor = NULL;
changed = false;
loadsChangedFlag = false;
showLoads = false;
atomDataDisplay = NONE;
referencePM = NULL;
constrainedAtomDataScale = false;
min = std::numeric_limits<double>::max();
max = -1.0;
// automatically create the dialog, but do not show yet (that will create the new toolbar)
simulationDialog = new LoadsSimulation(this, Application::getMainWindow());
simDock = new QDockWidget(Application::getMainWindow());
simDock->setAllowedAreas(Qt::RightDockWidgetArea);
simDock->setWidget(simulationDialog);
Application::getMainWindow()->addDockWidget(Qt::RightDockWidgetArea, simDock);
simulationDialog->init();
simDock->hide();
}
//----------------- destructor ----------------------------
LoadsManager::~LoadsManager() {
// remove dock widget
Application::getMainWindow()->removeDockWidget(simDock);
// delete dialogs
delete previewDialog;
previewDialog = NULL;
delete simulationDialog;
simulationDialog = NULL;
// reset color scale
setAtomDataDisplay(NONE);
// close current loads
close();
delete referencePM;
referencePM = NULL;
}
//----------------- userWantsToSave ----------------------------
bool LoadsManager::userWantsToSave() {
int exit = QMessageBox::warning(NULL, QMessageBox::tr("Save Changes?"),
QMessageBox::tr("Loads have changed!\nIf you want to save, clic on Ok, otherwise cancel."),
QMessageBox::Ok | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape);
return (exit == QMessageBox::Ok);
}
//----------------- rewind ----------------------------
void LoadsManager::rewind() {
// rewind everything
if (previewDialog) {
previewDialog->rewind();
previewDialog->reset();
}
if (simulationDialog)
simulationDialog->rewind();
}
//----------------- close ----------------------------
void LoadsManager::close() {
// ask the user
if (changed && userWantsToSave())
save();
changed = false;
delete loadsEditor;
loadsEditor = NULL;
delete myLoads;
myLoads = NULL;
fileName = "";
// hide 3D representation
foreach(AtomDecoration *deco, representation3D) {
// remove from the InteractiveViewer
deco->show(false);
}
// if (showDisplacements)
// myPMDC->destroyPointData();
// refresh the view
InteractiveViewer::get3DViewer()->refreshRenderer();
}
//----------------- deleteAllLoads ----------------------------
void LoadsManager::deleteAllLoads() {
delete myLoads;
myLoads = NULL;
loadsChangedFlag = true;
}
//----------------- pause ----------------------------
void LoadsManager::pause() {
if (previewDialog)
previewDialog->pause();
if (simulationDialog)
simulationDialog->pause();
}
//----------------- addLoad ----------------------------
void LoadsManager::addLoad(Load *l) {
if (!myLoads) {
// we want to add a load, but there are no list defined yet
myLoads = new Loads();
}
myLoads->addLoad(l);
changed = true;
loadsChangedFlag = true;
}
//----------------- open ----------------------------
bool LoadsManager::open(const QString & fileName) {
if (myLoads != NULL)
close();
// set the current filename
this->fileName = fileName;
// open the loads stored in filename
// and put everything in the list
myLoads = new Loads(fileName.toStdString().c_str());
changed = false;
loadsChangedFlag = true;
return (myLoads != NULL);
}
//----------------- save ----------------------------
void LoadsManager::save() {
if (myLoads && myLoads->numberOfLoads()>0) {
// test if the user already gave a filename
if (fileName == "") {
// open a file dialog to get a filename
QString newFileName = QFileDialog::getSaveFileName(NULL, "Save LML As...", QFileInfo(fileName).absoluteDir().absolutePath(), "(*.lml)");
if (!newFileName.isEmpty()) {
// set the current fileName
fileName = newFileName;
} else {
// tell the nice user that (s)he doesn't give a file name it will never
// be able to save anything
return;
}
}
// never save to test.lml
std::ofstream outputFile;
outputFile.open(fileName.toStdString().c_str());
myLoads->xmlPrint(outputFile);
outputFile.close();
changed = false;
}
}
//----------------- saveAs ----------------------------
void LoadsManager::saveAs(const QString & fileName) {
this->fileName = fileName;
save();
}
//----------------- getLoads ----------------------------
Loads * LoadsManager::getLoads() {
if (!myLoads)
return NULL;
return myLoads;
}
//----------------- setLoads ----------------------------
void LoadsManager::setLoads(Loads* loads) {
deleteAllLoads();
myLoads = loads;
}
//----------------- setDisplayLoads ----------------------------
void LoadsManager::setDisplayLoads(bool s) {
if (showLoads && !s) {
foreach(AtomDecoration * deco, representation3D) {
deco->show(false);
}
}
showLoads = s;
updateLoadsDisplay();
// refresh the view
getPMManagerDC()->refresh();
//InteractiveViewer::get3DViewer()->refreshRenderer();
}
//----------------- setAtomDataDisplay ----------------------------
void LoadsManager::setAtomDataDisplay(AtomDataDisplayType adt) {
if (atomDataDisplay != NONE) {
myPMManagerDC->destroyPointData();
}
atomDataDisplay = adt;
QString scaleName;
switch (atomDataDisplay) {
case NONE:
scaleName = "NONE! (if you see this message, congrats! you found a bug!)";
break;
case ADD_ON:
scaleName = atomDataName;
break;
case DISPLACEMENTS:
scaleName = "Displacements";
break;
case DISTANCES:
scaleName = "Distances";
break;
case RELATIVE_ENERGY_NORM_ERROR:
scaleName = "Relative Energy Norm Error (%)";
}
InteractiveViewer::get3DViewer()->setColorScale((atomDataDisplay != NONE));
if (atomDataDisplay != NONE) {
InteractiveViewer::get3DViewer()->setColorScaleTitle(scaleName);
myPMManagerDC->createPointData();
}
// refresh the view
// Application::getMainWindow()->getInteractiveViewer()->refresh();
// force update
if (previewDialog)
previewDialog->updateDisplay(true);
if (simulationDialog)
simulationDialog->updateDisplay(true);
}
//----------------- setAtomData ----------------------------
void LoadsManager::setAtomData(std::AtomDataVector &pData, QString name) {
atomData = pData;
atomDataName = name;
setAtomDataDisplay(ADD_ON);
}
//----------------- setReferencePML ----------------------------
void LoadsManager::setReferencePML(const QString & fileName) {
referencePM = new PhysicalModel(fileName.toStdString().c_str());
}
//----------------- updateTime ----------------------------
void LoadsManager::updateTime() {
if (simulationDialog)
simulationDialog->updateTime(true); // force the simulation dialog to get the motor add-on time
}
//--------------- updateAtomDataScale -----------------------
void LoadsManager::updateAtomDataScale(double min, double max) {
this->min = min;
this->max = max;
if (!constrainedAtomDataScale)
InteractiveViewer::get3DViewer()->setColorScaleMinMax(min, max);
}
//----------------- addDecoration ----------------------------
void LoadsManager::addDecoration(Atom *a, Load *ld, double time, double defaultSize, double *max, double *min, double *val) {
AtomDecoration *deco = NULL;
if (a) {
if (ld->getType() == "Translation") {
if (ld->getDirection().isXNull() && ld->getDirection().isYNull() && ld->getDirection().isZNull()) {
// null translation
deco = myPMManagerDC->getDC(a)->getDecoration("NullTranslation", GeometricObject::SPHERE);
deco->setColor(0.4, 0.67, 0.88);
deco->setSize(defaultSize);
}
else {
// other translation
deco = myPMManagerDC->getDC(a)->getDecoration("Translation", GeometricObject::ARROW);
deco->setColor(0.6, 0.88, 0.59);
}
}
else { // "Force", "Rotation", "Pressure"...
deco = myPMManagerDC->getDC(a)->getDecoration("Force", GeometricObject::ARROW);
deco->setColor(1.0, 0.3, 0.15);
}
}
// update size and direction depending on the load value
if (deco->getType() != GeometricObject::SPHERE) {
double pos2[3];
a->getPosition(pos2);
if (ld->getDirection().isToward()) {
int twdind = ld->getDirection().getToward();
Atom * twd = myPMManagerDC->getPhysicalModel()->getAtom(twdind);
if (twd) {
double pos1[3];
twd->getPosition(pos1);
deco->update(pos1[0] - pos2[0], pos1[1] - pos2[1], pos1[2] - pos2[2]);
}
}
else {
double x, y, z;
ld->getDirection(x, y , z);
deco->update(x, y, z);
}
*val = ld->getValue(time);
if (*max < *val)
*max = *val;
if (*min > *val)
*min = *val;
}
// show in scene
deco->show(true);
// store in case we need to hide all
representation3D.insert(deco);
}
//----------------- updateLoadsDisplay ----------------------------
void LoadsManager::updateLoadsDisplay() {
if (showLoads) {
PhysicalModel * myPM = myPMManagerDC->getPhysicalModel();
if (myLoads != NULL && myPM != NULL) {
// for all loads, display the decoration
Load * ld;
// ask the preview dialog or simulation dialog: "what time is it?"
double time = (previewDialog) ? previewDialog->getTime() :
((simulationDialog) ? simulationDialog->getTime() : 0.0);
// default size for sphere decoration
double defaultSize = myPMManagerDC->getBoundingRadius() / 15.0;
// max value of active loads
double max = -1.0, min = 99999999.9 /*DBL_MAX*/;
std::vector <double> loadsValue;
double val;
for (unsigned i = 0;i < myLoads->numberOfLoads();i++) {
ld = myLoads->getLoad(i);
if (ld->isActive(time)) {
for (unsigned j = 0;j < ld->numberOfTargets(); j++) {
if (ld->getTargetList().indexedTargets()) {
// get the atom ptr from its index
addDecoration(myPM->getAtom(ld->getTarget(j)), ld, time, defaultSize, &max, &min, &val);
loadsValue.push_back(val);
}
else {
// get all atoms from the named target
std::string targetComponent = ld->getTargetList().getNamedTarget(j);
// look for the component in the informative part of the physical model
::Component * c = myPM->getInformativeComponents()->getComponentByName(targetComponent);
if (c != NULL) {
for (unsigned int cellId = 0; cellId < c->getNumberOfCells(); cellId++) {
Cell *cell = c->getCell(cellId);
for (unsigned int atomId = 0; atomId < cell->getNumberOfStructures();atomId++) {
addDecoration((Atom*) cell->getStructure(atomId), ld, time, defaultSize, &max, &min, &val);
loadsValue.push_back(val);
}
}
}
}
}
}
}
// resize the arrows
double size;
double maxSize = defaultSize * 5.0;
double minSize = defaultSize;
unsigned int j = 0;
foreach(AtomDecoration * deco, representation3D) {
if (deco->getType() != GeometricObject::SPHERE) {
val = loadsValue[j++];
if (fabs(max - min) < 1e-10 || fabs(max - val) < 1e-10) {
size = maxSize;
}
else {
if (fabs(min - val) < 1e-10) {
size = minSize;
}
else {
// renormalized the size between min and max
size = ((val - min) / (max - min)) * (maxSize - minSize) + minSize;
}
}
deco->setSize(size);
}
}
}
}
}
//----------------- editLoads ----------------------------
void LoadsManager::editLoads() {
// open the load editor dialog
if (!loadsEditor)
loadsEditor = new LoadsEditor(this, NULL);
else {
loadsEditor->updateLoads();
}
// show it
loadsEditor->show();
loadsChangedFlag = true;
}
//----------------- previewLoads ----------------------------
void LoadsManager::previewLoads() {
if (!previewDialog)
previewDialog = new LoadsMovie(this, NULL);
previewDialog->show();
}
//----------------- addLoad ----------------------------
void LoadsManager::addLoad() {
// here selectedDC is a vector that contains all the selected DCs
std::vector<camitk::Component *> selectedDC;
foreach(camitk::Component *dc, camitk::Application::getSelectedComponents()) {
if (dc->isInstanceOf("AtomDC"))
selectedDC.push_back(dc);
}
LoadsEditor * addDialog = new LoadsEditor(selectedDC, this, NULL);
// show it
addDialog->show();
loadsChangedFlag = true;
}
//----------------- simulation ----------------------------
void LoadsManager::simulation() {
if (!simDock->isVisible())
simDock->show();
else
simDock->hide();
}
//----------------- addSimulationTab ----------------------------
void LoadsManager::addSimulationTab(QWidget*w) {
simulationDialog->show();
simulationDialog->addTab(w);
}
//----------------- getAnimationMotorAddonLocation ----------------------------
QString LoadsManager::getAnimationMotorAddonLocation() {
return motorAddonLocation;
}
//----------------- setAnimationMotorAddonLocation ----------------------------
void LoadsManager::setAnimationMotorAddonLocation(QString mloc) {
motorAddonLocation = mloc;
}
//----------------- setAnimationMotorAddonLocation ----------------------------
void LoadsManager::userConstrainedAtomDataScale ( bool constrained) {
constrainedAtomDataScale = constrained;
}
//----------------- setAnimationMotorAddonLocation ----------------------------
bool LoadsManager::getUserConstrainedAtomDataScale() {
return constrainedAtomDataScale;
}
//----------------- setAnimationMotorAddonLocation ----------------------------
void LoadsManager::getAtomDataScale ( double* min , double* max) {
*min = this->min;
*max = this->max;
}
|