Load.cpp 8.74 KB
Newer Older
1
/*****************************************************************************
2
3
4
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
promayon's avatar
promayon committed
5
 * (c) 2001-2013 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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$
 ****************************************************************************/
25
26
27
28
29
30
31
32
33



#include "Load.h"

#include "Translation.h"
#include "Rotation.h"
#include "Force.h"
#include "Pressure.h"
promayon's avatar
promayon committed
34
#include "Acceleration.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

//--------- static factory -------------
Load * Load::LoadFactory(std::string type) {
    Load * newOne = NULL;

    // instanciate depending on the load type
    if(type=="Translation") {
        newOne = new Translation();
    }
    // case rotation
    else if(type=="Rotation") {
        newOne = new Rotation();
    }
    // case force
    else if(type=="Force") {
        newOne = new Force();
    }
    // case pressure
    else if(type=="Pressure") {
        newOne = new Pressure();
    }
promayon's avatar
promayon committed
56
57
58
59
    // case acceleration
    else if(type=="Acceleration") {
        newOne = new Acceleration();
    }
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

    return newOne;
}

//--------- constructor -------------
Load::Load() {
    typeString = "unknown";
}

//--------- detructor -------------
Load::~Load() {
    deleteEventList();
}


//--------- deleteEventList -------------
void Load::deleteEventList() {
    std::vector<ValueEvent *>::iterator currentE;
    for (currentE = eventList.begin(); currentE != eventList.end(); currentE++) {
        delete (*currentE);
    }
    eventList.clear();
}

//--------- setAllEvents -------------
void Load::setAllEvents(std::vector<ValueEvent *>& newList) {
    deleteEventList();
    std::vector<ValueEvent *>::iterator currentE;
    for (currentE = newList.begin(); currentE != newList.end(); currentE++) {
        addEvent(*currentE);
    }
}

// --------------- isActive ---------------
bool Load::isActive(const double t) {
    std::vector<ValueEvent *>::iterator currentE;

    currentE = eventList.begin();
    while (currentE!=eventList.end()) {
        // current event is active
        if ((*currentE)->isActive(t))
            return true;
        currentE++;
    }

    return false;
}

// --------------- getValue ---------------
// the current norm value at time t
double Load::getValue(const double t) {
    std::vector<ValueEvent *>::iterator currentE;
    std::vector<ValueEvent *>::iterator nextE;

    // search the first active event
    currentE = eventList.begin();
    bool foundLastActive = false;
    bool isLast = false;
    while (currentE!=eventList.end() && !foundLastActive) {
        // current event is active
        if ((*currentE)->isActive(t)) {
            // check if this is the last event
            nextE = currentE + 1;
            if (nextE == eventList.end()) {
                // there is none, so currentE is the last active
                foundLastActive = true;
                isLast = true;
            } else {
                // if there is another event in the list, then check if it is active
                if ((*nextE)->isActive(t)) {
                    // it is active, we need to continue (at least one more step)
                    currentE++;
                } else {
                    // it is not active: currentE is the last active
                    foundLastActive = true;
                }
            }
        } else {
            // the current event is not active, check the next one
            currentE++;
        }
    }

    if (!foundLastActive) {
        // not active
        return 0.0;
    }

    // if
    if (isLast) {
        return (*currentE)->getValue(t);
    } else {
        return (*currentE)->getValue(t, (*nextE));
    }
}

#if defined(_WIN32) && !defined(__MINGW32__) // MSVC only
namespace std {
	bool greater(const ValueEvent* lhs, const ValueEvent* rhs) {
			return lhs->getDate() < rhs->getDate();
	}
}
#else
// ------------- sorting overloaded function ---------
// (saw some comments that say it is better to have
// this kind of overloads where you need them - compiler efficiency?
// so it is here cause needed in the addEvent method for the sort)
namespace std {
    template <>
    struct greater<ValueEvent*> {
        bool operator()(const ValueEvent* lhs, const ValueEvent* rhs) const {
            return lhs->getDate() < rhs->getDate();
        }
    };
}
#endif

