AnimatLab  2
Test
BlMaterialType.cpp
1 // BlMaterialType.cpp: implementation of the BlMaterialType class.
2 //
4 
5 #include "StdAfx.h"
6 #include "BlJoint.h"
7 #include "BlRigidBody.h"
8 #include "BlSimulator.h"
9 
10 namespace BulletAnimatSim
11 {
12  namespace Environment
13  {
14 
16 // Construction/Destruction
18 
19 BlMaterialType::BlMaterialType()
20 {
23  m_fltRestitution = 0;
24 }
25 
26 BlMaterialType::~BlMaterialType()
27 {
28 }
29 
39 
49 {
50  Std_IsAboveMin((float) 0, fltVal, true, "FrictionLinearPrimary", true);
51 
53  SetMaterialProperties();
54 }
55 
65 
66 
76 {
77  Std_IsAboveMin((float) 0, fltVal, true, "FrictionAngularPrimary", true);
78 
80  SetMaterialProperties();
81 }
82 
99 {
100  return m_fltFrictionAngularPrimary*6.3;
101 }
102 
120 
137 void BlMaterialType::Restitution(float fltVal)
138 {
139  Std_InValidRange((float) 0, (float) 1, fltVal, true, "Restitution");
140  m_fltRestitution = fltVal;
141  SetMaterialProperties();
142 }
143 
152 {
155  m_fltRestitution = 0;
156 }
157 
158 bool BlMaterialType::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
159 {
160  std::string strType = Std_CheckString(strDataType);
161 
162  if(MaterialType::SetData(strType, strValue, false))
163  return true;
164 
165  if(strType == "FRICTIONLINEARPRIMARY")
166  {
167  FrictionLinearPrimary((float) atof(strValue.c_str()));
168  return true;
169  }
170 
171  if(strType == "FRICTIONANGULARPRIMARY")
172  {
173  FrictionAngularPrimary((float) atof(strValue.c_str()));
174  return true;
175  }
176 
177  if(strType == "RESTITUTION")
178  {
179  Restitution((float) atof(strValue.c_str()));
180  return true;
181  }
182 
183  //If it was not one of those above then we have a problem.
184  if(bThrowError)
185  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
186 
187  return false;
188 }
189 
190 void BlMaterialType::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
191 {
192  MaterialType::QueryProperties(aryProperties);
193 
194  aryProperties.Add(new TypeProperty("FrictionLinearPrimary", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
195  aryProperties.Add(new TypeProperty("FrictionAngularPrimary", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
196  aryProperties.Add(new TypeProperty("Restitution", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
197 }
198 
199 void BlMaterialType::Load(CStdXml &oXml)
200 {
201  MaterialType::Load(oXml);
202 
203  oXml.IntoElem(); //Into MaterialType Element
204 
205  FrictionLinearPrimary(oXml.GetChildFloat("FrictionLinearPrimary", m_fltFrictionLinearPrimary));
206  FrictionAngularPrimary(oXml.GetChildFloat("FrictionAngularPrimary", m_fltFrictionAngularPrimary));
207  Restitution(oXml.GetChildFloat("Restitution", m_fltRestitution));
208 
209  oXml.OutOfElem(); //OutOf MaterialType Element
210 
211 }
212 
223 {
224  if(lpBody)
225  {
226  //If we do not already have this body in the list then add it.
227  if(!FindBodyByID(lpBody->ID(), false))
228  m_aryBodies.Add(lpBody->ID(), lpBody);
229  }
230 }
231 
241 {
242  if(lpBody)
243  {
244  if(FindBodyByID(lpBody->ID(), false))
245  m_aryBodies.Remove(lpBody->ID());
246  }
247 }
248 
249 RigidBody *BlMaterialType::FindBodyByID(std::string strID, bool bThrowError)
250 {
251  RigidBody *lpFind = NULL;
252  CStdMap<std::string, RigidBody *>::iterator oPos;
253  oPos = m_aryBodies.find(Std_CheckString(strID));
254 
255  if(oPos != m_aryBodies.end())
256  lpFind = oPos->second;
257  else if(bThrowError)
258  THROW_PARAM_ERROR(Al_Err_lIDNotFound, Al_Err_strIDNotFound, "ID", strID);
259 
260  return lpFind;
261 }
262 
263 int BlMaterialType::GetMaterialID(std::string strName)
264 {
265  return -1;
266 }
267 
269 {
270 }
271 
273 {
274  MaterialType::Initialize();
275 
277  SetMaterialProperties();
278 }
279 
280 void BlMaterialType::SetMaterialProperties()
281 {
282  CStdMap<std::string, RigidBody *>::iterator oPos;
283  RigidBody *lpBody = NULL;
284  BlRigidBody *lpBlBody = NULL;
285  for(oPos=m_aryBodies.begin(); oPos!=m_aryBodies.end(); ++oPos)
286  {
287  lpBody = oPos->second;
288  lpBlBody = dynamic_cast<BlRigidBody *>(lpBody);
289 
290  if(lpBlBody)
291  lpBlBody->MaterialTypeModified();
292  }
293 }
294 
295  } // Visualization
296 } //BulletAnimatSim
virtual void AddRigidBodyAssociation(RigidBody *lpBody)
Associates a rigid body with this material type. This is used when you change something about this ma...
virtual float FrictionAngularPrimaryConverted()
Gets the angular primary friction coefficient converted to match vortex values.
virtual void Initialize()
Initializes this object.
A common class for all rigid body data specific to vortex.
Definition: BlRigidBody.h:87
bool Std_InValidRange(int iMinVal, int iMaxVal, int iVal, bool bThrowError, std::string strParamName)
Tests whether a number is within a valid range.
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.
CStdMap< std::string, RigidBody * > m_aryBodies
All the rigid bodies associated with this material type.
float m_fltFrictionAngularPrimary
The angular primary coefficient of friction parameter.(this simulates rolling resistance) ...
virtual void CreateDefaultUnits()
This takes the default values defined in the constructor and scales them according to the distance an...
virtual float Restitution()
Gets the restitution for collisions between RigidBodies with these two materials. ...
virtual float FrictionLinearPrimary()
Gets the primary friction coefficient.
float m_fltRestitution
The restitution of the collision between those two materials.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual int GetMaterialID(std::string strName)
Gets a material identifier used by the physics engine.
virtual void RegisterMaterialType()
Registers the material types within the physics engine.
virtual void RemoveRigidBodyAssociation(RigidBody *lpBody)
Removes the rigid body association described by lpBody from this material.
float m_fltFrictionLinearPrimary
The primary linear coefficient of friction parameter.
virtual float FrictionAngularPrimary()
Gets the angular primary friction coefficient.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.