TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::PointHashGrid< Value, PosFunc, EqualsFunc > Class Template Reference

A sparse 3D grid of point-based data. More...

#include <PointHashGrid.h>

Classes

class  BoxIterator
 
class  CellIterator
 
class  Entry
 
class  Iterator
 
class  SphereIterator
 

Public Member Functions

void getCellCoord (const Point3 &pos, Point3int32 &cellCoord) const
 Compute the grid cell index of a real position. This is used extensively internally by PointHashGrid. It is useful to calling code to determine when an object is about to move between cells. More...
 
 PointHashGrid (float radiusHint, const MemoryManager::Ref &m=MemoryManager::create())
 
float radiusHint () const
 
void clear (float radiusHint)
 
void clearAndSetRadiusHint (float radiusHint)
 
 PointHashGrid (const Array< Value > &init, float radiusHint=-1.0f, const MemoryManager::Ref &m=MemoryManager::create())
 
int size () const
 
const AABoxconservativeBoxBounds () const
 
void debugPrintStatistics () const
 
void insert (const Value &v)
 
void insert (const Array< Value > &v)
 
bool remove (const Value &v, bool shrinkIfNecessary=true)
 
void remove (const Array< Value > &v, bool shrink=true)
 
Iterator begin () const
 
const Iteratorend () const
 
BoxIterator beginBoxIntersection (const AABox &box, bool exact=true) const
 
const BoxIteratorendBoxIntersection () const
 
SphereIterator begin (const Sphere &sphere) const
 
SphereIterator G3D_DEPRECATED beginSphereIntersection (const Sphere &sphere) const
 
const SphereIteratorendSphereIntersection () const
 
void getIntersectingMembers (const Sphere &sphere, Array< Value > &result) const
 
CellIterator beginCells () const
 
const CellIteratorendCells () const
 
bool contains (const Value &v) const
 
void deleteAll ()
 
void clearAndSetMemoryManager (const MemoryManager::Ref &m)
 
void clear (bool shrink=true)
 
int debugGetDeepestBucketSize () const
 
float debugGetAverageBucketSize () const
 

Protected Member Functions

void initOffsetArray ()
 

Private Types

typedef SmallArray< Entry,
expectedCellSize
Cell
 
typedef Table< Point3int32, CellCellTable
 

Private Member Functions

 PointHashGrid (const ThisType &)
 
PointHashGridoperator= (const ThisType &)
 
bool find (const Value &v, Point3int32 &foundCellCoord, Cell *&foundCell, int &index)
 

Private Attributes

Vector3int32 m_offsetArray [3 *3 *3]
 
int m_epoch
 
float m_cellWidth
 
float m_invCellWidth
 
AABox m_bounds
 
int m_size
 
CellTable m_data
 
MemoryManager::Ref m_memoryManager
 

Detailed Description

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
class G3D::PointHashGrid< Value, PosFunc, EqualsFunc >

A sparse 3D grid of point-based data.

The space cost for n elements is O(n). For data with approximately uniform density (with respect to the radius hint), the time cost of searching for neighbors is O(1).

You can move members of the data set by first removing them and then adding them with a new location.

Template argument PosFunc must provide a static getPosition method and EqualsFunc must provide a static equals method, as described below. You can either write classes that support these yourself, provide template specializations of G3D::PositionTrait and G3D::EqualsTrait, or rely on the default template specializations, which already exist for common G3D classes like G3D::Point3. For example:

class PosFunc {
public:
static void getPosition(const Data& d, Vector3& pos) {
pos = d.location;
}
};
class EqualsFunc {
public:
static bool equals(const Data& p, const Data& q) {
return p == q;
}
};
PointHashGrid<Data, Data::PosFunc, Data::EqualsFunc> grid;

If the Value class defines operator==, then the Equalsfunc is optional, so you can just write:

PointHashGrid<Data, Data::PosFunc> grid;

The simplest way to define these is often to make them both methods of the parameter class itself, e.g.,

