iMSTK
Interactive Medical Simulation Toolkit
imstkSpatialHashTableSeparateChaining.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkSpatialHashTable.h"
10 #include "imstkMath.h"
11 #include "imstkVecDataArray.h"
12 
13 #include <unordered_set>
14 
15 namespace imstk
16 {
17 struct PointEntry
18 {
19  Vec3d point;
20  unsigned long ID;
21  double* cellSize;
22 };
23 } // namespace imstk
24 
25 namespace std
26 {
31 template<> struct hash<imstk::PointEntry>
32 {
33  size_t operator()(const imstk::PointEntry& point) const
34  {
35  unsigned int x = (unsigned int)(point.point.x() / point.cellSize[0]);
36  unsigned int y = (unsigned int)(point.point.y() / point.cellSize[1]);
37  unsigned int z = (unsigned int)(point.point.z() / point.cellSize[2]);
38 
39  return (104729 * x + 104743 * y + 104759 * z);
40  }
41 };
42 
47 template<> struct equal_to<imstk::PointEntry>
48 {
49  size_t operator()(const imstk::PointEntry& point1, const imstk::PointEntry& point2) const
50  {
51  if (point1.ID != point2.ID)
52  {
53  return false;
54  }
55 
56  if (point1.point != point2.point)
57  {
58  return false;
59  }
60 
61  return true;
62  }
63 };
64 } // namespace std
65 
66 namespace imstk
67 {
74 {
75 public:
80 
85  void insertPoints(const VecDataArray<double, 3>& points);
86 
91  void insertPoint(const Vec3d& point);
92 
97  void setLoadFactorMax(float loadFactorMax);
98 
104  std::vector<size_t> getPointsInAABB(const Vec3d& corner1, const Vec3d& corner2);
105 
112  void getPointsInAABB(std::vector<size_t>& result, const Vec3d& corner1, const Vec3d& corner2);
113 
119  std::vector<size_t> getPointsInSphere(const Vec3d& ppos, double radius);
120 
127  void getPointsInSphere(std::vector<size_t>& result, const Vec3d& ppos, const double radius);
128 
132  void clear();
133 
138  virtual void setCellSize(double x, double y, double z) override;
139 
143  void recomputePointHash();
144 
145 protected:
149  virtual void rehash() override;
150 
151  float m_loadFactorMax = 10.0f;
152  unsigned long m_currentID = 0;
153  std::shared_ptr<std::unordered_set<PointEntry>> m_table;
154 };
155 } // namespace imstk
Implementation of SpatialHashTable using separate chaining.
Compound Geometry.
Returns a hash value for a PointEntry.
Abstract class for spatial hash tables.
Test if two points are the same or not by comparing their id and coordinates.