Atoms Crowd  7.0.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 <Atoms/BehaviourModuleAttributeCache.h>
14 #include <vector>
15 
16 namespace Atoms
17 {
18  class DrawContext;
19 
20  class AgentGroup;
21 
22  class Agent;
23 
25 
31  class ATOMS_EXPORT BehaviourModule
32  {
33  public:
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  void setSingleThread(bool value) { m_singleThread = value; }
154 
156  bool singleThread() const { return m_singleThread; }
157 
159  virtual const std::string& name() const { return m_name; }
160 
162  virtual void setName(const std::string& name) { m_name = name; }
163 
165  /* Use this function to add new attributes to the module based of external logic.
166  * For example some modules store some data in a string attribute and then use this data to generate new attributes
167  */
168  virtual void refreshAttributes();
169 
171  virtual bool hasDynamicAttributes() const;
172 
174  template<typename T>
175  T getAttributePerAgent(const T& defaultValue, AtomsCore::MapMetadata* overrideMap, const std::string& id);
176 
178  template<typename T>
179  T getAttributePerAgent(const T& defaultValue, AtomsCore::MapMetadata* overrideMap, const std::string& id, const AtomsCore::MapMetadata& agentMetadata, const std::string& metadataName);
180 
182  size_t profileTime(unsigned short index) const;
183 
184  void setProfileTime(unsigned short index, size_t us);
185 
186  static bool ignoreCurrentModule(Atoms::Agent* agent, const std::string& currentModuleName, const std::vector<std::string>& moduleNames, const std::string& metadataName);
187  static void enableCurrentActiveModuleMetadata(Atoms::Agent* agent, const std::string& currentModuleName, const std::string& metadataName, const bool enable);
188 
189  static void initializeAngularVelocity(Atoms::Agent* agent);
190  static void setAngularVelocities(Atoms::Agent* agent, const AtomsCore::Vector3& newDir, const AtomsCore::Vector3& preMaxTurnAngleDir, const double fps);
191  static void setAngularVelocities(Atoms::Agent* agent, const double angularVelocity, const double frameAngularVelocity);
192 
193  private:
194 
195  void addAttributeRandomization(const std::string& attributeName, AtomsCore::Metadata* metadata);
196 
197  void addAttributeExpression(const std::string& attributeName, AtomsCore::Metadata* metadata);
198 
199 
200  AtomsCore::MapMetadata m_attributes;
201  AtomsCore::MapMetadata m_attributeProperties;
202 
203  std::string m_typeName;
204 
205  std::string m_name;
206 
207  size_t m_profileTime[10];
208 
209  bool m_enabled;
210 
211  bool m_singleThread;
212  };
213 
214  template<typename T>
215  T BehaviourModule::getAttributePerAgent(const T& defaultValue, AtomsCore::MapMetadata* overrideMap, const std::string& id)
216  {
217  if (overrideMap)
218  {
219  auto dataPtr = overrideMap->getTypedEntry<AtomsCore::MetadataImpl<T>>(id);
220  if (dataPtr) return dataPtr->value();
221  }
222  return defaultValue;
223  }
224 
225  template<typename T>
226  T BehaviourModule::getAttributePerAgent(const T& defaultValue, AtomsCore::MapMetadata* overrideMap, const std::string& id, const AtomsCore::MapMetadata& agentMetadata, const std::string& metadataName)
227  {
228  auto agentDataPtr = agentMetadata.getTypedEntry<AtomsCore::MetadataImpl<T>>(metadataName);
229  if (agentDataPtr)
230  {
231  return agentDataPtr->value();
232  }
233 
234  if (overrideMap)
235  {
236  auto dataPtr = overrideMap->getTypedEntry<AtomsCore::MetadataImpl<T>>(id);
237  if (dataPtr) return dataPtr->value();
238  }
239 
240  return defaultValue;
241  }
242 
243 }
Agent group.
Definition: AgentGroup.h:36
Agent.
Definition: Agent.h:44
Behaviour module.
Definition: BehaviourModule.h:32
virtual void setTypeName(const std::string &typeName)
Sets the module type name.
Definition: BehaviourModule.h:133
size_t profileTime(unsigned short index) const
Get profile time.
virtual void setEnabled(bool value)
Set the enabled state for this module.
Definition: BehaviourModule.h:147
bool singleThread() const
Return true if this module use a single thread.
Definition: BehaviourModule.h:156
virtual void initSimulation(AgentGroup *agentGroup=nullptr)
Init simulation.
virtual void refreshAttributes()
Refresh the attribute map metadata.
virtual void addAttributeProperty(const std::string &attributeName, const std::string &propertyName, AtomsPtr< AtomsCore::Metadata > &metadata)
Add an attribute property to the module.
virtual AtomsCore::MapMetadata & attributes()
Returns the module attributes map.
Definition: BehaviourModule.h:124
virtual void initFrame(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Init frame.
virtual void preFrame(AgentGroup *agentGroup=nullptr)
Pre frame.
virtual void prePhysics(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Pre Physics.
virtual void resetSimulation(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Reset simulation.
virtual void endSimulation(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
End simulation.
virtual void preDraw(DrawContext *context, const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Pre draw.
virtual void setName(const std::string &name)
Sets the module name.
Definition: BehaviourModule.h:162
virtual bool isEnabled() const
Gets the enabled state for this module.
Definition: BehaviourModule.h:150
virtual void draw(DrawContext *context, const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Draw.
virtual ~BehaviourModule()
Destructor.
virtual AtomsCore::MapMetadata & attributeProperties()
Returns the module attribute properties map.
Definition: BehaviourModule.h:127
virtual void endFrame(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
End frame.
virtual void agentsKilled(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Post frame.
virtual bool removeAttribute(const std::string &attributeName)
Removes an attribute from the module, this also removes the _override entry if present.
BehaviourModule()
Constructor.
void setSingleThread(bool value)
Use single thread.
Definition: BehaviourModule.h:153
virtual const std::string & typeName() const
Gets the module type name.
Definition: BehaviourModule.h:130
T getAttributePerAgent(const T &defaultValue, AtomsCore::MapMetadata *overrideMap, const std::string &id)
Utils to get per agent attributes.
Definition: BehaviourModule.h:215
virtual void postPhysics(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Post Physics.
virtual void agentsCreated(const std::vector< Agent * > &agents, AgentGroup *agentGroup=nullptr)
Agents Created.
virtual const std::string & name() const
Gets the module name.
Definition: BehaviourModule.h:159
virtual bool hasDynamicAttributes() const
Check if this module can generate attribute dynamically.
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 ...
Definition: DrawContext.h:17
MapMetadata class.
Definition: MapMetadata.h:24
AtomsPtr< T > getTypedEntry(const Key &key)
Get a typed entry.
Definition: MapMetadata.impl.h:4
Base Metadata class.
Definition: Metadata.h:24
Metadata template class.
Definition: MetadataImpl.h:158
T value() const
Get value.
Definition: MetadataImpl.impl.h:76
AtomsMath::Vector3 Vector3
Vector3 class.
Definition: AtomsMath.h:57
Atoms namespace.
Definition: Agent.h:29