AnimatLab  2
Test
Mesh.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include "Node.h"
13 #include "IPhysicsMovableItem.h"
14 #include "IPhysicsBody.h"
15 #include "BoundingBox.h"
16 #include "MovableItem.h"
17 #include "BodyPart.h"
18 #include "Joint.h"
19 #include "ReceptiveField.h"
20 #include "ContactSensor.h"
21 #include "RigidBody.h"
22 #include "Mesh.h"
23 #include "Structure.h"
24 #include "Organism.h"
25 #include "ActivatedItem.h"
26 #include "ActivatedItemMgr.h"
27 #include "DataChartMgr.h"
28 #include "ExternalStimuliMgr.h"
29 #include "KeyFrame.h"
30 #include "SimulationRecorder.h"
31 #include "OdorType.h"
32 #include "Odor.h"
33 #include "Light.h"
34 #include "LightManager.h"
35 #include "Simulator.h"
36 
37 namespace AnimatSim
38 {
39  namespace Environment
40  {
41  namespace Bodies
42  {
43 
51 {
52  m_vScale.Set(1, 1, 1);
53 }
54 
62 {
63 }
64 
75 std::string Mesh::MeshFile() {return m_strMeshFile;}
76 
86 void Mesh::MeshFile(std::string strFile)
87 {
88  m_strMeshFile = strFile;
89  Resize();
90 }
91 
103 
114 void Mesh::CollisionMeshType(std::string strType)
115 {
116  std::string strUpType = Std_CheckString(strType);
117  if(strUpType != "TRIANGULAR" && strUpType != "CONVEX" && strUpType != "TERRAIN")
118  THROW_TEXT_ERROR(Al_Err_lInvalidCollisionMeshType, Al_Err_strInvalidCollisionMeshType, "Body: " + m_strName + " MeshType: " + m_strCollisionMeshType);
119 
120  m_strCollisionMeshType = strUpType;
121  Resize();
122 }
123 
124 
136 
146 void Mesh::ConvexMeshFile(std::string strFile)
147 {
148  m_strConvexMeshFile = strFile;
149  Resize();
150 }
151 
152 
161 CStdFPoint Mesh::Scale() {return m_vScale;}
162 
174 void Mesh::Scale(CStdFPoint &oPoint, bool bUpdateMatrix)
175 {
176  m_vScale = oPoint;
178 
179  if(m_lpPhysicsMovableItem && bUpdateMatrix)
180  m_lpPhysicsMovableItem->Physics_Resize();
181 }
182 
198 void Mesh::Scale(float fltX, float fltY, float fltZ, bool bUpdateMatrix)
199 {
200  CStdFPoint vPos(fltX, fltY, fltZ);
201  Scale(vPos);
202 }
203 
219 void Mesh::Scale(std::string strXml, bool bUpdateMatrix)
220 {
221  CStdXml oXml;
222  oXml.Deserialize(strXml);
223  oXml.FindElement("Root");
224  oXml.FindChildElement("Scale");
225 
226  CStdFPoint vPos;
227  Std_LoadPoint(oXml, "Scale", vPos);
228  Scale(vPos);
229 }
230 
231 void Mesh::SetBoundingBox(int iIdx, float fltVal)
232 {
233  if(iIdx >= 0 && iIdx <= 2 && fltVal > 0)
234  {
236 
237  //Calculate the scale change required.
238  float fltOldDim = bb.GetDimensionSize(iIdx);
239 
240  if(fltOldDim > 0)
241  {
242  float fltNewDim = fltVal*m_lpSim->InverseDistanceUnits();
243  float fltConvert = fltNewDim / fltOldDim;
244 
245  float fltNewX = m_vScale.x*fltConvert;
246  float fltNewY = m_vScale.y*fltConvert;
247  float fltNewZ = m_vScale.z*fltConvert;
248 
249  Scale(fltNewX, fltNewY, fltNewZ);
250  }
251  }
252  else
253  THROW_PARAM_ERROR(Al_Err_lInvalidMeshScaleParam, Al_Err_strInvalidMeshScaleParam, "Data", fltVal);
254 
255 }
256 
257 void Mesh::SetMeshFile(std::string strXml)
258 {
259  CStdXml oXml;
260  oXml.Deserialize(strXml);
261  oXml.FindElement("Root");
262  oXml.FindChildElement("MeshFile");
263 
264  m_strMeshFile = oXml.GetChildString("MeshFile");
265  m_strConvexMeshFile = oXml.GetChildString("ConvexMeshFile", "");
266  CollisionMeshType(oXml.GetChildString("MeshType"));
267 }
268 
269 float *Mesh::GetDataPointer(const std::string &strDataType)
270 {
271  float *lpData=NULL;
272  std::string strType = Std_CheckString(strDataType);
273 
274  float *lpVal = NULL;
275 
276  if(strType == "SCALE.X")
277  lpData = &m_vReportScale.x;
278  else if(strType == "SCALE.Y")
279  lpData = &m_vReportScale.y;
280  else if(strType == "SCALE.Z")
281  lpData = &m_vReportScale.z;
282  else
283  lpData = RigidBody::GetDataPointer(strDataType);
284 
285  return lpData;
286 }
287 
288 bool Mesh::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
289 {
290  std::string strType = Std_CheckString(strDataType);
291 
292  if(RigidBody::SetData(strType, strValue, false))
293  return true;
294 
295  if(strType == "MESHFILE")
296  {
297  MeshFile(strValue);
298  return true;
299  }
300 
301  if(strType == "MESHTYPE")
302  {
303  CollisionMeshType(strValue);
304  return true;
305  }
306 
307  if(strType == "CONVEXMESHFILE")
308  {
309  ConvexMeshFile(strValue);
310  return true;
311  }
312 
313  if(strType == "SETMESHFILE")
314  {
315  SetMeshFile(strValue);
316  return true;
317  }
318 
319  if(strType == "SCALE")
320  {
321  Scale(strValue);
322  return true;
323  }
324 
325  if(strType == "SCALE.X")
326  {
327  Scale(atof(strValue.c_str()), m_vScale.y, m_vScale.z);
328  return true;
329  }
330 
331  if(strType == "SCALE.Y")
332  {
333  Scale(m_vScale.x, atof(strValue.c_str()), m_vScale.z);
334  return true;
335  }
336 
337  if(strType == "SCALE.Z")
338  {
339  Scale(m_vScale.x, m_vScale.y, atof(strValue.c_str()));
340  return true;
341  }
342 
343  //If it was not one of those above then we have a problem.
344  if(bThrowError)
345  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
346 
347  return false;
348 }
349 
350 void Mesh::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
351 {
352  RigidBody::QueryProperties(aryProperties);
353 
354  aryProperties.Add(new TypeProperty("MeshFile", AnimatPropertyType::String, AnimatPropertyDirection::Set));
355  aryProperties.Add(new TypeProperty("MeshType", AnimatPropertyType::String, AnimatPropertyDirection::Set));
356  aryProperties.Add(new TypeProperty("ConvexMeshFile", AnimatPropertyType::String, AnimatPropertyDirection::Set));
357  aryProperties.Add(new TypeProperty("SetMeshFile", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
358  aryProperties.Add(new TypeProperty("Scale", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
359  aryProperties.Add(new TypeProperty("Scale.X", AnimatPropertyType::Float, AnimatPropertyDirection::Both));
360  aryProperties.Add(new TypeProperty("Scale.Y", AnimatPropertyType::Float, AnimatPropertyDirection::Both));
361  aryProperties.Add(new TypeProperty("Scale.Z", AnimatPropertyType::Float, AnimatPropertyDirection::Both));
362 }
363 
364 void Mesh::Load(CStdXml &oXml)
365 {
366  RigidBody::Load(oXml);
367 
368  oXml.IntoElem(); //Into RigidBody Element
369 
370  MeshFile(oXml.GetChildString("MeshFile"));
371  CollisionMeshType(oXml.GetChildString("MeshType"));
372  ConvexMeshFile(oXml.GetChildString("ConvexMeshFile", ""));
373 
374  Std_LoadPoint(oXml, "Scale", m_vScale, false);
376 
377  oXml.OutOfElem(); //OutOf RigidBody Element
378 
379 }
380 
381  } //Bodies
382  } //Environment
383 } //AnimatSim
virtual void Deserialize(std::string &strXml)
Deserializes a string into an xml document.
Definition: StdXml.cpp:162
Base class file for all Animat simulation objects.
Declares the simulation recorder class.
virtual ~Mesh()
Destructor.
Definition: Mesh.cpp:61
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
Root namespace for the base simulation library for AnimatLab.
virtual CStdFPoint Scale()
Gets the local position. (m_oPosition)
Definition: Mesh.cpp:161
std::string m_strConvexMeshFile
The collsion mesh file. If the type is convex then we load in this file instead of the graphical one...
Definition: Mesh.h:21
Declares the body part class.
std::string m_strCollisionMeshType
Type of the collision mesh. Can be Convex or Triangular.
Definition: Mesh.h:18
virtual void SetBoundingBox(int iIdx, float fltVal)
Sets one dimension of the bounding box. This does nothing for all parts except a mesh.
Definition: Mesh.cpp:231
virtual bool FindElement(std::string strElementName, bool fThrowError=true)
Finds an element with the specified name.
Definition: StdXml.cpp:179
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual std::string ConvexMeshFile()
Gets the convex mesh filename.
Definition: Mesh.cpp:135
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
CStdFPoint m_vReportScale
The scale factor that is used for reporting to the GUI.
Definition: Mesh.h:27
Declares the key frame class.
Declares the joint class.
Declares the organism class.
Declares a light object.
virtual void Resize()
Called when this object has been resized.
Definition: BodyPart.cpp:141
Declares the activated item class.
Declares a light manager object.
IPhysicsMovableItem * m_lpPhysicsMovableItem
Definition: MovableItem.h:103
virtual BoundingBox GetBoundingBox()
Gets the bounding box for this part.
Declares the bounding box class.
Mesh()
Default constructor.
Definition: Mesh.cpp:50
A standard xml manipulation class.
Definition: StdXml.h:19
Declares the node class.
Bounding box class for geometric objects.
Definition: BoundingBox.h:17
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
CStdFPoint m_vScale
The scale factor to use for this mesh.
Definition: Mesh.h:24
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
Declares the data chart manager class.
Declares the rigid body class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
bool Std_LoadPoint(CStdXml &oXml, std::string strName, CStdIPoint &oPoint, bool bThrowError)
Standard load point.
Declares the structure class.
Declares the odor type class.
float GetDimensionSize(int iAxis)
Gets the size along the supplied dimension.
Definition: BoundingBox.cpp:61
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
Declares the odor class.
Declares the simulator class.
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
virtual std::string CollisionMeshType()
Gets the collision mesh type.
Definition: Mesh.cpp:102
virtual std::string MeshFile()
Gets the mesh filename.
Definition: Mesh.cpp:75
std::string m_strMeshFile
The graphical mesh file to load.
Definition: Mesh.h:15
Declares the receptive field class.
std::string m_strName
The name for this object.
Definition: AnimatBase.h:61