AnimatLab  2
Test
RbMaterialType.cpp
1 // RbMaterialType.cpp: implementation of the RbMaterialType class.
2 //
4 
5 #include "StdAfx.h"
6 #include "RbMovableItem.h"
7 #include "RbBody.h"
8 #include "RbJoint.h"
9 #include "RbRigidBody.h"
10 #include "RbSimulator.h"
11 
12 namespace RoboticsAnimatSim
13 {
14  namespace Environment
15  {
16 
18 // Construction/Destruction
20 
21 RbMaterialType::RbMaterialType()
22 {
25  m_fltRestitution = 0;
26 }
27 
28 RbMaterialType::~RbMaterialType()
29 {
30 }
31 
41 
51 {
52  Std_IsAboveMin((float) 0, fltVal, true, "FrictionLinearPrimary", true);
53 
55  SetMaterialProperties();
56 }
57 
67 
68 
78 {
79  Std_IsAboveMin((float) 0, fltVal, true, "FrictionAngularPrimary", true);
80 
82  SetMaterialProperties();
83 }
84 
101 {
102  return m_fltFrictionAngularPrimary*6.3;
103 }
104 
122 
139 void RbMaterialType::Restitution(float fltVal)
140 {
141  Std_InValidRange((float) 0, (float) 1, fltVal, true, "Restitution");
142  m_fltRestitution = fltVal;
143  SetMaterialProperties();
144 }
145 
154 {
157  m_fltRestitution = 0;
158 }
159 
160 bool RbMaterialType::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
161 {
162  std::string strType = Std_CheckString(strDataType);
163 
164  if(MaterialType::SetData(strType, strValue, false))
165  return true;
166 
167  if(strType == "FRICTIONLINEARPRIMARY")
168  {
169  FrictionLinearPrimary((float) atof(strValue.c_str()));
170  return true;
171  }
172 
173  if(strType == "FRICTIONANGULARPRIMARY")
174  {
175  FrictionAngularPrimary((float) atof(strValue.c_str()));
176  return true;
177  }
178 
179  if(strType == "RESTITUTION")
180  {
181  Restitution((float) atof(strValue.c_str()));
182  return true;
183  }
184 
185  //If it was not one of those above then we have a problem.
186  if(bThrowError)
187  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
188 
189  return false;
190 }
191 
192 void RbMaterialType::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
193 {
194  MaterialType::QueryProperties(aryProperties);
195 
196  aryProperties.Add(new TypeProperty("FrictionLinearPrimary", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
197  aryProperties.Add(new TypeProperty("FrictionAngularPrimary", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
198  aryProperties.Add(new TypeProperty("Restitution", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
199 }
200 
201 void RbMaterialType::Load(CStdXml &oXml)
202 {
203  MaterialType::Load(oXml);
204 
205  oXml.IntoElem(); //Into MaterialType Element
206 
207  FrictionLinearPrimary(oXml.GetChildFloat("FrictionLinearPrimary", m_fltFrictionLinearPrimary));
208  FrictionAngularPrimary(oXml.GetChildFloat("FrictionAngularPrimary", m_fltFrictionAngularPrimary));
209  Restitution(oXml.GetChildFloat("Restitution", m_fltRestitution));
210 
211  oXml.OutOfElem(); //OutOf MaterialType Element
212 
213 }
214 
225 {
226  if(lpBody)
227  {
228  //If we do not already have this body in the list then add it.
229  if(!FindBodyByID(lpBody->ID(), false))
230  m_aryBodies.Add(lpBody->ID(), lpBody);
231  }
232 }
233 
243 {
244  if(lpBody)
245  {
246  if(FindBodyByID(lpBody->ID(), false))
247  m_aryBodies.Remove(lpBody->ID());
248  }
249 }
250 
251 RigidBody *RbMaterialType::FindBodyByID(std::string strID, bool bThrowError)
252 {
253  RigidBody *lpFind = NULL;
254  CStdMap<std::string, RigidBody *>::iterator oPos;
255  oPos = m_aryBodies.find(Std_CheckString(strID));
256 
257  if(oPos != m_aryBodies.end())
258  lpFind = oPos->second;
259  else if(bThrowError)
260  THROW_PARAM_ERROR(Al_Err_lIDNotFound, Al_Err_strIDNotFound, "ID", strID);
261 
262  return lpFind;
263 }
264 
265 int RbMaterialType::GetMaterialID(std::string strName)
266 {
267  return -1;
268 }
269 
271 {
272 }
273 
275 {
276  //MaterialType::Initialize();
277 
278  // RegisterMaterialType();
279  //SetMaterialProperties();
280 }
281 
282 void RbMaterialType::SetMaterialProperties()
283 {
284  //CStdMap<std::string, RigidBody *>::iterator oPos;
285  //RigidBody *lpBody = NULL;
286  //RbRigidBody *lpRbBody = NULL;
287  //for(oPos=m_aryBodies.begin(); oPos!=m_aryBodies.end(); ++oPos)
288  //{
289  // lpBody = oPos->second;
290  // lpRbBody = dynamic_cast<RbRigidBody *>(lpBody);
291 
292  // if(lpRbBody)
293  // lpRbBody->MaterialTypeModified();
294  //}
295 }
296 
297  } // Visualization
298 } //RoboticsAnimatSim
virtual void Initialize()
Initializes this object.
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 FrictionAngularPrimary()
Gets the angular primary friction coefficient.
virtual float FrictionLinearPrimary()
Gets the primary friction coefficient.
virtual float Restitution()
Gets the restitution for collisions between RigidBodies with these two materials. ...
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
bool Std_InValidRange(int iMinVal, int iMaxVal, int iVal, bool bThrowError, std::string strParamName)
Tests whether a number is within a valid range.
virtual void RemoveRigidBodyAssociation(RigidBody *lpBody)
Removes the rigid body association described by lpBody from this material.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
float m_fltFrictionLinearPrimary
The primary linear coefficient of friction parameter.
float m_fltRestitution
The restitution of the collision between those two materials.
CStdMap< std::string, RigidBody * > m_aryBodies
All the rigid bodies associated with this material type.
virtual float FrictionAngularPrimaryConverted()
Gets the angular primary friction coefficient converted to match vortex values.
virtual void CreateDefaultUnits()
This takes the default values defined in the constructor and scales them according to the distance an...
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.
float m_fltFrictionAngularPrimary
The angular primary coefficient of friction parameter.(this simulates rolling resistance) ...
virtual void RegisterMaterialType()
Registers the material types within the physics engine.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.