Open3D (C++ API)  0.11.0
InvertNeighborsList.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2020 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <tbb/parallel_for.h>
30 
31 #include "open3d/core/Atomic.h"
33 
34 namespace open3d {
35 namespace ml {
36 namespace impl {
37 
89 template <class TIndex, class TAttr>
90 void InvertNeighborsListCPU(const TIndex* const inp_neighbors_index,
91  const TAttr* const inp_neighbors_attributes,
92  const int num_attributes_per_neighbor,
93  const int64_t* const inp_neighbors_row_splits,
94  const size_t inp_num_queries,
95  TIndex* out_neighbors_index,
96  TAttr* out_neighbors_attributes,
97  const size_t index_size,
98  int64_t* out_neighbors_row_splits,
99  const size_t out_num_queries) {
100  using namespace open3d::utility;
101 
102  std::vector<uint32_t> tmp_neighbors_count(out_num_queries + 1, 0);
103 
104  // count how often an idx appears in inp_neighbors_index
105  tbb::parallel_for(tbb::blocked_range<size_t>(0, index_size),
106  [&](const tbb::blocked_range<size_t>& r) {
107  for (size_t i = r.begin(); i != r.end(); ++i) {
108  TIndex idx = inp_neighbors_index[i];
110  &tmp_neighbors_count[idx + 1], 1);
111  }
112  });
113 
114  InclusivePrefixSum(&tmp_neighbors_count[0],
115  &tmp_neighbors_count[tmp_neighbors_count.size()],
116  out_neighbors_row_splits);
117 
118  memset(tmp_neighbors_count.data(), 0,
119  sizeof(uint32_t) * tmp_neighbors_count.size());
120 
121  // fill the new index vector
122  tbb::parallel_for(
123  tbb::blocked_range<size_t>(0, inp_num_queries),
124  [&](const tbb::blocked_range<size_t>& r) {
125  for (size_t i = r.begin(); i != r.end(); ++i) {
126  TIndex query_idx = i;
127 
128  size_t begin_idx = inp_neighbors_row_splits[i];
129  size_t end_idx = inp_neighbors_row_splits[i + 1];
130  for (size_t j = begin_idx; j < end_idx; ++j) {
131  TIndex neighbor_idx = inp_neighbors_index[j];
132 
133  size_t list_offset =
134  out_neighbors_row_splits[neighbor_idx];
135  size_t item_offset = core::AtomicFetchAddRelaxed(
136  &tmp_neighbors_count[neighbor_idx], 1);
137  out_neighbors_index[list_offset + item_offset] =
138  query_idx;
139 
140  if (inp_neighbors_attributes) {
141  TAttr* attr_ptr =
142  out_neighbors_attributes +
143  num_attributes_per_neighbor *
144  (list_offset + item_offset);
145  for (int attr_i = 0;
146  attr_i < num_attributes_per_neighbor;
147  ++attr_i) {
148  attr_ptr[attr_i] = inp_neighbors_attributes
149  [num_attributes_per_neighbor * j +
150  attr_i];
151  }
152  }
153  }
154  }
155  });
156 }
157 
158 } // namespace impl
159 } // namespace ml
160 } // namespace open3d
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:554
void InclusivePrefixSum(const Tin *first, const Tin *last, Tout *out)
Definition: ParallelScan.h:67
void InvertNeighborsListCPU(const TIndex *const inp_neighbors_index, const TAttr *const inp_neighbors_attributes, const int num_attributes_per_neighbor, const int64_t *const inp_neighbors_row_splits, const size_t inp_num_queries, TIndex *out_neighbors_index, TAttr *out_neighbors_attributes, const size_t index_size, int64_t *out_neighbors_row_splits, const size_t out_num_queries)
Definition: InvertNeighborsList.h:90
Definition: PinholeCameraIntrinsic.cpp:35
uint32_t AtomicFetchAddRelaxed(uint32_t *address, uint32_t val)
Definition: Atomic.h:40
Definition: Console.cpp:51