AnimatLab  2
Test
LightManager.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 "Light.h"
18 #include "LightManager.h"
19 #include "BodyPart.h"
20 #include "Joint.h"
21 #include "ReceptiveField.h"
22 #include "ContactSensor.h"
23 #include "RigidBody.h"
24 #include "Structure.h"
25 #include "Organism.h"
26 #include "ActivatedItem.h"
27 #include "ActivatedItemMgr.h"
28 #include "DataChartMgr.h"
29 #include "ExternalStimuliMgr.h"
30 #include "KeyFrame.h"
31 #include "SimulationRecorder.h"
32 #include "OdorType.h"
33 #include "Odor.h"
34 #include "Light.h"
35 #include "LightManager.h"
36 #include "Simulator.h"
37 
38 namespace AnimatSim
39 {
40  namespace Environment
41  {
49 {
50 }
51 
59 {
60 }
61 
62 #pragma region AccessorMutators
63 
64 #pragma endregion
65 
66 #pragma region DataAccesMethods
67 
68 
69 #pragma endregion
70 
71 void LightManager::SetupLights()
72 {}
73 
74 
83 void LightManager::AddLight(std::string strXml)
84 {
85  CStdXml oXml;
86  oXml.Deserialize(strXml);
87  oXml.FindElement("Root");
88  oXml.FindChildElement("Light");
89 
90  Light *lpLight = LoadLight(oXml);
91 
92  lpLight->Create();
93 }
94 
105 void LightManager::RemoveLight(std::string strID, bool bThrowError)
106 {
107  int iPos = FindChildListPos(strID, bThrowError);
108  m_aryLights.RemoveAt(iPos);
109 }
110 
111 bool LightManager::AddItem(const std::string &strItemType, const std::string &strXml, bool bThrowError, bool bDoNotInit)
112 {
113  std::string strType = Std_CheckString(strItemType);
114 
115  if(strType == "LIGHT")
116  {
117  AddLight(strXml);
118  return true;
119  }
120 
121  //If it was not one of those above then we have a problem.
122  if(bThrowError)
123  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
124 
125  return false;
126 }
127 
128 bool LightManager::RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError)
129 {
130  std::string strType = Std_CheckString(strItemType);
131 
132  if(strType == "LIGHT")
133  {
134  RemoveLight(strID);
135  return true;
136  }
137 
138  //If it was not one of those above then we have a problem.
139  if(bThrowError)
140  THROW_PARAM_ERROR(Al_Err_lInvalidItemType, Al_Err_strInvalidItemType, "Item Type", strItemType);
141 
142  return false;
143 }
144 
158 int LightManager::FindChildListPos(std::string strID, bool bThrowError)
159 {
160  std::string sID = Std_ToUpper(Std_Trim(strID));
161 
162  int iCount = m_aryLights.GetSize();
163  for(int iIndex=0; iIndex<iCount; iIndex++)
164  if(m_aryLights[iIndex]->ID() == sID)
165  return iIndex;
166 
167  if(bThrowError)
168  THROW_TEXT_ERROR(Al_Err_lBodyOrJointIDNotFound, Al_Err_strBodyOrJointIDNotFound, "ID");
169 
170  return -1;
171 }
172 
174 {
175  int iCount = m_aryLights.GetSize();
176  for(int iLight=0; iLight<iCount; iLight++)
177  m_aryLights[iLight]->Create();
178 }
179 
180 void LightManager::Load(CStdXml &oXml)
181 {
182  if(oXml.FindChildElement("Lights", false))
183  {
184  AnimatBase::Load(oXml);
185 
186  oXml.IntoElem(); //Into Lights Element
187 
188  Light *lpLight = NULL;
189  int iCount = oXml.NumberOfChildren();
190  for(int iIndex=0; iIndex<iCount; iIndex++)
191  {
192  oXml.FindChildByIndex(iIndex);
193  lpLight = LoadLight(oXml);
194  }
195 
196  oXml.OutOfElem(); //Outof Lights Element
197  }
198 }
199 
200 
212 {
213  Light *lpLight=NULL;
214  std::string strModuleName, strType;
215 
216 try
217 {
218  oXml.IntoElem(); //Into Column Element
219  strModuleName = oXml.GetChildString("ModuleName", "");
220  strType = oXml.GetChildString("Type");
221  oXml.OutOfElem(); //OutOf Column Element
222 
223  lpLight = dynamic_cast<Light *>(m_lpSim->CreateObject(strModuleName, "Light", strType));
224  if(!lpLight)
225  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "Light");
226 
227  lpLight->SetSystemPointers(m_lpSim, NULL, NULL, NULL, true);
228  lpLight->Load(oXml);
229 
230  m_aryLights.Add(lpLight);
231  lpLight->LightNumber(m_aryLights.GetSize()-1);
232 
233  return lpLight;
234 }
235 catch(CStdErrorInfo oError)
236 {
237  if(lpLight) delete lpLight;
238  RELAY_ERROR(oError);
239  return NULL;
240 }
241 catch(...)
242 {
243  if(lpLight) delete lpLight;
244  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
245  return NULL;
246 }
247 }
248 
249  } //Environment
250 } //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 bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
virtual void AddLight(std::string strXml)
Creates and adds a light.
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
Base class for the light object.
Definition: Light.h:23
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
Declares the key frame 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.
Declares the joint class.
Declares the organism class.
Light * LoadLight(CStdXml &oXml)
Loads a Light.
std::string Std_Trim(std::string strVal)
Trims a string.
Declares a light object.
virtual bool RemoveItem(const std::string &strItemType, const std::string &strID, bool bThrowError=true)
Removes a child item from this parent.
virtual void RemoveLight(std::string strID, bool bThrowError=true)
Removes the light with the specified ID.
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 ~LightManager(void)
Destructor.
Declares the bounding box class.
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
CStdPtrArray< Light > m_aryLights
Array of Light objects.
Definition: LightManager.h:27
virtual int NumberOfChildren()
Gets the number of children of the current element.
Definition: StdXml.cpp:202
LightManager(void)
Default constructor.
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.
Declares the structure class.
Declares the odor type class.
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.
virtual int FindChildListPos(std::string strID, bool bThrowError=true)
Finds the array index for the light with the specified ID.
Declares the contact sensor class.
Declares the external stimuli manager class.
virtual void Initialize()
Initializes this object.
virtual void LightNumber(int iVal)
Sets the light number.
Definition: Light.cpp:201
Declares the receptive field class.