29 #include <tbb/parallel_for.h> 82 const size_t points_row_splits_size,
83 const int64_t* points_row_splits,
85 const size_t hash_table_cell_splits_size,
91 const int batch_size = points_row_splits_size - 1;
92 const T voxel_size = 2 * radius;
93 const T inv_voxel_size = 1 / voxel_size;
95 memset(&hash_table_cell_splits[0], 0,
96 sizeof(
uint32_t) * hash_table_cell_splits_size);
99 for (
int i = 0; i < batch_size; ++i) {
100 const size_t hash_table_size =
101 hash_table_splits[i + 1] - hash_table_splits[i];
102 const size_t first_cell_idx = hash_table_splits[i];
104 tbb::blocked_range<int64_t>(points_row_splits[i],
105 points_row_splits[i + 1]),
106 [&](
const tbb::blocked_range<int64_t>& r) {
107 for (int64_t i = r.begin(); i != r.end(); ++i) {
108 Vec3_t pos(points + 3 * i);
118 &hash_table_cell_splits[first_cell_idx + hash +
125 &hash_table_cell_splits[hash_table_cell_splits_size],
126 &hash_table_cell_splits[0]);
128 std::vector<uint32_t> count_tmp(hash_table_cell_splits_size - 1, 0);
131 for (
int i = 0; i < batch_size; ++i) {
132 const size_t hash_table_size =
133 hash_table_splits[i + 1] - hash_table_splits[i];
134 const size_t first_cell_idx = hash_table_splits[i];
136 tbb::blocked_range<size_t>(points_row_splits[i],
137 points_row_splits[i + 1]),
138 [&](
const tbb::blocked_range<size_t>& r) {
139 for (
size_t i = r.begin(); i != r.end(); ++i) {
140 Vec3_t pos(points + 3 * i);
148 [hash_table_cell_splits[hash + first_cell_idx] +
150 &count_tmp[hash + first_cell_idx],
174 template <
int METRIC,
class TDerived,
int VECSIZE>
175 Eigen::Array<typename TDerived::Scalar, VECSIZE, 1> NeighborsDist(
176 const Eigen::ArrayBase<TDerived>& p,
177 const Eigen::Array<typename TDerived::Scalar, VECSIZE, 3>& points) {
178 typedef Eigen::Array<typename TDerived::Scalar, VECSIZE, 1> VecN_t;
182 if (METRIC ==
Linf) {
183 dist = (points.rowwise() - p.transpose()).abs().rowwise().maxCoeff();
184 }
else if (METRIC ==
L1) {
185 dist = (points.rowwise() - p.transpose()).abs().rowwise().sum();
187 dist = (points.rowwise() - p.transpose()).square().rowwise().sum();
196 class OUTPUT_ALLOCATOR,
198 bool IGNORE_QUERY_POINT,
199 bool RETURN_DISTANCES>
200 void _FixedRadiusSearchCPU(int64_t* query_neighbors_row_splits,
202 const T*
const points,
204 const T*
const queries,
206 const size_t points_row_splits_size,
207 const int64_t*
const points_row_splits,
208 const size_t queries_row_splits_size,
209 const int64_t*
const queries_row_splits,
210 const uint32_t*
const hash_table_splits,
211 const size_t hash_table_cell_splits_size,
212 const uint32_t*
const hash_table_cell_splits,
213 const uint32_t*
const hash_table_index,
214 OUTPUT_ALLOCATOR& output_allocator) {
220 typedef Eigen::Array<T, VECSIZE, 1> Vec_t;
221 typedef Eigen::Array<TIndex, VECSIZE, 1> Veci_t;
223 typedef Eigen::Array<T, 3, 1> Pos_t;
224 typedef Eigen::Array<T, VECSIZE, 3> Poslist_t;
225 typedef Eigen::Array<bool, VECSIZE, 1> Result_t;
227 const int batch_size = points_row_splits_size - 1;
230 if (num_points == 0 || num_queries == 0) {
231 std::fill(query_neighbors_row_splits,
232 query_neighbors_row_splits + num_queries + 1, 0);
234 output_allocator.AllocIndices(&indices_ptr, 0);
237 output_allocator.AllocDistances(&distances_ptr, 0);
243 const T threshold = (METRIC ==
L2 ? radius * radius : radius);
245 const T voxel_size = 2 * radius;
246 const T inv_voxel_size = 1 / voxel_size;
250 size_t num_indices = 0;
255 for (
int i = 0; i < batch_size; ++i) {
256 const size_t hash_table_size =
257 hash_table_splits[i + 1] - hash_table_splits[i];
258 const size_t first_cell_idx = hash_table_splits[i];
260 tbb::blocked_range<size_t>(queries_row_splits[i],
261 queries_row_splits[i + 1]),
262 [&](
const tbb::blocked_range<size_t>& r) {
263 size_t num_indices_local = 0;
264 for (
size_t i = r.begin(); i != r.end(); ++i) {
265 size_t neighbors_count = 0;
267 Vec3_t pos(queries + i * 3);
269 std::set<size_t> bins_to_visit;
276 bins_to_visit.insert(first_cell_idx + hash);
278 for (
int dz = -1; dz <= 1; dz += 2)
279 for (
int dy = -1; dy <= 1; dy += 2)
280 for (
int dx = -1; dx <= 1; dx += 2) {
282 pos + radius * Vec3_t(T(dx), T(dy),
288 bins_to_visit.insert(first_cell_idx + hash);
294 for (
size_t bin : bins_to_visit) {
295 size_t begin_idx = hash_table_cell_splits[bin];
296 size_t end_idx = hash_table_cell_splits[bin + 1];
298 for (
size_t j = begin_idx; j < end_idx; ++j) {
300 if (IGNORE_QUERY_POINT) {
301 if (points[idx * 3 + 0] == pos[0] &&
302 points[idx * 3 + 1] == pos[1] &&
303 points[idx * 3 + 2] == pos[2])
306 xyz(vec_i, 0) = points[idx * 3 + 0];
307 xyz(vec_i, 1) = points[idx * 3 + 1];
308 xyz(vec_i, 2) = points[idx * 3 + 2];
311 Pos_t pos_arr(pos[0], pos[1], pos[2]);
312 Vec_t dist = NeighborsDist<METRIC, Pos_t,
315 Result_t test_result = dist <= threshold;
316 neighbors_count += test_result.count();
323 Pos_t pos_arr(pos[0], pos[1], pos[2]);
324 Vec_t dist = NeighborsDist<METRIC, Pos_t, VECSIZE>(
326 Result_t test_result = dist <= threshold;
327 for (
int k = 0; k < vec_i; ++k) {
328 neighbors_count +=
int(test_result(k));
332 num_indices_local += neighbors_count;
334 query_neighbors_row_splits[i + 1] = neighbors_count;
345 output_allocator.AllocIndices(&indices_ptr, num_indices);
349 if (RETURN_DISTANCES)
350 output_allocator.AllocDistances(&distances_ptr, num_indices);
352 output_allocator.AllocDistances(&distances_ptr, 0);
354 query_neighbors_row_splits[0] = 0;
356 query_neighbors_row_splits + num_queries + 1,
357 query_neighbors_row_splits + 1);
360 for (
int i = 0; i < batch_size; ++i) {
361 const size_t hash_table_size =
362 hash_table_splits[i + 1] - hash_table_splits[i];
363 const size_t first_cell_idx = hash_table_splits[i];
365 tbb::blocked_range<size_t>(queries_row_splits[i],
366 queries_row_splits[i + 1]),
367 [&](
const tbb::blocked_range<size_t>& r) {
368 for (
size_t i = r.begin(); i != r.end(); ++i) {
369 size_t neighbors_count = 0;
371 size_t indices_offset = query_neighbors_row_splits[i];
373 Vec3_t pos(queries[i * 3 + 0], queries[i * 3 + 1],
376 std::set<size_t> bins_to_visit;
383 bins_to_visit.insert(first_cell_idx + hash);
385 for (
int dz = -1; dz <= 1; dz += 2)
386 for (
int dy = -1; dy <= 1; dy += 2)
387 for (
int dx = -1; dx <= 1; dx += 2) {
389 pos + radius * Vec3_t(T(dx), T(dy),
395 bins_to_visit.insert(first_cell_idx + hash);
402 for (
size_t bin : bins_to_visit) {
403 size_t begin_idx = hash_table_cell_splits[bin];
404 size_t end_idx = hash_table_cell_splits[bin + 1];
406 for (
size_t j = begin_idx; j < end_idx; ++j) {
407 int64_t idx = hash_table_index[j];
408 if (IGNORE_QUERY_POINT) {
409 if (points[idx * 3 + 0] == pos[0] &&
410 points[idx * 3 + 1] == pos[1] &&
411 points[idx * 3 + 2] == pos[2])
414 xyz(vec_i, 0) = points[idx * 3 + 0];
415 xyz(vec_i, 1) = points[idx * 3 + 1];
416 xyz(vec_i, 2) = points[idx * 3 + 2];
417 idx_vec(vec_i) = idx;
420 Pos_t pos_arr(pos[0], pos[1], pos[2]);
421 Vec_t dist = NeighborsDist<METRIC, Pos_t,
424 Result_t test_result = dist <= threshold;
425 for (
int k = 0; k < vec_i; ++k) {
426 if (test_result(k)) {
427 indices_ptr[indices_offset +
430 if (RETURN_DISTANCES) {
431 distances_ptr[indices_offset +
436 neighbors_count +=
int(test_result(k));
444 Pos_t pos_arr(pos[0], pos[1], pos[2]);
445 Vec_t dist = NeighborsDist<METRIC, Pos_t, VECSIZE>(
447 Result_t test_result = dist <= threshold;
448 for (
int k = 0; k < vec_i; ++k) {
449 if (test_result(k)) {
450 indices_ptr[indices_offset +
451 neighbors_count] = idx_vec[k];
452 if (RETURN_DISTANCES) {
453 distances_ptr[indices_offset +
458 neighbors_count +=
int(test_result(k));
548 template <
class T,
class TIndex,
class OUTPUT_ALLOCATOR>
550 const size_t num_points,
552 const size_t num_queries,
553 const T*
const queries,
555 const size_t points_row_splits_size,
556 const int64_t*
const points_row_splits,
557 const size_t queries_row_splits_size,
558 const int64_t*
const queries_row_splits,
559 const uint32_t*
const hash_table_splits,
560 const size_t hash_table_cell_splits_size,
561 const uint32_t*
const hash_table_cell_splits,
562 const uint32_t*
const hash_table_index,
564 const bool ignore_query_point,
565 const bool return_distances,
566 OUTPUT_ALLOCATOR& output_allocator) {
569 #define FN_PARAMETERS \ 570 query_neighbors_row_splits, num_points, points, num_queries, queries, \ 571 radius, points_row_splits_size, points_row_splits, \ 572 queries_row_splits_size, queries_row_splits, hash_table_splits, \ 573 hash_table_cell_splits_size, hash_table_cell_splits, \ 574 hash_table_index, output_allocator 576 #define CALL_TEMPLATE(METRIC, IGNORE_QUERY_POINT, RETURN_DISTANCES) \ 577 if (METRIC == metric && IGNORE_QUERY_POINT == ignore_query_point && \ 578 RETURN_DISTANCES == return_distances) \ 579 _FixedRadiusSearchCPU<T, TIndex, OUTPUT_ALLOCATOR, METRIC, \ 580 IGNORE_QUERY_POINT, RETURN_DISTANCES>( \ 583 #define CALL_TEMPLATE2(METRIC) \ 584 CALL_TEMPLATE(METRIC, true, true) \ 585 CALL_TEMPLATE(METRIC, true, false) \ 586 CALL_TEMPLATE(METRIC, false, true) \ 587 CALL_TEMPLATE(METRIC, false, false) 589 #define CALL_TEMPLATE3 \ 597 #undef CALL_TEMPLATE2 598 #undef CALL_TEMPLATE3
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle uint32_t
Definition: K4aPlugin.cpp:567
Definition: NeighborSearchCommon.h:38
HOST_DEVICE size_t SpatialHash(int x, int y, int z)
Spatial hashing function for integer coordinates.
Definition: NeighborSearchCommon.h:47
void InclusivePrefixSum(const Tin *first, const Tin *last, Tout *out)
Definition: ParallelScan.h:90
Metric
Supported metrics.
Definition: NeighborSearchCommon.h:38
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample uint64_t
Definition: K4aPlugin.cpp:362
Definition: NeighborSearchCommon.h:38
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c int
Definition: K4aPlugin.cpp:489
void BuildSpatialHashTableCPU(const Tensor &points, double radius, const Tensor &points_row_splits, const Tensor &hash_table_splits, Tensor &hash_table_index, Tensor &hash_table_cell_splits)
Definition: FixedRadiusSearchOps.cpp:40
Definition: PinholeCameraIntrinsic.cpp:35
void FixedRadiusSearchCPU(int64_t *query_neighbors_row_splits, const size_t num_points, const T *const points, const size_t num_queries, const T *const queries, const T radius, const size_t points_row_splits_size, const int64_t *const points_row_splits, const size_t queries_row_splits_size, const int64_t *const queries_row_splits, const uint32_t *const hash_table_splits, const size_t hash_table_cell_splits_size, const uint32_t *const hash_table_cell_splits, const uint32_t *const hash_table_index, const Metric metric, const bool ignore_query_point, const bool return_distances, OUTPUT_ALLOCATOR &output_allocator)
Definition: FixedRadiusSearchImpl.h:549
uint32_t AtomicFetchAddRelaxed(uint32_t *address, uint32_t val)
Definition: Atomic.h:44
Definition: Dispatch.h:110
Definition: NeighborSearchCommon.h:38
HOST_DEVICE utility::MiniVec< int, 3 > ComputeVoxelIndex(const TVecf &pos, const typename TVecf::Scalar_t &inv_voxel_size)
Definition: NeighborSearchCommon.h:61