PointHashGridSearcher.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_POINT_HASH_GRID_SEARCHER_HPP
12 #define CUBBYFLOW_POINT_HASH_GRID_SEARCHER_HPP
13 
14 #include <Core/Array/Array.hpp>
15 #include <Core/Matrix/Matrix.hpp>
17 
18 namespace CubbyFlow
19 {
27 template <size_t N>
29 {
30  public:
32 
33  class Builder;
34 
37 
48  PointHashGridSearcher(const Vector<size_t, N>& resolution,
49  double gridSpacing);
50 
52  ~PointHashGridSearcher() override = default;
53 
56 
59 
62 
65 
68  void Build(const ConstArrayView1<Vector<double, N>>& points,
69  double maxSearchRadius) override;
70 
79  void ForEachNearbyPoint(
80  const Vector<double, N>& origin, double radius,
81  const ForEachNearbyPointFunc& callback) const override;
82 
92  [[nodiscard]] bool HasNearbyPoint(const Vector<double, N>& origin,
93  double radius) const override;
94 
104  void Add(const Vector<double, N>& point);
105 
114  [[nodiscard]] const Array1<Array1<size_t>>& Buckets() const;
115 
122  [[nodiscard]] std::shared_ptr<PointNeighborSearcher<N>> Clone()
123  const override;
124 
126  void Set(const PointHashGridSearcher& other);
127 
129  void Serialize(std::vector<uint8_t>* buffer) const override;
130 
132  void Deserialize(const std::vector<uint8_t>& buffer) override;
133 
135  static Builder GetBuilder();
136 
137  private:
138  template <size_t M = N>
139  static std::enable_if_t<M == 2, void> Serialize(
140  const PointHashGridSearcher<2>& searcher, std::vector<uint8_t>* buffer);
141 
142  template <size_t M = N>
143  static std::enable_if_t<M == 3, void> Serialize(
144  const PointHashGridSearcher<3>& searcher, std::vector<uint8_t>* buffer);
145 
146  template <size_t M = N>
147  static std::enable_if_t<M == 2, void> Deserialize(
148  const std::vector<uint8_t>& buffer, PointHashGridSearcher<2>& searcher);
149 
150  template <size_t M = N>
151  static std::enable_if_t<M == 3, void> Deserialize(
152  const std::vector<uint8_t>& buffer, PointHashGridSearcher<3>& searcher);
153 
154  double m_gridSpacing = 1.0;
156  Array1<Vector<double, N>> m_points;
157  Array1<Array1<size_t>> m_buckets;
158 };
159 
162 
165 
167 using PointHashGridSearcher2Ptr = std::shared_ptr<PointHashGridSearcher2>;
168 
170 using PointHashGridSearcher3Ptr = std::shared_ptr<PointHashGridSearcher3>;
171 
175 template <size_t N>
177  : public PointNeighborSearcherBuilder<N>
178 {
179  public:
181  Builder() = default;
182 
184  ~Builder() override = default;
185 
187  Builder(const Builder& other) = delete;
188 
190  Builder(Builder&& other) noexcept = delete;
191 
193  Builder& operator=(const Builder& other) = delete;
194 
196  Builder& operator=(Builder&& other) noexcept = delete;
197 
199  Builder& WithResolution(const Vector<size_t, N>& resolution);
200 
202  Builder& WithGridSpacing(double gridSpacing);
203 
206 
208  std::shared_ptr<PointHashGridSearcher<N>> MakeShared() const;
209 
211  std::shared_ptr<PointNeighborSearcher<N>> BuildPointNeighborSearcher()
212  const override;
213 
214  private:
216  double m_gridSpacing = 1.0;
217 };
218 } // namespace CubbyFlow
219 
220 #endif
Front-end to create PointHashGridSearcher objects step by step.
Definition: PointHashGridSearcher.hpp:176
std::shared_ptr< PointHashGridSearcher2 > PointHashGridSearcher2Ptr
Shared pointer for the PointHashGridSearcher2 type.
Definition: PointHashGridSearcher.hpp:167
void Set(const PointHashGridSearcher &other)
Copy from the other instance.
Abstract base class for N-D point neighbor searcher builders.
Definition: PointNeighborSearcher.hpp:119
void Build(const ConstArrayView1< Vector< double, N >> &points, double maxSearchRadius) override
void ForEachNearbyPoint(const Vector< double, N > &origin, double radius, const ForEachNearbyPointFunc &callback) const override
PointHashGridSearcher(const Vector< size_t, N > &resolution, double gridSpacing)
Constructs hash grid with given resolution and grid spacing.
std::function< void(size_t, const Vector< double, N > &)> ForEachNearbyPointFunc
Definition: PointNeighborSearcher.hpp:38
Abstract base class for N-D neighbor point searcher.
Definition: PointNeighborSearcher.hpp:32
Definition: Matrix.hpp:27
static std::enable_if_t< IsMatrixSizeStatic< Rows, Cols >), D > MakeConstant(ValueType val)
Makes a static matrix with constant entries.
Definition: MatrixDenseBase-Impl.hpp:152
static Builder GetBuilder()
Returns builder fox PointHashGridSearcher.
Definition: pybind11Utils.hpp:20
void Add(const Vector< double, N > &point)
Adds a single point to the hash grid.
Definition: Array-Impl.hpp:19
#define CUBBYFLOW_NEIGHBOR_SEARCHER_TYPE_NAME(DerivedClassName, N)
Definition: PointNeighborSearcher.hpp:163
Hash grid-based N-D point searcher.
Definition: PointHashGridSearcher.hpp:28
bool HasNearbyPoint(const Vector< double, N > &origin, double radius) const override
Generic N-dimensional array class interface.
Definition: Array.hpp:32
PointHashGridSearcher & operator=(const PointHashGridSearcher &other)
Copy assignment operator.
void Serialize(std::vector< uint8_t > *buffer) const override
Serializes the neighbor searcher into the buffer.
std::shared_ptr< PointHashGridSearcher3 > PointHashGridSearcher3Ptr
Shared pointer for the PointHashGridSearcher3 type.
Definition: PointHashGridSearcher.hpp:170
const Array1< Array1< size_t > > & Buckets() const
Returns the internal bucket.
void Deserialize(const std::vector< uint8_t > &buffer) override
Deserializes the neighbor searcher from the buffer.
~PointHashGridSearcher() override=default
Default virtual destructor.
std::shared_ptr< PointNeighborSearcher< N > > Clone() const override
Creates a new instance of the object with same properties than original.