AnimatLab  2
Test
Mouth.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 "Sensor.h"
23 #include "Stomach.h"
24 #include "Mouth.h"
25 #include "Structure.h"
26 #include "Organism.h"
27 #include "ActivatedItem.h"
28 #include "ActivatedItemMgr.h"
29 #include "DataChartMgr.h"
30 #include "ExternalStimuliMgr.h"
31 #include "KeyFrame.h"
32 #include "SimulationRecorder.h"
33 #include "OdorType.h"
34 #include "Odor.h"
35 #include "Light.h"
36 #include "LightManager.h"
37 #include "Simulator.h"
38 
39 namespace AnimatSim
40 {
41  namespace Environment
42  {
43  namespace Bodies
44  {
52 {
53  m_lpStomach = NULL;
54  m_fltEatingRate = 0;
55  m_fltMinFoodRadius = 10;
57 }
58 
66 {
67  try
68  {
69  m_lpStomach = NULL;
70  }
71  catch(...)
72  {Std_TraceMsg(0, "Caught Error in desctructor of Mouth\r\n", "", -1, false, true);}
73 }
74 
84 
86 
87 void Mouth::MinFoodRadius(float fltVal, bool bUseScaling)
88 {
89  Std_IsAboveMin((float) 0, fltVal, true, "Mouth.MinFoodRadius", true);
90  if(bUseScaling)
92  else
93  m_fltMinFoodRadius = fltVal;
94 }
95 
104 void Mouth::StomachID(std::string strID)
105 {
106  SetStomachPointer(strID);
107  m_strStomachID = strID;
108 }
109 
118 std::string Mouth::StomachID() {return m_strStomachID;}
119 
128 void Mouth::SetStomachPointer(std::string strID)
129 {
130  if(Std_IsBlank(strID))
131  m_lpStomach = NULL;
132  else
133  {
134  m_lpStomach = dynamic_cast<Stomach *>(m_lpSim->FindByID(strID));
135  if(!m_lpStomach)
136  THROW_PARAM_ERROR(Al_Err_lPartTypeNotStomach, Al_Err_strPartTypeNotStomach, "ID", strID);
137  }
138 }
139 
141 {
144 }
145 
147 {
149  m_fltEatingRate = 0;
150 }
151 
166 {
168 
169  if(m_lpStomach && m_fltEatingRate > 0)
170  {
171  //Now lets find the closest food source.
172  CStdArray<RigidBody *> arySources;
173  CStdArray<float> aryDistances;
174  CStdFPoint oPos = this->GetCurrentPosition();
175  m_lpSim->FindClosestFoodSources(oPos, m_fltMinFoodRadius, arySources, aryDistances);
176 
177  int iSources = arySources.GetSize();
178  if(iSources > 0)
179  {
180  RigidBody *lpFood = NULL;
181  float fltDist=0;
182  for(int iFoodIdx=0; iFoodIdx<iSources; iFoodIdx++)
183  {
184  lpFood = arySources[iFoodIdx];
185  fltDist = aryDistances[iFoodIdx];
186 
187  float fltBiteSize = 0;
188 
189  if(lpFood->FoodQuantity() >= m_fltEatingRate)
190  fltBiteSize = m_fltEatingRate;
191  else
192  fltBiteSize = lpFood->FoodQuantity();
193 
194  float fltEnergy = fltBiteSize*lpFood->FoodEnergyContent();
195 
196  if(fltEnergy > m_lpStomach->MaxEnergyLevel())
197  {
198  float fltNeededEnergy = m_lpStomach->MaxEnergyLevel() - m_lpStomach->EnergyLevel();
199  fltBiteSize = fltNeededEnergy/lpFood->FoodEnergyContent();
200  }
201 
202  lpFood->Eat(fltBiteSize, m_lpSim->TimeSlice());
203  m_lpStomach->AddEnergy(fltEnergy);
204  }
205  }
206  }
207 }
208 
209 bool Mouth::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
210 {
211  std::string strType = Std_CheckString(strDataType);
212 
213  if(Sensor::SetData(strType, strValue, false))
214  return true;
215 
216  if(strType == "STOMACHID")
217  {
218  StomachID(strValue);
219  return true;
220  }
221 
222  if(strType == "MINIMUMFOODRADIUS")
223  {
224  MinFoodRadius((float) atof(strValue.c_str()));
225  return true;
226  }
227 
228  //If it was not one of those above then we have a problem.
229  if(bThrowError)
230  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
231 
232  return false;
233 }
234 
235 void Mouth::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
236 {
237  Sensor::QueryProperties(aryProperties);
238 
239  aryProperties.Add(new TypeProperty("EatingRate", AnimatPropertyType::String, AnimatPropertyDirection::Get));
240  aryProperties.Add(new TypeProperty("FoodDistance", AnimatPropertyType::String, AnimatPropertyDirection::Get));
241 
242  aryProperties.Add(new TypeProperty("StomachID", AnimatPropertyType::String, AnimatPropertyDirection::Set));
243  aryProperties.Add(new TypeProperty("MinimumFoodRadius", AnimatPropertyType::Float, AnimatPropertyDirection::Set));
244 }
245 
246 float *Mouth::GetDataPointer(const std::string &strDataType)
247 {
248  std::string strType = Std_CheckString(strDataType);
249 
250  if(strType == "EATINGRATE")
251  return &m_fltEatingRate;
252 
253  if(strType == "FOODDISTANCE")
254  return &m_fltFoodDistance;
255 
256  return RigidBody::GetDataPointer(strDataType);
257 }
258 
259 void Mouth::AddExternalNodeInput(int iTargetDataType, float fltInput)
260 {
261  m_fltEatingRate = fltInput;
262  if(m_fltEatingRate < 0)
263  m_fltEatingRate = 0;
264 }
265 
266 void Mouth::Load(CStdXml &oXml)
267 {
268  Sensor::Load(oXml);
269 
270  oXml.IntoElem(); //Into RigidBody Element
271 
272  MinFoodRadius(oXml.GetChildFloat("MinimumFoodRadius", m_fltMinFoodRadius));
273  m_strStomachID = oXml.GetChildString("StomachID", "");
274 
275  oXml.OutOfElem(); //OutOf RigidBody Element
276 }
277 
278  } //Joints
279  } //Environment
280 } //AnimatSim
virtual float FoodEnergyContent()
Gets the food energy content.
Definition: RigidBody.cpp:749
Base class file for all Animat simulation objects.
Declares the simulation recorder class.
Declares the mouth class.
Root namespace for the base simulation library for AnimatLab.
Declares the body part class.
The Stomach object is responsible for holding food energy.
Definition: Stomach.h:26
Stomach * m_lpStomach
The pointer to stomach for this organism.
Definition: Mouth.h:29
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Mouth.cpp:146
virtual float FoodQuantity()
Gets the food quantity.
Definition: RigidBody.cpp:676
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: RigidBody.cpp:1473
virtual void Initialize()
Initializes this object.
Definition: Mouth.cpp:140
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
virtual std::string StomachID()
Gets the stomach identifier.
Definition: Mouth.cpp:118
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
virtual void Eat(float fltBiteSize, long lTimeSlice)
This item is eating the specified amount of food.
Definition: RigidBody.cpp:1320
Declares the key frame class.
virtual void Initialize()
Initializes this object.
Definition: Sensor.cpp:148
Declares the joint class.
Declares the organism class.
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
Mouth()
Default constructor.
Definition: Mouth.cpp:51
Declares a light object.
Declares the activated item class.
Declares the stomach class.
virtual void SetStomachPointer(std::string strID)
Sets the stomach pointer.
Definition: Mouth.cpp:128
Declares a light manager object.
Declares the bounding box class.
virtual CStdFPoint GetCurrentPosition()
Gets the current position of this part.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual float EnergyLevel()
Gets the energy level.
Definition: Stomach.cpp:85
virtual float MaxEnergyLevel()
Gets the maximum energy level.
Definition: Stomach.cpp:177
Declares the node class.
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
virtual AnimatBase * FindByID(std::string strID, bool bThrowError=true)
Searches for the object with the specified ID.
Definition: Simulator.cpp:4008
virtual void AddExternalNodeInput(int iTargetDataType, float fltInput)
Adds an external node input.
Definition: Mouth.cpp:259
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
float m_fltMinFoodRadius
Any food source that is further away than this minimum radius will not be available for eating...
Definition: Mouth.h:40
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
Declares the sensor class.
float m_fltFoodDistance
The current distance between the food and the mouth.
Definition: Mouth.h:43
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
Declares the data chart manager class.
Declares the rigid body class.
virtual void StepSimulation()
Step the simulation for this object.
Definition: RigidBody.cpp:1630
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
std::string m_strStomachID
Identifier for the stomach.
Definition: Mouth.h:32
virtual long TimeSlice()
Gets the current time slice.
Definition: Simulator.cpp:613
Declares the structure class.
Declares the odor type class.
virtual float MinFoodRadius()
Gets the minium radius where food can be eaten.
Definition: Mouth.cpp:85
virtual void StepSimulation()
Step the simulation.
Definition: Mouth.cpp:165
virtual float InverseDistanceUnits()
Gets the inverse distance units.
Definition: Simulator.cpp:1742
virtual ~Mouth()
Destructor.
Definition: Mouth.cpp:65
Declares the odor class.
Declares the simulator class.
virtual float EatingRate()
Gets the eating rate.
Definition: Mouth.cpp:83
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
The base class for all of the basic rigid body type of objects.
Definition: RigidBody.h:66
Declares the receptive field class.
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617