File: StructuralComponentDC.cpp

package info (click to toggle)
camitk 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 343,588 kB
  • sloc: cpp: 78,476; xml: 1,210; sh: 723; ansic: 142; makefile: 101; perl: 84; sed: 20
file content (777 lines) | stat: -rw-r--r-- 29,211 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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2016 Univ. Grenoble Alpes, 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 "PMManagerDC.h"
#include "StructuralComponentDC.h"
#include "StructuralComponentDCProperties.h"
#include "StructuralComponentDCPopup.h"
#include "AtomDC.h"
#include "CellDC.h"

#include <Log.h>
#include <Geometry.h>
#include <InteractiveViewer.h>
using namespace camitk;

#include <vtkUnstructuredGrid.h>
#include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkPointSet.h>
#include <vtkTransformFilter.h>
#include <vtkPolyVertex.h>
#include <vtkDoubleArray.h>
#include <vtkCellArray.h>
#include <vtkIdTypeArray.h>
#include <vtkSmartPointer.h>

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

// -------------------- default constructor  --------------------
StructuralComponentDC::StructuralComponentDC ( camitk::Component *parentDC, PMManagerDC * pmManagerDC, StructuralComponent *sc, bool delayRepresentationInit ) : ComponentDC ( parentDC, pmManagerDC, sc ) {

    myPopupMenu = NULL;
    myProp = NULL;
    pointData = NULL;

    camitk::Component::setName ( sc->getName().c_str() );

    // optimization for std::vector : keep these scalar updated
    nrOfParts = nrOfAtoms = 0;

    // Check if this is a list of atoms or of cells
    if ( sc->getNumberOfStructures() > 0 ) {
        if ( sc->composedBy() == StructuralComponent::ATOMS ) {
            Atom * a;
            AtomDC * adc;
            // update the counter
            nrOfAtoms = sc->getNumberOfStructures();

            // create the sub items (=create and register the DC)
            for ( unsigned int i = 0; i < nrOfAtoms; i++ ) {
                a = ( Atom * ) sc->getStructure ( i );

                // check if a dc already exist for this atom
                adc = myPMManagerDC->getDC ( a );

                if ( !adc ) {
                    // create a new atomdc (that will register itself)
                    adc = new AtomDC ( this, myPMManagerDC, a );
                }

                // force child addition
                attachChild ( adc );
            }
        }

        if ( sc->composedBy() == StructuralComponent::CELLS ) {
            Cell *c;
            CellDC *cdc;

            // create the Cells sub items
            for ( unsigned int i = 0; i < sc->getNumberOfStructures(); i++ ) {
                c = ( Cell * ) sc->getStructure ( i );

                // check if a dc already exist for this cell
                cdc = myPMManagerDC->getDC ( c );

                if ( !cdc ) {
                    // create a new atomdc (that will register itself)
                    cdc = new CellDC ( this, myPMManagerDC, c );
                }

                // force child addition
                attachChild ( cdc );

                // update the counter
                nrOfAtoms += c->getNumberOfStructures();
            }
        }
    }

    // associate this DC with this SC in the map
    myPMManagerDC->addStructuralComponentDCPair ( std::ComponentDCPair ( sc, this ) );

    // if the component should be seen by default
    if ( !delayRepresentationInit && getSC()->isVisible ( sc->getMode() ) ) {
        initRepresentation();
        // add it in the InteractiveViewer
        setVisibility ( InteractiveViewer::get3DViewer(), true );
    }

    myPMManagerDC->progressOneStep();

}

// --------------- destructor -------------------
StructuralComponentDC::~StructuralComponentDC() {
    destroyPointData();
    delete myPopupMenu;
    myPopupMenu = NULL;
    delete myProp;
    myProp = NULL;
}

// -------------------- getPropertyObject --------------------
QObject * StructuralComponentDC::getPropertyObject() {
    if ( !myProp ) {
        myProp = new StructuralComponentDCProperties ( this );
    }

    return myProp;
}

