File: StructuralComponent.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 (313 lines) | stat: -rw-r--r-- 10,491 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
/*****************************************************************************
 * $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 "StructuralComponent.h"
#include "Structure.h"
#include "StructuralComponentProperties.h"
#include "Cell.h"
#include "Atom.h"
#include <RenderingMode.h>

// pmlschema includes
#include <StructuralComponent.hxx>

// ------------------ constructors ---------------------
StructuralComponent::StructuralComponent(PhysicalModel *p) : Component(p) {
    // delete the properties that has just been instanciated by
    // the Component(..) constructor
    deleteProperties();

    // create a new proper one
    properties = new StructuralComponentProperties(p);
    atomList = NULL;
}

StructuralComponent::StructuralComponent(PhysicalModel *p, physicalModel::StructuralComponent xmlSC) : Component(p) {
    // delete the properties that has just been instanciated by
    // the Component(..) constructor
    deleteProperties();

    // create a new proper one
    properties = new StructuralComponentProperties(p, xmlSC);

    if(xmlSC.nrOfStructures().present())
        this->plannedNumberOfStructures(xmlSC.nrOfStructures().get().value());

    atomList = NULL;
}

StructuralComponent::StructuralComponent(PhysicalModel *p, std::string n) : Component(p,n) {
    // delete the properties that has just been instanciated by
    // the Component(..) constructor
    deleteProperties();

    // create a new proper one
    properties = new StructuralComponentProperties(p,n);
    atomList = NULL;
}

// ------------------ destructor ---------------------
StructuralComponent::~StructuralComponent() {
    deleteProperties();

    if (atomList)
        delete atomList;
    atomList = NULL;

    // delete all children
    deleteAllStructures();

    // tell all parents that I am going away to the paradise of pointers
    removeFromParents();
}

// ------------------ deleteAllStructures ---------------------
void StructuralComponent::deleteAllStructures() {
    std::vector <Structure *>::iterator it = structures.begin();
    while (it != structures.end()) {
        // (*it) is of type "Structure *"
        // delete (*it) only if "this" is the manager,
        // i.e. only if "this" is the first of the (*it) mySC vector
        if ((*it)->getStructuralComponent(0) == this)
            delete (*it);
        // "au suivant !"
        it ++;
    }
    structures.clear();
}

// ------------------ setColor ---------------------
void StructuralComponent::setColor(const StructuralComponentProperties::Color c) {
    ((StructuralComponentProperties *)properties)->setColor(c);
}
void StructuralComponent::setColor(const double r, const double g, const double b, const double a) {
    ((StructuralComponentProperties *)properties)->setRGBA(r,g,b,a);
}
void StructuralComponent::setColor(const double r, const double g, const double b) {
    ((StructuralComponentProperties *)properties)->setRGB(r,g,b);
}

// ------------------ getColor ---------------------
double * StructuralComponent::getColor() const {
    return ((StructuralComponentProperties *)properties)->getRGBA();
}

void StructuralComponent::getColor(double *r, double *g, double *b, double *a) const {
    *r = ((StructuralComponentProperties *)properties)->getRed();
    *g = ((StructuralComponentProperties *)properties)->getGreen();
    *b = ((StructuralComponentProperties *)properties)->getBlue();
    *a = ((StructuralComponentProperties *)properties)->getAlpha();
}

// ------------------ getStructuralComponentPropertiesColor ---------------------
StructuralComponentProperties::Color StructuralComponent::getStructuralComponentPropertiesColor() const {
    return ((StructuralComponentProperties *)properties)->getColor();
}


// ------------------ setMode ---------------------
void StructuralComponent::setMode(const RenderingMode::Mode m) {
    ((StructuralComponentProperties *)properties)->setMode(m);
}

// ------------------ getMode ---------------------
RenderingMode::Mode StructuralComponent::getMode() const {
    return ((StructuralComponentProperties *)properties)->getMode();
}

// ------------------ isVisible ---------------------
bool StructuralComponent::isVisible(const RenderingMode::Mode mode) const {
    return ((StructuralComponentProperties *)properties)->isVisible(mode);
}

// ------------------ setVisible ---------------------
void StructuralComponent::setVisible(const RenderingMode::Mode mode, const bool b) {
    ((StructuralComponentProperties *)properties)->setVisible(mode, b);
}

// --------------- xmlPrint ---------------
void StructuralComponent::xmlPrint(std::ostream &o) const {
    if (getNumberOfStructures()>0) {
        o << "<structuralComponent";

        // ...the properties...
        ((StructuralComponentProperties *)properties)->xmlPrint(o);

        o << ">" << std::endl;

        // print the color as well if it is not the default one
        if (((StructuralComponentProperties *)properties)->getColor() != StructuralComponentProperties::DEFAULT) {
            o << "<color r=\"" << ((StructuralComponentProperties *)properties)->getRed();
            o << "\" g=\"" << ((StructuralComponentProperties *)properties)->getGreen();
            o << "\" b=\"" << ((StructuralComponentProperties *)properties)->getBlue();
            o << "\" a=\"" << ((StructuralComponentProperties *)properties)->getAlpha() << "\"/>" << std::endl;
        }

        // optimize the memory allocation for reading
        o << "<nrOfStructures value=\"" << structures.size() << "\"/>" << std::endl;

        // print out all the structures
        for (unsigned int i=0; i<structures.size(); i++) {
            structures[i]->xmlPrint(o,this);
        }
        o << "</structuralComponent>" << std::endl;
    }
}

// --------------- getNumberOfCells ---------------
unsigned int StructuralComponent::getNumberOfCells() const {
    unsigned int nrOfCells = 0;

    // add all the cells of all the sub components
    for (unsigned int i=0; i<structures.size(); i++) {
        if (structures[i]->isInstanceOf("Cell"))
            nrOfCells++;
    }

    return nrOfCells;
}

// --------------- getCell ---------------
Cell * StructuralComponent::getCell(unsigned int cellOrderNr) const {
    unsigned int cellON;
    unsigned int i;
    Cell *c;

    if (structures.size()==0)
        return NULL;

    i = cellON = 0;
    c = NULL;
    do {
        if (structures[i]->isInstanceOf("Cell")) {
            if (cellON==cellOrderNr)
                c = (Cell *) structures[i];
            cellON++;
        }
    } while (!c && ++i<structures.size());

    return c;
}

// --------------- getCellPosition ---------------
/*
int StructuralComponent::getCellPosition(const Cell *cell) const {

	// look for the position of this cell in the SC structures list
	for (unsigned int i=0; i<structures.size(); i++) {
		if (structures[i]->isInstanceOf("Cell"))
		{
			if (cell->getIndex() == structures[i]->getIndex())
				return i;
		}

	// This cell is not in this StructuralComponent, return -1
	return -1;
}
*/