class Data {
public:
Point3 location;
...
bool operator==(const Data& other) const {
return (location == other.location) && ...;
}
static void getPosition(const Data& p, Vector3& pos) {
pos = p.location;
}
};
typedef PointHashGrid<Data, Data> DataGrid;

Member Typedef Documentation

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
typedef SmallArray<Entry, expectedCellSize> G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::Cell
private

One cell of the grid.

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
typedef Table<Point3int32, Cell > G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::CellTable
private

Constructor & Destructor Documentation

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::PointHashGrid ( const ThisType )
private

Intentionally unimplemented: prevent copy construction.

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::PointHashGrid ( float  radiusHint,
const MemoryManager::Ref m = MemoryManager::create() 
)
inline
Parameters
radiusHintthe radius that will typically be used with beginBallIntersection and beginBoxIntersection. If two Values are equal, their positions must be within this radius as well. You can later change this value with clearAndSetRadiusHint().
224  : m_size(0), m_memoryManager(m) {
225  initOffsetArray();
227 
228  debugAssertM(radiusHint > 0, "Cell radius must be positive");
230  m_invCellWidth = 1.0f / m_cellWidth;
231  }
MemoryManager::Ref m_memoryManager
Definition: PointHashGrid.h:136
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
void initOffsetArray()
Definition: PointHashGrid.h:195
int m_size
Definition: PointHashGrid.h:130
float m_invCellWidth
Definition: PointHashGrid.h:124
float m_cellWidth
Definition: PointHashGrid.h:121
CellTable m_data
Definition: PointHashGrid.h:134
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Table.h:309
float radiusHint() const
Definition: PointHashGrid.h:234
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::PointHashGrid ( const Array< Value > &  init,
float  radiusHint = -1.0f,
const MemoryManager::Ref m = MemoryManager::create() 
)
inline

If radiusHint is negative, it is automatically chosen to put about 5 values in each grid cell (which means about 27 * 5 values for each beginIntersection call).

See also
clearAndSetRadiusHint()
256  : m_size(0), m_memoryManager(m) {
257  initOffsetArray();
259 
260  Point3 lo(Vector3::inf());
261  Point3 hi(-lo);
262 
263  // Compute bounds
264  Array<Entry> entry(init.size());
265  for (int i = 0; i < entry.size(); ++i) {
266  const Value& value = init[i];
267  Point3 pos;
268 
269  entry[i].value = value;
270  entry[i].hashCode = m_hashFunc(value);
271  PosFunc::getPosition(value, entry[i].position);
272 
273  lo = lo.min(pos);
274  hi = hi.max(pos);
275  }
276 
277  m_bounds = AABox(lo, hi);
278 
279  if (radiusHint <= 0) {
280  // Compute a good cell width based on the bounds.
281  //
282  // N numPerCell
283  // ----- = ---------
284  // volume r^3
285 
286  float numPerCell = 5;
287  radiusHint =
288  (float)pow(numPerCell * m_bounds.volume() / init.size(), 1.0 / 3.0);
289 
290  if (radiusHint == 0) {
291  // Volume must have been zero because all points were colocated.
292  radiusHint = 0.1f;
293  }
294  }
295 
296  insert(init);
297  }
void insert(const Value &v)
Definition: PointHashGrid.h:319
MemoryManager::Ref m_memoryManager
Definition: PointHashGrid.h:136
void initOffsetArray()
Definition: PointHashGrid.h:195
int m_size
Definition: PointHashGrid.h:130
AABox m_bounds
Definition: PointHashGrid.h:127
CellTable m_data
Definition: PointHashGrid.h:134
G3D::Quat pow(const G3D::Quat &q, double x)
Definition: Quat.h:761
const FieldDescriptor value
Definition: descriptor.h:1522
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Table.h:309
static const Vector3 & inf()
Definition: Vector3.cpp:124
float radiusHint() const
Definition: PointHashGrid.h:234
Vector3 Point3
Definition: Vector3.h:820
float volume() const
Definition: AABox.h:264

Member Function Documentation

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
Iterator G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::begin ( ) const
inline

Iterate through all members. It is an error to mutate the HashGrid while iterating through it. Each member can be accessed by "dereferencing" the iterator:

