File: LoadsSimulationDriver.cpp

package info (click to toggle)
camitk 3.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 391,128 kB
  • ctags: 9,706
  • sloc: cpp: 76,454; xml: 2,665; sh: 287; ansic: 142; makefile: 99; perl: 84; sed: 20
file content (684 lines) | stat: -rw-r--r-- 21,866 bytes parent folder | download
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*****************************************************************************
 * $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 <QApplication>
#include <QCursor>
#include <QTimer>

#include <vtkTransform.h>

#include "PMManagerDC.h"

#include <pml/PhysicalModel.h>
#include <pml/Atom.h>
#include <pml/StructuralComponent.h>


#include <InteractiveViewer.h>
using namespace camitk;

#include "LoadsSimulationDriver.h"
#include "LoadsManager.h"

#include "AtomDC.h"
#include "StructuralComponentDC.h"

#include <lml/Loads.h>


#include <limits>

//--------------- Constructor ---------------------------------
LoadsSimulationDriver::LoadsSimulationDriver ( LoadsManager *lm, Loads * initialLoads ) {
    myLM = lm;
    timer = NULL; // not started yet

    lastUpdateTime = -1.0;
    lastRefreshTime = -1000.0;
    refreshDt = dt = 0.1;
    tMin = tMax = 0.0;

    // set the initial loads as current loads (needed by init)
    setLoads ( initialLoads );

    // init the display
    init();
}


//--------------- Destructor ---------------------------------
LoadsSimulationDriver::~LoadsSimulationDriver() {
    stopTimer();
    delete timer;
    timer = NULL;
}


//--------------- init ---------------------------------
void LoadsSimulationDriver::init() {
    // store the initial position
    // and make sure that all Atom DC's have a 3D representation
    PMManagerDC *pmMgrDC = myLM->getPMManagerDC();
    PhysicalModel * pm = pmMgrDC->getPhysicalModel();
    AtomDC * adc;
    Atom *a;

    
    /*
    if (you want to show the absolute displacement of only the
      implied atom, just use this code) {
// => the best in this case is to constrained the scale yourself (user-constrained)
 // which are the implied atoms
        // get the initial position
        for (unsigned int i = 0; i < currentLoads->numberOfLoads();i++) {
              Load *l = currentLoads->getLoad(i);

          for (unsigned int j = 0; j < l->numberOfTargets(); j++) {
            // get the target
            a = pm->getAtom(l->getTarget(j));
            // activate the target's 3D representation
            adc = pmMgrDC->getDC(a);

            if (adc != NULL) {
              double pos[3];
              a->getPosition(pos);
              Position p;
              p.x = pos[0];
              p.y = pos[1];
              p.z = pos[2];
              initialPositionMap.insert(l->getTarget(j), p);
            }
          }
        }
      }
    */
    StructuralComponent * theAtoms = pm->getAtoms();

    for ( unsigned int i = 0; i < theAtoms->getNumberOfStructures();i++ ) {
        // get the target index
        a = ( Atom * ) theAtoms->getStructure ( i );
        // activate the target's 3D representation
        adc = pmMgrDC->getDC ( a );

        if ( adc != NULL ) {
            double pos[3];
            a->getPosition ( pos );
            Position p;
            p.x = pos[0];
            p.y = pos[1];
            p.z = pos[2];
            initialPositionMap.insert ( a->getIndex(), p );
        }
    }

    if ( currentLoads ) {
        // look for default value in the loads
        resetTMinToDefault();

        resetTMaxToDefault();
    } else {
        // set the default values
        tMin = 0.0;
        tMax = 1.0;
    }

    // start from tMin
    rewind();

    // start the timer
    timer = new QTimer ( this );

    // connect the timer tick signal to play()
    connect ( timer, SIGNAL ( timeout() ), this, SLOT ( play() ) );

    // max interval is 1000 times per sec
    maxSpeed();
}

//--------------- setLoads ---------------
void LoadsSimulationDriver::setLoads ( Loads * l ) {
    currentLoads = l;
}


