neighbors.cpp 6.15 KB
Newer Older
Alexander Liao's avatar
Alexander Liao committed
1

2
// 3D Version https://github.com/HuguesTHOMAS/KPConv
Alexander Liao's avatar
Alexander Liao committed
3
4
5
6
7
8
9
10
11
12
13
14
15

#include "neighbors.h"

template<typename scalar_t>
int nanoflann_neighbors(vector<scalar_t>& queries, vector<scalar_t>& supports,
			vector<long>& neighbors_indices, float radius, int dim, int max_num){

	// Initiate variables
	// ******************

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

	// Counting vector
16
	size_t max_count = 1;
Alexander Liao's avatar
Alexander Liao committed
17
18
19
20
21
22
23
24
25
26
27
28

	// Nanoflann related variables
	// ***************************

	// CLoud variable
	PointCloud<scalar_t> pcd;
	pcd.set(supports, dim);
	//Cloud query
	PointCloud<scalar_t> pcd_query;
	pcd_query.set(queries, dim);

	// Tree parameters
29
	nanoflann::KDTreeSingleIndexAdaptorParams tree_params(15 /* max leaf */);
Alexander Liao's avatar
Alexander Liao committed
30
31
32
33
34
35
36
37
38
39
40
41
42

	// 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;
	index = new my_kd_tree_t(dim, pcd, tree_params);
	index->buildIndex();
	// Search neigbors indices
	// ***********************

	// Search params
	nanoflann::SearchParams search_params;
43
	// search_params.sorted = true;
Alexander Liao's avatar
Alexander Liao committed
44
45
	std::vector< std::vector<std::pair<size_t, scalar_t> > > list_matches(pcd_query.pts.size());

46
	float eps = 0.000001;
Alexander Liao's avatar
Alexander Liao committed
47

48
49
50
	// indices
	size_t i0 = 0;

Alexander Liao's avatar
Alexander Liao committed
51
52
53
	for (auto& p0 : pcd_query.pts){

		// Find neighbors
Alexander Liao's avatar
Alexander Liao committed
54
		scalar_t* query_pt = new scalar_t[dim];
Alexander Liao's avatar
Alexander Liao committed
55
56
57
58
59
60
61
62
		std::copy(p0.begin(), p0.end(), query_pt); 

		//for(int i=0; i < p0.size(); i++)
		//std::cout << query_pt[i] << '\n';

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

Alexander Liao's avatar
Alexander Liao committed
63
		const size_t nMatches = index->radiusSearch(query_pt, search_radius+eps, ret_matches, search_params);
64
65
66
67
68
69
		
		//cout << "radiusSearch(): radius=" << search_radius << " -> " << nMatches << " matches\n";
		//for (size_t i = 0; i < nMatches; i++)
		//	cout << "idx["<< i << "]=" << ret_matches[i].first << " dist["<< i << "]=" << ret_matches[i].second << endl;
		//cout << "\n";
		
Alexander Liao's avatar
Alexander Liao committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
		list_matches[i0] = ret_matches;
		if(max_count < nMatches) max_count = nMatches;
		i0++;


		// Get worst (furthest) point, without sorting:
		
		// cout << "\n neighbors: " << nMatches << "\n";

		// Get worst (furthest) point, without sorting:
		// for(int i=0; i < ret_matches.size(); i++)
		// std::cout << ret_matches.at(i) << '\n';

	}
	// Reserve the memory
	if(max_num > 0) {
		max_count = max_num;
	}
	
89
	size_t size = 0; // total number of edges
Alexander Liao's avatar
Alexander Liao committed
90
91
92
93
94
95
	for (auto& inds : list_matches){
		if(inds.size() <= max_count)
			size += inds.size();
		else
			size += max_count;
	}
96

Alexander Liao's avatar
Alexander Liao committed
97
	neighbors_indices.resize(size*2);
98
99
	size_t i1 = 0; // index of the query points
	size_t u = 0; // curent index of the neighbors_indices
Alexander Liao's avatar
Alexander Liao committed
100
	for (auto& inds : list_matches){
101
		for (size_t j = 0; j < max_count; j++){
Alexander Liao's avatar
Alexander Liao committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
			if(j < inds.size()){
				neighbors_indices[u] = inds[j].first;
				neighbors_indices[u + 1] = i1;
				u += 2;
			}
		}
		i1++;
	}

	return max_count;




116
117
118
119
120
121
122
123
124
125
126
127
128
129
}

template<typename scalar_t>
int batch_nanoflann_neighbors (vector<scalar_t>& queries,
                               vector<scalar_t>& supports,
                               vector<long>& q_batches,
                               vector<long>& s_batches,
                               vector<long>& neighbors_indices,
                               float radius, int dim, int max_num){


// Initiate variables
// ******************
// indices
130
	size_t i0 = 0;
131
132
133
134
135

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

	// Counting vector
136
	size_t max_count = 0;
137
138

	// batch index
139
140
141
	size_t b = 0;
	size_t sum_qb = 0;
	size_t sum_sb = 0;
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

	float eps = 0.000001;
	// Nanoflann related variables
	// ***************************

	// CLoud variable
	PointCloud<scalar_t> current_cloud;
	PointCloud<scalar_t> query_pcd;
	query_pcd.set(queries, dim);
	vector<vector<pair<size_t, scalar_t> > > all_inds_dists(query_pcd.pts.size());

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

	for (auto& p0 : query_pcd.pts){
// Check if we changed batch

174
		scalar_t* query_pt = new scalar_t[dim];
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
202
203
204
205
206
207
208
209
210
211
		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
	
212
	size_t size = 0; // total number of edges
213
214
215
216
217
218
219
220
221
222
223
	for (auto& inds_dists : all_inds_dists){
		if(inds_dists.size() <= max_count)
			size += inds_dists.size();
		else
			size += max_count;
	}
	neighbors_indices.resize(size * 2);
	i0 = 0;
	sum_sb = 0;
	sum_qb = 0;
	b = 0;
224
	size_t u = 0;
225
226
227
228
229
230
	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++;
		}
231
		for (size_t j = 0; j < max_count; j++){
232
233
234
235
236
237
238
239
240
241
242
			if (j < inds_dists.size()){
				neighbors_indices[u] = inds_dists[j].first + sum_sb;
				neighbors_indices[u + 1] = i0;
				u += 2;
			}
		}
		i0++;
	}
	
	return max_count;
}