AnimatLab  2
Test
BlConstraintRelaxation.cpp
1 // BlConstraintRelaxation.cpp: implementation of the BlConstraintRelaxation class.
2 //
4 
5 #include "StdAfx.h"
6 #include "BlConstraintRelaxation.h"
7 #include "BlJoint.h"
8 #include "BlMotorizedJoint.h"
9 #include "BlRigidBody.h"
10 #include "BlSimulator.h"
11 
12 namespace BulletAnimatSim
13 {
14  namespace Environment
15  {
16 
18 // Construction/Destruction
20 
21 BlConstraintRelaxation::BlConstraintRelaxation()
22 {
23  m_lpBlJoint = NULL;
24  m_lpConstraint = NULL;
25  m_fltMaxLimit = 0;
26  m_fltMinLimit = 0;
27  m_fltEqPos = 0;
28  m_fltInvDamping = 1/m_fltDamping;
29  m_bDisallowSpringEnable = false;
30 }
31 
32 BlConstraintRelaxation::~BlConstraintRelaxation()
33 {
34 }
35 
45 
55 void BlConstraintRelaxation::MinLimit(float fltVal, bool bUseScaling)
56 {
57  if(bUseScaling)
58  fltVal = fltVal * m_lpSim->InverseDistanceUnits();
59  else
60  fltVal = fltVal;
61 
62  m_fltMinLimit = fltVal;
63  SetRelaxationProperties();
64 }
65 
75 
85 void BlConstraintRelaxation::MaxLimit(float fltVal, bool bUseScaling)
86 {
87  if(bUseScaling)
88  fltVal = fltVal * m_lpSim->InverseDistanceUnits();
89  else
90  fltVal = fltVal;
91 
92  m_fltMaxLimit = fltVal;
93  SetRelaxationProperties();
94 }
95 
105 
115 void BlConstraintRelaxation::EqPos(float fltVal, bool bUseScaling)
116 {
117  Std_IsAboveMin((float) 0, fltVal, true, "EqPos", true);
118 
119  if(bUseScaling)
120  fltVal = fltVal * m_lpSim->InverseDistanceUnits();
121  else
122  fltVal = fltVal;
123 
124  m_fltEqPos = fltVal;
125  SetRelaxationProperties();
126 }
136 void BlConstraintRelaxation::Damping(float fltVal, bool bUseScaling)
137 {
138  Std_IsAboveMin((float) 0, fltVal, true, "Damping", true);
139 
140  if(bUseScaling)
141  fltVal = fltVal/m_lpSim->DisplayMassUnits();
142 
143  m_fltDamping = fltVal;
144 
145  if(m_fltDamping <= 0)
146  m_fltInvDamping = 1;
147  else
148  m_fltInvDamping = 1/m_fltDamping;
149 
150  SetRelaxationProperties();
151 }
152 
166 {
167  return m_bDisallowSpringEnable;
168 }
169 
183 {
184  m_bDisallowSpringEnable = bVal;
185 }
186 
188 {
189  ConstraintRelaxation::Initialize();
190 
191  if(m_lpSim && m_lpNode)
192  {
193  m_lpBlJoint = dynamic_cast<BlJoint *>(m_lpNode);
194  if(m_lpBlJoint)
195  m_lpConstraint = dynamic_cast<btAnimatGeneric6DofConstraint *>(m_lpBlJoint->Constraint());
196  }
197 
198  SetRelaxationProperties();
199 }
200 
209 {
210  ConstraintRelaxation::CreateDefaultUnits();
211 
212  if(m_fltDamping <= 0)
213  m_fltInvDamping = 1;
214  else
215  m_fltInvDamping = 1/m_fltDamping;
216 
220 }
221 
222 bool BlConstraintRelaxation::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
223 {
224  std::string strType = Std_CheckString(strDataType);
225 
226  if(ConstraintRelaxation::SetData(strType, strValue, false))
227  return true;
228 
229  if(strType == "MINLIMIT")
230  {
231  MinLimit((float) atof(strValue.c_str()));
232  return true;
233  }
234 
235  if(strType == "MAXLIMIT")
236  {
237  MaxLimit((float) atof(strValue.c_str()));
238  return true;
239  }
240 
241  if(strType == "EQPOS")
242  {
243  EqPos((float) atof(strValue.c_str()));
244  return true;
245  }
246 
247  //If it was not one of those above then we have a problem.
248  if(bThrowError)
249  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
250 
251  return false;
252 }
253 
254 void BlConstraintRelaxation::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
255 {
256  ConstraintRelaxation::QueryProperties(aryProperties);
257 
258  aryProperties.Add(new TypeProperty("MinLimit", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
259  aryProperties.Add(new TypeProperty("MaxLimit", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
260  aryProperties.Add(new TypeProperty("EqPos", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
261 }
262 
263 void BlConstraintRelaxation::Load(CStdXml &oXml)
264 {
265  ConstraintRelaxation::Load(oXml);
266 
267  oXml.IntoElem(); //Into ConstraintRelaxation Element
268 
269  MinLimit(oXml.GetChildFloat("MinLimit", m_fltMinLimit));
270  MaxLimit(oXml.GetChildFloat("MaxLimit", m_fltMaxLimit));
271  EqPos(oXml.GetChildFloat("EqPos", m_fltEqPos));
272 
273  oXml.OutOfElem(); //OutOf ConstraintRelaxation Element
274 
275 }
276 
277 void BlConstraintRelaxation::SetRelaxationProperties()
278 {
279  if(m_lpSim && m_lpNode && m_lpConstraint && m_lpBlJoint)
280  {
281  if(m_bDisallowSpringEnable)
282  m_lpBlJoint->AxisConstraintSpringEnableChanged(m_bEnabled);
283  else
284  m_lpConstraint->enableSpring(m_iCoordinateID, m_bEnabled);
285 
286  m_lpConstraint->setEquilibriumPoint(m_iCoordinateID, m_fltEqPos);
287  m_lpConstraint->setStiffness(m_iCoordinateID, m_fltStiffness);
288  m_lpConstraint->setDamping(m_iCoordinateID, m_fltInvDamping);
289 
290  m_lpBlJoint->SetLimitValues();
291  }
292 }
293 
294  } // Visualization
295 } //BulletAnimatSim
A common class for all joint data specific to vortex.
Definition: BlJoint.h:29
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
int m_iCoordinateID
The constraint coordinate ID.
virtual float EqPos()
Gets the equilibrium position for the spring that controls this relaxation.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
virtual float MinLimit()
Gets the minimum value that this relaxation can move.
float m_fltDamping
The damping of the collision between those two materials.
float m_fltStiffness
The compliance of the collision between those two materials.
virtual float DisplayMassUnits()
Gets the density mass units.
Definition: Simulator.cpp:1810
Node * m_lpNode
The pointer to this items parent Node. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:52
virtual float Damping()
Gets the damping for collisions between RigidBodies with these two materials.
virtual void CreateDefaultUnits()
This takes the default values defined in the constructor and scales them according to the distance an...
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
float m_fltMinLimit
The minimum amount that this relaxation is allowed to move.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual bool DisallowSpringEnable()
Gets the disallow spring enable.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
float m_fltMaxLimit
The maximum amount that this relaxation is allowed to move.
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
virtual float MaxLimit()
Gets the maximum value that this relaxation can move.
float m_fltEqPos
The equilibrium position of the spring that controls this relaxation.