space_hash.h
Go to the documentation of this file.
1 
7 #ifndef SPACE_HASH_H
8 #define SPACE_HASH_H
9 
10 namespace argos {
11  class CSpace;
12  class CVector3;
13  class CRay3;
14 }
15 
16 #include <argos3/core/utility/math/ray3.h>
17 #include <argos3/core/utility/datatypes/set.h>
18 #include <argos3/core/simulator/space/positional_indices/positional_index.h>
19 
20 namespace argos {
21 
22  /****************************************/
23  /****************************************/
24 
33  template <class ENTITY>
34  class CAbstractSpaceHash : public CPositionalIndex<ENTITY> {
35 
36  public:
37 
42 
43  public:
44 
52  CAbstractSpaceHash() : m_unSize(0) {}
53 
57  virtual ~CAbstractSpaceHash() {}
58 
63  virtual void AddEntity(ENTITY& c_entity) {
64  m_tEntities.insert(&c_entity);
65  }
66 
71  inline TEntityList& GetEntities() {
72  return m_tEntities;
73  }
74 
79  virtual void RemoveEntity(ENTITY& c_entity) {
80  typename TEntityList::iterator it = m_tEntities.find(&c_entity);
81  if(it != m_tEntities.end()) {
82  m_tEntities.erase(it);
83  }
84  else {
85  THROW_ARGOSEXCEPTION("Entity not found when removing it from space hash.");
86  }
87  }
88 
94  inline size_t GetSize() {
95  return m_unSize;
96  }
97 
103  virtual void SetSize(size_t un_size) {
104  m_unSize = un_size;
105  }
106 
112  inline CVector3& GetCellSize() {
113  return m_cCellSize;
114  }
115 
123  return m_cInvCellSize;
124  }
125 
131  virtual void SetCellSize(const CVector3& c_cell_size) {
132  m_cCellSize = c_cell_size;
133  m_cInvCellSize.Set(1.0f / m_cCellSize.GetX(),
134  1.0f / m_cCellSize.GetY(),
135  1.0f / m_cCellSize.GetZ());
136  }
137 
143  virtual void Update() = 0;
144 
152  virtual void UpdateCell(SInt32 n_x,
153  SInt32 n_y,
154  SInt32 n_z,
155  ENTITY& c_entity) = 0;
156 
163  virtual SInt32 SpaceToHashTable(Real f_coord,
164  UInt32 un_axis) {
165  return RoundClosestToZero(f_coord * GetInvCellSize()[un_axis]);
166  }
167 
174  virtual Real HashTableToSpace(SInt32 n_coord,
175  UInt32 un_axis) {
176  return n_coord * m_cCellSize[un_axis];
177  }
178 
187  virtual void SpaceToHashTable(SInt32& n_i,
188  SInt32& n_j,
189  SInt32& n_k,
190  const CVector3& c_pos) {
194  }
195 
204  virtual bool CheckCell(SInt32 n_i,
205  SInt32 n_j,
206  SInt32 n_k,
207  TEntityList& t_entities) = 0;
208 
209  virtual void Dump(CARGoSLog& c_os) = 0;
210 
211  protected:
212 
221  SInt32 n_j,
222  SInt32 n_k) {
223  return
224  ((73856093u * n_i) ^
225  (19349663u * n_j) ^
226  (83492791u * n_k)) %
227  m_unSize;
228  }
229 
230  private:
231 
235  TEntityList m_tEntities;
236 
240  size_t m_unSize;
241 
246  CVector3 m_cCellSize;
247 
252  CVector3 m_cInvCellSize;
253 
254  };
255 
256  /****************************************/
257  /****************************************/
258 
268  template <class ENTITY>
270 
271  public:
272 
276  virtual ~CSpaceHashUpdater() {}
277 
283  virtual void operator()(CAbstractSpaceHash<ENTITY>& c_space_hash,
284  ENTITY& c_entity) = 0;
285 
286  };
287 
288  /****************************************/
289  /****************************************/
290 
299  template <class ENTITY, class UPDATER>
300  class CSpaceHash : public CAbstractSpaceHash<ENTITY> {
301 
302  public:
303 
309  virtual void Update() {
310  /* Go through all the entities */
313  m_cUpdater(*this, **el);
314  }
315  }
316 
317  private:
318 
319  UPDATER m_cUpdater;
320 
321  };
322 
323  /****************************************/
324  /****************************************/
325 
326 }
327 
328 #endif
virtual void Update()=0
Updates the entire space hash.
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
virtual void AddEntity(ENTITY &c_entity)
Adds an entity to the space hash.
Definition: space_hash.h:63
A 3D vector class.
Definition: vector3.h:29
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
Real GetX() const
Returns the x coordinate of this vector.
Definition: vector3.h:93
The abstract definition of a space hash.
Definition: space_hash.h:34
Real GetY() const
Returns the y coordinate of this vector.
Definition: vector3.h:109
A data structure that contains positional entities.
void insert(const T &t_element, C comp=C())
Inserts an element to the list.
Definition: set.h:236
virtual ~CAbstractSpaceHash()
Class destructor.
Definition: space_hash.h:57
void erase(const T &t_element)
Removes the passed element from the list.
Definition: set.h:288
iterator find(const T &t_element)
Searches for an element in the list.
Definition: set.h:405
CVector3 & GetInvCellSize()
Returns the inverse size of the cells of the space hash.
Definition: space_hash.h:122
size_t GetSize()
Returns the size of the space hash.
Definition: space_hash.h:94
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
void Set(const Real f_x, const Real f_y, const Real f_z)
Sets the vector contents from Cartesian coordinates.
Definition: vector3.h:143
virtual void SetSize(size_t un_size)
Sets the size of the space hash.
Definition: space_hash.h:103
virtual void SetCellSize(const CVector3 &c_cell_size)
Sets the size of the cells of the space hash.
Definition: space_hash.h:131
SInt32 RoundClosestToZero(Real f_value)
Rounds the passed floating-point value to the integer closest to zero.
Definition: general.h:172
CAbstractSpaceHash()
Class constructor.
Definition: space_hash.h:52
virtual Real HashTableToSpace(SInt32 n_coord, UInt32 un_axis)
Converts a single space hash cell coordinate into a space coordinate.
Definition: space_hash.h:174
virtual ~CSpaceHashUpdater()
Class destructor.
Definition: space_hash.h:276
virtual SInt32 SpaceToHashTable(Real f_coord, UInt32 un_axis)
Converts a single space coordinate into a space hash cell coordinate.
Definition: space_hash.h:163
UInt32 CoordinateHash(SInt32 n_i, SInt32 n_j, SInt32 n_k)
Calculates the hash of a space hash coordinate.
Definition: space_hash.h:220
virtual void RemoveEntity(ENTITY &c_entity)
Remove an entity from the space hash.
Definition: space_hash.h:79
TEntityList & GetEntities()
Returns the list of entities held by this space hash.
Definition: space_hash.h:71
The CSet iterator.
Definition: set.h:39
virtual void UpdateCell(SInt32 n_x, SInt32 n_y, SInt32 n_z, ENTITY &c_entity)=0
Adds an entity to a cell of the space hash.
Defines the basic cell updater of the space hash.
Definition: space_hash.h:269
virtual bool CheckCell(SInt32 n_i, SInt32 n_j, SInt32 n_k, TEntityList &t_entities)=0
Looks for entities to process in a cell.
Defines the basic space hash.
Definition: space_hash.h:300
virtual void Update()
Updates the entire space hash.
Definition: space_hash.h:309
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
CVector3 & GetCellSize()
Returns the size of the cells of the space hash.
Definition: space_hash.h:112
Real GetZ() const
Returns the z coordinate of this vector.
Definition: vector3.h:125
virtual void Dump(CARGoSLog &c_os)=0
virtual void SpaceToHashTable(SInt32 &n_i, SInt32 &n_j, SInt32 &n_k, const CVector3 &c_pos)
Converts a space position into a space hash cell The values are written into n_i, n_j...
Definition: space_hash.h:187
CSet< ENTITY * > TEntityList
Type definition for the list of entities held by the space hash.
Definition: space_hash.h:41
iterator end() const
Returns an invalid iterator.
Definition: set.h:397
virtual void operator()(CAbstractSpaceHash< ENTITY > &c_space_hash, ENTITY &c_entity)=0
Updates the necessary cells of a space hash.