7 #include "imstkSpatialHashTableSeparateChaining.h" 21 for (
int i = 0; i < points.size(); i++)
32 entry.ID = m_currentID;
33 entry.cellSize = m_cellSize;
35 m_table->insert(entry);
49 std::vector<size_t> result;
57 auto min_x = std::fmin(corner1.x(), corner2.x());
58 auto max_x = std::fmax(corner1.x(), corner2.x());
59 auto min_y = std::fmin(corner1.y(), corner2.y());
60 auto max_y = std::fmax(corner1.y(), corner2.y());
61 auto min_z = std::fmin(corner1.z(), corner2.z());
62 auto max_z = std::fmax(corner1.z(), corner2.z());
64 std::unordered_set<PointEntry> tempPoints(0);
67 for (
double x = min_x; x < max_x + m_cellSize[0]; x += m_cellSize[0])
69 for (
double y = min_y; y < max_y + m_cellSize[1]; y += m_cellSize[1])
71 for (
double z = min_z; z < max_z + m_cellSize[2]; z += m_cellSize[2])
74 point.point = Vec3d(x, y, z);
75 point.cellSize = m_cellSize;
77 auto bucket = m_table->bucket(point);
79 auto first = m_table->begin(bucket);
80 auto last = m_table->end(bucket);
82 for (
auto p = first; p != last; ++p)
84 tempPoints.insert(*p);
92 result.reserve(tempPoints.size());
95 for (
auto p = tempPoints.begin(); p != tempPoints.end(); ++p)
97 Vec3d point = p->point;
98 if (point.x() >= min_x && point.x() <= max_x
99 && point.y() >= min_y && point.y() <= max_y
100 && point.z() >= min_z && point.z() <= max_z)
102 result.push_back(p->ID);
110 std::vector<size_t> result;
119 for (
int d = 0; d < 3; ++d)
121 cellSpan[d] =
static_cast<int>(std::ceil(radius / m_cellSize[d]));
124 double radiusSqr = radius * radius;
125 std::unordered_set<size_t> visited;
126 visited.reserve(static_cast<size_t>(cellSpan[0] * cellSpan[1] * cellSpan[2]));
131 for (
int i = -cellSpan[0]; i <= cellSpan[0]; ++i)
133 for (
int j = -cellSpan[1]; j <= cellSpan[1]; ++j)
135 for (
int k = -cellSpan[2]; k <= cellSpan[2]; ++k)
138 point.point = Vec3d(ppos[0] + m_cellSize[0] * i,
139 ppos[1] + m_cellSize[1] * j,
140 ppos[2] + m_cellSize[2] * k);
141 point.cellSize = m_cellSize;
143 auto bucket = m_table->bucket(point);
147 if (visited.find(bucket) != visited.end())
151 visited.insert(bucket);
153 auto first = m_table->begin(bucket);
154 auto last = m_table->end(bucket);
156 for (
auto it = first; it != last; ++it)
158 const Vec3d& qpos = it->point;
159 const Vec3d diff = ppos - qpos;
160 const auto d2 = diff[0] * diff[0] + diff[1] * diff[1] + diff[2] * diff[2];
163 result.push_back(it->ID);
174 m_loadFactorMax = loadFactorMax;
175 m_table->max_load_factor(m_loadFactorMax);
176 m_table->rehash(m_table->bucket_count());
193 std::vector<PointEntry> points;
194 points.reserve(m_table->size());
195 points.insert(points.end(), m_table->begin(), m_table->end());
198 for (
auto& point : points)
200 point.cellSize = m_cellSize;
204 for (
auto& point : points)
206 m_table->insert(point);
213 m_table->rehash(m_table->bucket_count());
virtual void setCellSize(double x, double y, double z) override
Protected constructor.
void insertPoint(const Vec3d &point)
Insert an array of points.
void clear()
Clears the table.
void insertPoints(const VecDataArray< double, 3 > &points)
Insert an array of points.
std::vector< size_t > getPointsInAABB(const Vec3d &corner1, const Vec3d &corner2)
Finds IDs of all points in an AABB.
virtual void rehash() override
Rehash the hash table.
SpatialHashTableSeparateChaining()
Default constructor.
void recomputePointHash()
Update cell size for all points and rehash. This is called after changing the cell dimensions...
Abstract class for spatial hash tables.
std::vector< size_t > getPointsInSphere(const Vec3d &ppos, double radius)
Find IDs of all points in a sphere centered at ppos and having given radius.
void setLoadFactorMax(float loadFactorMax)
Sets the max load factor.