Atoms Crowd  4.1.0
BehaviourModule.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <Atoms/Globals.h>
11 #include <AtomsCore/Metadata/MapMetadata.h>
12 #include <AtomsCore/Metadata/MetadataImpl.h>
13 #include <vector>
14 
15 namespace Atoms
16 {
17  class DrawContext;
18 
19  class AgentGroup;
20 
21  class Agent;
22 
24 
30  class ATOMS_EXPORT BehaviourModule
31  {
32  public:
33 
34  enum ProfileType
35  {
36  kInitSimulation = 0,
37  kPreFrame,
38  kAgentsCreated,
39  kInitFrame,
40  kEndFrame,
41  kPrePhysics,
42  kPostPhysics,
43  kAgentsKilled,
44  kEndSimulation,
45  kResetSimulation
46  };
47 
48  enum ModuleTpe
49  {
50  kNative = 0,
51  kScript = 1
52  };
53 
56 
58  virtual ~BehaviourModule();
59 
61 
64  virtual void agentsCreated(const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
65 
67 
70  virtual void agentsKilled(const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
71 
73 
75  virtual void initSimulation(AgentGroup* agentGroup = nullptr);
76 
78 
80  virtual void preFrame(AgentGroup* agentGroup = nullptr);
81 
83 
85  virtual void initFrame(const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
86 
88 
90  virtual void endFrame(const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
91 
93 
95  virtual void prePhysics(const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
96 
98 
100  virtual void postPhysics(const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
101 
103 
105  virtual void endSimulation(const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
106 
108 
110  virtual void resetSimulation(const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
111 
113 
116  virtual void preDraw(DrawContext* context, const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
117 
119 
121  virtual void draw(DrawContext* context, const std::vector<Agent*>& agents, AgentGroup* agentGroup = nullptr);
122 
124  virtual AtomsCore::MapMetadata& attributes() { return m_attributes; }
125 
127  virtual AtomsCore::MapMetadata& attributeProperties() { return m_attributeProperties; }
128 
130  virtual const std::string& typeName() const { return m_typeName; }
131 
133  virtual void setTypeName(const std::string& typeName) { m_typeName = typeName; }
134 
136  virtual void addAttribute(const std::string& attributeName, AtomsPtr<AtomsCore::Metadata>& metadata, bool perAgent = false);
137  virtual void addAttribute(const std::string& attributeName, AtomsCore::Metadata* metadata, bool perAgent = false);
138 
140  virtual void addAttributeProperty(const std::string& attributeName, const std::string& propertyName, AtomsPtr<AtomsCore::Metadata>& metadata);
141  virtual void addAttributeProperty(const std::string& attributeName, const std::string& propertyName, AtomsCore::Metadata* metadata);
142 
144  virtual bool removeAttribute(const std::string& attributeName);
145 
147  virtual void setEnabled(bool value) { m_enabled = value; }
148 
150  virtual bool isEnabled() const { return m_enabled; }
151 
153  virtual const std::string& name() const { return m_name; }
154 
156  virtual void setName(const std::string& name) { m_name = name; }
157 
159  /* Use this function to add new attributes to the module based of external logic.
160  * For example some modules store some data in a string attribute and then use this data to generate new attributes
161  */
162  virtual void refreshAttributes();
163 
165  virtual bool hasDynamicAttributes() const;
166 
168  template<typename T>
169  T getAttributePerAgent(const T& defaultValue, AtomsCore::MapMetadata* overrideMap, const std::string& id);
170 
172  template<typename T>
173  T getAttributePerAgent(const T& defaultValue, AtomsCore::MapMetadata* overrideMap, const std::string& id, const AtomsCore::MapMetadata& agentMetadata, const std::string& metadataName);
174 
176  size_t profileTime(unsigned short index) const;
177 
178  void setProfileTime(unsigned short index, size_t us);
179 
180  static bool ignoreCurrentModule(Atoms::Agent* agent, const std::string& currentModuleName, const std::vector<std::string>& moduleNames, const std::string& metadataName);
181  static void enableCurrentActiveModuleMetadata(Atoms::Agent* agent, const std::string& currentModuleName, const std::string& metadataName, const bool enable);
182 
183  static void initializeAngularVelocity(Atoms::Agent* agent);
184 
185  private:
186 
187 
188  AtomsCore::MapMetadata m_attributes;
189  AtomsCore::MapMetadata m_attributeProperties;
190 
191  std::string m_typeName;
192 
193  std::string m_name;
194 
195  size_t m_profileTime[10];
196 
197  bool m_enabled;
198  };
199 
200  template<typename T>
201  T BehaviourModule::getAttributePerAgent(const T& defaultValue, AtomsCore::MapMetadata* overrideMap, const std::string& id)
202  {
203  if (overrideMap)
204  {
205  auto dataPtr = overrideMap->getTypedEntry<AtomsCore::MetadataImpl<T>>(id);
206  if (dataPtr) return dataPtr->value();
207  }
208  return defaultValue;
209  }
210 
211  template<typename T>
212  T BehaviourModule::getAttributePerAgent(const T& defaultValue, AtomsCore::MapMetadata* overrideMap, const std::string& id, const AtomsCore::MapMetadata& agentMetadata, const std::string& metadataName)
213  {
214  auto agentDataPtr = agentMetadata.getTypedEntry<AtomsCore::MetadataImpl<T>>(metadataName);
215  if (agentDataPtr)
216  {
217  return agentDataPtr->value();
218  }
219 
220  if (overrideMap)
221  {
222  auto dataPtr = overrideMap->getTypedEntry<AtomsCore::MetadataImpl<T>>(id);
223  if (dataPtr) return dataPtr->value();
224  }
225 
226  return defaultValue;
227  }
228 }
Atoms::BehaviourModule::draw
virtual void draw(DrawContext *context, const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Draw.
Atoms::BehaviourModule::agentsCreated
virtual void agentsCreated(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Agents Created.
Atoms::AgentGroup
Agent group.
Definition: AgentGroup.h:35
AtomsCore::MetadataImpl::value
T value() const
Get value.
Definition: MetadataImpl.impl.h:76
Atoms::BehaviourModule::addAttribute
virtual void addAttribute(const std::string &attributeName, AtomsPtr< AtomsCore::Metadata > &metadata, bool perAgent=false)
Add an attribute to the module, if the perAgent flag is set to true an extra attribute is added with ...
Atoms::BehaviourModule::~BehaviourModule
virtual ~BehaviourModule()
Destructor.
Atoms::BehaviourModule::isEnabled
virtual bool isEnabled() const
Gets the enabled state for this module.
Definition: BehaviourModule.h:150
AtomsCore::Metadata
Base Metadata class.
Definition: Metadata.h:24
Atoms::BehaviourModule::preFrame
virtual void preFrame(AgentGroup *agentGroup=nullptr)
Pre frame.
Atoms::BehaviourModule::getAttributePerAgent
T getAttributePerAgent(const T &defaultValue, AtomsCore::MapMetadata *overrideMap, const std::string &id)
Utils to get per agent attributes.
Definition: BehaviourModule.h:201
Atoms::BehaviourModule::removeAttribute
virtual bool removeAttribute(const std::string &attributeName)
Removes an attribute from the module, this also removes the _override entry if present.
Atoms::Agent
Agent.
Definition: Agent.h:38
Atoms::BehaviourModule::BehaviourModule
BehaviourModule()
Constructor.
Atoms::BehaviourModule::addAttributeProperty
virtual void addAttributeProperty(const std::string &attributeName, const std::string &propertyName, AtomsPtr< AtomsCore::Metadata > &metadata)
Add an attribute property to the module.
Atoms::BehaviourModule::initSimulation
virtual void initSimulation(AgentGroup *agentGroup=nullptr)
Init simulation.
AtomsCore::MetadataImpl
Metadata template class.
Definition: MetadataImpl.h:155
Atoms::BehaviourModule::name
virtual const std::string & name() const
Gets the module name.
Definition: BehaviourModule.h:153
Atoms::BehaviourModule::resetSimulation
virtual void resetSimulation(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Reset simulation.
Atoms::BehaviourModule::setTypeName
virtual void setTypeName(const std::string &typeName)
Sets the module type name.
Definition: BehaviourModule.h:133
Atoms::BehaviourModule::profileTime
size_t profileTime(unsigned short index) const
Get profile time.
Atoms::DrawContext
Definition: DrawContext.h:17
Atoms::BehaviourModule::hasDynamicAttributes
virtual bool hasDynamicAttributes() const
Check if this module can generate attribute dynamically.
Atoms
Atoms namespace.
Definition: Agent.h:28
Atoms::BehaviourModule
Behaviour module.
Definition: BehaviourModule.h:31
AtomsCore::MapMetadata
MapMetadata class.
Definition: MapMetadata.h:24
Atoms::BehaviourModule::initFrame
virtual void initFrame(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Init frame.
Atoms::BehaviourModule::postPhysics
virtual void postPhysics(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Post Physics.
Atoms::BehaviourModule::prePhysics
virtual void prePhysics(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Pre Physics.
Atoms::BehaviourModule::attributes
virtual AtomsCore::MapMetadata & attributes()
Returns the module attributes map.
Definition: BehaviourModule.h:124
Atoms::BehaviourModule::typeName
virtual const std::string & typeName() const
Gets the module type name.
Definition: BehaviourModule.h:130
Atoms::BehaviourModule::agentsKilled
virtual void agentsKilled(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Post frame.
Atoms::BehaviourModule::endFrame
virtual void endFrame(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
End frame.
Atoms::BehaviourModule::setEnabled
virtual void setEnabled(bool value)
Set the enabled state for this module.
Definition: BehaviourModule.h:147
Atoms::BehaviourModule::attributeProperties
virtual AtomsCore::MapMetadata & attributeProperties()
Returns the module attribute properties map.
Definition: BehaviourModule.h:127
Atoms::BehaviourModule::setName
virtual void setName(const std::string &name)
Sets the module name.
Definition: BehaviourModule.h:156
Atoms::BehaviourModule::preDraw
virtual void preDraw(DrawContext *context, const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Pre draw.
Atoms::BehaviourModule::endSimulation
virtual void endSimulation(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
End simulation.
AtomsCore::MapMetadata::getTypedEntry
AtomsPtr< T > getTypedEntry(const Key &key)
Get a typed entry.
Definition: MapMetadata.impl.h:4
Atoms::BehaviourModule::refreshAttributes
virtual void refreshAttributes()
Refresh the attribute map metadata.