File: PMManagerDC.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 (406 lines) | stat: -rw-r--r-- 12,602 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
/*****************************************************************************
 * $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 "PMManagerDC.h"

// need to be before using namespace camitk
#include <pml/PhysicalModel.h>

//-- CamiTK
#include <Application.h>
#include <InteractiveViewer.h>

using namespace camitk;

//-- PMLComponents
#include "LoadsManager.h"
#include "StructuralComponentDC.h"
#include "MultiComponentDC.h"
#include "PMManagerDCPopup.h"
#include "CellDC.h"
#include "AtomDCWidget.h"
#include "AtomDC.h"

//-- Qt
#include <QFileInfo>

//-- Vtk
#include <vtkUnstructuredGrid.h>

// -------------------- default constructor --------------------
PMManagerDC::PMManagerDC(const QString &fileName) throw(AbortException) : MeshComponent(fileName) {
    myPM = NULL;
    initialBoundingRadius = 0.0;
    myLoadsManager = NULL;
    myAtomDCWidget = NULL;

    // create the Physical model DCs
    try {

        if (!QFileInfo(myFileName).exists()) {
            throw AbortException("Cannot Open File:" + myFileName.toStdString() + ": file does not exist!");
        }

        //-- create the PhysicalModel
        // directly instantiate physical model
        myPM = new PhysicalModel(myFileName.toStdString().c_str());

        if (!myPM || myPM->getAtoms() == NULL) {
            throw AbortException("Cannot Open PML, either not a valid PML document or no atoms in this PML (for file:" + myFileName.toStdString() + ")");
        }

        computeBoundingRadius();

        // -- initialize fields
        myPopupMenu = NULL;

        setName(myPM->getName().c_str());

        // build all the sub DCs
        buildPhysicalModelDCs();

        // add it in the InteractiveViewer
        setVisibility(InteractiveViewer::get3DViewer(), true);

        // -- create the Loads Manager
        myLoadsManager = new LoadsManager(this);

        //-- init fake representation
        initRepresentation();

        //--reset modification flag
        setModified(false);
    }
    catch (PMLAbortException & e) {
        // has to do that, because the PMManagerDC constructor
        // inherits from Component constructor, which register the object
        // as a member of the DataManager, i.e. myDCs.size() is equal to 1
        // although the DC is not completely initialized!
        // delete all the DCs (the Manager DC included)
        while (getChildren().size() > 0) {
            Component *dc = getChildren().last();
            removeChild(dc);
        }

        childrenComponent.clear();

        // re-throw the exception
        throw e;
    }
}

// --------------------- constructor ----------------------------
PMManagerDC::PMManagerDC(PhysicalModel *pm, const QString &fileName) : MeshComponent(fileName) {
    myPM = pm;
    initialBoundingRadius = 0.0;
    myLoadsManager = NULL;
    myAtomDCWidget = NULL;

    computeBoundingRadius();

    // -- initialize fields
    myPopupMenu = NULL;

    setName(myPM->getName().c_str());

    // build all the sub DCs
    buildPhysicalModelDCs();

    // add it in the InteractiveViewer
    setVisibility(InteractiveViewer::get3DViewer(), true);

    // -- create the Loads Manager
    myLoadsManager = new LoadsManager(this);

    //-- init fake representation
    initRepresentation();

    //--reset modification flag
    setModified(false);
}


// ---------------------- destructor ----------------------------
PMManagerDC::~PMManagerDC() {
    delete myPopupMenu;
    myPopupMenu = NULL;

    delete myAtomDCWidget;
    myAtomDCWidget = NULL;

    delete myLoadsManager;
    myLoadsManager = NULL;

    // remove all the adc myself
    for (unsigned int i = 0;i < myPM->getAtoms()->getNumberOfStructures(); i++) {
        AtomDC * adc = getDC(dynamic_cast<Atom*>(myPM->getAtoms()->getStructure(i)));
        delete adc;
    }

    delete myPM;
    myPM = NULL;
}

// -------------------- initRepresentation --------------------
void PMManagerDC::initRepresentation() {
    myGeometry = new Geometry(getName(), vtkSmartPointer<vtkUnstructuredGrid>::New(), InterfaceGeometry::None);
}

// -------------------- progressOneStep --------------------
void PMManagerDC::progressOneStep() {
    nrOfDoneSteps++;
    float done = (100.0 * ((float)nrOfDoneSteps) / (float)nrOfSteps);

    if (done > 99.0)
        nrOfDoneSteps--;

    Application::setProgressBarValue(50.0 + done*0.5);
}


// -------------------- buildPhysicalModelDCs --------------------
void PMManagerDC::buildPhysicalModelDCs() {
    nrOfDoneSteps = 0;
    nrOfSteps =  1 + myPM->getNumberOfCells();

    // create and add the atoms SC
    StructuralComponent *sc = myPM->getAtoms();

    if (sc)
        new StructuralComponentDC(this, this, sc);

    // create and add the exclusive components
    if (myPM->getNumberOfExclusiveComponents() > 0)
        new MultiComponentDC(this, this, myPM->getExclusiveComponents());

    // create and add the informative component
    if (myPM->getNumberOfInformativeComponents() > 0)
        new MultiComponentDC(this, this, myPM->getInformativeComponents());
}

// -------------------- createPointData --------------------
void PMManagerDC::createPointData() {
    // ask the exclusive and informative component to create the point data
    foreach(Component *child, getChildren()) {
        if (child->isInstanceOf("MultiComponentDC")) {
            dynamic_cast<MultiComponentDC *>(child)->createPointData();
        }
    }
}

// -------------------- destroyPointData --------------------
void PMManagerDC::destroyPointData() {
    // destroy all point data
    foreach(Component *child, getChildren()) {
        if (child->isInstanceOf("MultiComponentDC")) {
            dynamic_cast<MultiComponentDC *>(child)->destroyPointData();
        }
    }
}

// -------------------- setName --------------------
void PMManagerDC::setName(const QString & n) {
    myPM->setName(n.toStdString());
    Component::setName(n);
}

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

    dynamic_cast<PMManagerDCPopup *>(myPopupMenu)->updateMenuActions();

    return myPopupMenu;
}

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

    return (*myPixmap);
}

// ---------------------- getDC ----------------------------
MultiComponentDC * PMManagerDC::getDC(MultiComponent *mc) {
    if (!mc)
        return NULL;

    std::ComponentDCMapIterator result = myMCDCMap.find(dynamic_cast< ::Component *>(mc));

    return (result == myMCDCMap.end()) ? NULL : dynamic_cast<MultiComponentDC *>(result->second);
}

StructuralComponentDC * PMManagerDC::getDC(StructuralComponent *sc) {
    if (!sc)
        return NULL;

    std::ComponentDCMapIterator result = mySCDCMap.find(dynamic_cast< ::Component *>(sc));

    return (result == mySCDCMap.end()) ? NULL : dynamic_cast<StructuralComponentDC *>(result->second);
}

// ---------------------- static getModifiedFlag ----------------------------
bool PMManagerDC::getModified() const {
    return (modifiedFlag || myLoadsManager->isModified() || myPM->isModified());
}

// -------------------- Cell / DC Map : addCellDCPair --------------------
void PMManagerDC::addCellDCPair(std::ComponentDCPair p) {
    // cout << "add a new pair <" << p.first << "," << p.second << ">" << endl;
    myCDCMap.insert(p);
}

// -------------------- Cell / DC Map : : getDC --------------------
CellDC * PMManagerDC::getDC(Cell *sc) {
    /* Component *c;
     c = (StructuralComponent *) sc;
     cout << "search for " << sc << ": " << (Component *) sc << " / " << c << endl;
     */
    std::ComponentDCMapIterator result = myCDCMap.find(dynamic_cast< ::Component *>(sc));
    return (result == myCDCMap.end()) ? NULL : dynamic_cast<CellDC *>(result->second);
}