for (Grid::Iterator i = grid.begin(); i != grid.end(), ++i) {
const Value& = *i;
...
}
512  {
513  return Iterator(this);
514  }

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
SphereIterator G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::begin ( const Sphere sphere) const
inline

Finds all values whose positions are within sphere. It is an error to mutate the PointHashGrid while iterating through it.

821  {
822  return SphereIterator(this, sphere);
823  }
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
BoxIterator G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::beginBoxIntersection ( const AABox box,
bool  exact = true 
) const
inline

Finds all values whose positions are within box. It is an error to mutate the PointHashGrid while iterating through it.

Parameters
exactIf false, the iterator will execute more quickly but will likely return some values that lie outside the box. Set exact = false if you are going to test the results against the yourself box anyway.
704  {
705  return BoxIterator(this, exact, box);
706  }
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
CellIterator G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::beginCells ( ) const
inline

Iterates through the non-empty cells. This is intended primarily for debugging and visualizing the data structure.

972  {
973  return CellIterator(this);
974  }

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
SphereIterator G3D_DEPRECATED G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::beginSphereIntersection ( const Sphere sphere) const
inline
Deprecated:
See also
beginIntersection
827  {
828  return SphereIterator(this, sphere);
829  }

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::clear ( float  radiusHint)
inline
238  {
239  debugAssertM(radiusHint > 0, "Cell radius must be positive");
240  clear();
242  m_invCellWidth = 1.0f / m_cellWidth;
243  }
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
float m_invCellWidth
Definition: PointHashGrid.h:124
float m_cellWidth
Definition: PointHashGrid.h:121
float radiusHint() const
Definition: PointHashGrid.h:234
void clear(float radiusHint)
Definition: PointHashGrid.h:238

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::clear ( bool  shrink = true)
inline

Removes all data.

Parameters
shrinkIf true, underlying structures are deallocated as they are freed.
1020  {
1021  m_size = 0;
1022  m_bounds = AABox();
1023  if (! shrink) {
1024  // Remove all data
1025  for (CellIterator it = beginCells(); it.isValid(); ++it) {
1026  it.cell().clear(true);
1027  }
1028  } else {
1029  m_data.clear();
1030  }
1031  ++m_epoch;
1032  }
void clear()
Definition: Table.h:578
int m_size
Definition: PointHashGrid.h:130
int m_epoch
Definition: PointHashGrid.h:118
AABox m_bounds
Definition: PointHashGrid.h:127
CellTable m_data
Definition: PointHashGrid.h:134
CellIterator beginCells() const
Definition: PointHashGrid.h:972
bool isValid() const
Definition: PointHashGrid.h:965
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::clearAndSetMemoryManager ( const MemoryManager::Ref m)
inline
1008  {
1009  ++m_epoch;
1010  m_size = 0;
1011  m_bounds = AABox();
1012 
1014  m_memoryManager = m;
1015  }
MemoryManager::Ref m_memoryManager
Definition: PointHashGrid.h:136
int m_size
Definition: PointHashGrid.h:130
int m_epoch
Definition: PointHashGrid.h:118
AABox m_bounds
Definition: PointHashGrid.h:127
CellTable m_data
Definition: PointHashGrid.h:134
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Table.h:309
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::clearAndSetRadiusHint ( float  radiusHint)
inline
245  {
246  return clear(radiusHint);
247  }
float radiusHint() const
Definition: PointHashGrid.h:234
void clear(float radiusHint)
Definition: PointHashGrid.h:238
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
const AABox& G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::conservativeBoxBounds ( ) const
inline

Returns a conservative bounding box around the contents. This is conservative because it is not updated when elements are removed.

306  {
307  return m_bounds;
308  }
AABox m_bounds
Definition: PointHashGrid.h:127
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
bool G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::contains ( const Value v) const
inline

Returns true if there is a value that is exactly equal to v. This will check all neighboring cells to avoid roundoff error at cell boundaries.