// --------------- updateAtoms -------------------
void StructuralComponentDC::updateAtoms() {
    if ( getSC()->composedBy() == StructuralComponent::CELLS ) {
        CellDC *cdc;
        unsigned int atomIndex;
        AtomDC *adc;

        nrOfParts = 0;
        std::vector<AtomDC *>::iterator it;
        // build the vector of all the DCs, without two identical DCs
        atomIndex = 0;
        atomDCs.clear();
        atomDCs.reserve ( nrOfAtoms ); // optimize the vector

        foreach ( camitk::Component *dc, childrenComponent ) {
            cdc = dynamic_cast<CellDC *> ( dc );
            nrOfParts += 1 + cdc->getChildren().size();

            foreach ( camitk::Component *dc2, cdc->getChildren() ) {
                adc = dynamic_cast<AtomDC *> ( dc2 );

                // if the atom here is not already there, add <a,atomIndex> in the map and adc in the vector
                it = std::find ( atomDCs.begin(), atomDCs.end(), adc );

                if ( it == atomDCs.end() ) {
                    adc->registerIndexInSCDC ( std::IndexInParentItemPair ( this, atomIndex++ ) );
                    atomDCs.push_back ( adc );
                }
            }
        }

        // update the nr of atoms
        nrOfAtoms = atomDCs.size();
    } else // ComposedBy ATOMS
        if (getSC()->composedBy() == StructuralComponent::ATOMS) {
            // For structural component NOT featuring CELLS, i.e. only ATOMS
            unsigned int atomIndex = 0;
            AtomDC *adc;

            // loop over the atoms of the SC
            foreach ( camitk::Component *dc, childrenComponent ) {
                // add an atom index each time a vtkPoint representing an atom is updated
                // for instance when position changes during mechanical deformation
                // NOTE : without index, when running MMLDisplay::updateDisplay()
                // there is no way to get the correspondance between a 3D vtkPoint and the atom in the
                // physical model (the method 'getOrderNumberInSCDC' called in 'this->UpdatePosition()'
                // systematically returns -1 for SC only composed of ATOMS.
                adc = dynamic_cast<AtomDC *> ( dc );
                /*
                      // if the atom here is not already there, add <a,atomIndex> in the map and adc in the vector
                      std::vector<AtomDC *>::iterator it = std::find ( atomDCs.begin(), atomDCs.end(), adc );

                      if ( it == atomDCs.end() ) {
                          */
                adc->registerIndexInSCDC ( std::IndexInParentItemPair ( this, atomIndex++ ) );
                /*    atomDCs.push_back ( adc );
                }
                */

            }

            nrOfParts = nrOfAtoms = childrenComponent.size();
        } else // ComposedBy NOTHING
            nrOfParts = nrOfAtoms = childrenComponent.size();

}

// --------------- initRepresentation -------------------
void StructuralComponentDC::initRepresentation() {

    // check if it is a Structural Component containing a list of Cell or a list of Atom
    if ( getSC()->composedBy() == StructuralComponent::ATOMS ) {
        // create the vector and map
        updateAtoms();
        // create the geometry from the atom list
        myGeometry = atomsToGeometry();
    } else if ( getSC()->composedBy() == StructuralComponent::CELLS ) {
        // create the vector and map
        updateAtoms();
        // create the geometry from the cells
        myGeometry = cellsToGeometry();
    }

}