// -------------------- getAtomDCWidget --------------------
QWidget * PMManagerDC::getAtomDCWidget(AtomDC *adc, QWidget *parent) {

    // create if needed
    if (!myAtomDCWidget && adc) {
        myAtomDCWidget = new AtomDCWidget(adc, parent);
    }

    // update the properties
    if (myAtomDCWidget) {
        myAtomDCWidget->updateProperties(adc);
    }

    // return the ptr
    return myAtomDCWidget;
}

// -------------------- toPMRenderingMode --------------------
::RenderingMode::Mode PMManagerDC::toPMRenderingMode(InterfaceGeometry::RenderingModes renderingModes) {
    ::RenderingMode converted;

    if (renderingModes & InterfaceGeometry::Surface) {
        converted.setVisible(::RenderingMode::SURFACE, true);
    }

    if (renderingModes & InterfaceGeometry::Wireframe) {
        converted.setVisible(::RenderingMode::WIREFRAME, true);
    }

    if (renderingModes & InterfaceGeometry::Points) {
        converted.setVisible(::RenderingMode::POINTS, true);
    }

    return converted.getMode();
}

// -------------------- toDCRenderingMode --------------------
InterfaceGeometry::RenderingModes PMManagerDC::toDCRenderingMode(::RenderingMode::Mode renderingMode) {
    InterfaceGeometry::RenderingModes converted;

    switch (renderingMode)  {
    case ::RenderingMode::NONE:
        converted = InterfaceGeometry::None;
        break;
    case ::RenderingMode::POINTS:
        converted = InterfaceGeometry::Points;
        break;
    case ::RenderingMode::POINTS_AND_SURFACE:
        converted = InterfaceGeometry::Points | InterfaceGeometry::Surface;
        break;
    case ::RenderingMode::SURFACE:
        converted = InterfaceGeometry::Surface;
        break;
    case ::RenderingMode::WIREFRAME_AND_SURFACE_AND_POINTS:
        converted = InterfaceGeometry::Points | InterfaceGeometry::Surface | InterfaceGeometry::Wireframe;
        break;
    case ::RenderingMode::WIREFRAME_AND_SURFACE:
        converted = InterfaceGeometry::Surface | InterfaceGeometry::Wireframe;
        break;
    case ::RenderingMode::WIREFRAME_AND_POINTS:
        converted = InterfaceGeometry::Points | InterfaceGeometry::Wireframe;
        break;
    case ::RenderingMode::WIREFRAME:
        converted = InterfaceGeometry::Wireframe;
        break;
    }
    return converted;
}

