neighbors.cpp 8.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "cloud.h"
#include "nanoflann.hpp"
#include <set>
#include <cstdint>
#include <thread>

typedef struct thread_struct {
	void* kd_tree;
	void* matches;
	void* queries;
	size_t* max_count;
	std::mutex* ct_m;
	std::mutex* tree_m;
	size_t start;
	size_t end;
	double search_radius;
	bool small;
} thread_args;
Alexander Liao's avatar
Alexander Liao committed
19

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
template<typename scalar_t>
void thread_routine(thread_args* targs) {
	typedef nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Adaptor<scalar_t, PointCloud<scalar_t> > , PointCloud<scalar_t>> my_kd_tree_t;
	typedef std::vector< std::vector<std::pair<size_t, scalar_t> > > kd_pair;
	my_kd_tree_t* index = (my_kd_tree_t*) targs->kd_tree;
	kd_pair* matches = (kd_pair*)targs->matches;
	PointCloud<scalar_t>* pcd_query = (PointCloud<scalar_t>*)targs->queries;
	size_t* max_count = targs->max_count;
	std::mutex* ct_m = targs->ct_m;
	std::mutex* tree_m = targs->tree_m;
	double eps;
	if (targs->small) {
		eps = 0.000001;
	}
	else {
		eps = 0;
	}
	double search_radius = (double) targs->search_radius;
	size_t start = targs->start;
	size_t end = targs->end;
	
	for (size_t i = start; i < end; i++) {

		std::vector<scalar_t> p0 = *(((*pcd_query).pts)[i]);

		scalar_t* query_pt = new scalar_t[p0.size()];
		std::copy(p0.begin(), p0.end(), query_pt);
		(*matches)[i].reserve(*max_count);
		std::vector<std::pair<size_t, scalar_t> > ret_matches;

		tree_m->lock();

		const size_t nMatches = index->radiusSearch(query_pt, (scalar_t)(search_radius+eps), ret_matches, nanoflann::SearchParams());
		
		tree_m->unlock();

		(*matches)[i] = ret_matches;
		
		ct_m->lock();
		if(*max_count < nMatches) {
			*max_count = nMatches;
		}
		ct_m->unlock();
	
	}
Alexander Liao's avatar
Alexander Liao committed
65

66
}
Alexander Liao's avatar
Alexander Liao committed
67
68