// --------------- isStructureIn ---------------
bool StructuralComponent::isStructureIn(Structure* s) {
    std::vector<Structure *>::iterator it;
    it = std::find(structures.begin(), structures.end(), s);

    return (it != structures.end());
}


// --------------- addStructureIfNotIn ---------------
bool StructuralComponent::addStructureIfNotIn(Structure *s) {

    if (!isStructureIn(s)) {
        // not in, add it
        addStructure(s);
        return true; // structure added
    } else
        return false;
}

// --------------- getAtoms ---------------
StructuralComponent *StructuralComponent::getAtoms() {
    if (atomList)
        // already created, return it
        return atomList;

    //@@@ Define name : this name + atom List?
    //getName()
    //atomList = new StructuralComponent(myName);

    // Note: atom List is deleted in StructuralComponent destructor
    atomList = new StructuralComponent(properties->getPhysicalModel());

    // loop through all structures
    for (unsigned int i=0; i<this->getNumberOfStructures(); i++) {
        Structure *s = this->getStructure(i);

        if (s->isInstanceOf("Atom")) {
            atomList->addStructureIfNotIn(s);
        } else { // s is a Cell
            Cell *cell = (Cell *) s;
            for (unsigned int j=0; j<cell->getNumberOfStructures(); j++) {
                // there is only atoms in a Cell!
                atomList->addStructureIfNotIn(cell->getStructure(j));
            }
        }

    }

    return atomList;
}

// --------------- composedBy ---------------
StructuralComponent::ComposedBy StructuralComponent::composedBy() {
    if (getNumberOfStructures()==0)
        return NOTHING;
    if (getStructure(0)->isInstanceOf("Cell"))
        return CELLS;
    if (getStructure(0)->isInstanceOf("Atom"))
        return ATOMS;
    return NOTHING; // should never occurs!
}

// --------------- isCompatible ---------------
bool StructuralComponent::isCompatible(Structure *s) {
    StructuralComponent::ComposedBy cb;
    cb = composedBy();
    return ((s!=NULL) &&
            ((cb==NOTHING) ||
             (s->isInstanceOf("Atom") && cb==ATOMS) ||
             (s->isInstanceOf("Cell") && cb==CELLS)));

}

// --------------- setPhysicalModel ---------------
void StructuralComponent::setPhysicalModel(PhysicalModel* pm) {
    Component::setPhysicalModel(pm);
    // loop through all structures
    for (unsigned int i=0; i<this->getNumberOfStructures(); i++)
        this->getStructure(i)->setPhysicalModel(pm);
}