AnimatLab  2
Test
ActivatedItem.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 "Structure.h"
23 #include "Organism.h"
24 #include "ActivatedItem.h"
25 #include "ActivatedItemMgr.h"
26 #include "DataChartMgr.h"
27 #include "ExternalStimuliMgr.h"
28 #include "KeyFrame.h"
29 #include "SimulationRecorder.h"
30 #include "KeyFrame.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 {
48 {
49  m_bEnabled = true;
50  m_lStartSlice = 0;
51  m_lEndSlice = 0;
52  m_fltStartTime = 0;
53  m_fltEndTime = 0;
54  m_bLoadedTime = false;
55  m_iStepInterval = 1;
57  m_bAlwaysActive = false;
58  m_bInitialized = false;
59  m_bIsActivated = false;
60 }
61 
71 {
72 
73 }
74 
86 {return m_bEnabled;}
87 
98 void ActivatedItem::Enabled(bool bVal)
99 {
100  m_bEnabled = bVal;
101 
102  if(m_lpSim->Paused() && m_lpSim->TimeSlice() > 0)
103  {
104  if(!m_bEnabled && IsActivated())
105  Deactivate();
106  else if(m_bEnabled && !IsActivated() && NeedToActivate())
107  Activate();
108  }
109 }
110 
122 {return m_bLoadedTime;}
123 
135 {m_bLoadedTime = bVal;}
136 
148 {return m_lStartSlice;}
149 
161 void ActivatedItem::StartSlice(long lVal, bool bReInit)
162 {
163  Std_IsAboveMin((long) -1, lVal, true, "StartSlice");
164  Std_IsBelowMax(m_lEndSlice, lVal, true, "StartSlice");
165 
166  m_lStartSlice = lVal;
168 }
169 
181 {return m_lEndSlice;}
182 
194 void ActivatedItem::EndSlice(long lVal, bool bReInit)
195 {
196  Std_IsAboveMin((long) -1, lVal, true, "EndSlice");
197  Std_IsAboveMin(m_lStartSlice, lVal, true, "EndSlice");
198 
199  m_lEndSlice = lVal;
201 }
202 
214 {return m_fltStartTime;}
215 
227 void ActivatedItem::StartTime(float fltVal, bool bReInit)
228 {
229  Std_IsAboveMin((float) -1, fltVal, true, "StartTime");
230  Std_IsBelowMax(m_fltEndTime, fltVal, true, "StartTime");
231 
232  m_fltStartTime = fltVal;
233  m_lStartSlice = (long) (m_fltStartTime / m_lpSim->TimeStep() + 0.5);
234 }
235 
247 {return m_fltEndTime;}
248 
260 void ActivatedItem::EndTime(float fltVal, bool bReInit)
261 {
262  Std_IsAboveMin((float) -1, fltVal, true, "EndTime");
263  Std_IsAboveMin(m_fltStartTime, fltVal, true, "EndTime");
264 
265  m_fltEndTime = fltVal;
266  m_lEndSlice = (long) (m_fltEndTime / m_lpSim->TimeStep() + 0.5);
267 }
268 
280 {return m_iStepInterval;}
281 
293 {
294  Std_IsAboveMin((int) 0, iVal, true, "StepInterval");
295 
296  m_iStepInterval = iVal;
297 }
298 
312 {return m_iStepIntervalCount;}
313 
325 {m_iStepIntervalCount = iVal;}
326 
338 {return m_bAlwaysActive;}
339 
351 {m_bAlwaysActive = bVal;}
352 
364 
376 
390 {
391  if(!m_bIsActivated && m_bEnabled && (m_bAlwaysActive || (m_lStartSlice <= m_lpSim->TimeSlice() && m_lEndSlice >= m_lpSim->TimeSlice())))
392  return true;
393  return false;
394 }
395 
409 {
410  if(m_bIsActivated && !m_bEnabled)
411  return true;
412 
413  if(m_bIsActivated && !m_bAlwaysActive && !(m_lStartSlice <= m_lpSim->TimeSlice() && m_lEndSlice >= m_lpSim->TimeSlice()) )
414  return true;
415  return false;
416 }
417 
428 {
429  m_bIsActivated = true;
430 }
431 
442 {
443  m_bIsActivated = false;
444 }
445 
447 {
448  m_bIsActivated = false;
449 }
450 
464 {
465  if( (lpItem->StartSlice() >= this->StartSlice()) && (lpItem->StartSlice() <= this->EndSlice()) )
466  return true;
467 
468  if( (lpItem->EndSlice() >= this->StartSlice()) && (lpItem->EndSlice() <= this->EndSlice()) )
469  return true;
470 
471  return false;
472 }
473 
474 
476 {
478  SetSliceData();
479  m_bInitialized = true;
480 }
481 
482 void ActivatedItem::SetSliceData()
483 {
484  if(m_bLoadedTime)
485  {
486  m_lStartSlice = (long) (m_fltStartTime / m_lpSim->TimeStep() + 0.5);
487  m_lEndSlice = (long) (m_fltEndTime / m_lpSim->TimeStep() + 0.5);
488 
489  Std_IsAboveMin((long) -1, m_lStartSlice, true, "StartSlice");
490  Std_IsAboveMin(m_lStartSlice, m_lEndSlice, true, "EndSlice");
491  }
492  else
493  {
496  }
497 }
498 
500 {
501  if(!m_bInitialized)
502  Initialize();
503  else
504  SetSliceData();
505 }
506 
508 {
509  ReInitialize();
510 }
511 
525 {
526  if(m_iStepInterval <= 1)
527  return true;
529  {
531  return true;
532  }
533  else
534  {
536  return false;
537  }
538 }
539 
540 bool ActivatedItem::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
541 {
542  std::string strType = Std_CheckString(strDataType);
543 
544  if(AnimatBase::SetData(strDataType, strValue, false))
545  return true;
546 
547  if(strType == "STARTTIME")
548  {
549  StartTime((float) atof(strValue.c_str()));
550  return true;
551  }
552 
553  if(strType == "ENDTIME")
554  {
555  EndTime((float) atof(strValue.c_str()));
556  return true;
557  }
558 
559  if(strType == "ALWAYSACTIVE")
560  {
561  AlwaysActive(Std_ToBool(strValue));
562  return true;
563  }
564 
565  if(strType == "STEPINTERVAL")
566  {
567  StepInterval((float) atoi(strValue.c_str()));
568  return true;
569  }
570 
571  if(strType == "ENABLED")
572  {
573  Enabled(Std_ToBool(strValue));
574  return true;
575  }
576 
577  //If it was not one of those above then we have a problem.
578  if(bThrowError)
579  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
580 
581  return false;
582 }
583 
584 void ActivatedItem::Load(CStdXml &oXml)
585 {
586  AnimatBase::Load(oXml);
587 
588  oXml.IntoElem(); //Into Item Element
589 
590  //Do not call the mutator here because it will run code we do not want to run on load.
591  m_bEnabled = oXml.GetChildBool("Enabled", "True");
592 
593  if(oXml.FindChildElement("StartTime", false))
594  {
595  m_bLoadedTime = true;
596  EndTime(oXml.GetChildFloat("EndTime"), false);
597  StartTime(oXml.GetChildFloat("StartTime"), false);
598  }
599  else
600  {
601  m_bLoadedTime = false;
602  EndSlice(oXml.GetChildLong("EndSlice"), false);
603  StartSlice(oXml.GetChildLong("StartSlice"), false);
604  }
605 
606  StepInterval(oXml.GetChildInt("StepInterval", m_iStepInterval));
607  AlwaysActive(oXml.GetChildBool("AlwaysActive", m_bAlwaysActive));
608 
609  oXml.OutOfElem(); //OutOf Item Element
610 }
611 
629 {
630  return lpItem1->operator<(lpItem2);
631 }
632 
633 
634 } //AnimatSim
virtual ~ActivatedItem()
Destructor.
int m_iStepIntervalCount
Keeps track of how many time steps have occured since it was zeroed again.
Definition: ActivatedItem.h:56
Base class file for all Animat simulation objects.
virtual void Activate()
Activates this item.
Declares the simulation recorder class.
long m_lEndSlice
The time slice where this item is deactived.
Definition: ActivatedItem.h:39
virtual void ReInitialize()
Re-initialize this object.
bool LessThanActivatedItemCompare(ActivatedItem *lpItem1, ActivatedItem *lpItem2)
Compares the start times of two activated items to see which is sooner.
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 int StepInterval()
Gets the step interval.
Declares the body part class.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual bool Enabled()
Gets whether the item is enabled or not.
virtual bool Paused()
Gets whether the Simulation is paused.
Definition: Simulator.cpp:347
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.
Base class for all activated items.
Definition: ActivatedItem.h:27
bool IsActivated()
Query if this object is activated.
Declares the joint class.
Declares the organism class.
virtual int GetChildInt(std::string strElementName)
Gets an integer value from the element with the specified name.
Definition: StdXml.cpp:456
bool Std_IsAboveMin(int iMinVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is above a minimum value.
virtual bool Overlaps(ActivatedItem *lpItem)
Query if this object overlaps the time period for another ActivatedItem.
Declares a light object.
Declares the activated item class.
virtual float TimeStep()
Gets the smallest integration time step used within the simulation.
Definition: Simulator.cpp:951
ActivatedItem()
Default constructor.
Declares a light manager object.
Declares the bounding box class.
virtual long StartSlice()
Returns the starts slice for activation.
virtual float StartTime()
Gets the simulation start time for activation.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual int StepIntervalCount()
Gets the step interval count.
virtual void ResetSimulation()
Resets the simulation back to time 0.
bool NeedToActivate()
Determines if this item needs to be activated.
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
Declares the node class.
bool Std_ToBool(int iVal)
Converts a value toa bool.
bool Std_IsBelowMax(int iMaxVal, int iVal, bool bThrowError, std::string strParamName, bool bInclusiveLimit)
Tests if a number is below a maximum value.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Definition: AnimatBase.cpp:771
virtual bool GetChildBool(std::string strElementName)
Gets a bool value from the element with the specified name.
Definition: StdXml.cpp:699
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
bool m_bInitialized
true if item has been initialized
Definition: ActivatedItem.h:62
virtual long EndSlice()
Gets the end time slice for deactivation.
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
virtual void Deactivate()
Deactivates this item.
bool IsInitialized()
Query if this object is initialized.
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 long TimeSlice()
Gets the current time slice.
Definition: Simulator.cpp:613
Declares the structure class.
Declares the odor type class.
virtual void Initialize()
Initializes this object.
virtual bool NeedToStep()
Tells if this item needs to call StepSimulation or not.
virtual bool LoadedTime()
Gets whether time was loaded or time slices.
Declares the odor class.
Declares the simulator class.
virtual void TimeStepModified()
Notification method that the time step modified has been modified. Objects should recalculate any sli...
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
bool m_bAlwaysActive
Determines whether this item is always active during simulation.
Definition: ActivatedItem.h:59
virtual bool SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError=true)
Set a variable based on a string data type name.
Definition: AnimatBase.cpp:371
virtual bool AlwaysActive()
Gets whether this item is always active.
long m_lStartSlice
The time slice where this item becomes active.
Definition: ActivatedItem.h:36
bool NeedToDeactivate()
Determines if this item needs to be deactivated.
virtual long GetChildLong(std::string strElementName)
Gets a long value from the element with the specified name.
Definition: StdXml.cpp:371
Declares the receptive field class.
bool m_bIsActivated
true if item has been activated
Definition: ActivatedItem.h:65
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617
virtual float EndTime()
Gets the end simulation time for deactivation.