template<typename scalar_t>
69
70
size_t nanoflann_neighbors(std::vector<scalar_t>& queries, std::vector<scalar_t>& supports,
			std::vector<size_t>*& neighbors_indices, double radius, int dim, int64_t max_num, int64_t n_threads){
Alexander Liao's avatar
Alexander Liao committed
71
72
73
74

	const scalar_t search_radius = static_cast<scalar_t>(radius*radius);

	// Counting vector
75
76
	size_t* max_count = new size_t();
	*max_count = 1;
Alexander Liao's avatar
Alexander Liao committed
77

78
	size_t ssize = supports.size();
Alexander Liao's avatar
Alexander Liao committed
79
80
81
	// CLoud variable
	PointCloud<scalar_t> pcd;
	pcd.set(supports, dim);
82
	// Cloud query
83
84
	PointCloud<scalar_t>* pcd_query = new PointCloud<scalar_t>();
	(*pcd_query).set(queries, dim);
Alexander Liao's avatar
Alexander Liao committed
85
86

	// Tree parameters
87
	nanoflann::KDTreeSingleIndexAdaptorParams tree_params(15 /* max leaf */);
Alexander Liao's avatar
Alexander Liao committed
88
89
90

	// KDTree type definition
	typedef nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Adaptor<scalar_t, PointCloud<scalar_t> > , PointCloud<scalar_t>> my_kd_tree_t;
91
	typedef std::vector< std::vector<std::pair<size_t, scalar_t> > > kd_pair;
Alexander Liao's avatar
Alexander Liao committed
92
93
94
95
96
97
98
99
100

	// Pointer to trees
	my_kd_tree_t* index;
	index = new my_kd_tree_t(dim, pcd, tree_params);
	index->buildIndex();
	// Search neigbors indices

	// Search params
	nanoflann::SearchParams search_params;
101
	// search_params.sorted = true;
102
103
104
105
106
107
108
109
110
111
112
113
	kd_pair* list_matches = new kd_pair((*pcd_query).pts.size());

	// single threaded routine
	if (n_threads == 1){
		size_t i0 = 0;
		double eps;
		if (ssize < 10) {
			eps = 0.000001;
		}
		else {
			eps = 0;
		}
Alexander Liao's avatar
Alexander Liao committed
114

115
116
117
118
119
		for (auto& p : (*pcd_query).pts){
			auto p0 = *p;
			// Find neighbors
			scalar_t* query_pt = new scalar_t[dim];
			std::copy(p0.begin(), p0.end(), query_pt); 
Alexander Liao's avatar
Alexander Liao committed
120

121
122
			(*list_matches)[i0].reserve(*max_count);
			std::vector<std::pair<size_t, scalar_t> > ret_matches;
123

124
125
126
127
128
			const size_t nMatches = index->radiusSearch(query_pt, (scalar_t)(search_radius+eps), ret_matches, search_params);
			
			(*list_matches)[i0] = ret_matches;
			if(*max_count < nMatches) *max_count = nMatches;
			i0++;
Alexander Liao's avatar
Alexander Liao committed
129

130
131
132
133
134
		}
	}
	else {// Multi-threaded routine
		std::mutex* mtx = new std::mutex();
		std::mutex* mtx_tree = new std::mutex();
Alexander Liao's avatar
Alexander Liao committed
135

136
137
138
		size_t n_queries = (*pcd_query).pts.size();
		size_t actual_threads = std::min((long long)n_threads, (long long)n_queries);

139
		std::vector<std::thread*> tid(actual_threads);
Alexander Liao's avatar
Alexander Liao committed
140

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
		size_t start, end;
		size_t length;
		if (n_queries) {
			length = 1;
		}
		else {
			auto res = std::lldiv((long long)n_queries, (long long)n_threads);
			length = (size_t)res.quot;
		}
		for (size_t t = 0; t < actual_threads; t++) {
			start = t*length;
			if (t == actual_threads-1) {
				end = n_queries;
			}
			else {
				end = (t+1)*length;
			}
			thread_args* targs = new thread_args();
			targs->kd_tree = index;
			targs->matches = list_matches;
			targs->max_count = max_count;
			targs->ct_m = mtx;
			targs->tree_m = mtx_tree;
			targs->search_radius = search_radius;
			targs->queries = pcd_query;
			targs->start = start;
			targs->end = end;
			if (ssize < 10) {
				targs->small = true;
			}
			else {
				targs->small = false;
			}
			std::thread* temp = new std::thread(thread_routine<scalar_t>, targs);
			tid[t] = temp;
		}

		for (size_t t = 0; t < actual_threads; t++){
			tid[t]->join();
		}
Alexander Liao's avatar
Alexander Liao committed
181
	}
182

Alexander Liao's avatar
Alexander Liao committed
183
184
	// Reserve the memory
	if(max_num > 0) {
185
		*max_count = max_num;
Alexander Liao's avatar
Alexander Liao committed
186
187
	}
	
188
	size_t size = 0; // total number of edges
189
190
	for (auto& inds : *list_matches){
		if(inds.size() <= *max_count)
Alexander Liao's avatar
Alexander Liao committed
191
192
			size += inds.size();
		else
193
			size += *max_count;
Alexander Liao's avatar
Alexander Liao committed
194
	}
195

196
	neighbors_indices->resize(size*2);
197
198
	size_t i1 = 0; // index of the query points
	size_t u = 0; // curent index of the neighbors_indices
199
200
	for (auto& inds : *list_matches){
		for (size_t j = 0; j < *max_count; j++){
Alexander Liao's avatar
Alexander Liao committed
201
			if(j < inds.size()){
202
203
				(*neighbors_indices)[u] = inds[j].first;
				(*neighbors_indices)[u + 1] = i1;
Alexander Liao's avatar
Alexander Liao committed
204
205
206
207
208
209
				u += 2;
			}
		}
		i1++;
	}

210
	return *max_count;
Alexander Liao's avatar
Alexander Liao committed
211
212
213
214




215
216
217
}

