Atoms Crowd  4.1.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 
26 namespace Atoms
27 {
28  class AtomsPhysx;
29 
31 
34  class ATOMS_EXPORT AgentGroup
35  {
36  public:
37 
38  enum ProfileType
39  {
40  kInitSimulation = 0,
41  kPreFrame,
42  kAgentsCreated,
43  kInitFrame,
44  kEndFrame,
45  kPrePhysics,
46  kPostPhysics,
47  kAgentsKilled,
48  kEndSimulation,
49  kResetSimulation,
50  kComputePose,
51  kGenerateAgents,
52  kClearAgents
53  };
54 
55 
56 
57  class ATOMS_EXPORT Cache
58  {
59  public:
60 
61  enum CacheState
62  {
63  Empty = 0,
64  Dirty,
65  Updated
66  };
67 
68  class Frame
69  {
70  public:
71  std::vector<Atoms::Agent*> agents;
72  std::vector<unsigned int> agentsCreated;
73  std::vector<unsigned int> agentsKilled;
74  AtomsCore::Matrix matrix;
75  std::unordered_map<std::string, std::vector<Agent*>> agentTypeMapper;
76  std::unordered_map<int, Agent*> localAgents;
77  std::map<size_t, AtomsPtr<Atoms::Agent>> agentsCached;
79  };
80 
81  Cache();
82 
83  ~Cache();
84 
85  void clear();
86 
87  void setCacheState(short value);
88 
89  short cacheState() const;
90 
91  std::vector<int> cacheFrames() const;
92 
93  public:
94 
95  std::map<long, Frame> frames;
96  //std::map<size_t, AtomsPtr<Atoms::Agent>> agents;
97  short m_cacheState;
98  double m_prevCacheFrame;
99  double m_oldSimtime;
100  int m_oldEnableCacheValue;
101  bool m_isCachePlaying;
102  bool m_saveFrame;
103  bool m_validFrame;
104  };
105 
108 
111 
113 
117  void addBehaviourModule(const std::string& name, const std::string& moduleTypeName);
118  void addBehaviourModule(const std::string& name, AtomsPtr<BehaviourModule>& module);
119 
121 
124  void removeBehaviourModule(const std::string& name);
125 
127 
131  void reorderBehaviourModule(const std::string& name, unsigned int newPosition);
132 
134 
137  const std::vector<std::string>& behaviourModuleNames() const;
138 
140 
144  AtomsPtr<BehaviourModule> behaviourModule(const std::string& name);
145 
147 
151 
153 
157  AtomsCore::MapMetadata behaviourModuleData(const std::string& name);
158 
160 
163  void reloadBehaviourModule(const std::string& name);
164 
166 
169  const std::vector<Agent*>& agents() const;
170 
172 
174  void removeAgent(Agent* agent);
175 
177 
180  const std::string& name() const;
181 
183  /*
184  \param name Name
185  */
186  void setName(const std::string& name);
187 
189 
192  const std::string& simulationName() const;
193 
195  /*
196  \param name Name
197  */
198  void setSimulationName(const std::string& name);
199 
201 
204  const AtomsCore::Matrix& matrix() const;
205 
207  /*
208  \param matrix Matrix
209  */
210  void setMatrix(const AtomsCore::Matrix& matrix);
211 
213  void initializeNewAgents(std::vector<AgentInitData>& newAgentsData);
214 
216  void runAgentsCreatedOnAgent(Atoms::Agent *agent, const std::string& breakAtModuleName);
217 
219 
221  void killAgents(std::vector<unsigned int>& localIds);
222 
224 
226  void generateAgents(double time);
227 
229 
234 
236 
238  void preFrame();
239 
241 
243  void initFrame();
244 
246 
248  void computePose();
249 
251 
253  void endFrame();
254 
256 
258  void prePhysics();
259 
261 
263  void postPhysics();
264 
266 
269 
271 
274 
276  void setLayoutGenerator(LayoutGeneratorPtr& layoutGen);
277 
279  void addLayoutGenerator(LayoutGeneratorPtr& layoutGen);
280 
282  const std::vector<LayoutGeneratorPtr>& layoutGenerators();
283 
285  LayoutGeneratorPtr getLayoutGenerator(const std::string& name) const;
286 
288 
290  void clear();
291 
293 
295  void clearAgents();
296 
298  void reset();
299 
301 
304  bool multithread();
305 
307 
310  void setMultithread(bool value);
311 
313 
316  const std::unordered_map<std::string, std::vector<Agent*>> agentTypeMapper() const;
317 
319 
322  const std::unordered_map<std::string, std::vector<Agent*>>& agentTypeMapperRef() const;
323 
326 
328 
330  const std::unordered_map<int, int>& agentsSelection() const;
331 
333 
336  void setAgentsSelection(std::unordered_map<int, int>& selectionData);
337 
339 
341  void setActive(bool value);
342 
344  bool isActive() const;
345 
347 
350  const std::unordered_map<int, Agent*>& localAgents() const;
351 
353  Agent* agent(unsigned int groupId);
354 
356 
359  void filterAgent(unsigned int agentId);
360 
362  void filterAgents(const std::vector<int>& agentIds, bool invert = false);
363 
365  bool isAgentFilterInverted() const;
366 
368  void setAgentFilterMap(const std::unordered_map<int, bool>& data, bool invert = false);
369 
371  const std::unordered_map<int, bool>& agentFilter() const;
372 
375 
377  AtomsPtr<Atoms::AgentGroup> parent();
378 
380  void setParent(AtomsPtr<Atoms::AgentGroup>& parent);
381 
383  const std::vector<unsigned int>& agentsCreated() const;
384 
386  const std::vector<unsigned int>& agentsKilled() const;
387 
388  void setCacheData(const std::vector<Agent*>& agents,
389  const std::vector<unsigned int>& agentsCreated,
390  const std::vector<unsigned int>& agentsKilled);
391 
394 
397 
400 
403 
406 
408  const HeightFields& heightFields() const;
409 
412 
415 
416  AtomsPhysx* physics();
417 
418  void setPhysics(AtomsPhysx* physic);
419 
421  size_t profileTime(unsigned short index) const;
422 
423  void setProfileTime(unsigned short index, size_t us);
424 
426  void lock() const;
427 
429  void unlock() const;
430 
432  void enableCache(bool value);
433 
435  bool isCacheEnabled() const;
436 
438  void clearCache();
439 
441  const Cache& cache() const;
442 
445 
446  void enablePositionsKdtree(bool value);
447 
449  void enablePositionsKdTreeCache(bool value);
450 
451  const AtomsUtils::KdTreePoint& positionsKdTree() const;
452 
453  void updatePositionsKdTree();
454 
455  private:
456 
458  Cache m_cache;
459 
461  AtomsUtils::KdTreePoint m_positionsKdtree;
462 
464  AtomsCore::MapMetadata m_metadata;
465 
467  std::map<std::string, AtomsPtr<BehaviourModule>> m_behaviourModules;
468 
470 
472  std::vector<std::string> m_moduleOrder;
473 
475  std::vector<Agent*> m_agents;
476 
478 
480  std::unordered_map<std::string, std::vector<Agent*>> m_agentTypeMapper;
481 
483  std::unordered_map<int, int> m_agentsSelection;
484 
486  std::unordered_map<int, Agent*> m_localAgents;
487 
489  std::unordered_map<int, bool> m_agentFilter;
490 
492  std::vector<unsigned int> m_agentsCreated;
493 
495  std::vector<unsigned int> m_agentsKilled;
496 
498  std::string m_name;
499 
501  std::string m_simulationName;
502 
504  AtomsCore::Matrix m_matrix;
505 
507  std::vector<LayoutGeneratorPtr> m_layoutGenerators;
508 
510  std::atomic<size_t> m_localIdCounter;
511 
513  AtomsPtr<AgentGroup> m_parent;
514 
516  SimulationTime m_simTime;
517 
519  HeightFields m_heightFields;
520 
522  NavigationMeshes m_navigationMeshes;
523 
525  AtomsPhysx* m_physx;
526 
528  OperatorComputeData m_computeData;
529 
531  size_t m_profileTime[14];
532 
534  mutable std::mutex m_mutex;
535 
537  bool m_active;
538 
540  bool m_multiThread;
541 
543  bool m_status;
544 
546  bool m_cacheEnabled;
547 
549  bool m_invertAgentFilter;
550 
552  bool m_enableKdTree;
553 
555  bool m_cacheKdTree;
556  };
557 }
558 
559 #include "AgentGroup.impl.h"
Atoms::AgentGroup::~AgentGroup
~AgentGroup()
Destructor.
Atoms::AgentGroup::agentTypeMapperRef
const std::unordered_map< std::string, std::vector< Agent * > > & agentTypeMapperRef() const
Gets the agent type.
Atoms::AgentGroup::Cache
Definition: AgentGroup.h:58
Atoms::AgentGroup::reloadBehaviourModule
void reloadBehaviourModule(const std::string &name)
Reloads a behaviour module.
Atoms::OperatorComputeData
Definition: OperatorComputeData.h:20
Atoms::AgentGroup::updateAgentTypeMapper
void updateAgentTypeMapper()
Update the agent type mapper.
Atoms::AgentGroup::postPhysics
void postPhysics()
Post Physics.
Atoms::AgentGroup::profileTime
size_t profileTime(unsigned short index) const
Get profile time.
Atoms::AgentGroup::killAgents
void killAgents(std::vector< unsigned int > &localIds)
Register the agents to be killed at the end of the compute pose.
Atoms::HeightFields
Container for all agent types.
Definition: HeightFields.h:19
Atoms::AgentGroup::initSimulation
void initSimulation()
Initializes the simulation.
Atoms::AgentGroup::setActive
void setActive(bool value)
Sets the active state.
Atoms::AgentGroup
Agent group.
Definition: AgentGroup.h:35
Atoms::AgentGroup::multithread
bool multithread()
Gets the multithread state.
Atoms::AgentGroup::reset
void reset()
Delete everything.
Atoms::AgentGroup::clearCache
void clearCache()
Clear the sim cache.
Atoms::AgentGroup::clearAgentFilter
void clearAgentFilter()
Clear the agent filter.
Atoms::AgentGroup::navigationMeshes
const NavigationMeshes & navigationMeshes() const
Return internal navigation meshes.
Atoms::AgentGroup::agentTypeMapper
const std::unordered_map< std::string, std::vector< Agent * > > agentTypeMapper() const
Gets the agent type.
Atoms::AgentGroup::clearAgents
void clearAgents()
Clear agents.
Atoms::AgentGroup::name
const std::string & name() const
Gets the agent group name.
Atoms::AgentGroup::addBehaviourModule
void addBehaviourModule(const std::string &name, const std::string &moduleTypeName)
Adds a behaviour module.
Atoms::NavigationMeshes
Container for all agent types.
Definition: NavigationMeshes.h:20
Atoms::AgentGroup::parent
AtomsPtr< Atoms::AgentGroup > parent()
Gets the parent.
Atoms::AgentGroup::enablePositionsKdTreeCache
void enablePositionsKdTreeCache(bool value)
Enables caching the internal kd tree so that it is not updated at every frame.
Atoms::AgentGroup::isCacheEnabled
bool isCacheEnabled() const
Return the cache enable flag.
Atoms::AgentGroup::endSimulation
void endSimulation()
End simulation.
Atoms::AgentGroup::removeAgent
void removeAgent(Agent *agent)
Removes an agent from this agent group.
Atoms::AgentGroup::lock
void lock() const
Lock mutex.
Atoms::SimulationTime
Global simulation time.
Definition: SimulationTime.h:21
Atoms::AgentGroup::generateAgents
void generateAgents(double time)
Events.
Atoms::Agent
Agent.
Definition: Agent.h:38
Atoms::AgentGroup::setAgentFilterMap
void setAgentFilterMap(const std::unordered_map< int, bool > &data, bool invert=false)
Sets the agent filter map.
Atoms::AgentGroup::agentsSelection
const std::unordered_map< int, int > & agentsSelection() const
Gets the selected agents data.
Atoms::AgentGroup::AgentGroup
AgentGroup()
Constructor.
Atoms::AgentGroup::preFrame
void preFrame()
Pre frame.
AtomsUtils::KdTreePoint
Definition: KdTreePoint.h:19
Atoms::AgentGroup::heightFields
HeightFields & heightFields()
Return internal height fields.
Atoms::AgentGroup::addLayoutGenerator
void addLayoutGenerator(LayoutGeneratorPtr &layoutGen)
Sets the layout generator.
AtomsCore::Matrix
AtomsMath::Matrix Matrix
Matrix class.
Definition: AtomsMath.h:63
Atoms::AgentGroup::cache
const Cache & cache() const
Get the cache object.
Atoms::AgentGroup::runAgentsCreatedOnAgent
void runAgentsCreatedOnAgent(Atoms::Agent *agent, const std::string &breakAtModuleName)
Runs the agentsCreated module methods on the given agent.
Atoms::AgentGroup::removeBehaviourModule
void removeBehaviourModule(const std::string &name)
Removes a behaviour module.
Atoms::AgentGroup::agent
Agent * agent(unsigned int groupId)
Gets an agent using its local id.
Atoms::AgentGroup::initializeNewAgents
void initializeNewAgents(std::vector< AgentInitData > &newAgentsData)
Initializes new agents with the given data.
Atoms::AgentGroup::simulationTime
const SimulationTime & simulationTime() const
Return the internal simulation time.
Atoms::AgentGroup::matrix
const AtomsCore::Matrix & matrix() const
Gets the agent group matrix.
Atoms::AgentGroup::getLayoutGenerator
LayoutGeneratorPtr getLayoutGenerator(const std::string &name) const
Returns the layout generator with the given name.
Atoms::AgentGroup::clear
void clear()
Clears all data.
Atoms::AgentGroup::unlock
void unlock() const
Unlock mutex.
Atoms::AgentGroup::setSimulationName
void setSimulationName(const std::string &name)
Sets the simulation pool name.
Atoms::AgentGroup::agentFilter
const std::unordered_map< int, bool > & agentFilter() const
Gets the agent filter map.
Atoms::AgentGroup::simulationName
const std::string & simulationName() const
Gets the simulation pool name.
Atoms::AgentGroup::endFrame
void endFrame()
End frame.
Atoms::AgentGroup::behaviourModuleData
AtomsCore::MapMetadata behaviourModuleData(const std::string &name)
Gets data of the behaviour module with the given name.
Atoms
Atoms namespace.
Definition: Agent.h:28
Atoms::AgentGroup::simulationTime
SimulationTime & simulationTime()
Return the internal simulation time.
Atoms::AgentGroup::reorderBehaviourModule
void reorderBehaviourModule(const std::string &name, unsigned int newPosition)
Moves a behaviour module to a different position in the behaviour list.
Atoms::AgentGroup::metadata
AtomsCore::MapMetadata & metadata()
Get agent group metadata.
Atoms::AgentGroup::behaviourModuleNames
const std::vector< std::string > & behaviourModuleNames() const
Gets the behaviour module names.
Atoms::AgentGroup::heightFields
const HeightFields & heightFields() const
Return internal height fields.
Atoms::AgentGroup::prePhysics
void prePhysics()
Pre Physics.
Atoms::AgentGroup::agentsKilled
const std::vector< unsigned int > & agentsKilled() const
Get the agents goupdId that are killed at the current frame.
Atoms::AgentGroup::metadata
const AtomsCore::MapMetadata & metadata() const
Get agent group metadata.
Atoms::AgentGroup::layoutGenerators
const std::vector< LayoutGeneratorPtr > & layoutGenerators()
Returns the layout generator.
Atoms::AgentGroup::setMatrix
void setMatrix(const AtomsCore::Matrix &matrix)
Sets the agent group matrix.
AtomsCore::MapMetadata
MapMetadata class.
Definition: MapMetadata.h:24
Atoms::AgentGroup::setName
void setName(const std::string &name)
Sets the agent group name.
Atoms::AgentGroup::localAgents
const std::unordered_map< int, Agent * > & localAgents() const
Gets the local agents map.
Atoms::AgentGroup::filterAgent
void filterAgent(unsigned int agentId)
Filter an agent.
Atoms::AtomsPhysx
Definition: AtomsPhysx.h:40
Atoms::AgentGroup::isAgentFilterInverted
bool isAgentFilterInverted() const
Check if the agent filter is inverted.
Atoms::AgentGroup::Cache::Frame
Definition: AgentGroup.h:69
Atoms::AgentGroup::setAgentsSelection
void setAgentsSelection(std::unordered_map< int, int > &selectionData)
Sets the selected agents data.
Atoms::AgentGroup::initFrame
void initFrame()
Init frame.
Atoms::AgentGroup::isActive
bool isActive() const
CHeck if the agent group is active.
Atoms::AgentGroup::navigationMeshes
NavigationMeshes & navigationMeshes()
Return internal navigation meshes.
Atoms::AgentGroup::behaviourModule
AtomsPtr< BehaviourModule > behaviourModule(const std::string &name)
Gets the behaviour module with the given name.
Atoms::AgentGroup::setLayoutGenerator
void setLayoutGenerator(LayoutGeneratorPtr &layoutGen)
Sets the layout generator.
Atoms::AgentGroup::agents
const std::vector< Agent * > & agents() const
Gets the group agents.
Atoms::AgentGroup::computePose
void computePose()
Compute agent pose.
Atoms::AgentGroup::agentsCreated
const std::vector< unsigned int > & agentsCreated() const
Get the agents goupdId that are created at the current frame.
Atoms::AgentGroup::setMultithread
void setMultithread(bool value)
Sets the multithread state.
Atoms::AgentGroup::agentGroupData
AtomsCore::MapMetadata agentGroupData()
Gets all data in a MapMetadata.
Atoms::AgentGroup::resetSimulation
void resetSimulation()
Reset simulation.
Atoms::AgentGroup::cache
Cache & cache()
Get the cache object.
Atoms::AgentGroup::enableCache
void enableCache(bool value)
Enable or disable the sim cache.
Atoms::AgentGroup::filterAgents
void filterAgents(const std::vector< int > &agentIds, bool invert=false)
Filter some agents.
Atoms::AgentGroup::setParent
void setParent(AtomsPtr< Atoms::AgentGroup > &parent)
Sets the parent.
AtomsCore::MurmurHash3
Definition: Hash.h:23