SPH
UniformGrid.cpp
Go to the documentation of this file.
2 #include "system/Profiler.h"
3 
5 
7  : relativeCellCnt(relativeCellCnt) {
9 }
10 
12 
14  PROFILE_SCOPE("VoxelFinder::buildImpl");
15  // number of voxels, free parameter
16  const Size lutSize = Size(relativeCellCnt * root<3>(points.size())) + 1;
17  if (lut.empty() || lutSize != lut.getDimensionSize()) {
18  // build lookup map if not yet build or we have a significantly different number of points
19  lut = LookupMap(lutSize);
20  }
21  if (SPH_LIKELY(!points.empty())) {
22  lut.update(points);
23  }
24 }
25 
26 template <bool FindAll>
28  const Size index,
29  const Float radius,
30  Array<NeighbourRecord>& neighbours) const {
31  const Vector refPosition = lut.clamp(pos);
32  Indices lower = lut.map(refPosition);
33  Indices upper = lower;
34  Box voxel = lut.voxel(lower);
35  const Vector size = lut.getVoxelSize();
36  Vector diffUpper = voxel.upper() - pos;
37  Vector diffLower = pos - voxel.lower();
38 
40  const int upperLimit = lut.getDimensionSize() - 1;
41  while (upper[X] < upperLimit && diffUpper[X] < radius) {
42  diffUpper[X] += size[X];
43  upper[X]++;
44  }
45  while (lower[X] > 0 && diffLower[X] < radius) {
46  diffLower[X] += size[X];
47  lower[X]--;
48  }
49  while (upper[Y] < upperLimit && diffUpper[Y] < radius) {
50  diffUpper[Y] += size[Y];
51  upper[Y]++;
52  }
53  while (lower[Y] > 0 && diffLower[Y] < radius) {
54  diffLower[Y] += size[Y];
55  lower[Y]--;
56  }
57  while (upper[Z] < upperLimit && diffUpper[Z] < radius) {
58  diffUpper[Z] += size[Z];
59  upper[Z]++;
60  }
61  while (lower[Z] > 0 && diffLower[Z] < radius) {
62  diffLower[Z] += size[Z];
63  lower[Z]--;
64  }
65 
66  for (int x = lower[X]; x <= upper[X]; ++x) {
67  for (int y = lower[Y]; y <= upper[Y]; ++y) {
68  for (int z = lower[Z]; z <= upper[Z]; ++z) {
69  for (Size i : lut(Indices(x, y, z))) {
70  const Float distSqr = getSqrLength(values[i] - pos);
71  if (distSqr < sqr(radius) && (FindAll || rank[i] < rank[index])) {
72  neighbours.emplaceBack(NeighbourRecord{ i, distSqr });
73  }
74  }
75  }
76  }
77  }
78  return neighbours.size();
79 }
80 
81 template Size UniformGridFinder::find<true>(const Vector& pos,
82  const Size index,
83  const Float radius,
84  Array<NeighbourRecord>& neighbours) const;
85 
86 template Size UniformGridFinder::find<false>(const Vector& pos,
87  const Size index,
88  const Float radius,
89  Array<NeighbourRecord>& neighbours) const;
90 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
const float radius
Definition: CurveDialog.cpp:18
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
INLINE Float root< 3 >(const Float f)
Definition: MathUtils.h:105
#define UNUSED(x)
Definition: Object.h:37
#define NAMESPACE_SPH_END
Definition: Object.h:12
#define SPH_LIKELY(x)
Branch prediction hints.
Definition: Object.h:49
Tool to measure time spent in functions and profile the code.
#define PROFILE_SCOPE(name)
Definition: Profiler.h:175
Simple algorithm for finding nearest neighbours using spatial partitioning of particles.
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE bool empty() const
Definition: ArrayView.h:105
INLINE TCounter size() const
Definition: ArrayView.h:101
StorageType & emplaceBack(TArgs &&... args)
Constructs a new element at the end of the array in place, using the provided arguments.
Definition: Array.h:332
INLINE TCounter size() const noexcept
Definition: Array.h:193
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE const Vector & lower() const
Returns lower bounds of the box.
Definition: Box.h:82
INLINE const Vector & upper() const
Returns upper bounds of the box.
Definition: Box.h:94
ArrayView< const Vector > values
View of the source datapoints, updated every time (re)build is called.
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
Definition: Scheduler.h:27
Order rank
Ranks of particles according to their smoothing lengths.
Helper object for storing three (possibly four) int or bool values.
Definition: Indices.h:16
static INLINE void init()
Must be called once before Indices are used.
Definition: Indices.h:60
INLINE Vector getVoxelSize() const
Definition: LookupMap.h:80
void update(ArrayView< const Vector > points)
Definition: LookupMap.h:31
INLINE bool empty() const
Definition: LookupMap.h:47
INLINE Size getDimensionSize() const
Definition: LookupMap.h:84
INLINE Indices map(const Vector &v) const
Definition: LookupMap.h:88
INLINE Box voxel(const Indices idxs) const
Definition: LookupMap.h:74
INLINE Vector clamp(const Vector &pos) const
Definition: LookupMap.h:51
Size find(const Vector &pos, const Size index, const Float radius, Array< NeighbourRecord > &neighs) const
Definition: UniformGrid.cpp:27
virtual void buildImpl(IScheduler &scheduler, ArrayView< const Vector > points) override
Builds finder from set of vectors.
Definition: UniformGrid.cpp:13
UniformGridFinder(const Float relativeCellCnt=1)
Definition: UniformGrid.cpp:6
Float relativeCellCnt
Multiplier of the number of cells.
Definition: UniformGrid.h:19
Holds information about a neighbour particles.