11 #ifndef CUBBYFLOW_CUDA_POINT_HASH_GRID_SEARCHER3_IMPL_HPP 12 #define CUBBYFLOW_CUDA_POINT_HASH_GRID_SEARCHER3_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 CUDAPointHashGridSearcher3::HashUtils::HashUtils(
24 float gridSpacing, uint3 resolution)
25 : m_gridSpacing{ gridSpacing }, m_resolution{ resolution }
30 inline CUBBYFLOW_CUDA_HOST_DEVICE
void 31 CUDAPointHashGridSearcher3::HashUtils::GetNearbyKeys(float4 position,
32 uint32_t* nearbyKeys)
const 34 int3 originIndex = GetBucketIndex(position), nearbyBucketIndices[8];
36 for (
int i = 0; i < 8; i++)
38 nearbyBucketIndices[i] = originIndex;
41 if ((originIndex.x + 0.5f) * m_gridSpacing <= position.x)
43 nearbyBucketIndices[1].x += 1;
44 nearbyBucketIndices[3].x += 1;
45 nearbyBucketIndices[5].x += 1;
46 nearbyBucketIndices[7].x += 1;
50 nearbyBucketIndices[1].x -= 1;
51 nearbyBucketIndices[3].x -= 1;
52 nearbyBucketIndices[5].x -= 1;
53 nearbyBucketIndices[7].x -= 1;
56 if ((originIndex.y + 0.5f) * m_gridSpacing <= position.y)
58 nearbyBucketIndices[2].y += 1;
59 nearbyBucketIndices[3].y += 1;
60 nearbyBucketIndices[6].y += 1;
61 nearbyBucketIndices[7].y += 1;
65 nearbyBucketIndices[2].y -= 1;
66 nearbyBucketIndices[3].y -= 1;
67 nearbyBucketIndices[6].y -= 1;
68 nearbyBucketIndices[7].y -= 1;
71 if ((originIndex.z + 0.5f) * m_gridSpacing <= position.z)
73 nearbyBucketIndices[4].z += 1;
74 nearbyBucketIndices[5].z += 1;
75 nearbyBucketIndices[6].z += 1;
76 nearbyBucketIndices[7].z += 1;
80 nearbyBucketIndices[4].z -= 1;
81 nearbyBucketIndices[5].z -= 1;
82 nearbyBucketIndices[6].z -= 1;
83 nearbyBucketIndices[7].z -= 1;
86 for (
int i = 0; i < 8; i++)
88 nearbyKeys[i] = GetHashKeyFromBucketIndex(nearbyBucketIndices[i]);
92 inline CUBBYFLOW_CUDA_HOST_DEVICE int3
93 CUDAPointHashGridSearcher3::HashUtils::GetBucketIndex(float4 position)
const 97 bucketIndex.x =
static_cast<int>(floorf(position.x / m_gridSpacing));
98 bucketIndex.y =
static_cast<int>(floorf(position.y / m_gridSpacing));
99 bucketIndex.z =
static_cast<int>(floorf(position.z / m_gridSpacing));
104 inline CUBBYFLOW_CUDA_HOST_DEVICE uint32_t
105 CUDAPointHashGridSearcher3::HashUtils::GetHashKeyFromBucketIndex(
106 int3 bucketIndex)
const 109 bucketIndex.x = bucketIndex.x & (m_resolution.x - 1);
110 bucketIndex.y = bucketIndex.y & (m_resolution.y - 1);
111 bucketIndex.z = bucketIndex.z & (m_resolution.z - 1);
112 return bucketIndex.z * m_resolution.y * m_resolution.x +
113 bucketIndex.y * m_resolution.x + bucketIndex.x;
116 inline CUBBYFLOW_CUDA_HOST_DEVICE uint32_t
117 CUDAPointHashGridSearcher3::HashUtils::GetHashKeyFromPosition(
118 float4 position)
const 120 int3 bucketIndex = GetBucketIndex(position);
121 return GetHashKeyFromBucketIndex(bucketIndex);
124 template <
typename Callback>
125 inline CUBBYFLOW_CUDA_HOST_DEVICE CUDAPointHashGridSearcher3::
126 ForEachNearbyPointFunc<Callback>::ForEachNearbyPointFunc(
127 float r,
float gridSpacing, uint3 resolution,
const uint32_t* sit,
128 const uint32_t* eit,
const uint32_t* si,
const float4* p,
129 const float4* o, Callback cb)
130 : m_hashUtils(gridSpacing, resolution),
132 m_startIndexTable(sit),
133 m_endIndexTable(eit),
142 template <
typename Callback>
143 template <
typename Index>
144 inline CUBBYFLOW_CUDA_HOST_DEVICE
void 145 CUDAPointHashGridSearcher3::ForEachNearbyPointFunc<Callback>::operator()(
148 const float4 origin = m_origins[idx];
150 uint32_t nearbyKeys[8];
151 m_hashUtils.GetNearbyKeys(origin, nearbyKeys);
153 const float queryRadiusSquared = m_radius * m_radius;
155 for (
int i = 0; i < 8; i++)
157 uint32_t nearbyKey = nearbyKeys[i];
158 uint32_t start = m_startIndexTable[nearbyKey];
161 if (start == 0xffffffff)
166 uint32_t
end = m_endIndexTable[nearbyKey];
168 for (uint32_t jj = start; jj <
end; ++jj)
170 uint32_t j = m_sortedIndices[jj];
171 float4 p = m_points[jj];
172 float4 direction = p - origin;
174 float distanceSquared = LengthSquared(direction);
175 if (distanceSquared <= queryRadiusSquared)
177 if (distanceSquared > 0)
179 float distance = sqrtf(distanceSquared);
180 direction /= distance;
183 m_callback(idx, origin, j, p);
189 template <
typename Callback>
190 void CUDAPointHashGridSearcher3::ForEachNearbyPoint(
191 const ConstCUDAArrayView1<float4>& origins,
float radius,
192 Callback callback)
const 195 thrust::counting_iterator<size_t>(0),
196 thrust::counting_iterator<size_t>(0) + origins.Length(),
197 ForEachNearbyPointFunc<Callback>(
198 radius, m_gridSpacing, m_resolution, m_startIndexTable.data(),
199 m_endIndexTable.data(), m_sortedIndices.data(), m_points.data(),
200 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