987  {
988  Cell* cell = NULL;
989  int index = 0;
990  Vector3int32 cellCoord;
991  return const_cast<ThisType*>(this)->find(v, cellCoord, cell, index);
992  }
Vector3int32
Definition: Vector3int32.h:29
arena_t NULL
Definition: jemalloc_internal.h:624
bool find(const Value &v, Point3int32 &foundCellCoord, Cell *&foundCell, int &index)
Definition: PointHashGrid.h:149
Definition: Cell.h:49
#define ThisType
Definition: PointHashGrid.h:99
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
float G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::debugGetAverageBucketSize ( ) const
inline
1038  {
1040  }
float debugGetAverageBucketSize() const
Definition: Table.h:382
CellTable m_data
Definition: PointHashGrid.h:134
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
int G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::debugGetDeepestBucketSize ( ) const
inline
1034  {
1035  return (int)m_data.debugGetDeepestBucketSize();
1036  }
size_t debugGetDeepestBucketSize() const
Definition: Table.h:360
CellTable m_data
Definition: PointHashGrid.h:134
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::debugPrintStatistics ( ) const
inline
310  {
311  debugPrintf("Deepest bucket size = %d\n", (int)m_data.debugGetDeepestBucketSize());
312  debugPrintf("Average bucket size = %g\n", m_data.debugGetAverageBucketSize());
313  debugPrintf("Load factor = %g\n", m_data.debugGetLoad());
314  }
std::string __cdecl debugPrintf(const char *fmt...) G3D_CHECK_PRINTF_ARGS
Definition: debugAssert.cpp:355
float debugGetAverageBucketSize() const
Definition: Table.h:382
double debugGetLoad() const
Definition: Table.h:402
size_t debugGetDeepestBucketSize() const
Definition: Table.h:360
CellTable m_data
Definition: PointHashGrid.h:134
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::deleteAll ( )
inline

Calls delete on all of the values, which are assumed to be pointers. This is a helper to avoid requiring you to iterate through the data structure, removing and deleting each one. Clears the PointHashGrid at the end.

Using objects (instead of pointers) or reference counted pointers is recommended over using pointers and this deleteAll method.

1001  {
1002  for (Iterator it = begin(); it.isValid(); ++it) {
1003  delete *it;
1004  }
1005  clear();
1006  }
bool isValid() const
Definition: PointHashGrid.h:453
Iterator begin() const
Definition: PointHashGrid.h:512
void clear(float radiusHint)
Definition: PointHashGrid.h:238
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
const Iterator& G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::end ( ) const
inline
516  {
517  static const Iterator it;
518  return it;
519  }
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
const BoxIterator& G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::endBoxIntersection ( ) const
inline
708  {
709  static const BoxIterator it;
710  return it;
711  }
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
const CellIterator& G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::endCells ( ) const
inline
976  {
977  static const CellIterator it;
978  return it;
979  }
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
const SphereIterator& G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::endSphereIntersection ( ) const
inline
Deprecated:
See also
SphereIterator::hasMore
833  {
834  static const SphereIterator it;
835  return it;
836  }
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
bool G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::find ( const Value v,
Point3int32 foundCellCoord,
Cell *&  foundCell,
int &  index 
)
inlineprivate

Locate the cell and index within that cell containing v. Called by remove() and contains().

152  {
153 
154  Point3 pos;
155  PosFunc::getPosition(v, pos);
156 
157  Point3int32 cellCoord;
158  getCellCoord(pos, cellCoord);
159  for (int i = 0; i < 27; ++i) {
160  Point3int32 c = cellCoord + m_offsetArray[i];
161  Cell* cell = m_data.getPointer(c);
162  if (cell != NULL) {
163  // The cell exists
164  for (int j = 0; j < cell->size(); ++j) {
165  if (EqualsFunc::equals((*cell)[j].value, v)) {
166  foundCell = cell;
167  index = j;
168  foundCellCoord = c;
169  return true;
170  }
171  }
172  }
173  }
174 
175  // Not found
176  return false;
177  }
Value * getPointer(const Key &key) const
Definition: Table.h:731
Vector3int32 m_offsetArray[3 *3 *3]
Definition: PointHashGrid.h:113
void getCellCoord(const Point3 &pos, Point3int32 &cellCoord) const
Compute the grid cell index of a real position. This is used extensively internally by PointHashGrid...
Definition: PointHashGrid.h:186
arena_t NULL
Definition: jemalloc_internal.h:624
Vector3int32 Point3int32
Definition: Vector3int32.h:176
CellTable m_data
Definition: PointHashGrid.h:134
Definition: Cell.h:49
const FieldDescriptor value
Definition: descriptor.h:1522
Vector3 Point3
Definition: Vector3.h:820

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::getCellCoord ( const Point3 pos,
Point3int32 cellCoord 
) const
inline