// --------------- getBounds -------------------
void PMManagerDC::getBounds(double bounds[6]) {
    // compute the bounding radius
    // bounds = [xmin,xmax, ymin,ymax, zmin,zmax]
    bounds[0] = bounds[2] = bounds[4] = 999999999.9f; //MAXFLOAT;
    bounds[3] = bounds[1] = bounds[5] = -999999999.9f; //MINFLOAT;
    Atom *a;
    double pos[3];

    StructuralComponent *theAtoms;
    theAtoms = myPM->getAtoms();

    for (unsigned int i = 0;i < theAtoms->getNumberOfStructures();i++) {
        a = (Atom *) theAtoms->getStructure(i);
        a->getPosition(pos);

        if (pos[0] < bounds[0])
            bounds[0] = pos[0];

        if (pos[0] > bounds[1])
            bounds[1] = pos[0];

        if (pos[1] < bounds[2])
            bounds[2] = pos[1];

        if (pos[1] > bounds[3])
            bounds[3] = pos[1];

        if (pos[2] < bounds[4])
            bounds[4] = pos[2];

        if (pos[2] > bounds[5])
            bounds[5] = pos[2];
    }

}

// --------------- computeBoundingRadius -------------------
void PMManagerDC::computeBoundingRadius() {
    double bounds[6];
    getBounds(bounds);
    double xLength = fabs(bounds[1] - bounds[0]);
    double yLength = fabs(bounds[3] - bounds[2]);
    double zLength = fabs(bounds[5] - bounds[4]);
    initialBoundingRadius = sqrt(xLength * xLength + yLength * yLength + zLength * zLength) / 2.0;
}