AnimatLab  2
Test
ContactAdapter.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 "Link.h"
14 #include "IPhysicsMovableItem.h"
15 #include "IPhysicsBody.h"
16 #include "BoundingBox.h"
17 #include "MovableItem.h"
18 #include "BodyPart.h"
19 #include "Gain.h"
20 #include "Adapter.h"
21 #include "ReceptiveField.h"
22 #include "ReceptiveFieldPair.h"
23 #include "ContactAdapter.h"
24 #include "Joint.h"
25 #include "ContactSensor.h"
26 #include "RigidBody.h"
27 #include "Structure.h"
28 #include "NeuralModule.h"
29 #include "NervousSystem.h"
30 #include "Organism.h"
31 #include "ActivatedItem.h"
32 #include "ActivatedItemMgr.h"
33 #include "DataChartMgr.h"
34 #include "ExternalStimuliMgr.h"
35 #include "KeyFrame.h"
36 #include "SimulationRecorder.h"
37 #include "OdorType.h"
38 #include "Odor.h"
39 #include "Light.h"
40 #include "LightManager.h"
41 #include "Simulator.h"
42 
43 
44 namespace AnimatSim
45 {
46  namespace Adapters
47  {
55 {
56 }
57 
65 {
66 
67 try
68 {
69  m_aryFieldPairs.RemoveAll();
70 }
71 catch(...)
72 {Std_TraceMsg(0, "Caught Error in desctructor of ContactAdapter\r\n", "", -1, false, true);}
73 }
74 
84 
93 void ContactAdapter::SourceBodyID(std::string strID)
94 {
95  if(Std_IsBlank(strID))
96  THROW_ERROR(Al_Err_lBodyIDBlank, Al_Err_strBodyIDBlank);
97  m_strSourceBodyID = strID;
98 }
99 
101 {return "AnimatLab";}
102 
104 {return m_strTargetModule;}
105 
114 void ContactAdapter::TargetModule(std::string strModule)
115 {
116  if(Std_IsBlank(strModule))
117  THROW_TEXT_ERROR(Al_Err_lModuleNameBlank, Al_Err_strModuleNameBlank, " Target Module");
118  m_strTargetModule = strModule;
119 }
120 
121 
122 void ContactAdapter::AddFieldPair(std::string strXml, bool bDoNotInit)
123 {
124  CStdXml oXml;
125  oXml.Deserialize(strXml);
126  oXml.FindElement("Root");
127  oXml.FindChildElement("FieldPair");
128 
129  ReceptiveFieldPair *lpPair = LoadFieldPair(oXml);
130  if(!bDoNotInit)
131  lpPair->Initialize();
132 }
133 
134 void ContactAdapter::RemoveFieldPair(std::string strID, bool bThrowError)
135 {
136  int iPos = FindFieldPairListPos(strID, bThrowError);
137  m_aryFieldPairs.RemoveAt(iPos);
138 }
139 
140 int ContactAdapter::FindFieldPairListPos(std::string strID, bool bThrowError)
141 {
142  std::string sID = Std_ToUpper(Std_Trim(strID));
143 
144  int iCount = m_aryFieldPairs.GetSize();
145  for(int iIndex=0; iIndex<iCount; iIndex++)
146  if(m_aryFieldPairs[iIndex]->ID() == sID)
147  return iIndex;
148 
149  if(bThrowError)
150  THROW_TEXT_ERROR(Al_Err_lReceptiveFieldIDNotFound, Al_Err_strReceptiveFieldIDNotFound, "ID");
151 
152  return -1;
153 }
154 
156 {
158 
160  if(!m_lpSourceNode)
161  THROW_PARAM_ERROR(Al_Err_lNodeNotFound, Al_Err_strNodeNotFound, "ID: ", m_strSourceBodyID);
162 
163  m_lpSim->AttachSourceAdapter(m_lpStructure, this);
164  m_lpSim->AttachTargetAdapter(m_lpStructure, this);
165 
166  int iCount = m_aryFieldPairs.GetSize();
167  ReceptiveFieldPair *lpPair=NULL;
168  for(int iIndex=0; iIndex<iCount; iIndex++)
169  {
170  lpPair = m_aryFieldPairs[iIndex];
171  lpPair->Initialize();
172  }
173 }
174 
175 bool ContactAdapter::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
176 {
177  std::string strType = Std_CheckString(strItemType);
178 
179  if(strType == "FIELDPAIR")
180  {
181  AddFieldPair(strXml, bDoNotInit);
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_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
188 
189  return false;
190 }
191 
192 bool ContactAdapter::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
193 {
194  std::string strType = Std_CheckString(strItemType);
195 
196  if(strType == "FIELDPAIR")
197  {
198  RemoveFieldPair(strID);
199  return true;
200  }
201 
202  //If it was not one of those above then we have a problem.
203  if(bThrowError)
204  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
205 
206  return false;
207 }
208 
210 {
211  int iCount = m_aryFieldPairs.GetSize();
212  for(int iIndex=0; iIndex<iCount; iIndex++)
213  m_aryFieldPairs[iIndex]->StepSimulation();
214 }
215 
216 void ContactAdapter::Load(CStdXml &oXml)
217 {
218  Node::Load(oXml);
219 
220  oXml.IntoElem(); //Into Adapter Element
221 
222  SourceBodyID(oXml.GetChildString("SourceBodyID"));
223  TargetModule(oXml.GetChildString("TargetModule"));
224 
225  m_aryFieldPairs.RemoveAll();
226 
227  if(oXml.FindChildElement("FieldPairs", false))
228  {
229  oXml.IntoElem(); //Into FieldPairs Element
230  int iCount = oXml.NumberOfChildren();
231 
232  for(int iIndex=0; iIndex<iCount; iIndex++)
233  {
234  oXml.FindChildByIndex(iIndex);
235  LoadFieldPair(oXml);
236  }
237  oXml.OutOfElem(); //OutOf FieldPairs Element
238  }
239 
240  oXml.OutOfElem(); //OutOf Adapter Element
241 }
242 
255 {
256  ReceptiveFieldPair *lpPair = NULL;
257 
258 try
259 {
260  lpPair = new ReceptiveFieldPair();
261  lpPair->SetSystemPointers(m_lpSim, m_lpStructure, NULL, m_lpNode, true);
262  lpPair->Load(oXml);
263  m_aryFieldPairs.Add(lpPair);
264 
265  return lpPair;
266 }
267 catch(CStdErrorInfo oError)
268 {
269  if(lpPair) delete lpPair;
270  RELAY_ERROR(oError);
271  return NULL;
272 }
273 catch(...)
274 {
275  if(lpPair) delete lpPair;
276  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
277  return NULL;
278 }
279 }
280 
281  } //Adapters
282 } //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 nervous system class.
Declares the simulation recorder class.
virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
virtual std::string SourceBodyID()
Gets the GUID ID of the source RigidBody.
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.
Declares the body part class.
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
ContactAdapter()
Default constructor.
Information about the standard error.
Definition: StdErrorInfo.h:19
virtual std::string ID()
Gets the unique GUID ID of this object.
Definition: AnimatBase.cpp:167
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
std::string m_strSourceBodyID
GUID ID of the source RigidBody.
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
Declares the key frame class.
Node * m_lpSourceNode
Pointer to the source node.
Definition: Adapter.h:52
Declares the joint class.
Declares the organism class.
AnimatSim::Environment::Structure * m_lpStructure
The pointer to this items parent Structure. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:46
virtual void Initialize()
Initializes this object.
std::string Std_Trim(std::string strVal)
Trims a string.
Declares a light object.
Declares the activated item class.
Declares a light manager object.
Declares the receptive field pair class.
Declares the bounding box class.
Declares the gain base class.
virtual ~ContactAdapter()
Destructor.
A standard xml manipulation class.
Definition: StdXml.h:19
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
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
CStdPtrArray< ReceptiveFieldPair > m_aryFieldPairs
The array of ReceptiveFieldPair objects.
virtual std::string TargetModule()
Gets the name of the target NeuralModule.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Definition: AnimatBase.cpp:771
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.
virtual int NumberOfChildren()
Gets the number of children of the current element.
Definition: StdXml.cpp:202
Base class for body parts and neural network nodes.
Definition: Node.h:25
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
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.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual std::string SourceModule()
Gets the name of the source NeuralModule.
Declares the structure class.
virtual void StepSimulation()
Step the simulation for this object.
Declares the odor type class.
std::string m_strTargetModule
The name of the target NeuralModule.
virtual void Initialize()
Initializes this object.
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
virtual bool FindChildByIndex(int iIndex, bool bThrowError=true)
Finds a child element by index.
Definition: StdXml.cpp:225
Declares the contact adapter class.
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
ReceptiveFieldPair * LoadFieldPair(CStdXml &oXml)
Loads a ReceptiveFieldPair object.
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
Declares the receptive field class.