Compute the grid cell index of a real position. This is used extensively internally by PointHashGrid. It is useful to calling code to determine when an object is about to move between cells.

186  {
187  for (int a = 0; a < 3; ++a) {
188  cellCoord[a] = iFloor(pos[a] * m_invCellWidth);
189  }
190  }
int iFloor(double fValue)
Definition: g3dmath.h:603
float m_invCellWidth
Definition: PointHashGrid.h:124

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::getIntersectingMembers ( const Sphere sphere,
Array< Value > &  result 
) const
inline

Appends results

840  {
841  for (SphereIterator it = beginSphereIntersection(sphere); it.isValid(); ++it) {
842  result.append(*it);
843  }
844  }
bool isValid() const
Definition: PointHashGrid.h:813
SphereIterator G3D_DEPRECATED beginSphereIntersection(const Sphere &sphere) const
Definition: PointHashGrid.h:827
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::initOffsetArray ( )
inlineprotected

Initializes m_offsetArray.

195  {
196  int i = 0;
197  Point3int32 d;
198  for (d.x = -1; d.x <= +1; ++d.x) {
199  for (d.y = -1; d.y <= +1; ++d.y) {
200  for (d.z = -1; d.z <= +1; ++d.z) {
201  m_offsetArray[i] = d;
202  ++i;
203  }
204  }
205  }
206 
207  // Put (0, 0, 0) first, so that contains() is most likely to find
208  // the value quickly.
209  i = (1 * 3 + 1) * 3 + 1;
210  debugAssert(m_offsetArray[i] == Vector3int32(0,0,0));
211  Vector3int32 temp = m_offsetArray[0];
213  m_offsetArray[i] = temp;
214  }
Vector3int32
Definition: Vector3int32.h:29
Vector3int32 m_offsetArray[3 *3 *3]
Definition: PointHashGrid.h:113
Vector3int32 Point3int32
Definition: Vector3int32.h:176
#define debugAssert(exp)
Definition: debugAssert.h:160

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::insert ( const Value v)
inline

Insert v at position p given by getPosition(v, p). Multiple elements that are equal may be inserted; all copies will be in the data structure.

319  {
320  Point3 pos;
321  PosFunc::getPosition(v, pos);
322  Point3int32 cellCoord;
323  getCellCoord(pos, cellCoord);
324 
325  // See if the cell already exists
326  Cell& cell = m_data.getCreate(cellCoord);
327 
328  if (cell.size() == 0) {
329  // Use the same memory manager as for the whole class
330  cell.clearAndSetMemoryManager(m_memoryManager);
331  }
332 
333  Entry& entry = cell.next();
334  entry.value = v;
335  entry.position = pos;
336 
337  // Update the bounds
338  if (size() == 0) {
339  m_bounds = AABox(pos);
340  } else {
341  m_bounds.merge(pos);
342  }
343 
344  ++m_size;
345  ++m_epoch;
346  }
void merge(const AABox &a)
Definition: AABox.h:106
void getCellCoord(const Point3 &pos, Point3int32 &cellCoord) const
Compute the grid cell index of a real position. This is used extensively internally by PointHashGrid...
Definition: PointHashGrid.h:186
Vector3int32 Point3int32
Definition: Vector3int32.h:176
MemoryManager::Ref m_memoryManager
Definition: PointHashGrid.h:136
int size() const
Definition: PointHashGrid.h:300
int m_size
Definition: PointHashGrid.h:130
Entry
Definition: boss_headless_horseman.cpp:50
int m_epoch
Definition: PointHashGrid.h:118
AABox m_bounds
Definition: PointHashGrid.h:127
CellTable m_data
Definition: PointHashGrid.h:134
Value & getCreate(const Key &key)
Definition: Table.h:861
Definition: Cell.h:49
Vector3 Point3
Definition: Vector3.h:820

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::insert ( const Array< Value > &  v)
inline

