"tutorials/models/4_old_wines/README.txt" did not exist on "dafe46710b5f4a93bfdceb84c7201d1c83423394"
neighbors.cpp 10.2 KB
Newer Older
1
2
3
4
5
#include "cloud.h"
#include "nanoflann.hpp"
#include <set>
#include <cstdint>
#include <thread>
6
#include <iostream>
7
8
9
10
11
12
13
14
15
16
17
18

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;
19
20
	bool option;
	size_t k;
21
} thread_args;
Alexander Liao's avatar
Alexander Liao committed
22

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
43
	auto k = targs->k;
44
45
46
47
48
49
50
51
	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;
52
53
		std::vector<size_t>* knn_ret_matches = new std::vector<size_t>(k);
		std::vector<scalar_t>* knn_dist_matches = new std::vector<scalar_t>(k);
54
55
56

		tree_m->lock();

57
58
59
60
61
62
63
64
65
66
67
68
		size_t nMatches;
		if (targs->option){
			nMatches = index->radiusSearch(query_pt, (scalar_t)(search_radius+eps), ret_matches, nanoflann::SearchParams());
		}
		else {
			nMatches = index->knnSearch(query_pt, k, &(*knn_ret_matches)[0],&(* knn_dist_matches)[0]);
			auto temp = new std::vector<std::pair<size_t, scalar_t> >((*knn_dist_matches).size());
			for (size_t j = 0; j < (*knn_ret_matches).size(); j++){
				(*temp)[j] = std::make_pair( (*knn_ret_matches)[j],(*knn_dist_matches)[j] );
			}
			ret_matches = *temp;
		}
69
70
71
72
73
74
75
76
77
78
79
		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
80

81
}
Alexander Liao's avatar
Alexander Liao committed
82
83

template<typename scalar_t>
84
size_t nanoflann_neighbors(std::vector<scalar_t>& queries, std::vector<scalar_t>& supports,
85
86
			std::vector<size_t>*& neighbors_indices, double radius, int dim, 
			int64_t max_num, int64_t n_threads, int64_t k, int option){
Alexander Liao's avatar
Alexander Liao committed
87
88
89
90

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

	// Counting vector
91
92
	size_t* max_count = new size_t();
	*max_count = 1;
Alexander Liao's avatar
Alexander Liao committed
93

94
	size_t ssize = supports.size();
Alexander Liao's avatar
Alexander Liao committed
95
96
97
	// CLoud variable
	PointCloud<scalar_t> pcd;
	pcd.set(supports, dim);
98
	// Cloud query
99
100
	PointCloud<scalar_t>* pcd_query = new PointCloud<scalar_t>();
	(*pcd_query).set(queries, dim);
Alexander Liao's avatar
Alexander Liao committed
101
102

	// Tree parameters
103
	nanoflann::KDTreeSingleIndexAdaptorParams tree_params(15 /* max leaf */);
Alexander Liao's avatar
Alexander Liao committed
104
105
106

	// KDTree type definition
	typedef nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Adaptor<scalar_t, PointCloud<scalar_t> > , PointCloud<scalar_t>> my_kd_tree_t;
107
	typedef std::vector< std::vector<std::pair<size_t, scalar_t> > > kd_pair;
Alexander Liao's avatar
Alexander Liao committed
108
109
110
111
112
113
114
115
116

	// 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;
117
	// search_params.sorted = true;
118
119
120
121
122
123
124
125
126
127
128
129
	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
130

131
132
133
134
135
		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
136

137
138
			(*list_matches)[i0].reserve(*max_count);
			std::vector<std::pair<size_t, scalar_t> > ret_matches;
139
140
			std::vector<size_t>* knn_ret_matches = new std::vector<size_t>(k);
			std::vector<scalar_t>* knn_dist_matches = new std::vector<scalar_t>(k);
141

142
143
144
145
146
147
148
149
150
151
152
153
			size_t nMatches;
			if (!!(option)){
				nMatches = index->radiusSearch(query_pt, (scalar_t)(search_radius+eps), ret_matches, search_params);
			}
			else {
				nMatches = index->knnSearch(query_pt, (size_t)k, &(*knn_ret_matches)[0],&(* knn_dist_matches)[0]);
				auto temp = new std::vector<std::pair<size_t, scalar_t> >((*knn_dist_matches).size());
				for (size_t j = 0; j < (*knn_ret_matches).size(); j++){
					(*temp)[j] = std::make_pair( (*knn_ret_matches)[j],(*knn_dist_matches)[j] );
				}
				ret_matches = *temp;
			}
154
155
156
			(*list_matches)[i0] = ret_matches;
			if(*max_count < nMatches) *max_count = nMatches;
			i0++;
Alexander Liao's avatar
Alexander Liao committed
157

158
159
160
161
162
		}
	}
	else {// Multi-threaded routine
		std::mutex* mtx = new std::mutex();
		std::mutex* mtx_tree = new std::mutex();
Alexander Liao's avatar
Alexander Liao committed
163

164
165
166
		size_t n_queries = (*pcd_query).pts.size();
		size_t actual_threads = std::min((long long)n_threads, (long long)n_queries);

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

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		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;
			}
202
203
			targs->option = !!(option);
			targs->k = k;
204
205
206
207
208
209
210
			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
211
	}
212