// --------------- cellsToGeometry -------------------
Geometry * StructuralComponentDC::cellsToGeometry() {
    Geometry *o;
    vtkSmartPointer<vtkUnstructuredGrid> uGrid;
    vtkSmartPointer<vtkPolyData> polyD;
    AtomDC *adc;
    Cell *c;
    CellDC *cdc;
    int atomOrderNumber;
    int *types;
    int *t;
    vtkSmartPointer<vtkCellArray> array;
    vtkSmartPointer<vtkIdTypeArray> idTypeArray;
    double pos[3];
    unsigned int i;
    unsigned int nrOfCellAtoms;
    unsigned int partId;
    unsigned int nrOfCells = childrenComponent.size();
    bool madeOfPolyLines = true;

    // create the vtkCellArray and other needed arrays
    types = new int [nrOfCells];
    t = types;
    array = vtkSmartPointer<vtkCellArray>::New();
    array->Allocate ( nrOfCells );
    idTypeArray = vtkSmartPointer<vtkIdTypeArray>::New();
    idTypeArray->SetNumberOfValues ( nrOfParts );

    // instanciate a new unstructured grid
    uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
    polyD = vtkSmartPointer<vtkPolyData>::New();

    partId = 0;

    foreach ( camitk::Component *dc, childrenComponent ) {
        // use the same order as the sub items, so that the order in the
        // sub item vector is the same as the order in the vtk cells
        cdc = dynamic_cast<CellDC *> ( dc );
        c = cdc->getCell();
        nrOfCellAtoms = c->getNumberOfStructures();

        // 1. build the array of point index (for all the cell's atom)
        // the order of the atoms should be the same as the SC::etAtoms() method
        idTypeArray->SetValue ( partId++, nrOfCellAtoms );

        foreach ( camitk::Component *dc2, cdc->getChildren() ) {
            // get the atom order number
            adc = dynamic_cast<AtomDC *> ( dc2 );
            atomOrderNumber = adc->getOrderNumberInSCDC ( this );
            // push the index in the VtkPoints of the j-th atom in the i-th cell
            idTypeArray->SetValue ( partId++, atomOrderNumber );
        }

        // 2. register the type for the i-th cell
        // get the vtk type

        switch ( c->getType() ) {
        case StructureProperties::TETRAHEDRON:
            *t++ = VTK_TETRA;
            break;
        case StructureProperties::HEXAHEDRON:
            *t++ = VTK_HEXAHEDRON;
            break;
        case StructureProperties::WEDGE:
            *t++ = VTK_WEDGE;
            break;
        case StructureProperties::PYRAMID:
            *t++ = VTK_PYRAMID;
            break;
        case StructureProperties::POLY_LINE:
            *t++ = VTK_POLY_LINE;
            break;
        case StructureProperties::LINE:
            *t++ = VTK_LINE;
            break;
        case StructureProperties::POLY_VERTEX:
            *t++ = VTK_POLY_VERTEX;
            break;
        case StructureProperties::TRIANGLE:
            *t++ = VTK_TRIANGLE;
            break;
        case StructureProperties::QUAD:
            *t++ = VTK_QUAD;
            break;
        default:
            *t++ = VTK_VERTEX;
            break;
        }

        madeOfPolyLines = madeOfPolyLines && ( ( c->getType() == StructureProperties::POLY_LINE ) || ( c->getType() == StructureProperties::LINE ) );
    }

    // create the vtkPoints array for the cells
    vtkSmartPointer<vtkPoints> thePoints = vtkSmartPointer<vtkPoints>::New();

    thePoints->SetNumberOfPoints ( nrOfAtoms );

    // insert only the atoms that are in the vector (the one needed by the cells) (see updateAtoms() method)
    for ( i = 0; i < nrOfAtoms; i++ ) {
        atomDCs[i]->getAtom()->getPosition ( pos );
        thePoints->SetPoint ( i, pos[0], pos[1], pos[2] );
    }

    // insert everything everywhere (but at the right place!)
    array->SetCells ( nrOfCells, idTypeArray );

    if ( !madeOfPolyLines ) {
        uGrid->SetCells ( types, array );
        uGrid->SetPoints ( thePoints );
        // create the new geometry
        o = new Geometry ( this->getName(), uGrid, myPMManagerDC->toDCRenderingMode ( getSC()->getMode() ) );
    } else {
        // there are only polyline
        polyD->SetLines ( array );
        polyD->SetPoints ( thePoints );
        // create the new geometry
        o = new Geometry ( this->getName(), polyD, myPMManagerDC->toDCRenderingMode ( getSC()->getMode() ) );
    }

    // free up some memory
    delete [] types;

    // if the sc is not of the default color, change it
    o->setActorColor ( InterfaceGeometry::Surface, getSC()->getColor() );

    return o;
}