Inserts all elements of the array.

350  {
351  for (int i = 0; i < v.size(); ++i) {
352  insert(v[i]);
353  }
354  }
void insert(const Value &v)
Definition: PointHashGrid.h:319
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
PointHashGrid& G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::operator= ( const ThisType )
private

Intentionally unimplemented: prevent assignment.

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
float G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::radiusHint ( ) const
inline
See also
clearAndSetRadiusHint()
234  {
235  return m_cellWidth;
236  }
float m_cellWidth
Definition: PointHashGrid.h:121

+ Here is the caller graph for this function:

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
bool G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::remove ( const Value v,
bool  shrinkIfNecessary = true 
)
inline

If there are multiple copies of an element, you must delete them multiple times.

Parameters
shrinkIfNecessaryIf true, deallocate underlying data structures as they are emptied. False increases performace at the cost of memory overhead for dynamic structures.
Returns
true if the element was found.
366  {
367  Cell* cell = NULL;
368  int index = 0;
369  Vector3int32 cellCoord;
370 
371  if (find(v, cellCoord, cell, index)) {
372  cell->fastRemove(index, shrinkIfNecessary);
373  --m_size;
374  ++m_epoch;
375 
376  if ((cell->size() == 0) && shrinkIfNecessary) {
377  // Remove the cell itself
378 
379  // Drop our pointer, which is about to dangle
380  cell = NULL;
381  const bool success = m_data.remove(cellCoord);
382  (void)success;
383  debugAssertM(success, "Data structure corrupt: "
384  "tried to remove a cell that doesn't exist.");
385  }
386 
387  return true;
388 
389  } else {
390  return false;
391  }
392  }
Vector3int32
Definition: Vector3int32.h:29
arena_t NULL
Definition: jemalloc_internal.h:624
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
bool find(const Value &v, Point3int32 &foundCellCoord, Cell *&foundCell, int &index)
Definition: PointHashGrid.h:149
int m_size
Definition: PointHashGrid.h:130
int m_epoch
Definition: PointHashGrid.h:118
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
CellTable m_data
Definition: PointHashGrid.h:134
Definition: Cell.h:49
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
void G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::remove ( const Array< Value > &  v,
bool  shrink = true 
)
inline

Removes all elements of v.

395  {
396  for (int i = 0; i < v.size(); ++i) {
397  remove(v[i], shrink);
398  }
399  }
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
int G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::size ( ) const
inline

Returns the number of elements.

300  {
301  return m_size;
302  }
int m_size
Definition: PointHashGrid.h:130

+ Here is the caller graph for this function:

Member Data Documentation

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
AABox G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::m_bounds
private

Conservative bounds; the actual data may be smaller.

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
float G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::m_cellWidth
private

Extent of a cell along one dimension.

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
CellTable G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::m_data
private

Non-empty cells indexed by grid position. Actual 3D position is position * m_cellWidth

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
int G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::m_epoch
private

Incremented every time the data structure is mutated. Used by the iterators to determine if the data structure has changed since iteration began.

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
float G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::m_invCellWidth
private

1.0 / cell width

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
MemoryManager::Ref G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::m_memoryManager
private
template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
Vector3int32 G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::m_offsetArray[3 *3 *3]
private

The cube of +/-1 along each dimension. Initialized by initOffsetArray.

template<class Value, class PosFunc = PositionTrait<Value>, class EqualsFunc = EqualsTrait<Value>>
int G3D::PointHashGrid< Value, PosFunc, EqualsFunc >::m_size
private

Number of elements.


The documentation for this class was generated from the following file: