CUDAPointHashGridSearcher2.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_CUDA_POINT_HASH_GRID_SEARCHER2_HPP
12 #define CUBBYFLOW_CUDA_POINT_HASH_GRID_SEARCHER2_HPP
13 
14 #ifdef CUBBYFLOW_USE_CUDA
15 
17 #include <Core/Matrix/Matrix.hpp>
18 
19 #include <cuda_runtime.h>
20 
21 namespace CubbyFlow
22 {
30 class CUDAPointHashGridSearcher2 final
31 {
32  public:
33  class Builder;
34 
35  class HashUtils
36  {
37  public:
38  HashUtils() = default;
39 
40  inline CUBBYFLOW_CUDA_HOST_DEVICE HashUtils(float gridSpacing,
41  uint2 resolution);
42 
43  inline CUBBYFLOW_CUDA_HOST_DEVICE void GetNearbyKeys(
44  float2 position, uint32_t* nearbyKeys) const;
45 
46  inline CUBBYFLOW_CUDA_HOST_DEVICE int2
47  GetBucketIndex(float2 position) const;
48 
49  inline CUBBYFLOW_CUDA_HOST_DEVICE uint32_t
50  GetHashKeyFromBucketIndex(int2 bucketIndex) const;
51 
52  inline CUBBYFLOW_CUDA_HOST_DEVICE uint32_t
53  GetHashKeyFromPosition(float2 position) const;
54 
55  private:
56  float m_gridSpacing;
57  uint32_t m_dummy;
58  uint2 m_resolution;
59  };
60 
61  template <typename Callback>
62  class ForEachNearbyPointFunc
63  {
64  public:
65  inline CUBBYFLOW_CUDA_HOST_DEVICE ForEachNearbyPointFunc(
66  float r, float gridSpacing, uint2 resolution, const uint32_t* sit,
67  const uint32_t* eit, const uint32_t* si, const float2* p,
68  const float2* o, Callback cb);
69 
70  template <typename Index>
71  inline CUBBYFLOW_CUDA_HOST_DEVICE void operator()(Index idx);
72 
73  private:
74  HashUtils m_hashUtils;
75  float m_radius;
76  const uint32_t* m_startIndexTable;
77  const uint32_t* m_endIndexTable;
78  const uint32_t* m_sortedIndices;
79  const float2* m_points;
80  const float2* m_origins;
81  Callback m_callback;
82  };
83 
94  CUDAPointHashGridSearcher2(const uint2& resolution, float gridSpacing);
95 
107  CUDAPointHashGridSearcher2(uint32_t resolutionX, uint32_t resolutionY,
108  float gridSpacing);
109 
111  CUDAPointHashGridSearcher2(const CUDAPointHashGridSearcher2& other);
112 
114  CUDAPointHashGridSearcher2(CUDAPointHashGridSearcher2&& other) noexcept;
115 
117  ~CUDAPointHashGridSearcher2() = default;
118 
120  CUDAPointHashGridSearcher2& operator=(
121  const CUDAPointHashGridSearcher2& other);
122 
124  CUDAPointHashGridSearcher2& operator=(
125  CUDAPointHashGridSearcher2&& other) noexcept;
126 
134  void Build(const ConstCUDAArrayView1<float2>& points);
135 
144  template <typename Callback>
145  void ForEachNearbyPoint(const ConstCUDAArrayView1<float2>& origins,
146  float radius, Callback callback) const;
147 
148  float GridSpacing() const;
149 
150  Vector2UZ Resolution() const;
151 
152  ConstCUDAArrayView1<float2> SortedPoints() const;
153 
162  ConstCUDAArrayView1<uint32_t> Keys() const;
163 
187  ConstCUDAArrayView1<uint32_t> StartIndexTable() const;
188 
212  ConstCUDAArrayView1<uint32_t> EndIndexTable() const;
213 
224  ConstCUDAArrayView1<uint32_t> SortedIndices() const;
225 
227  void Set(const CUDAPointHashGridSearcher2& other);
228 
230  static Builder GetBuilder();
231 
232  private:
233  float m_gridSpacing = 1.0f;
234  uint1 m_dummy;
235  uint2 m_resolution = make_uint2(1, 1);
236  CUDAArray1<float2> m_points;
237  CUDAArray1<uint32_t> m_keys;
238  CUDAArray1<uint32_t> m_startIndexTable;
239  CUDAArray1<uint32_t> m_endIndexTable;
240  CUDAArray1<uint32_t> m_sortedIndices;
241 };
242 
244 using CUDAPointHashGridSearcher2Ptr =
245  std::shared_ptr<CUDAPointHashGridSearcher2>;
246 
251 class CUDAPointHashGridSearcher2::Builder final
252 {
253  public:
255  Builder& WithResolution(const Vector2UZ& resolution);
256 
258  Builder& WithGridSpacing(float gridSpacing);
259 
261  CUDAPointHashGridSearcher2 Build() const;
262 
264  CUDAPointHashGridSearcher2Ptr MakeShared() const;
265 
266  private:
267  Vector2UZ m_resolution{ 64, 64 };
268  float m_gridSpacing = 1.0f;
269 };
270 } // namespace CubbyFlow
271 
273 
274 #endif
275 
276 #endif
Vector2< size_t > Vector2UZ
Definition: Matrix.hpp:776
Definition: pybind11Utils.hpp:20