File: PMLComponentExtension.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 (202 lines) | stat: -rw-r--r-- 7,267 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
/*****************************************************************************
 * $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 "PMLComponentExtension.h"

// CEP stuff
#include <PMLComponent.h>

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

// CamiTK stuff
#include <Application.h>

using namespace camitk;

//-- vtk
#include <vtkCell.h>

// --------------- getName -------------------
QString PMLComponentExtension::getName() const {
    return "PML Component";
}

// --------------- getDescription -------------------
QString PMLComponentExtension::getDescription() const {
    return "<b>New PML COMPONENT!</b>";// Manage Physical Model <em>.pml</em> files in <b>CamiTK</b>.<br/>CamiTK was initially mainly developed to support this format. Lots of things are possible with a physical model!";
}

// --------------- getFileExtensions -------------------
QStringList PMLComponentExtension::getFileExtensions() const {
    QStringList ext;
    ext << "pml";
    return ext;
}

// --------------- open -------------------
camitk::Component * PMLComponentExtension::open(const QString & fileName) throw(AbortException) {

    Application::showStatusBarMessage("Loading " + fileName + "...");

    // instanciate the component
    PMLComponent * pmlComponent = new PMLComponent(fileName);

    // reset the progress bar
    Application::resetProgressBar();

    return pmlComponent;
}

// --------------- save -------------------
bool PMLComponentExtension::save(camitk::Component * component) const {
    PMLComponent * comp = dynamic_cast<PMLComponent *>(component);

    if (comp) {
        // easy!
        // Just generate an ostream from the filename and then xmlPrint pop
        std::ofstream outputFile(comp->getFileName().toStdString().c_str());
        comp->getPhysicalModel()->xmlPrint(outputFile);
        outputFile.close();
        comp->setModified(false);
        return true;
    } else {
        // save from generic MeshComponent
        MeshComponent * meshComp = dynamic_cast<MeshComponent *>(component);

        if (meshComp && meshComp->getPointSet()->GetNumberOfPoints() > 0) {
            //-- create a new physical model
            PhysicalModel * newPM = new PhysicalModel();

            // extract the atoms from the Geometry
            vtkSmartPointer<vtkPoints> thePoints = vtkSmartPointer<vtkPoints>::New();
            thePoints->DeepCopy(meshComp->getPointSet()->GetPoints());

            //-- create the structural components for the atoms
            StructuralComponent * theAtoms = new StructuralComponent(newPM, "All Atoms");
            double pos[3];

            // create the atom structures
            for (int i = 0; i < thePoints->GetNumberOfPoints(); i++) {
                thePoints->GetPoint(i, pos);
                theAtoms->addStructure(new Atom(newPM, pos), false);
            }

            // set the atom sc
            newPM->setAtoms(theAtoms);

            //-- create the unique structural components containing all the vtkCell, this is an exclusive component
            unsigned int nrOfCells = meshComp->getPointSet()->GetNumberOfCells();
            StructuralComponent * sc = new StructuralComponent(newPM, "All Cells");

            // fill in this new structural component with all vtk cells
            for (vtkIdType i = 0; i < (vtkIdType) nrOfCells; i++) {
                // create a cell for each cell
                vtkCell * theCell = meshComp->getPointSet()->GetCell(i);

                // translate cell type
                StructureProperties::GeometricType cellType;

                switch (theCell->GetCellType()) {
                case VTK_TETRA:
                    cellType = StructureProperties::TETRAHEDRON;
                    break;

                case VTK_HEXAHEDRON:
                    cellType = StructureProperties::HEXAHEDRON;
                    break;

                case VTK_WEDGE:
                    cellType = StructureProperties::WEDGE;
                    break;

                case VTK_PYRAMID:
                    cellType = StructureProperties::PYRAMID;
                    break;

                case VTK_LINE:
                    cellType = StructureProperties::LINE;
                    break;

                case VTK_POLY_LINE:
                    cellType = StructureProperties::POLY_LINE;
                    break;

                case VTK_POLY_VERTEX:
                    cellType = StructureProperties::POLY_VERTEX;
                    break;

                case VTK_TRIANGLE:
                    cellType = StructureProperties::TRIANGLE;
                    break;

                case VTK_QUAD:
                    cellType = StructureProperties::QUAD;
                    break;

                default:
                    cellType = StructureProperties::INVALID;
                    break;
                }

                // create the corresponding PML cell
                Cell * c = new Cell(newPM, cellType);

                // fill-in the cell structures (atoms)
                for (int i = 0; i < theCell->GetNumberOfPoints(); i++) {
                    // get the corresponding atom
                    Atom * a = newPM->getAtom(theCell->GetPointId(i)); // dynamic_cast<Atom *>(theAtoms->getStructure(theCell->GetPointId(i)));
                    // set the corresponding atom as a structure composing the cell
                    c->addStructure(a, false);
                }

                // insert the cell in the structure
                sc->addStructure(c, false);
            }

            // create the exclusive multi-component
            MultiComponent * exclusiveComponents = new MultiComponent(newPM);

            // insert all the cells (i.e. the sc component)
            exclusiveComponents->addSubComponent(sc);
            exclusiveComponents->setName("Exclusive Components");

            // insert this exclusive component into the pm
            newPM->setExclusiveComponents(exclusiveComponents);

            // save it!
            std::ofstream outputFile(meshComp->getFileName().toStdString().c_str());
            newPM->xmlPrint(outputFile);
            meshComp->setModified(false);
            outputFile.close();
            return true;
        }
    }

    return false;

}