11 #ifndef G3D_PointHashGrid_h
12 #define G3D_PointHashGrid_h
97 # define expectedCellSize (10)
99 # define ThisType PointHashGrid<Value, PosFunc, EqualsFunc>
155 PosFunc::getPosition(v, pos);
159 for (
int i = 0; i < 27; ++i) {
164 for (
int j = 0; j < cell->
size(); ++j) {
165 if (EqualsFunc::equals((*cell)[j].
value, v)) {
187 for (
int a = 0; a < 3; ++a) {
188 cellCoord[a] =
iFloor(pos[a] * m_invCellWidth);
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;
209 i = (1 * 3 + 1) * 3 + 1;
212 m_offsetArray[0] = m_offsetArray[i];
213 m_offsetArray[i] = temp;
228 debugAssertM(radiusHint > 0,
"Cell radius must be positive");
239 debugAssertM(radiusHint > 0,
"Cell radius must be positive");
246 return clear(radiusHint);
265 for (
int i = 0; i < entry.size(); ++i) {
269 entry[i].value =
value;
270 entry[i].hashCode = m_hashFunc(value);
271 PosFunc::getPosition(value, entry[i].position);
277 m_bounds =
AABox(lo, hi);
279 if (radiusHint <= 0) {
286 float numPerCell = 5;
288 (float)
pow(numPerCell * m_bounds.
volume() / init.
size(), 1.0 / 3.0);
290 if (radiusHint == 0) {
321 PosFunc::getPosition(v, pos);
326 Cell& cell = m_data.
getCreate(cellCoord);
328 if (cell.
size() == 0) {
335 entry.position = pos;
339 m_bounds =
AABox(pos);
351 for (
int i = 0; i < v.
size(); ++i) {
366 bool remove(
const Value& v,
bool shrinkIfNecessary =
true) {
371 if (
find(v, cellCoord, cell, index)) {
376 if ((cell->
size() == 0) && shrinkIfNecessary) {
381 const bool success = m_data.
remove(cellCoord);
384 "tried to remove a cell that doesn't exist.");
396 for (
int i = 0; i < v.size(); ++i) {
397 remove(v[i], shrink);
423 m_arrayIndex(0), m_epoch(0) {}
426 m_isEnd(grid->
size() == 0),
428 m_tableIterator( grid->m_data.
begin() ),
430 m_epoch(grid->m_epoch) { }
444 if (other.
m_isEnd && m_isEnd) {
447 return (m_isEnd != other.
m_isEnd) ||
463 return !(*
this != other);
470 "It is illegal to mutate the HashGrid "
471 "while iterating through it.");
475 if (m_arrayIndex >= m_tableIterator->value.size()) {
481 m_isEnd = (m_tableIterator == m_grid->m_data.end());
513 return Iterator(
this);
516 const Iterator&
end()
const {
517 static const Iterator it;
525 class SphereIterator;
565 if (m_current.x > m_hi.x) {
566 m_current.x = m_lo.x;
568 if (m_current.y > m_hi.y) {
569 m_current.y = m_lo.y;
571 if (m_current.z > m_hi.z) {
579 m_cell = m_grid->m_data.getPointer(m_current);
581 }
while ((m_cell ==
NULL) || (m_cell->
size() == 0));
590 bool inConstructor = (m_cell ==
NULL);
591 if (inConstructor || m_arrayIndex >= m_cell->
size()) {
621 m_epoch(grid->m_epoch) {
623 m_grid->getCellCoord(box.
low(),
m_lo);
624 m_grid->getCellCoord(box.
high(),
m_hi);
650 if (other.
m_isEnd && m_isEnd) {
653 return (m_isEnd != other.
m_isEnd) ||
654 (m_cell != other.
m_cell) ||
660 return !(*
this != other);
667 "It is illegal to mutate the HashGrid "
668 "while iterating through it.");
705 return BoxIterator(
this, exact, box);
709 static const BoxIterator it;
728 if (! m_boxIterator.
isValid()) {
736 if (! m_boxIterator.
isValid()) {
773 if (other.
m_isEnd && m_isEnd) {
784 return !(*
this != other);
822 return SphereIterator(
this, sphere);
828 return SphereIterator(
this, sphere);
834 static const SphereIterator it;
872 return m_tableIterator->value;
889 return AABox(
Vector3(k) * m_parent->m_grid->m_cellWidth,
896 return m_parent->m_tableIterator->value.
size();
910 m_tableIterator( CellTable().
end() ),
916 m_tableIterator( grid->m_data.
begin()),
917 m_epoch(grid->m_epoch) {
919 m_isEnd = ! m_tableIterator.
isValid();
940 return !(*
this != other);
946 "It is illegal to mutate the HashGrid while "
947 "iterating through it.");
949 m_isEnd = ! m_tableIterator.isValid();
973 return CellIterator(
this);
977 static const CellIterator it;
991 return const_cast<ThisType*
>(
this)->
find(v, cellCoord, cell, index);
1014 m_memoryManager = m;
1026 it.cell().clear(
true);
CellTable::Iterator m_tableIterator
Definition: PointHashGrid.h:412
bool operator!=(const Iterator &other) const
Definition: PointHashGrid.h:443
std::string __cdecl debugPrintf(const char *fmt...) G3D_CHECK_PRINTF_ARGS
Definition: debugAssert.cpp:355
void fastRemove(int i, bool shrinkIfNecessary=false)
Definition: SmallArray.h:115
CellObject m_indirection
Definition: PointHashGrid.h:902
Vector3int32
Definition: Vector3int32.h:29
bool operator==(const CellIterator &other) const
Definition: PointHashGrid.h:939
AABox m_box
Definition: PointHashGrid.h:545
Value * getPointer(const Key &key) const
Definition: Table.h:731
BoxIterator beginBoxIntersection(const AABox &box, bool exact=true) const
Definition: PointHashGrid.h:704
const Iterator & end() const
Definition: PointHashGrid.h:516
void merge(const AABox &a)
Definition: AABox.h:106
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
float debugGetAverageBucketSize() const
Definition: Table.h:382
void insert(const Value &v)
Definition: PointHashGrid.h:319
friend class ThisType
Definition: PointHashGrid.h:406
const BoxIterator & endBoxIntersection() const
Definition: PointHashGrid.h:708
static MemoryManager::Ref create()
Definition: MemoryManager.cpp:35
Cell * m_cell
Definition: PointHashGrid.h:553
const int m_epoch
Definition: PointHashGrid.h:868
AABox bounds() const
Definition: PointHashGrid.h:887
Definition: SmallArray.h:23
Definition: PointHashGrid.h:716
void clearAndSetRadiusHint(float radiusHint)
Definition: PointHashGrid.h:245
A sparse 3D grid of point-based data.
Definition: PointHashGrid.h:94
bool operator!=(const BoxIterator &other) const
Definition: PointHashGrid.h:649
bool contains(const AABox &other) const
Definition: AABox.h:238
Iterator & operator++()
Definition: PointHashGrid.h:467
Vector3 __fastcall max(const Vector3 &v) const
Definition: Vector3.h:794
const Value & operator*() const
Definition: PointHashGrid.h:682
void clear()
Definition: Table.h:578
Vector3int32 m_current
Definition: PointHashGrid.h:550
const CellObject & operator*() const
Definition: PointHashGrid.h:927
bool m_isEnd
Definition: PointHashGrid.h:721
const CellIterator & endCells() const
Definition: PointHashGrid.h:976
Definition: PositionTrait.h:5
Dynamic 1D array tuned for performance.
Definition: Array.h:95
double debugGetLoad() const
Definition: Table.h:402
CellIterator()
Definition: PointHashGrid.h:907
BoxIterator & operator++()
Definition: PointHashGrid.h:664
const Value * operator->() const
Definition: PointHashGrid.h:683
arena_t NULL
Definition: jemalloc_internal.h:624
const ThisType * m_grid
Definition: PointHashGrid.h:533
#define false
Definition: CascPort.h:18
int iFloor(double fValue)
Definition: g3dmath.h:603
Vector3int32 Point3int32
Definition: Vector3int32.h:176
const int m_epoch
Definition: PointHashGrid.h:417
void debugPrintStatistics() const
Definition: PointHashGrid.h:310
T & next()
Definition: SmallArray.h:153
static AABox getBoundingBox(const Sphere &s)
Definition: PointHashGrid.h:743
void advanceCell()
Definition: PointHashGrid.h:562
shared_ptr< class MemoryManager > Ref
Definition: MemoryManager.h:31
bool hasMore() const
Definition: PointHashGrid.h:961
const Point3 & low() const
Definition: AABox.h:136
void getBounds(class AABox &out) const
Definition: Sphere.cpp:252
Iterator()
Definition: PointHashGrid.h:422
bool m_isEnd
Definition: PointHashGrid.h:865
Cell & cell()
Definition: PointHashGrid.h:871
MemoryManager::Ref m_memoryManager
Definition: PointHashGrid.h:136
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: PointHashGrid.h:1008
int size() const
Definition: PointHashGrid.h:300
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
Definition: PointHashGrid.h:526
void initOffsetArray()
Definition: PointHashGrid.h:195
Iterator(const ThisType *grid)
Definition: PointHashGrid.h:425
bool operator!=(const SphereIterator &other) const
Definition: PointHashGrid.h:772
Vector3 __fastcall min(const Vector3 &v) const
Definition: Vector3.h:789
int m_arrayIndex
Definition: PointHashGrid.h:415
SphereIterator(const ThisType *grid, const Sphere &sphere)
Definition: PointHashGrid.h:749
Definition: PointHashGrid.h:102
Table< Point3int32, Cell > CellTable
Definition: PointHashGrid.h:110
Sphere m_sphere
Definition: PointHashGrid.h:722
#define true
Definition: CascPort.h:17
bool m_exact
Definition: PointHashGrid.h:542
bool m_isEnd
Definition: PointHashGrid.h:531
bool find(const Value &v, Point3int32 &foundCellCoord, Cell *&foundCell, int &index)
Definition: PointHashGrid.h:149
const Value & value() const
Definition: PointHashGrid.h:759
const Value & value() const
Definition: PointHashGrid.h:434
bool isValid() const
Definition: PointHashGrid.h:813
Value value
Definition: PointHashGrid.h:105
void deleteAll()
Definition: PointHashGrid.h:1001
const ThisType * m_grid
Definition: PointHashGrid.h:866
int m_size
Definition: PointHashGrid.h:130
void clearAndSetMemoryManager(MemoryManager::Ref &m)
Definition: SmallArray.h:50
Entry
Definition: boss_headless_horseman.cpp:50
int m_epoch
Definition: PointHashGrid.h:118
Definition: PointHashGrid.h:877
const CellIterator * m_parent
Definition: PointHashGrid.h:880
PointHashGrid(const Array< Value > &init, float radiusHint=-1.0f, const MemoryManager::Ref &m=MemoryManager::create())
Definition: PointHashGrid.h:256
const Value * operator->() const
Definition: PointHashGrid.h:805
AABox m_bounds
Definition: PointHashGrid.h:127
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:1706
PointHashGrid & operator=(const ThisType &)
SphereIterator & operator=(const SphereIterator &)
bool operator==(const Iterator &other) const
Definition: PointHashGrid.h:462
size_t debugGetDeepestBucketSize() const
Definition: Table.h:360
bool operator==(const SphereIterator &other) const
Definition: PointHashGrid.h:783
#define debugAssert(exp)
Definition: debugAssert.h:160
void advance()
Definition: PointHashGrid.h:585
CellIterator & operator++()
Definition: PointHashGrid.h:944
CellIterator operator++(int)
Definition: PointHashGrid.h:954
float m_invCellWidth
Definition: PointHashGrid.h:124
int debugGetDeepestBucketSize() const
Definition: PointHashGrid.h:1034
float m_cellWidth
Definition: PointHashGrid.h:121
CellTable::Iterator m_tableIterator
Definition: PointHashGrid.h:867
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
SmallArray< Entry, expectedCellSize > Cell
Definition: PointHashGrid.h:109
const AABox & conservativeBoxBounds() const
Definition: PointHashGrid.h:306
void clear(bool shrink=true)
Definition: PointHashGrid.h:1020
PointHashGrid(float radiusHint, const MemoryManager::Ref &m=MemoryManager::create())
Definition: PointHashGrid.h:224
int size() const
Definition: PointHashGrid.h:894
Iterator operator++(int)
Definition: PointHashGrid.h:488
BoxIterator m_boxIterator
Definition: PointHashGrid.h:723
Definition: PointHashGrid.h:404
CellTable m_data
Definition: PointHashGrid.h:134
int size() const
Definition: Array.h:430
Value & getCreate(const Key &key)
Definition: Table.h:861
Definition: PointHashGrid.h:861
void getIntersectingMembers(const Sphere &sphere, Array< Value > &result) const
Definition: PointHashGrid.h:840
BoxIterator operator++(int)
Definition: PointHashGrid.h:676
const Value * operator->() const
Definition: PointHashGrid.h:496
const Value & value() const
Definition: PointHashGrid.h:633
CellIterator beginCells() const
Definition: PointHashGrid.h:972
const Vector3 & position() const
Definition: PointHashGrid.h:639
bool contains(const Point3 &point) const
Definition: Sphere.cpp:79
bool isValid() const
Definition: PointHashGrid.h:691
int size() const
Definition: SmallArray.h:37
G3D::Quat pow(const G3D::Quat &q, double x)
Definition: Quat.h:761
float debugGetAverageBucketSize() const
Definition: PointHashGrid.h:1038
friend class ThisType
Definition: PointHashGrid.h:528
void advance()
Definition: PointHashGrid.h:727
CellIterator & operator=(const CellIterator &)
bool hasMore() const
Definition: PointHashGrid.h:687
bool operator!=(const CellIterator &other) const
Definition: PointHashGrid.h:931
Vector3int32 m_hi
Definition: PointHashGrid.h:539
BoxIterator & operator=(const BoxIterator &)
BoxIterator(const ThisType *grid, bool exact, const AABox &box)
Definition: PointHashGrid.h:613
const SphereIterator & endSphereIntersection() const
Definition: PointHashGrid.h:833
bool operator==(const BoxIterator &other) const
Definition: PointHashGrid.h:659
Point3 position
Definition: PointHashGrid.h:104
PointHashGrid(const ThisType &)
friend class ThisType
Definition: PointHashGrid.h:863
bool contains(const Value &v) const
Definition: PointHashGrid.h:987
SphereIterator begin(const Sphere &sphere) const
Definition: PointHashGrid.h:821
bool hasMore() const
Definition: PointHashGrid.h:458
bool isValid() const
Definition: PointHashGrid.h:965
bool m_isEnd
Definition: PointHashGrid.h:408
CellObject()
Definition: PointHashGrid.h:882
SphereIterator()
Definition: PointHashGrid.h:725
bool isValid() const
Definition: PointHashGrid.h:453
const Value & operator*() const
Definition: PointHashGrid.h:804
const Value & operator*() const
Definition: PointHashGrid.h:495
const CellObject * operator->() const
Definition: PointHashGrid.h:928
const FieldDescriptor value
Definition: descriptor.h:1522
const int m_epoch
Definition: PointHashGrid.h:558
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Table.h:309
#define ThisType
Definition: PointHashGrid.h:99
SphereIterator operator++(int)
Definition: PointHashGrid.h:798
void insert(const Array< Value > &v)
Definition: PointHashGrid.h:350
const Point3 & high() const
Definition: AABox.h:141
Definition: EqualsTrait.h:19
BoxIterator()
Definition: PointHashGrid.h:610
static const Vector3 & inf()
Definition: Vector3.cpp:124
bool G3D_DEPRECATED hasMore() const
Definition: PointHashGrid.h:809
friend class ThisType
Definition: PointHashGrid.h:719
void append(const T &value)
Definition: Array.h:583
CellIterator(const ThisType *grid)
Definition: PointHashGrid.h:913
float radiusHint() const
Definition: PointHashGrid.h:234
SphereIterator & operator++()
Definition: PointHashGrid.h:788
Iterator begin() const
Definition: PointHashGrid.h:512
const ThisType * m_grid
Definition: PointHashGrid.h:410
int m_arrayIndex
Definition: PointHashGrid.h:556
float volume() const
Definition: AABox.h:264
SphereIterator G3D_DEPRECATED beginSphereIntersection(const Sphere &sphere) const
Definition: PointHashGrid.h:827
Vector3int32 m_lo
Definition: PointHashGrid.h:536
void clear(float radiusHint)
Definition: PointHashGrid.h:238