//--------------- rewind -----------------------
void LoadsSimulationDriver::rewind() {
    if ( myLM->getLoads() ) {
        // set time just before the first active load
        t = myLM->getLoads()->getFirstEventDate() - dt;
    } else
        t = tMin - dt;

    resetPositions();

    // see the time passing by
    while ( t < tMin - dt ) {
        t += dt;
        updatePositions ( false );
    }

    t = tMin - dt;
}


//--------------- resetPositions -----------------------
void LoadsSimulationDriver::resetPositions() {
    AtomDC *adc;
    PMManagerDC *pmMgrDC = myLM->getPMManagerDC();
    PhysicalModel * pm = pmMgrDC->getPhysicalModel();
    Atom *a;

    // reset all the stored initial position
    QMapIterator<unsigned int, Position> it ( initialPositionMap );
    while ( it.hasNext() ) {
        it.next();
        // get the target
        a = pm->getAtom ( it.key() );
        adc = pmMgrDC->getDC ( a );

        if ( adc != NULL ) {
            adc->resetAlreadyMovedFlag();
            adc->setPosition ( it.value().x, it.value().y, it.value().z );
        }

    }

    lastRefreshTime = -1000.0;
    lastUpdateTime = -1.0;
}

//---------------  updateAtomData -----------------------
void LoadsSimulationDriver::updateAtomData() {
    if ( myLM->getAtomDataDisplay() != LoadsManager::NONE ) {
        //-- get the double value of the atom data
        std::AtomDataVector atomDataValues;

        //-- initialize the referencePositionMap
        if ( ( myLM->getAtomDataDisplay() == LoadsManager::DISTANCES ||
               myLM->getAtomDataDisplay() == LoadsManager::RELATIVE_ENERGY_NORM_ERROR ) &&
             ( referencePositionMap.size() == 0 ) ) {

            StructuralComponent * theAtoms = myLM->getReferencePM()->getAtoms();

            for ( unsigned int i = 0; i < theAtoms->getNumberOfStructures();i++ ) {
                // get the target index
                Atom *a = ( Atom * ) theAtoms->getStructure ( i );
                double pos[3];
                a->getPosition ( pos );
                Position p;
                p.x = pos[0];
                p.y = pos[1];
                p.z = pos[2];
                referencePositionMap.insert ( a->getIndex(), p );
            }
        }

        switch ( myLM->getAtomDataDisplay() ) {

        case LoadsManager::ADD_ON:
            // simply get the add-on atom data range
            atomDataValues = myLM->getAtomData();
            break;

        case LoadsManager::DISPLACEMENTS: {
            // compute the total displacements
            PhysicalModel * pm = myLM->getPMManagerDC()->getPhysicalModel();
            double pos[3], distance;
            Atom *a;

            // compute distance between initial position and now, and min/max
            QMapIterator<unsigned int, Position> it ( initialPositionMap );
            while ( it.hasNext() ) {
                it.next();
                // get the atom position
                a = pm->getAtom ( it.key() );
                a->getPosition ( pos );
                distance = sqrt ( ( pos[0] - it.value().x ) * ( pos[0] - it.value().x )
                                  + ( pos[1] - it.value().y ) * ( pos[1] - it.value().y )
                                  + ( pos[2] - it.value().z ) * ( pos[2] - it.value().z ) );
                atomDataValues.push_back ( std::AtomDataPair ( a, distance ) );
            }

            break;
        }

        case LoadsManager::DISTANCES: {
            //-- compute the distance
            PhysicalModel * pm = myLM->getPMManagerDC()->getPhysicalModel();
            double pos[3], distance;
            Atom *a;

            // compute distance between initial position and now, and min/max
            QMapIterator<unsigned int, Position> it ( referencePositionMap );
            while ( it.hasNext() ) {
                it.next();
                // get the atom position
                a = pm->getAtom ( it.key() );
                a->getPosition ( pos );
                distance = sqrt ( ( pos[0] - it.value().x ) * ( pos[0] - it.value().x )
                                  + ( pos[1] - it.value().y ) * ( pos[1] - it.value().y )
                                  + ( pos[2] - it.value().z ) * ( pos[2] - it.value().z ) );

                atomDataValues.push_back ( std::AtomDataPair ( a, distance ) );
            }

            break;
        }

        case LoadsManager::RELATIVE_ENERGY_NORM_ERROR: {
            //-- compute the error percentage
            PhysicalModel * pm = myLM->getPMManagerDC()->getPhysicalModel();
            double pos[3], percentage;
            double simulatedToReal;
            double totalDisplacement;
            Atom *a;

            // compute distance between initial position and now, and min/max
            QMapIterator<unsigned int, Position> it ( referencePositionMap );
            while ( it.hasNext() ) {
                it.next();
                // get the atom position
                a = pm->getAtom ( it.key() );
                a->getPosition ( pos );
                simulatedToReal = sqrt ( ( pos[0] - it.value().x ) * ( pos[0] - it.value().x )
                                         + ( pos[1] - it.value().y ) * ( pos[1] - it.value().y )
                                         + ( pos[2] - it.value().z ) * ( pos[2] - it.value().z ) );
                // get the init position
                getInitialPosition ( it.key(), pos );
                totalDisplacement = sqrt ( ( pos[0] - it.value().x ) * ( pos[0] - it.value().x )
                                           + ( pos[1] - it.value().y ) * ( pos[1] - it.value().y )
                                           + ( pos[2] - it.value().z ) * ( pos[2] - it.value().z ) );

                if ( totalDisplacement < 1e-10 )
                    percentage = 0.0;
                else
                    percentage = simulatedToReal / totalDisplacement;

                //percentage *= 100.0;

                atomDataValues.push_back ( std::AtomDataPair ( a, percentage ) );
            }

            break;
        }

        default: // NONE
            break;
        }

        //-- check the range if needed
        double min, max;
        std::AtomDataVector::iterator it;
        std::vector<AtomDC *> atomDCs; // for efficiency, to store the DCs
        Atom *a;
        AtomDC *adc;
        PMManagerDC *pmMgrDC = myLM->getPMManagerDC();

        // destroy existing atom data (in case not all atom is given a point data)
        myLM->getPMManagerDC()->destroyPointData();

        if ( !myLM->getUserConstrainedAtomDataScale() ) {
            min = std::numeric_limits<double>::max();
            max = -1.0;

            if ( atomDataValues.size() == 0 ) {
                min = -1.0;
                max = 1.0;
            }

            for ( it = atomDataValues.begin(); it != atomDataValues.end(); it++ ) {
                // get the atom
                a = it->first;
                // ...and its dc
                adc = pmMgrDC->getDC ( a );

                if ( adc != NULL ) {
                    if ( ( it->second ) > max )
                        max = ( it->second );

                    if ( ( it->second ) < min )
                        min = ( it->second );

                    atomDCs.push_back ( adc );
                }
            }
        } else {
            myLM->getAtomDataScale ( &min,&max );
            for ( it = atomDataValues.begin(); it != atomDataValues.end(); it++ ) {
                // get the atom
                a = it->first;
                // ...and its dc
                adc = pmMgrDC->getDC ( a );

                if ( adc != NULL ) {
                    atomDCs.push_back ( adc );
                }
            }
        }

        //-- rescale the distance between 0 and 1 and tell the atoms
        double val;
        double range = max - min;

        for ( unsigned int i = 0; i < atomDCs.size();i++ ) {
            val = atomDataValues[i].second;

            if ( atomDataValues.size() == 0 )
                val = 0.0;
            else
                if ( fabs ( range ) < 1e-10 || val <= min ) {
                    val = 0.0;
                } else {
                    if ( val >= max ) {
                        val = 1.0;
                    } else {
                        // renormalized the size between min and max
                        val = ( ( val - min ) / range );
                    }
                }

            atomDCs[i]->updatePointData ( 1.0 - val );
        }

        // show values on the scale
        myLM->updateAtomDataScale ( min, max );

    }

}