// --------------- atomsToGeometry -------------------
Geometry * StructuralComponentDC::atomsToGeometry() {
    Geometry *o;
    Atom *a;
    double pos[3];

    // create the vtk cell
    vtkSmartPointer<vtkPolyVertex> vtkCell = vtkSmartPointer<vtkPolyVertex>::New();
    vtkCell->GetPointIds()->SetNumberOfIds ( nrOfAtoms );

    // create the points for the cell
    vtkSmartPointer<vtkPoints> thePoints = vtkSmartPointer<vtkPoints>::New();
    thePoints->SetNumberOfPoints ( nrOfAtoms );

    // NOTE : the index of each atom in the vtk structure is in fact its order number in the atoms structure (not its actual index)
    unsigned int i = 0;

    foreach ( camitk::Component *dc, childrenComponent ) {
        // use the same order as the sub items, so that the order in the
        // sub item vector is the same as the order in the vtk points
        a = dynamic_cast<AtomDC *> ( dc )->getAtom();
        a->getPosition ( pos );
        thePoints->SetPoint ( i, pos[0], pos[1], pos[2] );
        // build the id table
        vtkCell->GetPointIds()->SetId ( i, i );
        i++;
    }

    // instanciate a new unstructured grid
    vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
    uGrid->Allocate ( 1, 1 );
    uGrid->InsertNextCell ( vtkCell->GetCellType(), vtkCell->GetPointIds() );
    uGrid->SetPoints ( thePoints );
    uGrid->Update();

    // create the new geometry (and give away the management of the uGrid...)
    o = new Geometry ( this->getName(), uGrid, myPMManagerDC->toDCRenderingMode ( getSC()->getMode() ) );

    return o;
}


// -------------------- getPopupMenu --------------------
QMenu * StructuralComponentDC::getPopupMenu ( QWidget* parent ) {
    if ( !myPopupMenu ) {
        myPopupMenu = new StructuralComponentDCPopup ( this, parent );
    }

    return myPopupMenu;
}

// -------------------- updatePosition --------------------
void StructuralComponentDC::updatePosition ( AtomDC *modifiedAtomDC, int atomOrderNumber ) {
    // update the 3D representation

    if ( myGeometry ) {
        // the order nr was not given, find it then !
        if ( atomOrderNumber == -1 ) {
            // I'm a cell or a SC called by my composing atoms
            if ( getSC()->composedBy() == StructuralComponent::ATOMS ) {
                // get the index of the modified  atom in the list
                atomOrderNumber = modifiedAtomDC->getOrderNumberInSCDC ( this );
            } else

                // I'm a SC whose cell has an atom that has changed
                if ( isInstanceOf ( "StructuralComponentDC" ) ) {
                    // should never happen normally as the AtomDC which is calling
                    // is able to give it order number in this (in 99% of the cases)
                    atomOrderNumber = modifiedAtomDC->getOrderNumberInSCDC ( this );
                }
        }

        // now, do it!
        if ( atomOrderNumber >= 0 ) {
            // get the new atom position
            double pos[3];
            modifiedAtomDC->getAtom()->getPosition ( pos );
            // set the new position in my geometry
            myGeometry->setPointPosition ( atomOrderNumber, pos[0], pos[1], pos[2] );
        }
    }

}


// -------------------- setPointSet --------------------
void StructuralComponentDC::setPointSet ( vtkSmartPointer<vtkPointSet> newPointSet ) {
    if ( !myGeometry )
        return;

    vtkSmartPointer<vtkPoints> vtkPts = newPointSet->GetPoints();

    // 1. tag this DC as modified
    getTopLevelComponent()->setModified();

    // 2. really update all the atom positions
    double pos[3];

    if ( getSC()->composedBy() == StructuralComponent::ATOMS ) {
        AtomDC *adc;

        // the atom's DC are the sub items
        for ( int i = 0; i < childrenComponent.size(); i++ ) {
            // get the new position
            vtkPts->GetPoint ( i, pos );
            // get the corresponding DC
            adc = dynamic_cast<AtomDC*> ( childrenComponent[i] );
            // set the new position for the atom (it will automatically update the DC, the atom, and all the cells, SC containing this atom)
            adc->setPosition ( pos[0], pos[1], pos[2] );
        }
    } else if ( getSC()->composedBy() == StructuralComponent::CELLS ) {
        // the atom's DC are in the vector
        for ( unsigned int i = 0; i < atomDCs.size(); i++ ) {
            // get the new position
            vtkPts->GetPoint ( i, pos );
            // set the new position for the atom (it will automatically update the DC, the atom, and all the cells, SC containing this atom)
            atomDCs[i]->setPosition ( pos[0], pos[1], pos[2] );
        }
    }

    // now call parent's method
    camitk::Component::setPointSet ( newPointSet );
}


