neighbors.cpp 8.76 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
82
	// CLoud variable
	PointCloud<scalar_t> pcd;
	pcd.set(supports, dim);
	//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
101

	// 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;
102
	// search_params.sorted = true;
103
104
105
106
107
108
109
110
111
112
113
114
	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
115

116
117
118
119
120
		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
121

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

125
126
127
128
129
			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
130

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

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

		std::thread* tid[actual_threads];
Alexander Liao's avatar
Alexander Liao committed
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
181
182
183
184
185
186
187
188
189
190
		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;
			/*
			if (res.rem == 0) {
				length = res.quot;
			}
			else {
				length = 
			}
			*/
		}
		for (size_t t = 0; t < actual_threads; t++) {
			//sem->wait();
			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
191
	}
192

Alexander Liao's avatar
Alexander Liao committed
193
194
	// Reserve the memory
	if(max_num > 0) {
195
		*max_count = max_num;
Alexander Liao's avatar
Alexander Liao committed
196
197
	}
	
198
	size_t size = 0; // total number of edges
199
200
	for (auto& inds : *list_matches){
		if(inds.size() <= *max_count)
Alexander Liao's avatar
Alexander Liao committed
201
202
			size += inds.size();
		else
203
			size += *max_count;
Alexander Liao's avatar
Alexander Liao committed
204
	}
205

206
	neighbors_indices->resize(size*2);
207
208
	size_t i1 = 0; // index of the query points
	size_t u = 0; // curent index of the neighbors_indices
209
210
	for (auto& inds : *list_matches){
		for (size_t j = 0; j < *max_count; j++){
Alexander Liao's avatar
Alexander Liao committed
211
			if(j < inds.size()){
212
213
				(*neighbors_indices)[u] = inds[j].first;
				(*neighbors_indices)[u + 1] = i1;
Alexander Liao's avatar
Alexander Liao committed
214
215
216
217
218
219
				u += 2;
			}
		}
		i1++;
	}

220
	return *max_count;
Alexander Liao's avatar
Alexander Liao committed
221
222
223
224




225
226
227
}

template<typename scalar_t>
228
229
230
231
232
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,
233
                               double radius, int dim, int64_t max_num){
234
235
236
237
238


// Initiate variables
// ******************
// indices
239
	size_t i0 = 0;
240
241
242
243
244

// Square radius
	const scalar_t r2 = static_cast<scalar_t>(radius*radius);

	// Counting vector
245
	size_t max_count = 0;
246
247

	// batch index
248
249
250
	size_t b = 0;
	size_t sum_qb = 0;
	size_t sum_sb = 0;
251

252
253
254
255
256
257
258
	double eps;
	if (supports.size() < 10){
		eps = 0.000001;
	}
	else {
		eps = 0;
	}
259
260
261
262
263
264
265
	// Nanoflann related variables
	// ***************************

	// CLoud variable
	PointCloud<scalar_t> current_cloud;
	PointCloud<scalar_t> query_pcd;
	query_pcd.set(queries, dim);
266
	std::vector<std::vector<std::pair<size_t, scalar_t> > > all_inds_dists(query_pcd.pts.size());
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285

	// 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;

// Pointer to trees
	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();
// Search neigbors indices
// ***********************
// Search params
	nanoflann::SearchParams search_params;
	search_params.sorted = true;

286
287
	for (auto& p : query_pcd.pts){
		auto p0 = *p;
288
289
// Check if we changed batch

290
		scalar_t* query_pt = new scalar_t[dim];
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
		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++;

// Change the points
			current_cloud.pts.clear();
			current_cloud.set_batch(supports, sum_sb, s_batches[b], dim);
// Build KDTree of the current element of the batch
			delete index;
			index = new my_kd_tree_t(dim, current_cloud, tree_params);
			index->buildIndex();
		}
// Initial guess of neighbors size
		all_inds_dists[i0].reserve(max_count);
// Find neighbors
		size_t nMatches = index->radiusSearch(query_pt, r2+eps, all_inds_dists[i0], search_params);
// Update max count

		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;
// Increment query idx
		i0++;
	}
	// how many neighbors do we keep
	if(max_num > 0) {
		max_count = max_num;
	}
// Reserve the memory
	
328
	size_t size = 0; // total number of edges
329
330
331
332
333
334
	for (auto& inds_dists : all_inds_dists){
		if(inds_dists.size() <= max_count)
			size += inds_dists.size();
		else
			size += max_count;
	}
335
	neighbors_indices->resize(size * 2);
336
337
338
339
	i0 = 0;
	sum_sb = 0;
	sum_qb = 0;
	b = 0;
340
	size_t u = 0;
341
342
343
344
345
346
	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++;
		}
347
		for (size_t j = 0; j < max_count; j++){
348
			if (j < inds_dists.size()){
349
350
				(*neighbors_indices)[u] = inds_dists[j].first + sum_sb;
				(*neighbors_indices)[u + 1] = i0;
351
352
353
354
355
356
357
358
				u += 2;
			}
		}
		i0++;
	}
	
	return max_count;
}