11 #ifndef CUBBYFLOW_CUDA_POINT_HASH_GRID_SEARCHER2_IMPL_HPP 12 #define CUBBYFLOW_CUDA_POINT_HASH_GRID_SEARCHER2_IMPL_HPP 14 #ifdef CUBBYFLOW_USE_CUDA 18 #include <thrust/for_each.h> 19 #include <thrust/iterator/counting_iterator.h> 23 CUBBYFLOW_CUDA_HOST_DEVICE CUDAPointHashGridSearcher2::HashUtils::HashUtils(
24 float gridSpacing, uint2 resolution)
25 : m_gridSpacing{ gridSpacing }, m_resolution{ resolution }
30 inline CUBBYFLOW_CUDA_HOST_DEVICE
void 31 CUDAPointHashGridSearcher2::HashUtils::GetNearbyKeys(float2 position,
32 uint32_t* nearbyKeys)
const 34 int2 originIndex = GetBucketIndex(position), nearbyBucketIndices[8];
36 for (
int i = 0; i < 4; i++)
38 nearbyBucketIndices[i] = originIndex;
41 if ((originIndex.x + 0.5f) * m_gridSpacing <= position.x)
43 nearbyBucketIndices[2].x += 1;
44 nearbyBucketIndices[3].x += 1;
48 nearbyBucketIndices[2].x -= 1;
49 nearbyBucketIndices[3].x -= 1;
52 if ((originIndex.y + 0.5f) * m_gridSpacing <= position.y)
54 nearbyBucketIndices[1].y += 1;
55 nearbyBucketIndices[3].y += 1;
59 nearbyBucketIndices[1].y -= 1;
60 nearbyBucketIndices[3].y -= 1;
63 for (
int i = 0; i < 4; i++)
65 nearbyKeys[i] = GetHashKeyFromBucketIndex(nearbyBucketIndices[i]);
69 inline CUBBYFLOW_CUDA_HOST_DEVICE int2
70 CUDAPointHashGridSearcher2::HashUtils::GetBucketIndex(float2 position)
const 74 bucketIndex.x =
static_cast<int>(floorf(position.x / m_gridSpacing));
75 bucketIndex.y =
static_cast<int>(floorf(position.y / m_gridSpacing));
80 inline CUBBYFLOW_CUDA_HOST_DEVICE uint32_t
81 CUDAPointHashGridSearcher2::HashUtils::GetHashKeyFromBucketIndex(
82 int2 bucketIndex)
const 85 bucketIndex.x = bucketIndex.x & (m_resolution.x - 1);
86 bucketIndex.y = bucketIndex.y & (m_resolution.y - 1);
87 return bucketIndex.y * m_resolution.x + bucketIndex.x;
90 inline CUBBYFLOW_CUDA_HOST_DEVICE uint32_t
91 CUDAPointHashGridSearcher2::HashUtils::GetHashKeyFromPosition(
92 float2 position)
const 94 int2 bucketIndex = GetBucketIndex(position);
95 return GetHashKeyFromBucketIndex(bucketIndex);
98 template <
typename Callback>
99 inline CUBBYFLOW_CUDA_HOST_DEVICE CUDAPointHashGridSearcher2::
100 ForEachNearbyPointFunc<Callback>::ForEachNearbyPointFunc(
101 float r,
float gridSpacing, uint2 resolution,
const uint32_t* sit,
102 const uint32_t* eit,
const uint32_t* si,
const float2* p,
103 const float2* o, Callback cb)
104 : m_hashUtils(gridSpacing, resolution),
106 m_startIndexTable(sit),
107 m_endIndexTable(eit),
116 template <
typename Callback>
117 template <
typename Index>
118 inline CUBBYFLOW_CUDA_HOST_DEVICE
void 119 CUDAPointHashGridSearcher2::ForEachNearbyPointFunc<Callback>::operator()(
122 const float2 origin = m_origins[idx];
124 uint32_t nearbyKeys[4];
125 m_hashUtils.GetNearbyKeys(origin, nearbyKeys);
127 const float queryRadiusSquared = m_radius * m_radius;
129 for (
int i = 0; i < 4; i++)
131 uint32_t nearbyKey = nearbyKeys[i];
132 uint32_t start = m_startIndexTable[nearbyKey];
135 if (start == 0xffffffff)
140 uint32_t
end = m_endIndexTable[nearbyKey];
142 for (uint32_t jj = start; jj <
end; ++jj)
144 uint32_t j = m_sortedIndices[jj];
145 float2 p = m_points[jj];
146 float2 direction = p - origin;
148 float distanceSquared = LengthSquared(direction);
149 if (distanceSquared <= queryRadiusSquared)
151 if (distanceSquared > 0)
153 float distance = sqrtf(distanceSquared);
154 direction /= distance;
157 m_callback(idx, origin, j, p);
163 template <
typename Callback>
164 void CUDAPointHashGridSearcher2::ForEachNearbyPoint(
165 const ConstCUDAArrayView1<float2>& origins,
float radius,
166 Callback callback)
const 169 thrust::counting_iterator<size_t>(0),
170 thrust::counting_iterator<size_t>(0) + origins.Length(),
171 ForEachNearbyPointFunc<Callback>(
172 radius, m_gridSpacing, m_resolution, m_startIndexTable.data(),
173 m_endIndexTable.data(), m_sortedIndices.data(), m_points.data(),
174 origins.data(), callback));
Iterator end()
Definition: ArrayBase-Impl.hpp:102
Definition: pybind11Utils.hpp:20
size_t Index(size_t i) const
Definition: ArrayBase-Impl.hpp:17