//------------------------- pointPicked ------------------------
void StructuralComponentDC::pointPicked ( vtkIdType pointId, bool pickingIsSelecting  ) {
    if ( getSC()->composedBy() == StructuralComponent::ATOMS ) {
        //CAMITK_ERROR("StructuralComponentDC", "pointPicked", "Picking SC \"" << getName().toStdString() << "\": pointId #" << pointId << " in children DC named " << childrenComponent[pointId]->getName().toStdString());
        // the atomsToGeometry was used to create the Geometry...
        // ... thus the atoms are in the same order as the structures in the SC
        childrenComponent[pointId]->setSelected ( pickingIsSelecting );
    } else {
        if ( getSC()->composedBy() == StructuralComponent::CELLS) {
            // the pointId can be different than
            //CAMITK_ERROR("StructuralComponentDC", "pointPicked", "Picking SC \"" << getName().toStdString() << "\": pointId #" << pointId << " vs atomDCs size=" << atomDCs.size());
            if (pointId >= (vtkIdType) atomDCs.size()) {
                CAMITK_ERROR("StructuralComponentDC", "pointPicked", "Picking SC \"" << getName().toStdString() << "\": pointId #" << pointId << " is not a valid id (max=" << atomDCs.size() << ")");
            } else {
                //CAMITK_ERROR("StructuralComponentDC", "pointPicked", "Picking SC \"" << getName().toStdString() << "\": pointId #" << pointId << " in children DC named " << atomDCs[pointId]->getName().toStdString());
                // the cellsToGeometry method was used to create the Geometry...
                // ... thus the atomDC where placed in the vector
                atomDCs[pointId]->setSelected ( pickingIsSelecting );
            }
        }
    }
}

//------------------------ cellPicked ------------------------
void StructuralComponentDC::cellPicked ( vtkIdType cellId, bool pickingIsSelecting ) {
    if ( getSC()->composedBy() == StructuralComponent::CELLS ) {
        // the cellsToGeometry method was used to create the Geometry...
        // ... thus the cells are in the same order as the structures in the SC
        childrenComponent[cellId]->setSelected ( pickingIsSelecting );
    }

    // if the atomsToGeometry method was used to create the Geometry...
    // ... thus there are no cells! (the user should use point picking)
}

//------------------------ setSelected ------------------------
void StructuralComponentDC::setSelected ( const bool s, const bool recursive ) {
    // do not select sub atoms if this is not selected
    if ( recursive && !s && getSC()->composedBy() == StructuralComponent::ATOMS ) {
        camitk::Component::setSelected ( s, true );
    } else
        camitk::Component::setSelected ( s, false );
}

//-------------------------- setEnhancedModes --------------------------
void StructuralComponentDC::setEnhancedModes ( const EnhancedModes em ) {
    if ( getSC()->composedBy() != StructuralComponent::ATOMS )
        camitk::Component::setEnhancedModes ( em );
}

//-------------------------- setRenderingMode --------------------------
void StructuralComponentDC::setRenderingModes ( const RenderingModes m ) {
    camitk::Component::setRenderingModes ( m );
    getSC()->setMode ( myPMManagerDC->toPMRenderingMode ( m ) );
}

//---------------------------- setModeColor -------------------
void StructuralComponentDC::setActorColor ( const RenderingModes mode, const double r, const double g, const double b ) {
    camitk::Component::setActorColor ( mode, r, g, b );
    // get the current opacity for this mode
    double oldColor[4];
    getActorColor ( mode, oldColor );
    // set the color for of the SC
    getSC()->setColor ( r, g, b, oldColor[3] );
}

void StructuralComponentDC::setActorColor ( const RenderingModes mode, double newColor[4] ) {
    camitk::Component::setActorColor ( mode, newColor );
    getSC()->setColor ( newColor[0], newColor[1], newColor[2], newColor[3] );
}

