File: Context.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (463 lines) | stat: -rw-r--r-- 16,080 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library 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 *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                              SOFA :: Framework                              *
*                                                                             *
* Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
* H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
* M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
#include <sofa/core/objectmodel/Context.h>
// #include <sofa/simulation/common/Visitor.h>


namespace sofa
{

namespace core
{

namespace objectmodel
{

Context::Context()
  : is_activated(initData(&is_activated, true, "activated", "To Activate a node"))
  , worldGravity_(initData(&worldGravity_, Vec3(0,0,0),"gravity","Gravity in the world coordinate system"))
  , dt_(initData(&dt_,0.01,"dt","Time step"))
  , time_(initData(&time_,0.,"time","Current time"))
  , animate_(initData(&animate_,false,"animate","Animate the Simulation(applied at initialization only)"))
  , showVisualModels_           (initData(&showVisualModels_,           -1, "showVisualModels","display Visual Models"))
  , showBehaviorModels_         (initData(&showBehaviorModels_,         -1,"showBehaviorModels","display Behavior Models"))
  , showCollisionModels_        (initData(&showCollisionModels_,        -1,"showCollisionModels","display Collision Models"))
  , showBoundingCollisionModels_(initData(&showBoundingCollisionModels_,-1,"showBoundingCollisionModels","display Bounding Collision Models"))
  , showMappings_               (initData(&showMappings_,               -1,"showMappings","display Mappings"))
  , showMechanicalMappings_     (initData(&showMechanicalMappings_,     -1,"showMechanicalMappings","display Mechanical Mappings"))
  , showForceFields_            (initData(&showForceFields_,            -1,"showForceFields","display Force Fields"))
  , showInteractionForceFields_ (initData(&showInteractionForceFields_, -1,"showInteractionForceFields","display Interaction Force Fields"))
  , showWireFrame_              (initData(&showWireFrame_,              -1,"showWireFrame","display in WireFrame"))
  , showNormals_                (initData(&showNormals_,                -1,"showNormals","display Normals"))
  , multiThreadSimulation_(initData(&multiThreadSimulation_,false,"multiThreadSimulation","Apply multithreaded simulation"))
  , currentLevel_(initData(&currentLevel_,0,"currentLevel","Current level of details"))
  , coarsestLevel_(initData(&coarsestLevel_,3,"coarsestLevel","Coarsest level of details"))
  , finestLevel_(initData(&finestLevel_,0,"finestLevel","Finest level of details"))
{
    setPositionInWorld(objectmodel::BaseContext::getPositionInWorld());
    setGravityInWorld(objectmodel::BaseContext::getLocalGravity());
    setVelocityInWorld(objectmodel::BaseContext::getVelocityInWorld());
    setVelocityBasedLinearAccelerationInWorld(objectmodel::BaseContext::getVelocityBasedLinearAccelerationInWorld());

    addAlias(&showVisualModels_,           "showAll"); addAlias(&showVisualModels_,           "showVisual");

    addAlias(&showBehaviorModels_,         "showAll"); addAlias(&showBehaviorModels_,         "showBehavior");
    addAlias(&showForceFields_,            "showAll"); addAlias(&showForceFields_,            "showBehavior");
    addAlias(&showInteractionForceFields_, "showAll"); addAlias(&showInteractionForceFields_, "showBehavior");


    addAlias(&showCollisionModels_,        "showAll"); addAlias(&showCollisionModels_,        "showCollision");
    addAlias(&showBoundingCollisionModels_,"showAll"); addAlias(&showBoundingCollisionModels_,"showCollision");

    addAlias(&showMappings_,               "showAll"); addAlias(&showMappings_,               "showMapping");
    addAlias(&showMechanicalMappings_,     "showAll"); addAlias(&showMechanicalMappings_,     "showMapping");

    //setDt(objectmodel::BaseContext::getDt());
    //setTime(objectmodel::BaseContext::getTime());
    //setAnimate(objectmodel::BaseContext::getAnimate());
    //setShowCollisionModels(objectmodel::BaseContext::getShowCollisionModels());
    //setShowBoundingCollisionModels(objectmodel::BaseContext::getShowBoundingCollisionModels());
    //setShowBehaviorModels(objectmodel::BaseContext::getShowBehaviorModels());
    //setShowVisualModels(objectmodel::BaseContext::getShowVisualModels());
    //setShowMappings(objectmodel::BaseContext::getShowMappings());
    //setShowMechanicalMappings(objectmodel::BaseContext::getShowMechanicalMappings());
    //setShowForceFields(objectmodel::BaseContext::getShowForceFields());
    //setShowInteractionForceFields(objectmodel::BaseContext::getShowInteractionForceFields());
    //setShowWireFrame(objectmodel::BaseContext::getShowWireFrame());
    //setShowNormals(objectmodel::BaseContext::getShowNormals());
    //setMultiThreadSimulation(objectmodel::BaseContext::getMultiThreadSimulation());
}

/// The Context is active
bool Context::isActive() const {return is_activated.getValue();}

/// State of the context
void Context::setActive(bool val){ is_activated.setValue(val);}

/// Projection from the local coordinate system to the world coordinate system.
const Context::Frame& Context::getPositionInWorld() const
{
    return localFrame_;
}
/// Projection from the local coordinate system to the world coordinate system.
void Context::setPositionInWorld(const Frame& f)
{
    localFrame_ = f;
}

/// Spatial velocity (linear, angular) of the local frame with respect to the world
const Context::SpatialVector& Context::getVelocityInWorld() const
{
    return spatialVelocityInWorld_;
}
/// Spatial velocity (linear, angular) of the local frame with respect to the world
void Context::setVelocityInWorld(const SpatialVector& v)
{
    spatialVelocityInWorld_ = v;
}

/// Linear acceleration of the origin induced by the angular velocity of the ancestors
const Context::Vec3& Context::getVelocityBasedLinearAccelerationInWorld() const
{
    return velocityBasedLinearAccelerationInWorld_;
}
/// Linear acceleration of the origin induced by the angular velocity of the ancestors
void Context::setVelocityBasedLinearAccelerationInWorld(const Vec3& a )
{
    velocityBasedLinearAccelerationInWorld_ = a;
}



/// Simulation timestep
double Context::getDt() const
{
    return dt_.getValue();
}

/// Simulation time
double Context::getTime() const
{
    return time_.getValue();
}

/// Gravity vector in local coordinates
// const Context::Vec3& Context::getGravity() const
// {
// 	return gravity_;
// }

/// Gravity vector in local coordinates
Context::Vec3 Context::getLocalGravity() const
{
    return getPositionInWorld().backProjectVector(worldGravity_.getValue());
}

/// Gravity vector in world coordinates
const Context::Vec3& Context::getGravityInWorld() const
{
    return worldGravity_.getValue();
}



/// Animation flag
bool Context::getAnimate() const
{
    return animate_.getValue();
}

/// MultiThreading activated
bool Context::getMultiThreadSimulation() const
{
    return multiThreadSimulation_.getValue();
}

/// Display flags: Collision Models
bool Context::getShowCollisionModels() const
{
  if (showCollisionModels_.getValue() < 0) return false;
  else return showCollisionModels_.getValue() != 0;
}

/// Display flags: Bounding Collision Models
bool Context::getShowBoundingCollisionModels() const
{
  if (showBoundingCollisionModels_.getValue() < 0) return false;
  else return showBoundingCollisionModels_.getValue()!= 0;
}

/// Display flags: Behavior Models
bool Context::getShowBehaviorModels() const
{
  if (showBehaviorModels_.getValue() < 0) return false;
  else  return showBehaviorModels_.getValue()!= 0;
}

/// Display flags: Visual Models
bool Context::getShowVisualModels() const
{
  if (showVisualModels_.getValue() < 0) return true;
  else return showVisualModels_.getValue()!= 0;
}

/// Display flags: Mappings
bool Context::getShowMappings() const
{
  if (showMappings_.getValue() < 0) return false;
  else return showMappings_.getValue()!= 0;
}

/// Display flags: Mechanical Mappings
bool Context::getShowMechanicalMappings() const
{
  if (showMechanicalMappings_.getValue() < 0) return false;
  else return showMechanicalMappings_.getValue()!= 0;
}

/// Display flags: ForceFields
bool Context::getShowForceFields() const
{
  if (showForceFields_.getValue() < 0) return false;
  else return showForceFields_.getValue()!= 0;
}

/// Display flags: InteractionForceFields
bool Context::getShowInteractionForceFields() const
{
  if (showInteractionForceFields_.getValue() < 0) return false;
  else return showInteractionForceFields_.getValue()!= 0;
}

/// Display flags: WireFrame
bool Context::getShowWireFrame() const
{
  if (showWireFrame_.getValue() < 0) return false;
  else return showWireFrame_.getValue()!= 0;
}

/// Display flags: Normal
bool Context::getShowNormals() const
{
  if (showNormals_.getValue() < 0) return false;
  else return showNormals_.getValue()!= 0;
}


// Multiresolution

int Context::getCurrentLevel() const
{
	return currentLevel_.getValue();
}
int Context::getCoarsestLevel() const
{
	return coarsestLevel_.getValue();
}
int Context::getFinestLevel() const
{
	return finestLevel_.getValue();
}


//===============================================================================

/// Simulation timestep
void Context::setDt(double val)
{
    dt_.setValue(val);
}

/// Simulation time
void Context::setTime(double val)
{
    time_.setValue(val);
}

/// Gravity vector
// void Context::setGravity(const Vec3& g)
// {
// 	gravity_ = g;
// }

/// Gravity vector
void Context::setGravityInWorld(const Vec3& g)
{
    worldGravity_ .setValue(g);
}

/// Animation flag
void Context::setAnimate(bool val)
{
    animate_.setValue(val);
}

/// MultiThreading activated
void Context::setMultiThreadSimulation(bool val)
{
    multiThreadSimulation_.setValue(val);
}

/// Display flags: Collision Models
void Context::setShowCollisionModels(bool val)
{
    showCollisionModels_.setValue(val);
}

/// Display flags: Bounding Collision Models
void Context::setShowBoundingCollisionModels(bool val)
{
    showBoundingCollisionModels_.setValue(val);
}

/// Display flags: Behavior Models
void Context::setShowBehaviorModels(bool val)
{
    showBehaviorModels_.setValue(val);
}

/// Display flags: Visual Models
void Context::setShowVisualModels(bool val)
{
    showVisualModels_.setValue(val);
}

/// Display flags: Mappings
void Context::setShowMappings(bool val)
{
    showMappings_.setValue(val);
}

/// Display flags: Mechanical Mappings
void Context::setShowMechanicalMappings(bool val)
{
    showMechanicalMappings_.setValue(val);
}

/// Display flags: ForceFields
void Context::setShowForceFields(bool val)
{
    showForceFields_.setValue(val);
}

/// Display flags: InteractionForceFields
void Context::setShowInteractionForceFields(bool val)
{
    showInteractionForceFields_.setValue(val);
}

/// Display flags: WireFrame
void Context::setShowWireFrame(bool val)
{
    showWireFrame_.setValue(val);
}

/// Display flags: Normals
void Context::setShowNormals(bool val)
{
    showNormals_.setValue(val);
}


// Multiresolution

bool Context::setCurrentLevel(int l)
{
	if( l > coarsestLevel_.getValue() )
	{
		currentLevel_.setValue(coarsestLevel_.getValue());
		return false;
	}
	else if( l < 0 /*finestLevel_.getValue()*/ )
	{
// 		currentLevel_.setValue(finestLevel_.getValue());
		currentLevel_.setValue( 0 );
		return false;
	}
    currentLevel_.setValue(l);
	if( l == coarsestLevel_.getValue() ) return false;
    return true;
}
void Context::setCoarsestLevel(int l)
{
	coarsestLevel_.setValue( l );
}
void Context::setFinestLevel(int l)
{
	finestLevel_.setValue( l );
}

//======================


void Context::copyContext(const Context& c)
{
    // BUGFIX 12/01/06 (Jeremie A.): Can't use operator= on the class as it will copy other data in the BaseContext class (such as name)...
    // *this = c;

    copySimulationContext(c);
    copyVisualContext(c);
}


void Context::copySimulationContext(const Context& c)
{
  worldGravity_.setValue(c.worldGravity_.getValue());  ///< Gravity IN THE WORLD COORDINATE SYSTEM.
  dt_.setValue(c.dt_.getValue());
  time_.setValue(c.time_.getValue());
  animate_.setValue(c.animate_.getValue());
  multiThreadSimulation_.setValue(c.multiThreadSimulation_.getValue());

  localFrame_ = c.localFrame_;
  spatialVelocityInWorld_ = c.spatialVelocityInWorld_;
  velocityBasedLinearAccelerationInWorld_ = c.velocityBasedLinearAccelerationInWorld_;

	// for multiresolution
// 	finestLevel_ = c.finestLevel_;
// 	coarsestLevel_ = c.coarsestLevel_;
// 	currentLevel_ = c.currentLevel_;

}

void Context::copyVisualContext(const Context& c)
{
  showCollisionModels_.setValue(c.showCollisionModels_.getValue());
  showBoundingCollisionModels_.setValue(c.showBoundingCollisionModels_.getValue());
  showBehaviorModels_.setValue(c.showBehaviorModels_.getValue());
  showVisualModels_.setValue(c.showVisualModels_.getValue());
  showMappings_.setValue(c.showMappings_.getValue());
  showMechanicalMappings_.setValue(c.showMechanicalMappings_.getValue());
  showForceFields_.setValue(c.showForceFields_.getValue());
  showInteractionForceFields_.setValue(c.showInteractionForceFields_.getValue());
  showWireFrame_.setValue(c.showWireFrame_.getValue());
  showNormals_.setValue(c.showNormals_.getValue());
}


void Context::fusionVisualContext(const Context& c)
{
  showCollisionModels_.setValue(showCollisionModels_.getValue() || c.showCollisionModels_.getValue());
  showBoundingCollisionModels_.setValue(showBoundingCollisionModels_.getValue() || c.showBoundingCollisionModels_.getValue());
  showBehaviorModels_.setValue(showBehaviorModels_.getValue() || c.showBehaviorModels_.getValue());
  showVisualModels_.setValue(showVisualModels_.getValue() || c.showVisualModels_.getValue());
  showMappings_.setValue(showMappings_.getValue() || c.showMappings_.getValue());
  showMechanicalMappings_.setValue(showMechanicalMappings_.getValue() || c.showMechanicalMappings_.getValue());
  showForceFields_.setValue(showForceFields_.getValue() || c.showForceFields_.getValue());
  showInteractionForceFields_.setValue(showInteractionForceFields_.getValue() || c.showInteractionForceFields_.getValue());
  showWireFrame_.setValue(showWireFrame_.getValue() || c.showWireFrame_.getValue());
  showNormals_.setValue(showNormals_.getValue() || c.showNormals_.getValue());
}




} // namespace objectmodel

} // namespace core

} // namespace sofa