// --------------- addEvent ---------------
void Load::addEvent(ValueEvent * ve) {
    // insert the event
    eventList.push_back(ve);

promayon's avatar
promayon committed
182
    //-- sort the list by date
183
#if defined(_WIN32) && !defined(__MINGW32__) && (_MSC_VER < 1600) // MSVC<10 only, see http://msdn.microsoft.com/en-us/library/b0084kay%28v=vs.100%29.aspx
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
	std::sort(eventList.begin(), eventList.end(), std::greater); // use the greater() method (see above)
#else
	std::sort(eventList.begin(), eventList.end(), std::greater<ValueEvent *>()); // use the greater() method (see above)
#endif

}

// --------------- addValueEvent ---------------
void Load::addValueEvent(const double v, const double d) {
    addEvent(new ValueEvent(v,d));
}


// --------------- addTarget ---------------
void Load::addTarget(unsigned int target) {
    targetList.add(target);
}

// --------------- addTarget ---------------
void Load::addTarget(std::string l) {
    targetList.add(l);
}

// --------------- addTarget ---------------
TargetList Load::getTargetList() const {
    return targetList;
}

// --------------- addTarget ---------------
void Load::setTargetList(const TargetList & t) {
    targetList = t;
}

// --------------- numberOfTargets ---------------
unsigned int Load::numberOfTargets() const {
    return targetList.getNumberOfTargets();
}

// --------------- getTarget ---------------
int Load::getTarget(const unsigned int targetIndex) const {
    return targetList.getIndexedTarget(targetIndex);
}


// --------------- getDirection ---------------
void Load::getDirection(double &x, double &y, double &z) const {
    x = dir.getX();
    y = dir.getY();
    z = dir.getZ();
}

Direction Load::getDirection() const {
    return dir;
}

// --------------- setDirection ---------------
void Load::setDirection(double x, double y, double z) {
    dir.set(x ,y , z);
}

void Load::setDirection(const Direction &d) {
    dir = d;
}


// --------------- getValueEvent ---------------
ValueEvent * Load::getValueEvent(const unsigned int i) const {
    if (i<eventList.size())
    return eventList[i];
    else
        return NULL;
    }

    // --------------- numberOfValueEvents ---------------
    unsigned int Load::numberOfValueEvents() const {
            return eventList.size();
        }


        // --------------- getUnit ---------------
        Unit Load::getUnit() const {
            return unit;
        }

        // --------------- setUnit ---------------
        void Load::setUnit(const Unit u) {
        unit = u;
    }


// --------------- getType --------------
std::string Load::getType() const {
    return typeString;
}


// --------------- xmlPrint --------------
//Write the xml borns in the xml file
void  Load::xmlPrint(std::ostream & o) const {
    // the Load tag
    o << "<load xsi:type=\"" << getType() << "\">" << std::endl;

    unsigned int i;
    /*  o << "\t<appliedTo>";
      for (i = 0; i < numberOfTargets(); i++) {
            if (i>0)
                o << ",";
             o << getTarget(i);
        }
        o << "</appliedTo>" << std::endl;
    */
    o << "\t<appliedTo>" << targetList.toString() << "</appliedTo>" << std::endl;

    // the event tags
    ValueEvent *ve;
    for (i=0; i<numberOfValueEvents(); i++) {
        ve = getValueEvent(i);
        ve->xmlPrint(o);
    }

    dir.xmlPrint(o);
   
    o << "\t<unit>" << getUnit().getUnitName() << "</unit>" << std::endl;

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

}

// --------------- ansysPrint --------------
void  Load::ansysPrint(std::ostream & o) const {
    // selection of points
    o << targetList.toAnsys() << std::endl;
}

// --------------- operator << ---------------
std::ostream & operator << (std::ostream &o, Load ) {
    // the Load tag
    o << "<load xsi:type=\"" << "not implemented yet, USE xmlPrint() instead" /*<< ld.getType()*/ << "\">" << std::endl;

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

    return o;
}