//------------------------ setColor ---------------------------
void StructuralComponentDC::setColor ( const double r, const double g , const double b ) {
    camitk::Component::setColor ( r, g, b );
    getSC()->setColor ( r, g, b );
}

void StructuralComponentDC::setColor ( const double r, const double g, const double b, const double a ) {
    camitk::Component::setColor ( r, g, b, a );
    getSC()->setColor ( r, g, b, a );
}

//-------------------- composedBy --------------------
StructuralComponent::ComposedBy StructuralComponentDC::composedBy() {
    return getSC()->composedBy();
}

//-------------------- addChild --------------------
void StructuralComponentDC::addChild ( InterfaceNode *childNode ) {
    Structure *s = NULL;

    if ( dynamic_cast<AtomDC *> ( childNode ) )
        s = dynamic_cast<AtomDC *> ( childNode )->getAtom();
    else if ( dynamic_cast<CellDC *> ( childNode ) )
        s = dynamic_cast<CellDC *> ( childNode )->getCell();

    // check compatibility
    if ( s && getSC()->isCompatible ( s ) ) {
        // add with no check (that just has been done in the previous if statement)
        // but add only if not already there
        getSC()->addStructureIfNotIn ( s );
        // if it was possible to add it, then do the normal stuff
        camitk::Component::addChild ( childNode );
    }
}

//-------------------- removeChild -------------------
void StructuralComponentDC::removeChild ( InterfaceNode *childNode ) {
    Structure *s = NULL;

    if ( dynamic_cast<AtomDC *> ( childNode ) )
        s = dynamic_cast<AtomDC *> ( childNode )->getAtom();
    else if ( dynamic_cast<CellDC *> ( childNode ) )
        s = dynamic_cast<CellDC *> ( childNode )->getCell();

    if ( s ) {
        // update
        if ( dynamic_cast<AtomDC *> ( childNode ) ) {
            dynamic_cast<AtomDC *> ( childNode )->unregisterIndexInSCDC ( this );
        } else {
            int id = childrenComponent.indexOf ( dynamic_cast<camitk::Component *> ( childNode ) );

            if ( id != -1 )
                cellIdToBeRemoved.push_back ( id );
        }

        camitk::Component::removeChild ( childNode );

        getSC()->removeStructure ( s );
    }
}


