AnimatLab  2
Test
Materials.cpp
1 // Materials.cpp: implementation of the Materials class.
2 //
4 
5 #include "StdAfx.h"
6 #include "IMovableItemCallback.h"
7 #include "ISimGUICallback.h"
8 #include "AnimatBase.h"
9 
10 #include "Node.h"
11 #include "IPhysicsMovableItem.h"
12 #include "IPhysicsBody.h"
13 #include "BoundingBox.h"
14 #include "MovableItem.h"
15 #include "BodyPart.h"
16 #include "Joint.h"
17 #include "ReceptiveField.h"
18 #include "ContactSensor.h"
19 #include "RigidBody.h"
20 #include "Sensor.h"
21 #include "Structure.h"
22 #include "Organism.h"
23 #include "ActivatedItem.h"
24 #include "ActivatedItemMgr.h"
25 #include "DataChartMgr.h"
26 #include "ExternalStimuliMgr.h"
27 #include "KeyFrame.h"
28 #include "SimulationRecorder.h"
29 #include "OdorType.h"
30 #include "Odor.h"
31 #include "Light.h"
32 #include "LightManager.h"
33 #include "Simulator.h"
34 
35 namespace AnimatSim
36 {
37  namespace Environment
38  {
39 
41 // Construction/Destruction
43 
44 Materials::Materials()
45 {
46 }
47 
48 Materials::~Materials()
49 {
50 
51 try
52 {
53  m_aryMaterialTypes.RemoveAll();
54 }
55 catch(...)
56 {Std_TraceMsg(0, "Caught Error in desctructor of Materials\r\n", "", -1, false, true);}
57 }
58 
60 {
61  m_aryMaterialTypes.RemoveAll();
62 }
63 
72 void Materials::AddMaterialType(std::string strXml, bool bDoNotInit)
73 {
74  CStdXml oXml;
75  oXml.Deserialize(strXml);
76  oXml.FindElement("Root");
77  oXml.FindChildElement("MaterialType");
78 
79  MaterialType *lpType = LoadMaterialType(oXml);
80 
81  if(!bDoNotInit)
82  lpType->Initialize();
83 
84  m_aryMaterialTypes.Add(lpType);
85 }
86 
97 void Materials::RemoveMaterialType(std::string strID, bool bThrowError)
98 {
99  int iPos = FindTypeListPos(strID, bThrowError);
100  m_aryMaterialTypes.RemoveAt(iPos);
101 }
102 
103 
117 int Materials::FindTypeListPos(std::string strID, bool bThrowError)
118 {
119  std::string sID = Std_ToUpper(Std_Trim(strID));
120 
121  int iCount = m_aryMaterialTypes.GetSize();
122  for(int iIndex=0; iIndex<iCount; iIndex++)
123  if(m_aryMaterialTypes[iIndex]->ID() == sID)
124  return iIndex;
125 
126  if(bThrowError)
127  THROW_TEXT_ERROR(Al_Err_lMaterialTypeIDNotFound, Al_Err_strMaterialTypeIDNotFound, "ID");
128 
129  return -1;
130 }
131 
132 
134 {
136 
137  if(m_aryMaterialTypes.GetSize() == 0)
138  THROW_ERROR(Al_Err_lDefaultMaterialNotFound, Al_Err_strDefaultMaterialNotFound);
139 
140  MaterialType *lpItem = NULL;
141  int iCount = m_aryMaterialTypes.GetSize();
142  for(int iIndex = 0; iIndex < iCount; iIndex++)
143  {
144  lpItem = m_aryMaterialTypes[iIndex];
145  lpItem->Initialize();
146  }
147 }
148 
149 bool Materials::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
150 {
151  std::string strType = Std_CheckString(strItemType);
152 
153  if(strType == "MATERIALTYPE")
154  {
155  AddMaterialType(strXml, bDoNotInit);
156  return true;
157  }
158 
159  //If it was not one of those above then we have a problem.
160  if(bThrowError)
161  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
162 
163  return false;
164 }
165 
166 bool Materials::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
167 {
168  std::string strType = Std_CheckString(strItemType);
169 
170  if(strType == "MATERIALTYPE")
171  {
172  RemoveMaterialType(strID);
173  return true;
174  }
175 
176  //If it was not one of those above then we have a problem.
177  if(bThrowError)
178  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
179 
180  return false;
181 }
182 
183 void Materials::CreateDefaultMaterial()
184 {
185  MaterialType *lpType=NULL;
186 
187  m_aryMaterialTypes.RemoveAll();
188 
189  lpType = dynamic_cast<MaterialType *>(m_lpSim->CreateObject("", "Material", "Default"));
190  if(!lpType)
191  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Material");
192 
193  lpType->ID("DEFAULTMATERIAL");
194  lpType->Name("Default");
195  lpType->SetSystemPointers(m_lpSim, NULL, NULL, NULL, true);
196  m_aryMaterialTypes.Add(lpType);
197 }
198 
199 void Materials::LoadMaterialTypes(CStdXml &oXml)
200 {
201  oXml.FindChildElement("MaterialTypes");
202  oXml.IntoElem(); //Into MaterialsTypes Element
203 
204  std::string strMaterial;
205  MaterialType *lpItem = NULL;
206  bool bDefaultFound = false;
207  int iCount = oXml.NumberOfChildren();
208  for(int iIndex=0; iIndex<iCount; iIndex++)
209  {
210  oXml.FindChildByIndex(iIndex);
211  lpItem = LoadMaterialType(oXml);
212  m_aryMaterialTypes.Add(lpItem);
213 
214  if(lpItem->ID() == "DEFAULTMATERIAL")
215  bDefaultFound = true;
216  }
217 
218  oXml.OutOfElem(); //Outof MaterialsTypes Element
219 
220  if(!bDefaultFound)
221  THROW_ERROR(Al_Err_lDefaultMaterialNotFound, Al_Err_strDefaultMaterialNotFound);
222 }
223 
224 void Materials::Load(CStdXml &oXml)
225 {
226  AnimatBase::Load(oXml);
227 
228  m_aryMaterialTypes.RemoveAll();
229 
230  if(oXml.FindChildElement("Materials", false))
231  {
232  oXml.IntoElem(); //Into Materials Element
233 
234  LoadMaterialTypes(oXml);
235 
236  oXml.OutOfElem(); //Outof Materials Element
237  }
238  else
239  CreateDefaultMaterial(); //Always create a default material.
240 }
241 
242 MaterialType *Materials::LoadMaterialType(CStdXml &oXml)
243 {
244  MaterialType *lpItem=NULL;
245  std::string strModuleName, strType;
246 
247 try
248 {
249  oXml.IntoElem(); //Into Column Element
250  strModuleName = oXml.GetChildString("ModuleName", "");
251  strType = oXml.GetChildString("Type");
252  oXml.OutOfElem(); //OutOf Column Element
253 
254  lpItem = dynamic_cast<MaterialType *>(m_lpSim->CreateObject(strModuleName, "Material", strType));
255  if(!lpItem)
256  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Material");
257 
258  lpItem->SetSystemPointers(m_lpSim, m_lpStructure, NULL, NULL, true);
259  lpItem->Load(oXml);
260 
261  return lpItem;
262 }
263 catch(CStdErrorInfo oError)
264 {
265  if(lpItem) delete lpItem;
266  RELAY_ERROR(oError);
267  return NULL;
268 }
269 catch(...)
270 {
271  if(lpItem) delete lpItem;
272  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
273  return NULL;
274 }
275 }
276 
277 
278  } // Visualization
279 } //VortexAnimatSim
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 void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
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 void Reset()
Resets this object.
Definition: Materials.cpp:59
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
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
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
Declares the key frame class.
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 int FindTypeListPos(std::string strID, bool bThrowError=true)
Finds the array index for the material type with the specified ID.
Definition: Materials.cpp:117
std::string Std_Trim(std::string strVal)
Trims a string.
Declares a light object.
Declares the activated item class.
virtual CStdSerialize * CreateObject(std::string strModule, std::string strClassName, std::string strType, bool bThrowError=true)
Creates an object using a class factory.
Definition: Simulator.cpp:3440
Declares a light manager object.
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
Definition: Materials.cpp:166
Declares the bounding box class.
virtual std::string Name()
Gets the name of this object.
Definition: AnimatBase.cpp:195
virtual void RemoveMaterialType(std::string strID, bool bThrowError=true)
Removes the material type with the specified ID.
Definition: Materials.cpp:97
A standard xml manipulation class.
Definition: StdXml.h:19
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 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
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void Initialize()
Initializes this object.
Definition: Materials.cpp:133
Declares the sensor class.
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.
Declares the structure class.
Declares the odor type class.
virtual void AddMaterialType(std::string strXml, bool bDoNotInit)
Creates and adds a new material type.
Definition: Materials.cpp:72
Declares the odor class.
Declares the simulator class.
virtual bool FindChildByIndex(int iIndex, bool bThrowError=true)
Finds a child element by index.
Definition: StdXml.cpp:225
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.
virtual bool AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError=true, bool bDoNotInit=false)
Adds a new object to this parent.
Definition: Materials.cpp:149
Declares the receptive field class.