15 #ifndef __UT_HashGrid_C__
16 #define __UT_HashGrid_C__
27 #define ut_CELL_SHIFT 21
28 #define ut_CELL_SHIFT2 42
29 #define ut_CELL_MASK (( ((int64) 1) << ut_CELL_SHIFT)-1)
31 static const int ut_NUM_CELL_NEIGHBOURS = 27;
33 template <
typename utPtr>
35 bool fullinit,
unsigned int sz)
37 myCellWidth(cellsize),
45 myCellWidth2 = myCellWidth * myCellWidth;
46 myInvCellWidth = 1.0f / myCellWidth;
54 template <
typename utPtr>
58 int64 memuse = inclusive ?
sizeof(*this) : 0;
59 memuse += myPositions.getMemoryUsage(
false);
60 memuse += myElements.getMemoryUsage(
false);
61 memuse += myCellArray.getMemoryUsage(
false);
62 for (
exint i = 0; i < myCellArray.entries(); i++)
64 memuse += myCellArray(i).myIndices.getMemoryUsage(
false);
66 memuse += myGridSet.getMemoryUsage(
false);
71 template <
typename utPtr>
82 int pointindex = myPositions.entries();
84 myPositions.append(p);
91 i = SYSfastFloor((p.
x() - myOrigin.x()) * myInvCellWidth);
92 j = SYSfastFloor((p.
y() - myOrigin.y()) * myInvCellWidth);
93 k = SYSfastFloor((p.
z() - myOrigin.z()) * myInvCellWidth);
95 cell = getCell(i, j, k,
true);
100 if (newindex > myMaxElements)
101 myMaxElements = newindex;
105 for (
int dz = -1; dz <= 1; dz++)
107 for (
int dy = -1; dy <= 1; dy++)
109 for (
int dx = -1; dx <= 1; dx++)
112 if (!dx && !dy && !dz)
115 othercell = getCell(i+dx, j+dy, k+dz,
true);
118 if (newindex > myMaxElements)
119 myMaxElements = newindex;
129 template <
typename utPtr>
133 utPtr blankelem = utPtr();
136 myPositions.appendMultiple(zero, nblanks);
137 myElements.appendMultiple(blankelem, nblanks);
141 template <
typename utPtr>
164 for (i = 0; i <
n; i++)
167 if ((p - myPositions(index)).length2() < myCellWidth2)
168 list.
append(myElements(index));
178 i = SYSfastFloor((p.
x() - myOrigin.x()) * myInvCellWidth);
179 j = SYSfastFloor((p.
y() - myOrigin.y()) * myInvCellWidth);
180 k = SYSfastFloor((p.
z() - myOrigin.z()) * myInvCellWidth);
185 for (
int dz = -1; dz <= 1; dz++)
187 for (
int dy = -1; dy <= 1; dy++)
189 for (
int dx = -1; dx <= 1; dx++)
191 currcell = getCell(i+dx, j+dy, k+dz);
195 for (ptnum = 0; ptnum < npts; ptnum++)
199 if ( (p - myPositions(index)).length2() < myCellWidth2)
200 list.
append(myElements(index));
210 template <
typename utPtr>
236 i = SYSfastFloor((p.
x() - myOrigin.x()) * myInvCellWidth);
237 j = SYSfastFloor((p.
y() - myOrigin.y()) * myInvCellWidth);
238 k = SYSfastFloor((p.
z() - myOrigin.z()) * myInvCellWidth);
244 for (
int dz = -1; dz <= 1; dz++)
246 for (
int dy = -1; dy <= 1; dy++)
248 for (
int dx = -1; dx <= 1; dx++)
250 currcell = getCell(i+dx, j+dy, k+dz);
260 template <
typename utPtr>
268 myCellArray.entries(0);
269 myCellArray.setCapacity(0);
271 myPositions.entries(0);
272 myPositions.setCapacity(sz);
274 myElements.entries(0);
275 myElements.setCapacity(sz);
278 template <
typename utPtr>
281 bool fullinit,
unsigned int sz)
285 myCellWidth = cellsize;
287 myFullInit = fullinit;
290 template <
typename utPtr>
296 xi = SYSfastFloor((p.
x() - myOrigin.x()) * myInvCellWidth);
297 yi = SYSfastFloor((p.
y() - myOrigin.y()) * myInvCellWidth);
298 zi = SYSfastFloor((p.
z() - myOrigin.z()) * myInvCellWidth);
300 return getCell(xi, yi, zi, create);
303 template <
typename utPtr>
309 xi = SYSfastFloor((p.
x() - myOrigin.x()) * myInvCellWidth);
310 yi = SYSfastFloor((p.
y() - myOrigin.y()) * myInvCellWidth);
311 zi = SYSfastFloor((p.
z() - myOrigin.z()) * myInvCellWidth);
313 return getCell(xi, yi, zi);
316 template <
typename utPtr>
328 exint arrayindex = -1;
329 auto &&it = myGridSet.find(index);
330 if (it != myGridSet.end())
332 arrayindex = it->second;
336 return &myCellArray(arrayindex);
339 exint cellindex = myCellArray.append();
342 myGridSet[
index] = cellindex;
344 return &myCellArray(cellindex);
350 template <
typename utPtr>
362 exint arrayindex = -1;
363 auto &&it = myGridSet.find(index);
364 if (it != myGridSet.end())
366 arrayindex = it->second;
370 return &myCellArray(arrayindex);
375 #endif // __UT_HashGrid.C
void insertBlanks(int nblanks)
constexpr SYS_FORCE_INLINE T & z() noexcept
UT_HashGrid(fpreal cellsize, const UT_Vector3 &origin, bool fullinit, unsigned int sz=0)
exint concat(const UT_Array< T > &a)
Takes another T array and concatenate it onto my end.
void reset(unsigned int sz=0)
void findCloseElements(const UT_Vector3 &p, UT_Array< utPtr > &list) const
int64 getMemoryUsage(bool inclusive) const
void findAllCloseIndices(const UT_Vector3 &p, UT_IntArray &idxlist) const
void setCapacityIfNeeded(exint min_capacity)
constexpr SYS_FORCE_INLINE bool isNan() const noexcept
exint entries() const
Alias of size(). size() is preferred.
A class that defines a cubic grid cell.
constexpr SYS_FORCE_INLINE T & y() noexcept
ImageBuf OIIO_API zero(ROI roi, int nthreads=0)
bool insert(const UT_Vector3 &p, utPtr t)
constexpr SYS_FORCE_INLINE T & x() noexcept