grid.h
Go to the documentation of this file.
1 #ifndef GRID_H
2 #define GRID_H
3 
4 #include <argos3/core/utility/datatypes/set.h>
5 #include <argos3/core/utility/math/range.h>
6 #include <argos3/core/utility/math/ray3.h>
7 #include <argos3/core/simulator/space/positional_indices/positional_index.h>
8 
9 namespace argos {
10 
11  template<class ENTITY>
12  class CGrid : public CPositionalIndex<ENTITY> {
13 
14  public:
15 
17 
18  struct SCell {
20  size_t Timestamp;
21 
22  SCell() : Timestamp(0) {}
23  inline void Reset() {
24  Entities.clear();
25  Timestamp = 0;
26  }
27  };
28 
30  public:
31  virtual ~CCellOperation() {}
32  virtual bool operator()(SInt32 n_i,
33  SInt32 n_j,
34  SInt32 n_k,
35  SCell& s_cell) = 0;
36  };
37 
38  public:
39 
40  CGrid(const CVector3& c_area_min_corner,
41  const CVector3& c_area_max_corner,
42  SInt32 n_size_i,
43  SInt32 n_size_j,
44  SInt32 n_size_k);
45 
46  virtual ~CGrid();
47 
48  virtual void Init(TConfigurationNode& t_tree);
49  virtual void Reset();
50  virtual void Destroy();
51 
52  virtual void AddEntity(ENTITY& c_entity);
53 
54  virtual void RemoveEntity(ENTITY& c_entity);
55 
56  virtual void Update();
57 
58  virtual void GetEntitiesAt(CSet<ENTITY*,SEntityComparator>& c_entities,
59  const CVector3& c_position) const;
60 
61  virtual void ForAllEntities(CEntityOperation& c_operation);
62 
63  virtual void ForEntitiesInSphereRange(const CVector3& c_center,
64  Real f_radius,
65  CEntityOperation& c_operation);
66 
67  virtual void ForEntitiesInBoxRange(const CVector3& c_center,
68  const CVector3& c_half_size,
69  CEntityOperation& c_operation);
70 
71  virtual void ForEntitiesInCircleRange(const CVector3& c_center,
72  Real f_radius,
73  CEntityOperation& c_operation);
74 
75  virtual void ForEntitiesInRectangleRange(const CVector3& c_center,
76  const CVector2& c_half_size,
77  CEntityOperation& c_operation);
78 
79  virtual void ForEntitiesAlongRay(const CRay3& c_ray,
80  CEntityOperation& c_operation,
81  bool b_stop_at_closest_match = false);
82 
83  virtual void ForAllCells(CCellOperation& c_operation);
84 
85  virtual void ForCellsInSphereRange(const CVector3& c_center,
86  Real f_radius,
87  CCellOperation& c_operation);
88 
89  virtual void ForCellsInBoxRange(const CVector3& c_center,
90  const CVector3& c_half_size,
91  CCellOperation& c_operation);
92 
93  virtual void ForCellsInCircleRange(const CVector3& c_center,
94  Real f_radius,
95  CCellOperation& c_operation);
96 
97  virtual void ForCellsInRectangleRange(const CVector3& c_center,
98  const CVector2& c_half_size,
99  CCellOperation& c_operation);
100 
101  virtual void ForCellsAlongRay(const CRay3& c_ray,
102  CCellOperation& c_operation);
103 
104  inline SInt32 GetSizeI() const {
105  return m_nSizeI;
106  }
107 
108  inline SInt32 GetSizeJ() const {
109  return m_nSizeJ;
110  }
111 
112  inline SInt32 GetSizeK() const {
113  return m_nSizeK;
114  }
115 
116  inline void SetUpdateEntityOperation(CEntityOperation* pc_operation);
117 
118  void UpdateCell(SInt32 n_i,
119  SInt32 n_j,
120  SInt32 n_k,
121  ENTITY& c_entity);
122 
123  inline void PositionToCell(SInt32& n_i,
124  SInt32& n_j,
125  SInt32& n_k,
126  const CVector3& c_position) const;
127 
128  inline void PositionToCellUnsafe(SInt32& n_i,
129  SInt32& n_j,
130  SInt32& n_k,
131  const CVector3& c_position) const;
132 
133  inline void ClampCoordinates(SInt32& n_i,
134  SInt32& n_j,
135  SInt32& n_k) const;
136 
137  inline void ClampCoordinates(CVector3& c_pos) const;
138 
139  inline SCell& GetCellAt(SInt32 n_i,
140  SInt32 n_j,
141  SInt32 n_k);
142 
143  inline const SCell& GetCellAt(SInt32 n_i,
144  SInt32 n_j,
145  SInt32 n_k) const;
146 
147  protected:
148 
163 
164  };
165 
166 }
167 
168 #include <argos3/core/simulator/space/positional_indices/grid_impl.h>
169 
170 #endif
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
The basic operation to be stored in the vtable.
Definition: entity.h:331
virtual void Reset()
Resets the resource.
Definition: grid_impl.h:91
SCell & GetCellAt(SInt32 n_i, SInt32 n_j, SInt32 n_k)
Definition: grid_impl.h:976
virtual void Update()
Updates this positional index.
Definition: grid_impl.h:130
CVector3 m_cCellSize
Definition: grid.h:157
virtual void ForCellsInBoxRange(const CVector3 &c_center, const CVector3 &c_half_size, CCellOperation &c_operation)
Definition: grid_impl.h:624
void UpdateCell(SInt32 n_i, SInt32 n_j, SInt32 n_k, ENTITY &c_entity)
Definition: grid_impl.h:884
void ClampCoordinates(SInt32 &n_i, SInt32 &n_j, SInt32 &n_k) const
Definition: grid_impl.h:948
SInt32 m_nSizeK
Definition: grid.h:153
SInt32 m_nSizeI
Definition: grid.h:151
SInt32 GetSizeJ() const
Definition: grid.h:108
virtual void ForEntitiesInRectangleRange(const CVector3 &c_center, const CVector2 &c_half_size, CEntityOperation &c_operation)
Executes an operation on all entities within the specified rectangle range.
Definition: grid_impl.h:321
virtual void Init(TConfigurationNode &t_tree)
Initializes the resource.
Definition: grid_impl.h:84
SCell * m_psCells
Definition: grid.h:159
CEntityOperation * m_pcUpdateEntityOperation
Definition: grid.h:162
virtual void Destroy()
Undoes whatever was done by Init().
Definition: grid_impl.h:107
size_t m_unCurTimestamp
Definition: grid.h:160
virtual void ForEntitiesInSphereRange(const CVector3 &c_center, Real f_radius, CEntityOperation &c_operation)
Executes an operation on all entities within the specified sphere range.
Definition: grid_impl.h:171
CVector3 m_cAreaMaxCorner
Definition: grid.h:150
CRange< Real > m_cRangeZ
Definition: grid.h:156
CSet< ENTITY *, SEntityComparator > m_cEntities
Definition: grid.h:161
virtual void ForEntitiesInCircleRange(const CVector3 &c_center, Real f_radius, CEntityOperation &c_operation)
Executes an operation on all entities within the specified circle range.
Definition: grid_impl.h:283
virtual void ForCellsAlongRay(const CRay3 &c_ray, CCellOperation &c_operation)
Definition: grid_impl.h:706
virtual void ForCellsInCircleRange(const CVector3 &c_center, Real f_radius, CCellOperation &c_operation)
Definition: grid_impl.h:647
virtual void AddEntity(ENTITY &c_entity)
Adds an entity to this index.
Definition: grid_impl.h:114
virtual ~CGrid()
Definition: grid_impl.h:76
void PositionToCellUnsafe(SInt32 &n_i, SInt32 &n_j, SInt32 &n_k, const CVector3 &c_position) const
Definition: grid_impl.h:935
virtual void ForEntitiesAlongRay(const CRay3 &c_ray, CEntityOperation &c_operation, bool b_stop_at_closest_match=false)
Executes an operation on all entities that intersect the given ray.
Definition: grid_impl.h:342
CRange< Real > m_cRangeX
Definition: grid.h:154
CVector3 m_cAreaMinCorner
Definition: grid.h:149
SInt32 GetSizeK() const
Definition: grid.h:112
CPositionalIndex< ENTITY >::COperation CEntityOperation
Definition: grid.h:16
void PositionToCell(SInt32 &n_i, SInt32 &n_j, SInt32 &n_k, const CVector3 &c_position) const
Definition: grid_impl.h:915
SInt32 GetSizeI() const
Definition: grid.h:104
virtual void ForEntitiesInBoxRange(const CVector3 &c_center, const CVector3 &c_half_size, CEntityOperation &c_operation)
Executes an operation on all entities within the specified box range.
Definition: grid_impl.h:260
CRange< Real > m_cRangeY
Definition: grid.h:155
void SetUpdateEntityOperation(CEntityOperation *pc_operation)
Definition: grid_impl.h:907
virtual void RemoveEntity(ENTITY &c_entity)
Removes an entity from this index.
Definition: grid_impl.h:122
virtual void ForAllEntities(CEntityOperation &c_operation)
Executes an operation on all the indexed entities.
Definition: grid_impl.h:161
virtual void ForCellsInSphereRange(const CVector3 &c_center, Real f_radius, CCellOperation &c_operation)
Definition: grid_impl.h:535
virtual void ForCellsInRectangleRange(const CVector3 &c_center, const CVector2 &c_half_size, CCellOperation &c_operation)
Definition: grid_impl.h:685
SInt32 m_nSizeJ
Definition: grid.h:152
virtual void ForAllCells(CCellOperation &c_operation)
Definition: grid_impl.h:521
CVector3 m_cInvCellSize
Definition: grid.h:158
virtual void GetEntitiesAt(CSet< ENTITY *, SEntityComparator > &c_entities, const CVector3 &c_position) const
Puts the entities located at the given point in the passed buffer.
Definition: grid_impl.h:139
CGrid(const CVector3 &c_area_min_corner, const CVector3 &c_area_max_corner, SInt32 n_size_i, SInt32 n_size_j, SInt32 n_size_k)
Definition: grid_impl.h:48
void Reset()
Definition: grid.h:23
size_t Timestamp
Definition: grid.h:20
CSet< ENTITY *, SEntityComparator > Entities
Definition: grid.h:19
virtual ~CCellOperation()
Definition: grid.h:31
virtual bool operator()(SInt32 n_i, SInt32 n_j, SInt32 n_k, SCell &s_cell)=0
A data structure that contains positional entities.
The operation to perform on each entity found in range.
Defines a very simple double-linked list that stores unique elements.
Definition: set.h:101
A 2D vector class.
Definition: vector2.h:27
A 3D vector class.
Definition: vector3.h:31