22// Taken from https://github.com/HuguesTHOMAS/KPConv
33
44#include " neighbors.h"
5+ #include < chrono>
56#include < random>
67
78template <typename scalar_t >
@@ -29,6 +30,7 @@ int nanoflann_neighbors(vector<scalar_t>& queries, vector<scalar_t>& supports,
2930 // CLoud variable
3031 PointCloud<scalar_t > pcd;
3132 pcd.set (supports);
33+
3234 // Cloud query
3335 PointCloud<scalar_t > pcd_query;
3436 pcd_query.set (queries);
@@ -50,17 +52,17 @@ int nanoflann_neighbors(vector<scalar_t>& queries, vector<scalar_t>& supports,
5052 // Search params
5153 nanoflann::SearchParams search_params;
5254 search_params.sorted = sorted;
53- std::vector<std::vector<std::pair<size_t , scalar_t >>> list_matches (pcd_query.pts .size ());
55+ auto num_query_points = pcd_query.get_point_count ();
56+ std::vector<std::vector<std::pair<size_t , scalar_t >>> list_matches (num_query_points);
5457
55- for (auto & p0 : pcd_query. pts )
58+ for (size_t i = 0 ; i < num_query_points; i++ )
5659 {
5760 // Find neighbors
58- scalar_t query_pt[3 ] = {p0.x , p0.y , p0.z };
5961 list_matches[i0].reserve (max_count);
6062 std::vector<std::pair<size_t , scalar_t >> ret_matches;
6163
62- const size_t nMatches =
63- index-> radiusSearch (&query_pt[ 0 ], search_radius, ret_matches, search_params);
64+ const size_t nMatches = index-> radiusSearch (pcd_query. get_point_ptr (i), search_radius,
65+ ret_matches, search_params);
6466 if (nMatches == 0 )
6567 list_matches[i0] = {std::make_pair (0 , -1 )};
6668 else
@@ -164,10 +166,11 @@ int batch_nanoflann_neighbors(vector<scalar_t>& queries, vector<scalar_t>& suppo
164166 PointCloud<scalar_t > current_cloud;
165167 PointCloud<scalar_t > query_pcd;
166168 query_pcd.set (queries);
167- vector<vector<pair<size_t , scalar_t >>> all_inds_dists (query_pcd.pts .size ());
169+ auto num_query_points = query_pcd.get_point_count ();
170+ vector<vector<pair<size_t , scalar_t >>> all_inds_dists (num_query_points);
168171
169172 // Tree parameters
170- nanoflann::KDTreeSingleIndexAdaptorParams tree_params (10 /* max leaf */ );
173+ nanoflann::KDTreeSingleIndexAdaptorParams tree_params (15 /* max leaf */ );
171174
172175 // KDTree type definition
173176 typedef nanoflann::KDTreeSingleIndexAdaptor<
@@ -178,34 +181,33 @@ int batch_nanoflann_neighbors(vector<scalar_t>& queries, vector<scalar_t>& suppo
178181 current_cloud.set_batch (supports, s_batches[b], s_batches[b + 1 ]);
179182 std::unique_ptr<my_kd_tree_t > index (new my_kd_tree_t (3 , current_cloud, tree_params));
180183 index->buildIndex ();
184+
181185 // Search neigbors indices
182186 // ***********************
183187 // Search params
184188 nanoflann::SearchParams search_params;
185189 search_params.sorted = sorted;
186- for (auto & p0 : query_pcd.pts )
190+ std::chrono::microseconds duration_search (0 );
191+ for (size_t i = 0 ; i < num_query_points; i++)
187192 {
188193 // Check if we changed batch
189-
190194 if (i0 == q_batches[b + 1 ] && b < (int )s_batches.size () - 1 &&
191195 b < (int )q_batches.size () - 1 )
192196 {
193197 // Change the points
194198 b++;
195- current_cloud.pts .clear ();
196199 if (s_batches[b] < s_batches[b + 1 ])
197200 current_cloud.set_batch (supports, s_batches[b], s_batches[b + 1 ]);
198201
199- // Build KDTree of the current element of the batch
200202 index.reset (new my_kd_tree_t (3 , current_cloud, tree_params));
201203 index->buildIndex ();
202204 }
203205
204206 // Find neighboors
205207 std::vector<std::pair<size_t , scalar_t >> ret_matches;
206208 ret_matches.reserve (max_count);
207- scalar_t query_pt[ 3 ] = {p0. x , p0. y , p0. z };
208- size_t nMatches = index->radiusSearch (query_pt , r2, ret_matches, search_params);
209+ size_t nMatches =
210+ index->radiusSearch (query_pcd. get_point_ptr (i) , r2, ret_matches, search_params);
209211
210212 // Shuffle if needed
211213 if (!sorted)
@@ -225,8 +227,8 @@ int batch_nanoflann_neighbors(vector<scalar_t>& queries, vector<scalar_t>& suppo
225227 const int token = -1 ;
226228 if (mode == 0 )
227229 {
228- neighbors_indices.resize (query_pcd.pts . size () * max_count);
229- dists.resize (query_pcd.pts . size () * max_count);
230+ neighbors_indices.resize (query_pcd.get_point_count () * max_count);
231+ dists.resize (query_pcd.get_point_count () * max_count);
230232 i0 = 0 ;
231233 b = 0 ;
232234
@@ -319,14 +321,15 @@ void nanoflann_knn_neighbors(vector<scalar_t>& queries, vector<scalar_t>& suppor
319321 // Search neigbors indices
320322 // ***********************
321323 size_t current_pos = 0 ;
322- for (auto & p0 : pcd_query.pts )
324+ auto num_query_points = pcd_query.get_point_count ();
325+ for (size_t i = 0 ; i < num_query_points; i++)
323326 {
324327 // Find neighbors
325- scalar_t query_pt[3 ] = {p0.x , p0.y , p0.z };
326328 std::vector<size_t > ret_index (k);
327329 std::vector<scalar_t > out_dist_sqr (k);
328330
329- const size_t nMatches = index->knnSearch (&query_pt[0 ], k, &ret_index[0 ], &out_dist_sqr[0 ]);
331+ const size_t nMatches =
332+ index->knnSearch (pcd_query.get_point_ptr (i), k, &ret_index[0 ], &out_dist_sqr[0 ]);
330333 for (size_t i = 0 ; i < nMatches; i++)
331334 {
332335 neighbors_indices[i + current_pos] = ret_index[i];
0 commit comments