SPH
LookupMap.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 #include "objects/geometry/Box.h"
11 
13 
14 class LookupMap : public Noncopyable {
15 private:
17  Array<Array<Size>> storage;
18 
19  Box tightBox;
20  Box boundingBox;
21 
22  Size dimensionSize;
23 
24 public:
25  LookupMap() = default;
26 
27  LookupMap(const Size n)
28  : storage(pow<3>(n))
29  , dimensionSize(n) {}
30 
32  tightBox = Box();
33  for (const Vector& v : points) {
34  tightBox.extend(v);
35  }
36  boundingBox = this->extendBox(tightBox, 1.e-6_f);
37  for (auto& a : storage) {
38  a.clear();
39  }
40  // put particles into voxels
41  for (Size i = 0; i < points.size(); ++i) {
42  Indices idxs = this->map(points[i]);
43  (*this)(idxs).push(i);
44  }
45  }
46 
47  INLINE bool empty() const {
48  return storage.empty();
49  }
50 
51  INLINE Vector clamp(const Vector& pos) const {
52  return tightBox.clamp(pos);
53  }
54 
56  storage = std::move(other.storage);
57  dimensionSize = other.dimensionSize;
58  boundingBox = other.boundingBox;
59  return *this;
60  }
61 
62  INLINE const Array<Size>& operator()(const Indices& idxs) const {
63  const Size idx = idxs[X] * sqr(dimensionSize) + idxs[Y] * dimensionSize + idxs[Z];
64  SPH_ASSERT(unsigned(idx) < unsigned(storage.size()));
65  return storage[idx];
66  }
67 
69  const Size idx = idxs[X] * sqr(dimensionSize) + idxs[Y] * dimensionSize + idxs[Z];
70  SPH_ASSERT(unsigned(idx) < unsigned(storage.size()));
71  return storage[idx];
72  }
73 
74  INLINE Box voxel(const Indices idxs) const {
75  Vector lower(boundingBox.lower() + boundingBox.size() * Vector(idxs) / dimensionSize);
76  Vector upper(boundingBox.lower() + boundingBox.size() * Vector(idxs + Indices(1)) / dimensionSize);
77  return Box(lower, upper);
78  }
79 
81  return boundingBox.size() / dimensionSize;
82  }
83 
85  return dimensionSize;
86  }
87 
88  INLINE Indices map(const Vector& v) const {
89  SPH_ASSERT(boundingBox.size()[X] > 0._f && boundingBox.size()[Y] > 0._f && boundingBox.size()[Z] > 0._f);
90  SPH_ASSERT(dimensionSize >= 1);
91  Vector idxs = (v - boundingBox.lower()) / (boundingBox.size()) * dimensionSize;
92  // Ordinarily, idxs are never <0 or >=dimensionSize, BUT in case the points are slightly move, this
93  // can happen. In this case, we want to return a valid result rather than crashing, even though some
94  // neighbours might be missed if the tree is not rebuilt
95  return Indices(Sph::clamp(idxs, Vector(0._f), Vector(dimensionSize - 1._f)));
96  }
97 
98 private:
105  Box extendBox(const Box& box, const Float eps) const {
106  const Vector extension =
107  max(eps * box.size(), eps * abs(box.lower()), eps * abs(box.upper()), Vector(eps));
108  Box extendedBox = box;
109  extendedBox.extend(box.upper() + extension);
110  extendedBox.extend(box.lower() - extension);
111  return extendedBox;
112  }
113 };
114 
Generic dynamically allocated resizable storage.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Object representing a three-dimensional axis-aligned box.
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 max(const T &f1, const T &f2)
Definition: MathBasic.h:20
constexpr INLINE T clamp(const T &f, const T &f1, const T &f2)
Definition: MathBasic.h:35
constexpr INLINE T sqr(const T &f) noexcept
Return a squared value.
Definition: MathUtils.h:67
constexpr INLINE Float pow(const Float v)
Power for floats.
INLINE auto abs(const T &f)
Definition: MathUtils.h:276
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Basic vector algebra. Computations are accelerated using SIMD.
BasicVector< Float > Vector
Definition: Vector.h:539
@ 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 TCounter size() const
Definition: ArrayView.h:101
Generic dynamically allocated resizable storage.
Definition: Array.h:43
INLINE TCounter size() const noexcept
Definition: Array.h:193
INLINE bool empty() const noexcept
Definition: Array.h:201
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE void extend(const Vector &v)
Enlarges the box to contain the vector.
Definition: Box.h:49
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
INLINE Vector size() const
Returns box dimensions.
Definition: Box.h:106
INLINE Vector clamp(const Vector &v) const
Clamps all components of the vector to fit within the box.
Definition: Box.h:76
Helper object for storing three (possibly four) int or bool values.
Definition: Indices.h:16
LookupMap(const Size n)
Definition: LookupMap.h:27
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 Array< Size > & operator()(const Indices &idxs)
Definition: LookupMap.h:68
INLINE Size getDimensionSize() const
Definition: LookupMap.h:84
LookupMap & operator=(LookupMap &&other)
Definition: LookupMap.h:55
INLINE Indices map(const Vector &v) const
Definition: LookupMap.h:88
INLINE Box voxel(const Indices idxs) const
Definition: LookupMap.h:74
INLINE const Array< Size > & operator()(const Indices &idxs) const
Definition: LookupMap.h:62
LookupMap()=default
INLINE Vector clamp(const Vector &pos) const
Definition: LookupMap.h:51
Object with deleted copy constructor and copy operator.
Definition: Object.h:54