template<typename scalar_t>
218
219
220
221
222
size_t batch_nanoflann_neighbors (std::vector<scalar_t>& queries,
                               std::vector<scalar_t>& supports,
                               std::vector<long>& q_batches,
                               std::vector<long>& s_batches,
                               std::vector<size_t>*& neighbors_indices,
223
                               double radius, int dim, int64_t max_num){
224
225


226
	// indices
227
	size_t i0 = 0;
228

229
	// Square radius
230
231
232
	const scalar_t r2 = static_cast<scalar_t>(radius*radius);

	// Counting vector
233
	size_t max_count = 0;
234
235

	// batch index
236
237
238
	size_t b = 0;
	size_t sum_qb = 0;
	size_t sum_sb = 0;
239

240
241
242
243
244
245
246
	double eps;
	if (supports.size() < 10){
		eps = 0.000001;
	}
	else {
		eps = 0;
	}
247
248
249
250
251
252
	// Nanoflann related variables

	// CLoud variable
	PointCloud<scalar_t> current_cloud;
	PointCloud<scalar_t> query_pcd;
	query_pcd.set(queries, dim);
253
	std::vector<std::vector<std::pair<size_t, scalar_t> > > all_inds_dists(query_pcd.pts.size());
254
255
256
257
258
259
260

	// Tree parameters
	nanoflann::KDTreeSingleIndexAdaptorParams tree_params(10 /* max leaf */);

	// KDTree type definition
	typedef nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Adaptor<scalar_t, PointCloud<scalar_t> > , PointCloud<scalar_t>> my_kd_tree_t;

261
	// Pointer to trees
262
263
264
265
266
	my_kd_tree_t* index;
    // Build KDTree for the first batch element
	current_cloud.set_batch(supports, sum_sb, s_batches[b], dim);
	index = new my_kd_tree_t(dim, current_cloud, tree_params);
	index->buildIndex();
267
268
	// Search neigbors indices
	// Search params
269
270
271
	nanoflann::SearchParams search_params;
	search_params.sorted = true;

272
273
	for (auto& p : query_pcd.pts){
		auto p0 = *p;
274
		// Check if we changed batch
275

276
		scalar_t* query_pt = new scalar_t[dim];
277
278
279
280
281
282
283
		std::copy(p0.begin(), p0.end(), query_pt); 

		if (i0 == sum_qb + q_batches[b]){
			sum_qb += q_batches[b];
			sum_sb += s_batches[b];
			b++;

284
			// Change the points
285
286
			current_cloud.pts.clear();
			current_cloud.set_batch(supports, sum_sb, s_batches[b], dim);
287
			// Build KDTree of the current element of the batch
288
289
290
291
			delete index;
			index = new my_kd_tree_t(dim, current_cloud, tree_params);
			index->buildIndex();
		}
292
		// Initial guess of neighbors size
293
		all_inds_dists[i0].reserve(max_count);
294
		// Find neighbors
295
		size_t nMatches = index->radiusSearch(query_pt, r2+eps, all_inds_dists[i0], search_params);
296
		// Update max count
297
298
299
300
301
302
303
304

		std::vector<std::pair<size_t, float> > indices_dists;
		nanoflann::RadiusResultSet<float,size_t> resultSet(r2, indices_dists);

		index->findNeighbors(resultSet, query_pt, search_params);

		if (nMatches > max_count)
			max_count = nMatches;
305
		// Increment query idx
306
307
		i0++;
	}
308
309
310


	
311
312
313
314
	// how many neighbors do we keep
	if(max_num > 0) {
		max_count = max_num;
	}
315
	// Reserve the memory
316
	
317
	size_t size = 0; // total number of edges
318
319
320
321
322
323
	for (auto& inds_dists : all_inds_dists){
		if(inds_dists.size() <= max_count)
			size += inds_dists.size();
		else
			size += max_count;
	}
324

325
	neighbors_indices->resize(size * 2);
326
327
328
329
	i0 = 0;
	sum_sb = 0;
	sum_qb = 0;
	b = 0;
330
	size_t u = 0;
331
332
333
334
335
336
	for (auto& inds_dists : all_inds_dists){
		if (i0 == sum_qb + q_batches[b]){
			sum_qb += q_batches[b];
			sum_sb += s_batches[b];
			b++;
		}
337
		for (size_t j = 0; j < max_count; j++){
338
			if (j < inds_dists.size()){
339
340
				(*neighbors_indices)[u] = inds_dists[j].first + sum_sb;
				(*neighbors_indices)[u + 1] = i0;
341
342
343
344
345
346
347
348
				u += 2;
			}
		}
		i0++;
	}
	
	return max_count;
}