//---------------  updatePositions -----------------------
void LoadsSimulationDriver::updatePositions ( bool force ) {
    Load * l;
    double dir[3];
    double displ[3];
    double pos[3];
    double value;
    double lastValue;
    AtomDC *adc;
    Atom *a;
    bool refresh3D;
    PMManagerDC *pmMgrDC = myLM->getPMManagerDC();
    PhysicalModel * pm = pmMgrDC->getPhysicalModel();

    refresh3D = ( getNextRefreshTime() < t ) || ( fabs ( getNextRefreshTime() - t ) < 1e-15 );

    if ( refresh3D || force ) {
        // if there are no curernt load, just display the atom
        // (some simulation motors can actually give no current loads, but also
        // directly modify the positions of the atoms, in order to increase speed)
        if ( !currentLoads ) {
            StructuralComponent * theAtoms = pm->getAtoms();

            for ( unsigned int i = 0; i < theAtoms->getNumberOfStructures();i++ ) {
                // get the target index
                a = ( Atom * ) theAtoms->getStructure ( i );
                // activate the target's 3D representation
                adc = pmMgrDC->getDC ( a );

                if ( adc != NULL ) {
                    a->getPosition ( pos );
                    adc->resetAlreadyMovedFlag();
                    adc->setPosition ( pos[0], pos[1], pos[2] );
                }
            }
        } else {
            // get the informations from the current Loads
            for ( unsigned int i = 0; i < currentLoads->numberOfLoads();i++ ) {
                l = currentLoads->getLoad ( i );
                // get the informations
                value = l->getValue ( t );  // now
                // display only active translation

                if ( l->getType() == "Translation" && value != 0.0 ) {
                    dir[0] = l->getDirection().getX();
                    dir[1] = l->getDirection().getY();
                    dir[2] = l->getDirection().getZ();
                    // is that a zero-translation constraint?

                    if ( ! ( dir[0] == 0.0 && dir[1] == 0.0 && dir[2] == 0.0 ) ) {
                        // the last time we updated the display (so it is possible to linearly interpolate)
                        lastValue = l->getValue ( lastUpdateTime );
                        // normalize
                        // AND multiply by the difference between the current value and the last time value,
                        // so that when t is equal to the date of the current value event, the total
                        // of displacement will sum up to value. (linear interpolation)
                        double n = sqrt ( dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2] );

                        for ( unsigned int j = 0;j < 3;j++ )
                            dir[j] *= ( value - lastValue ) / n;

                        // apply the translation to the targets (atoms)
                        for ( unsigned int j = 0; j < l->numberOfTargets(); j++ ) {
                            // get the target
                            a = pm->getAtom ( l->getTarget ( j ) );

                            if ( a ) {
                                // compute new displacement
                                for ( unsigned int j = 0;j < 3;j++ )
                                    displ[j] = dir[j];

                                // displ is the displacement vector to transform to a ImpTransform
                                a->getPosition ( pos );

                                for ( unsigned int j = 0;j < 3;j++ )
                                    displ[j] += pos[j];

                                a->setPosition ( displ[0], displ[1], displ[2] );

                                if ( refresh3D ) {
                                    adc = pmMgrDC->getDC ( a );

                                    if ( adc != NULL ) {
                                        adc->resetAlreadyMovedFlag();
                                        adc->setPosition ( displ[0], displ[1], displ[2] );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // update the atom data
        updateAtomData();

        // remember the last time we updated the positions
        lastUpdateTime = t;
    }
}

//--------------- play -----------------------
void LoadsSimulationDriver::play() {
    if ( !timer->isActive() ) {
        // timer wasn't active, action to be done = launch the timer
        startTimer();
    } else {
        // timer is active, action to be done = play one step
        emit doOneStep();
    }
}

//---------------  updateDisplay -----------------------
void LoadsSimulationDriver::updateDisplay ( bool force ) {

    // update all the positions & displacements
    updatePositions ( force );

    // refresh the view
    if ( force || getNextRefreshTime() <= t ) {
        InteractiveViewer::get3DViewer()->refreshRenderer();
        lastRefreshTime = t;
        //Application::getMainWindow()->statusMessage ( QString ( "t=%1" ).arg ( t ) );
    }

    updateAtomData();

    myLM->updateLoadsDisplay();

}

//---------------  getNextRefreshTime -----------------------
double LoadsSimulationDriver::getNextRefreshTime() {
    return ( lastRefreshTime + refreshDt );
}


//--------------- startTimer -------------------
void LoadsSimulationDriver::startTimer() {
    timer->start ( interval );  // 200 times per second
}

//--------------- stopTimer -------------------
void LoadsSimulationDriver::stopTimer() {
    timer->stop();
}

//--------------- isTimerActive -------------------
bool LoadsSimulationDriver::isTimerActive() {
    return timer->isActive();
}


//--------------- resetToDefault ---------------
void LoadsSimulationDriver::resetToDefault() {
    resetTMinToDefault();
    resetTMaxToDefault();
    dt = refreshDt = 0.1;
}

//--------------- resetTMinToDefault ---------------
void LoadsSimulationDriver::resetTMinToDefault() {
    if ( myLM != NULL && currentLoads != NULL ) {
        // search for the first event
        tMin = currentLoads->getFirstEventDate();
    }
}

//--------------- getTMin -------------------
double LoadsSimulationDriver::getTMin() const {
    return tMin;
}

//--------------- setTMin -------------------
void LoadsSimulationDriver::setTMin ( double newTMin ) {
    tMin = newTMin;
}

//--------------- resetTMaxToDefault ---------------
void LoadsSimulationDriver::resetTMaxToDefault() {
    if ( myLM != NULL && currentLoads != NULL ) {
        // search for the last event
        tMax  = currentLoads->getLastEventDate();
    }
}

//--------------- getTMax -------------------
double LoadsSimulationDriver::getTMax() const {
    return tMax;
}

//--------------- setTMax -------------------
void LoadsSimulationDriver::setTMax ( double newTMax ) {
    tMax = newTMax;
}

//--------------- getDt -------------------
double LoadsSimulationDriver::getDt() const {
    return dt;
}

//--------------- setDt ---------------
void LoadsSimulationDriver::setDt ( double dt ) {
    // default dt value
    this->dt = dt;
}

//--------------- getTime -------------------
double LoadsSimulationDriver::getTime() const {
    return t;
}

//--------------- setTime -------------------
void LoadsSimulationDriver::setTime ( double newTime ) {
    // we have to move along the time line little by little, otherwise
    // we might jump over some translations

    // in case we have to comm
    if ( newTime < t ) {
        // restart from the beginning
        //resetPositions();
        rewind();

        // see the time passing by up to newTime-dt

        for ( t = tMin - dt; ( t < ( newTime - dt ) ) && fabs ( t - ( newTime - dt ) ) > 1e-15;t += dt )
            updatePositions ( false );

    }

    t = newTime;
}

//--------------- getRefreshDt -------------------
double LoadsSimulationDriver::getRefreshDt() const {
    return refreshDt;
}

//--------------- setRefreshDt ---------------
void LoadsSimulationDriver::setRefreshDt ( double dt ) {
    refreshDt = dt;
}

//--------------- slower ---------------
void LoadsSimulationDriver::slower() {
    interval += 5;

    if ( timer->isActive() )
        timer->setInterval ( interval );
}

//--------------- quicker ---------------
void LoadsSimulationDriver::quicker() {
    interval -= 5;

    if ( interval <= 0 )
        maxSpeed();
    else {
        if ( timer->isActive() )
            timer->setInterval ( interval );
    }
}

//--------------- maxSpeed ---------------
void LoadsSimulationDriver::maxSpeed() {
    interval = 1;

    if ( timer->isActive() )
        timer->setInterval ( interval );
}