File: StructuralComponent.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 88,828 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (287 lines) | stat: -rw-r--r-- 9,988 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
                           StructuralComponent.cpp
                             -------------------
    begin             : Wed Aug 8 2001
    copyright         : (C) 2001 TIMC (Emmanuel Promayon, Matthieu Chabanas)
    email             : Emmanuel.Promayon@imag.fr
    Date              : $Date: 2006/10/17 14:33:22 $
    Version           : $Revision: 1.24 $
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "StructuralComponent.h"
#include "Structure.h"
#include "StructuralComponentProperties.h"
#include "Cell.h"
#include "Atom.h"
#include <RenderingMode.h>

// ------------------ 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, xmlNodePtr node) : Component(p) {
    // delete the properties that has just been instanciated by
    // the Component(..) constructor
    deleteProperties();
    
    // create a new proper one
    properties = new StructuralComponentProperties(p, node);

    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 SReal r, const SReal g, const SReal b, const SReal a) {
    ((StructuralComponentProperties *)properties)->setRGBA(r,g,b,a);
}
void StructuralComponent::setColor(const SReal r, const SReal g, const SReal 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;
}
*/

// --------------- addStructureIfNotIn ---------------
bool StructuralComponent::addStructureIfNotIn(Structure *s) {
    // loof for this Structure in the current list
    std::vector<Structure *>::iterator it;
    it = std::find(structures.begin(), structures.end(), s);

    if (it == structures.end()) {
        // 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));

}