Atoms Crowd  7.0.0
AgentGroup.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 <Atoms/BehaviourModule.h>
12 #include <Atoms/LayoutGenerators/LayoutGenerator.h>
13 #include <Atoms/Agent.h>
14 #include <Atoms/SimulationTime.h>
15 #include <Atoms/HeightFields.h>
16 #include <Atoms/NavigationMeshes.h>
17 #include <Atoms/Graph/OperatorComputeData.h>
18 #include <AtomsUtils/KdTreePoint.h>
19 #include <AtomsCore/Metadata/MapMetadata.h>
20 #include <AtomsCore/Metadata/ArrayMetadata.h>
21 #include <map>
22 #include <vector>
23 #include <atomic>
24 #include <mutex>
25 #include <functional>
26 
27 namespace Atoms
28 {
29  class AtomsPhysx;
30 
32 
35  class ATOMS_EXPORT AgentGroup
36  {
37  public:
38 
39  enum ProfileType
40  {
41  kInitSimulation = 0,
42  kPreFrame,
43  kAgentsCreated,
44  kInitFrame,
45  kEndFrame,
46  kPrePhysics,
47  kPostPhysics,
48  kAgentsKilled,
49  kEndSimulation,
50  kResetSimulation,
51  kComputePose,
52  kGenerateAgents,
53  kClearAgents
54  };
55 
56 
57 
58  class ATOMS_EXPORT Cache
59  {
60  public:
61 
62  enum CacheState
63  {
64  Empty = 0,
65  Dirty,
66  Updated
67  };
68 
69  class Frame
70  {
71  public:
72  std::vector<Atoms::Agent*> agents;
73  std::vector<unsigned int> agentsCreated;
74  std::vector<unsigned int> agentsKilled;
75  AtomsCore::Matrix matrix;
76  std::unordered_map<std::string, std::vector<Agent*>> agentTypeMapper;
77  std::unordered_map<int, Agent*> localAgents;
78  std::map<size_t, AtomsPtr<Atoms::Agent>> agentsCached;
80  };
81 
82  Cache();
83 
84  ~Cache();
85 
86  void clear();
87 
88  void setCacheState(short value);
89 
90  short cacheState() const;
91 
92  std::vector<int> cacheFrames() const;
93 
94  public:
95 
96  std::map<long, Frame> frames;
97  //std::map<size_t, AtomsPtr<Atoms::Agent>> agents;
98  short m_cacheState;
99  double m_prevCacheFrame;
100  double m_oldSimtime;
101  int m_oldEnableCacheValue;
102  bool m_isCachePlaying;
103  bool m_saveFrame;
104  bool m_validFrame;
105  };
106 
109 
112 
114 
118  void addBehaviourModule(const std::string& name, const std::string& moduleTypeName);
119  void addBehaviourModule(const std::string& name, AtomsPtr<BehaviourModule>& module);
120 
122 
125  void removeBehaviourModule(const std::string& name);
126 
128 
132  void reorderBehaviourModule(const std::string& name, unsigned int newPosition);
133 
135 
138  const std::vector<std::string>& behaviourModuleNames() const;
139 
141 
145  AtomsPtr<BehaviourModule> behaviourModule(const std::string& name);
146 
148 
152 
154 
158  AtomsCore::MapMetadata behaviourModuleData(const std::string& name);
159 
161 
164  void reloadBehaviourModule(const std::string& name);
165 
167 
170  const std::vector<Agent*>& agents() const;
171 
173 
175  void removeAgent(Agent* agent);
176 
178 
181  const std::string& name() const;
182 
184  /*
185  \param name Name
186  */
187  void setName(const std::string& name);
188 
190 
193  const std::string& simulationName() const;
194 
196  /*
197  \param name Name
198  */
199  void setSimulationName(const std::string& name);
200 
202 
205  const AtomsCore::Matrix& matrix() const;
206 
208  /*
209  \param matrix Matrix
210  */
211  void setMatrix(const AtomsCore::Matrix& matrix);
212 
214  void initializeNewAgents(std::vector<AgentInitData>& newAgentsData);
215 
217  void runAgentsCreatedOnAgent(Atoms::Agent *agent, const std::string& breakAtModuleName);
218 
220 
222  void killAgents(const std::vector<unsigned int>& localIds);
223 
225 
227  void generateAgents(double time);
228 
230 
235 
237 
239  void preFrame();
240 
242 
244  void initFrame();
245 
247 
249  void computePose();
250 
252 
254  void endFrame();
255 
257 
259  void prePhysics();
260 
262 
264  void postPhysics();
265 
267 
270 
272 
275 
277  void setLayoutGenerator(LayoutGeneratorPtr& layoutGen);
278 
280  void addLayoutGenerator(LayoutGeneratorPtr& layoutGen);
281 
283  const std::vector<LayoutGeneratorPtr>& layoutGenerators();
284 
286  LayoutGeneratorPtr getLayoutGenerator(const std::string& name) const;
287 
290 
292 
294  void clear();
295 
297 
299  void clearAgents();
300 
302  void reset();
303 
305 
308  bool multithread();
309 
311 
314  void setMultithread(bool value);
315 
317 
320  const std::unordered_map<std::string, std::vector<Agent*>> agentTypeMapper() const;
321 
323 
326  const std::unordered_map<std::string, std::vector<Agent*>>& agentTypeMapperRef() const;
327 
330 
332 
334  const std::unordered_map<int, int>& agentsSelection() const;
335 
337 
340  void setAgentsSelection(std::unordered_map<int, int>& selectionData);
341 
343 
345  void setActive(bool value);
346 
348  bool isActive() const;
349 
351 
354  const std::unordered_map<int, Agent*>& localAgents() const;
355 
357  Agent* agent(unsigned int groupId);
358 
360 
363  void filterAgent(unsigned int agentId);
364 
366  void filterAgents(const std::vector<int>& agentIds, bool invert = false);
367 
369  bool isAgentFilterInverted() const;
370 
372  void setAgentFilterMap(const std::unordered_map<int, bool>& data, bool invert = false);
373 
375  const std::unordered_map<int, bool>& agentFilter() const;
376 
379 
381  AtomsPtr<Atoms::AgentGroup> parent();
382 
384  void setParent(AtomsPtr<Atoms::AgentGroup>& parent);
385 
387  const std::vector<unsigned int>& agentsCreated() const;
388 
390  const std::vector<unsigned int>& agentsKilled() const;
391 
392  void setCacheData(const std::vector<Agent*>& agents,
393  const std::vector<unsigned int>& agentsCreated,
394  const std::vector<unsigned int>& agentsKilled);
395 
398 
401 
404 
407 
410 
412  const HeightFields& heightFields() const;
413 
416 
419 
420  AtomsPhysx* physics();
421 
422  void setPhysics(AtomsPhysx* physic);
423 
425  size_t profileTime(unsigned short index) const;
426 
427  void setProfileTime(unsigned short index, size_t us);
428 
430  void lock() const;
431 
433  void unlock() const;
434 
436  void enableCache(bool value);
437 
439  bool isCacheEnabled() const;
440 
442  void clearCache();
443 
445  const Cache& cache() const;
446 
449 
450  void enablePositionsKdtree(bool value);
451 
453  void enablePositionsKdTreeCache(bool value);
454 
455  const AtomsUtils::KdTreePoint& positionsKdTree() const;
456 
457  void updatePositionsKdTree();
458 
459  void setAgentInitializationCallback(std::function<void(Agent*)>& callback);
460 
461  bool hasTag(const std::string& tag) const;
462 
463  inline std::vector<std::string>& tags() { return m_tags; }
464 
465  inline const std::vector<std::string>& tags() const { return m_tags; }
466 
467  private:
468 
469  std::function<void(Agent*)> m_agentInitializationCallBack;
470 
472  Cache m_cache;
473 
475  AtomsUtils::KdTreePoint m_positionsKdtree;
476 
478  AtomsCore::MapMetadata m_metadata;
479 
481  std::map<std::string, AtomsPtr<BehaviourModule>> m_behaviourModules;
482 
484 
486  std::vector<std::string> m_moduleOrder;
487 
489  std::vector<Agent*> m_agents;
490 
492 
494  std::unordered_map<std::string, std::vector<Agent*>> m_agentTypeMapper;
495 
497  std::unordered_map<int, int> m_agentsSelection;
498 
500  std::unordered_map<int, Agent*> m_localAgents;
501 
503  std::unordered_map<int, bool> m_agentFilter;
504 
506  std::vector<unsigned int> m_agentsCreated;
507 
509  std::vector<unsigned int> m_agentsKilled;
510 
512  std::string m_name;
513 
515  std::string m_simulationName;
516 
518  AtomsCore::Matrix m_matrix;
519 
521  std::vector<LayoutGeneratorPtr> m_layoutGenerators;
522 
524  std::atomic<size_t> m_localIdCounter;
525 
527  AtomsPtr<AgentGroup> m_parent;
528 
530  SimulationTime m_simTime;
531 
533  HeightFields m_heightFields;
534 
536  NavigationMeshes m_navigationMeshes;
537 
539  AtomsPhysx* m_physx;
540 
542  OperatorComputeData m_computeData;
543 
545  std::vector<std::string> m_tags;
546 
548  size_t m_profileTime[14];
549 
551  mutable std::mutex m_mutex;
552 
554  bool m_active;
555 
557  bool m_multiThread;
558 
560  bool m_status;
561 
563  bool m_cacheEnabled;
564 
566  bool m_invertAgentFilter;
567 
569  bool m_enableKdTree;
570 
572  bool m_cacheKdTree;
573  };
574 }
575 
576 #include "AgentGroup.impl.h"
Definition: AgentGroup.h:70
Definition: AgentGroup.h:59
Agent group.
Definition: AgentGroup.h:36
void enableCache(bool value)
Enable or disable the sim cache.
void reorderBehaviourModule(const std::string &name, unsigned int newPosition)
Moves a behaviour module to a different position in the behaviour list.
void setAgentFilterMap(const std::unordered_map< int, bool > &data, bool invert=false)
Sets the agent filter map.
void setName(const std::string &name)
Sets the agent group name.
void reloadBehaviourModule(const std::string &name)
Reloads a behaviour module.
void filterAgent(unsigned int agentId)
Filter an agent.
void initFrame()
Init frame.
void postPhysics()
Post Physics.
bool multithread()
Gets the multithread state.
const std::unordered_map< std::string, std::vector< Agent * > > & agentTypeMapperRef() const
Gets the agent type.
void addLayoutGenerator(LayoutGeneratorPtr &layoutGen)
Sets the layout generator.
void setLayoutGenerator(LayoutGeneratorPtr &layoutGen)
Sets the layout generator.
const SimulationTime & simulationTime() const
Return the internal simulation time.
const std::unordered_map< std::string, std::vector< Agent * > > agentTypeMapper() const
Gets the agent type.
Cache & cache()
Get the cache object.
const std::unordered_map< int, bool > & agentFilter() const
Gets the agent filter map.
void clearAgents()
Clear agents.
const AtomsCore::Matrix & matrix() const
Gets the agent group matrix.
void endFrame()
End frame.
bool isAgentFilterInverted() const
Check if the agent filter is inverted.
void clearAgentFilter()
Clear the agent filter.
bool isActive() const
CHeck if the agent group is active.
void killAgents(const std::vector< unsigned int > &localIds)
Register the agents to be killed at the end of the compute pose.
void setSimulationName(const std::string &name)
Sets the simulation pool name.
void prePhysics()
Pre Physics.
~AgentGroup()
Destructor.
const std::vector< LayoutGeneratorPtr > & layoutGenerators()
Returns the layout generator.
const AtomsCore::MapMetadata & metadata() const
Get agent group metadata.
void addBehaviourModule(const std::string &name, const std::string &moduleTypeName)
Adds a behaviour module.
AgentGroup()
Constructor.
AtomsCore::MapMetadata & metadata()
Get agent group metadata.
void computePose()
Compute agent pose.
const std::vector< unsigned int > & agentsKilled() const
Get the agents goupdId that are killed at the current frame.
void clear()
Clears all data.
void runAgentsCreatedOnAgent(Atoms::Agent *agent, const std::string &breakAtModuleName)
Runs the agentsCreated module methods on the given agent.
size_t profileTime(unsigned short index) const
Get profile time.
const std::unordered_map< int, Agent * > & localAgents() const
Gets the local agents map.
const std::vector< unsigned int > & agentsCreated() const
Get the agents goupdId that are created at the current frame.
void removeAgent(Agent *agent)
Removes an agent from this agent group.
void setMultithread(bool value)
Sets the multithread state.
SimulationTime & simulationTime()
Return the internal simulation time.
LayoutGeneratorPtr getLayoutGenerator(const std::string &name) const
Returns the layout generator with the given name.
HeightFields & heightFields()
Return internal height fields.
void lock() const
Lock mutex.
const std::unordered_map< int, int > & agentsSelection() const
Gets the selected agents data.
void updateAgentTypeMapper()
Update the agent type mapper.
void resetSimulation()
Reset simulation.
AtomsCore::MapMetadata agentGroupData()
Gets all data in a MapMetadata.
const std::string & simulationName() const
Gets the simulation pool name.
AtomsCore::MapMetadata behaviourModuleData(const std::string &name)
Gets data of the behaviour module with the given name.
void clearCache()
Clear the sim cache.
NavigationMeshes & navigationMeshes()
Return internal navigation meshes.
void setMatrix(const AtomsCore::Matrix &matrix)
Sets the agent group matrix.
AtomsPtr< BehaviourModule > behaviourModule(const std::string &name)
Gets the behaviour module with the given name.
const std::vector< Agent * > & agents() const
Gets the group agents.
Agent * agent(unsigned int groupId)
Gets an agent using its local id.
void endSimulation()
End simulation.
const HeightFields & heightFields() const
Return internal height fields.
bool isCacheEnabled() const
Return the cache enable flag.
void initSimulation()
Initializes the simulation.
AtomsPtr< Atoms::AgentGroup > parent()
Gets the parent.
void setParent(AtomsPtr< Atoms::AgentGroup > &parent)
Sets the parent.
void removeBehaviourModule(const std::string &name)
Removes a behaviour module.
const Cache & cache() const
Get the cache object.
void setAgentsSelection(std::unordered_map< int, int > &selectionData)
Sets the selected agents data.
void filterAgents(const std::vector< int > &agentIds, bool invert=false)
Filter some agents.
const NavigationMeshes & navigationMeshes() const
Return internal navigation meshes.
void preFrame()
Pre frame.
void removeLayoutGenerators()
Remove all layout generators.
const std::string & name() const
Gets the agent group name.
void reset()
Delete everything.
void enablePositionsKdTreeCache(bool value)
Enables caching the internal kd tree so that it is not updated at every frame.
void unlock() const
Unlock mutex.
const std::vector< std::string > & behaviourModuleNames() const
Gets the behaviour module names.
void initializeNewAgents(std::vector< AgentInitData > &newAgentsData)
Initializes new agents with the given data.
void setActive(bool value)
Sets the active state.
void generateAgents(double time)
Events.
Agent.
Definition: Agent.h:44
Definition: AtomsPhysx.h:40
Container for all agent types.
Definition: HeightFields.h:19
Container for all agent types.
Definition: NavigationMeshes.h:20
Global simulation time.
Definition: SimulationTime.h:21
MapMetadata class.
Definition: MapMetadata.h:24
Definition: Hash.h:23
Definition: KdTreePoint.h:19
AtomsMath::Matrix Matrix
Matrix class.
Definition: AtomsMath.h:63
Atoms namespace.
Definition: Agent.h:29