Alexander Liao's avatar
Alexander Liao committed
213
214
	// Reserve the memory
	if(max_num > 0) {
215
		*max_count = max_num;
Alexander Liao's avatar
Alexander Liao committed
216
217
	}
	
218
	size_t size = 0; // total number of edges
219
220
	for (auto& inds : *list_matches){
		if(inds.size() <= *max_count)
Alexander Liao's avatar
Alexander Liao committed
221
222
			size += inds.size();
		else
223
			size += *max_count;
Alexander Liao's avatar
Alexander Liao committed
224
	}
225

226
	neighbors_indices->resize(size*2);
227
228
	size_t i1 = 0; // index of the query points
	size_t u = 0; // curent index of the neighbors_indices
229
230
	for (auto& inds : *list_matches){
		for (size_t j = 0; j < *max_count; j++){
Alexander Liao's avatar
Alexander Liao committed
231
			if(j < inds.size()){
232
233
				(*neighbors_indices)[u] = inds[j].first;
				(*neighbors_indices)[u + 1] = i1;
Alexander Liao's avatar
Alexander Liao committed
234
235
236
237
238
239
				u += 2;
			}
		}
		i1++;
	}

240
	return *max_count;
Alexander Liao's avatar
Alexander Liao committed
241
242
243
244




245
246
247
}

template<typename scalar_t>
248
249
250
251
252
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,
253
                               double radius, int dim, int64_t max_num, int64_t k, int option){
254
255


256
	// indices
257
	size_t i0 = 0;
258

259
	// Square radius
260
261
262
	const scalar_t r2 = static_cast<scalar_t>(radius*radius);

	// Counting vector
263
	size_t max_count = 0;
264
265

	// batch index
266
267
268
	size_t b = 0;
	size_t sum_qb = 0;
	size_t sum_sb = 0;
269

270
271
272
273
274
275
276
	double eps;
	if (supports.size() < 10){
		eps = 0.000001;
	}
	else {
		eps = 0;
	}
277
278
279
280
281
282
	// Nanoflann related variables

	// CLoud variable
	PointCloud<scalar_t> current_cloud;
	PointCloud<scalar_t> query_pcd;
	query_pcd.set(queries, dim);
283
	std::vector<std::vector<std::pair<size_t, scalar_t> > > all_inds_dists(query_pcd.pts.size());
284
285
286
287
288
289
290

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

291
	// Pointer to trees
292
293
294
295
296
	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();
297
298
	// Search neigbors indices
	// Search params
299
300
301
	nanoflann::SearchParams search_params;
	search_params.sorted = true;

302
303
	for (auto& p : query_pcd.pts){
		auto p0 = *p;
304
		// Check if we changed batch
305

306
		scalar_t* query_pt = new scalar_t[dim];
307
308
309
310
311
312
313
		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++;

314
			// Change the points
315
316
			current_cloud.pts.clear();
			current_cloud.set_batch(supports, sum_sb, s_batches[b], dim);
317
			// Build KDTree of the current element of the batch
318
319
320
321
			delete index;
			index = new my_kd_tree_t(dim, current_cloud, tree_params);
			index->buildIndex();
		}
322
		// Initial guess of neighbors size
323
		all_inds_dists[i0].reserve(max_count);
324
		// Find neighbors
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
		size_t nMatches;
		if (!!option) {
			nMatches = index->radiusSearch(query_pt, r2+eps, all_inds_dists[i0], search_params);
			// Update max count
		}
		else {
			std::vector<size_t>* knn_ret_matches = new std::vector<size_t>(k);
			std::vector<scalar_t>* knn_dist_matches = new std::vector<scalar_t>(k);
			nMatches = index->knnSearch(query_pt, (size_t)k, &(*knn_ret_matches)[0],&(*knn_dist_matches)[0]);
			auto temp = new std::vector<std::pair<size_t, scalar_t> >((*knn_dist_matches).size());
			for (size_t j = 0; j < (*knn_ret_matches).size(); j++){
				(*temp)[j] = std::make_pair( (*knn_ret_matches)[j],(*knn_dist_matches)[j] );
			}
			all_inds_dists[i0] = *temp;
		}
341
342
		if (nMatches > max_count)
			max_count = nMatches;
343
		// Increment query idx
344
345
		i0++;
	}
346
347
348


	
349
350
351
352
	// how many neighbors do we keep
	if(max_num > 0) {
		max_count = max_num;
	}
353
	// Reserve the memory
354
	
355
	size_t size = 0; // total number of edges
356
357
358
359
360
361
	for (auto& inds_dists : all_inds_dists){
		if(inds_dists.size() <= max_count)
			size += inds_dists.size();
		else
			size += max_count;
	}
362

363
	neighbors_indices->resize(size * 2);
364
365
366
367
	i0 = 0;
	sum_sb = 0;
	sum_qb = 0;
	b = 0;
368
	size_t u = 0;
369
370
371
372
373
374
	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++;
		}
375
		for (size_t j = 0; j < max_count; j++){
376
			if (j < inds_dists.size()){
377
378
				(*neighbors_indices)[u] = inds_dists[j].first + sum_sb;
				(*neighbors_indices)[u + 1] = i0;
379
380
381
382
383
384
385
386
				u += 2;
			}
		}
		i0++;
	}
	
	return max_count;
}