File: Load.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (369 lines) | stat: -rw-r--r-- 8,140 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
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
/***************************************************************************
                          Load.cpp  -  description
                             -------------------
    begin                : mar f�v 11 2003
    copyright            : (C) 2003 by Emmanuel Promayon
    email                : Emmanuel.Promayon@imag.fr

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "Load.h"

#include "Translation.h"
#include "Rotation.h"
#include "Force.h"
#include "Pressure.h"

//--------- 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();
	}

	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 SReal 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
SReal Load::getValue ( const SReal 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 ) );
	}
}

#ifdef WIN32
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 );

	// !TODO : SORT the <value event *> by date
	//* sort the list
#ifdef WIN32
	std::sort ( eventList.begin(), eventList.end(), std::greater<ValueEvent *>() ); // 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 SReal v, const SReal 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 ( SReal &x, SReal &y, SReal &z ) const
{
	x = dir.getX();
	y = dir.getY();
	z = dir.getZ();
}

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

// --------------- setDirection ---------------
void Load::setDirection ( SReal x, SReal y, SReal 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;
}