//-------------------- structuralComponentToVtk -------------------
vtkSmartPointer<vtkUnstructuredGrid> StructuralComponentDC::structuralComponentToVtk ( StructuralComponent * sc, std::AtomVtkPointsIndexMap *atomVtkPointsIndexMap ) {

    unsigned int totalNumberOfCells = sc->getNumberOfCells();

    // create the vtkCellArray and other needed arrays
    int * types = new int [totalNumberOfCells];
    int * t = types;
    vtkSmartPointer<vtkCellArray> array = vtkSmartPointer<vtkCellArray>::New();
    array->Allocate ( totalNumberOfCells );
    vtkSmartPointer<vtkIdTypeArray> idTypeArray = vtkSmartPointer<vtkIdTypeArray>::New();

    // instanciate a new unstructured grid
    vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();

    unsigned int vtkPointsAtomIndex = 0; // index of the atom in the vtkPoints array

    std::AtomVtkPointsIndexMapIterator result; // result of a seach in the map
    Atom *a;
    Cell *c;
    int atomOrderNumber;
    unsigned int i;

    for ( i = 0; i < totalNumberOfCells; i++ ) {
        c = ( Cell * ) sc->getStructure ( i );

        // 1. build the array of point index (for all the cell's atom)
        // the order of the atoms should be the same as the SC::Atoms() method
        idTypeArray->InsertNextValue ( c->getNumberOfStructures() );

        for ( unsigned int j = 0; j < c->getNumberOfStructures(); j++ ) {
            a = ( Atom * ) c->getStructure ( j );

            // test if the atom here is not already in the list
            // In any case retrieve the index of a in the vtkPoints array
            result = atomVtkPointsIndexMap->find ( a );

            if ( result == atomVtkPointsIndexMap->end() )
                atomOrderNumber = -1;
            else
                atomOrderNumber = result->second;

            if ( atomOrderNumber == -1 ) {
                atomOrderNumber = vtkPointsAtomIndex;
                atomVtkPointsIndexMap->insert ( std::AtomVtkPointsIndexPair ( a, vtkPointsAtomIndex++ ) );
            }

            // push the index in the VtkPoints of the j-th atom in the i-th cell
            idTypeArray->InsertNextValue ( atomOrderNumber );
        }

        // 2. register the type for the i-th cell
        // get the vtk type
        switch ( c->getType() ) {
        case StructureProperties::TETRAHEDRON:
            *t++ = VTK_TETRA;
            break;
        case StructureProperties::HEXAHEDRON:
            *t++ = VTK_HEXAHEDRON;
            break;
        case StructureProperties::WEDGE:
            *t++ = VTK_WEDGE;
            break;
        case StructureProperties::PYRAMID:
            *t++ = VTK_PYRAMID;
            break;
        case StructureProperties::LINE:
            *t++ = VTK_LINE;
            break;
        case StructureProperties::POLY_LINE:
            *t++ = VTK_POLY_LINE;
            break;
        case StructureProperties::POLY_VERTEX:
            *t++ = VTK_POLY_VERTEX;
            break;
        case StructureProperties::TRIANGLE:
            *t++ = VTK_TRIANGLE;
            break;
        case StructureProperties::QUAD:
            *t++ = VTK_QUAD;
            break;
        default:
            *t++ = VTK_VERTEX;
            break;
        }
    }

    // create the vtkPoints array for the cells
    vtkSmartPointer<vtkPoints> thePoints = vtkSmartPointer<vtkPoints>::New();

    // insert only the atoms that are in the map (the one needed by the cells) (thanks to the STL god!)
    std::AtomVtkPointsIndexMapIterator it;

    double pos[3];

    for ( it = atomVtkPointsIndexMap->begin(); it != atomVtkPointsIndexMap->end(); it++ ) {
        a = it->first;
        a->getPosition ( pos );
        thePoints->InsertPoint ( it->second, pos[0], pos[1], pos[2] );
    }

    // insert everything everywhere (but at the right place!)
    array->SetCells ( totalNumberOfCells, idTypeArray );
    uGrid->SetPoints ( thePoints );
    uGrid->SetCells ( types, array );
    delete [] types;

    return uGrid;
}

vtkSmartPointer<vtkUnstructuredGrid> StructuralComponentDC::structuralComponentToVtk ( StructuralComponent * sc ) {
    std::AtomVtkPointsIndexMap atomMap;
    return StructuralComponentDC::structuralComponentToVtk ( sc, &atomMap );
}


//------------------------ getPixmap ---------------------
#include "structuralcomponent_20x20.xpm"
QPixmap * StructuralComponentDC::myPixmap = NULL;
QPixmap StructuralComponentDC::getIcon() {
    if ( !myPixmap ) {
        myPixmap = new QPixmap ( structuralcomponent_20x20 );
    }

    return ( *myPixmap );
}

//-------------------- createPointData -------------------
void StructuralComponentDC::createPointData() {
    // check that we are really composed only by cells
    if ( !pointData && getSC()->composedBy() == StructuralComponent::CELLS ) {
        // create a new vtkDataArray
        pointData = vtkSmartPointer<vtkDoubleArray>::New();
        pointData->SetNumberOfTuples ( atomDCs.size() );
        setPointData ( pointData );

        // give the atom DCs the address of the corresponding point data
        for ( unsigned int i = 0; i < atomDCs.size(); i++ ) {
            atomDCs[i]->addPointData ( this, pointData->GetPointer ( i ) );
        }
    }
}

//-------------------- destroyPointData -------------------
void StructuralComponentDC::destroyPointData() {
    // delete the pointData
    if ( pointData ) {
        pointData = NULL;
        setPointData ( pointData );

        // clear the AtomDC's pointers
        for ( unsigned int i = 0; i < atomDCs.size(); i++ ) {
            atomDCs[i]->clearPointData();
        }
    }
}