CUDAPointHashGridSearcher3.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_SEARCHER3_HPP
12 #define CUBBYFLOW_CUDA_POINT_HASH_GRID_SEARCHER3_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 CUDAPointHashGridSearcher3 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  uint3 resolution);
42 
43  inline CUBBYFLOW_CUDA_HOST_DEVICE void GetNearbyKeys(
44  float4 position, uint32_t* nearbyKeys) const;
45 
46  inline CUBBYFLOW_CUDA_HOST_DEVICE int3
47  GetBucketIndex(float4 position) const;
48 
49  inline CUBBYFLOW_CUDA_HOST_DEVICE uint32_t
50  GetHashKeyFromBucketIndex(int3 bucketIndex) const;
51 
52  inline CUBBYFLOW_CUDA_HOST_DEVICE uint32_t
53  GetHashKeyFromPosition(float4 position) const;
54 
55  private:
56  float m_gridSpacing;
57  uint3 m_resolution;
58  };
59 
60  template <typename Callback>
61  class ForEachNearbyPointFunc
62  {
63  public:
64  inline CUBBYFLOW_CUDA_HOST_DEVICE ForEachNearbyPointFunc(
65  float r, float gridSpacing, uint3 resolution, const uint32_t* sit,
66  const uint32_t* eit, const uint32_t* si, const float4* p,
67  const float4* o, Callback cb);
68 
69  template <typename Index>
70  inline CUBBYFLOW_CUDA_HOST_DEVICE void operator()(Index idx);
71 
72  private:
73  HashUtils m_hashUtils;
74  float m_radius;
75  const uint32_t* m_startIndexTable;
76  const uint32_t* m_endIndexTable;
77  const uint32_t* m_sortedIndices;
78  const float4* m_points;
79  const float4* m_origins;
80  Callback m_callback;
81  };
82 
93  CUDAPointHashGridSearcher3(const uint3& resolution, float gridSpacing);
94 
107  CUDAPointHashGridSearcher3(uint32_t resolutionX, uint32_t resolutionY,
108  uint32_t resolutionZ, float gridSpacing);
109 
111  CUDAPointHashGridSearcher3(const CUDAPointHashGridSearcher3& other);
112 
114  CUDAPointHashGridSearcher3(CUDAPointHashGridSearcher3&& other) noexcept;
115 
117  ~CUDAPointHashGridSearcher3() = default;
118 
120  CUDAPointHashGridSearcher3& operator=(
121  const CUDAPointHashGridSearcher3& other);
122 
124  CUDAPointHashGridSearcher3& operator=(
125  CUDAPointHashGridSearcher3&& other) noexcept;
126 
134  void Build(const ConstCUDAArrayView1<float4>& points);
135 
144  template <typename Callback>
145  void ForEachNearbyPoint(const ConstCUDAArrayView1<float4>& origins,
146  float radius, Callback callback) const;
147 
148  float GridSpacing() const;
149 
150  Vector3UZ Resolution() const;
151 
152  ConstCUDAArrayView1<float4> 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 CUDAPointHashGridSearcher3& other);
228 
230  static Builder GetBuilder();
231 
232  private:
233  float m_gridSpacing = 1.0f;
234  uint3 m_resolution = make_uint3(1, 1, 1);
235  CUDAArray1<float4> m_points;
236  CUDAArray1<uint32_t> m_keys;
237  CUDAArray1<uint32_t> m_startIndexTable;
238  CUDAArray1<uint32_t> m_endIndexTable;
239  CUDAArray1<uint32_t> m_sortedIndices;
240 };
241 
243 using CUDAPointHashGridSearcher3Ptr =
244  std::shared_ptr<CUDAPointHashGridSearcher3>;
245 
250 class CUDAPointHashGridSearcher3::Builder final
251 {
252  public:
254  Builder& WithResolution(const Vector3UZ& resolution);
255 
257  Builder& WithGridSpacing(float gridSpacing);
258 
260  CUDAPointHashGridSearcher3 Build() const;
261 
263  CUDAPointHashGridSearcher3Ptr MakeShared() const;
264 
265  private:
266  float m_gridSpacing = 1.0f;
267  Vector3UZ m_resolution{ 64, 64, 64 };
268 };
269 } // namespace CubbyFlow
270 
272 
273 #endif
274 
275 #endif
Definition: pybind11Utils.hpp:20
Vector3< size_t > Vector3UZ
Definition: Matrix.hpp:789