-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeriodicNearestNeighbors.hpp
291 lines (240 loc) · 8.38 KB
/
PeriodicNearestNeighbors.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (C) <[email protected]>
//
// This file is part of TOOFUS (TOols OFten USued)
//
// It can not be copied and/or distributed without the express
// permission of the authors.
// It is coded for academic purposes.
//
// Note
// Without a license, the code is copyrighted by default.
// People can read the code, but they have no legal right to use it.
// To use the code, you must contact the author directly and ask permission.
#ifndef PERIODIC_NN_HPP
#define PERIODIC_NN_HPP
#include <cmath>
#include <set>
#include <vector>
struct IdPoint {
size_t id;
double x, y, z;
IdPoint(size_t t_id, double t_x, double t_y, double t_z) : id(t_id), x(t_x), y(t_y), z(t_z) {}
};
class PeriodicNearestNeighbors {
public:
/// Constructs a PeriodicNearestNeighbors object with the given points and gridSize.
///
/// @param t_points The vector of points.
/// @param t_gridSize The size of the grid.
///
PeriodicNearestNeighbors(const std::vector<IdPoint> &t_points, size_t t_gridSize)
: points(t_points), gridSize(t_gridSize), numCells(t_gridSize * t_gridSize * t_gridSize),
gridSizeDouble(static_cast<double>(gridSize)), gridSizeInt(static_cast<int>(gridSize)),
gridSizeSquared(gridSize * gridSize) {
buildGrid();
}
std::vector<std::vector<size_t>> getNeighbors(double dmax) {
const size_t numPoints = points.size();
std::vector<std::vector<size_t>> neighbors(numPoints);
for (size_t i = 0; i < numPoints; ++i) {
const IdPoint &point = points[i];
size_t cellIndex = calculateCellIndex(point);
std::set<size_t> uniqueNeighborIndices;
const std::vector<size_t> &neighborIndices = neighborCellIndices[cellIndex];
for (size_t neighborCellIndex : neighborIndices) {
for (size_t neighborIndex : grid[neighborCellIndex]) {
if (neighborIndex <= i)
continue;
const IdPoint &neighbor = points[neighborIndex];
double sqrDistance = calculateSqrDistance(point, neighbor);
if (sqrDistance <= dmax * dmax) {
uniqueNeighborIndices.insert(neighborIndex);
}
}
}
// neighbors[i].reserve(uniqueNeighborIndices.size());
for (size_t neighborIndex : uniqueNeighborIndices) {
neighbors[i].push_back(neighborIndex);
}
}
return neighbors;
}
std::vector<std::vector<size_t>> getNeighbors() {
const size_t numPoints = points.size();
std::vector<std::vector<size_t>> neighbors(numPoints);
for (size_t i = 0; i < numPoints; ++i) {
const IdPoint &point = points[i];
size_t cellIndex = calculateCellIndex(point);
std::set<size_t> uniqueNeighborIndices;
const std::vector<size_t> &neighborIndices = neighborCellIndices[cellIndex];
for (size_t neighborCellIndex : neighborIndices) {
for (size_t neighborIndex : grid[neighborCellIndex]) {
if (neighborIndex <= i)
continue;
uniqueNeighborIndices.insert(neighborIndex);
}
}
// neighbors[i].reserve(uniqueNeighborIndices.size());
for (size_t neighborIndex : uniqueNeighborIndices) {
neighbors[i].push_back(neighborIndex);
}
}
return neighbors;
}
private:
/// Calculates the cell index for a given point.
///
/// @param point The point.
/// @return The cell index.
///
size_t calculateCellIndex(const IdPoint &point) const {
size_t ix = (size_t)floor(point.x * gridSizeDouble);
size_t iy = (size_t)floor(point.y * gridSizeDouble);
size_t iz = (size_t)floor(point.z * gridSizeDouble);
return iz * gridSizeSquared + iy * gridSize + ix;
}
std::vector<size_t> calculateNeighborCellIndices(size_t cellIndex) const {
const size_t z = cellIndex / gridSizeSquared;
const size_t y = (cellIndex % gridSizeSquared) / gridSize;
const size_t x = cellIndex % gridSize;
std::vector<size_t> neighborIndices;
// neighborIndices.reserve(27);
for (int dz = -1; dz <= 1; ++dz) {
for (int dy = -1; dy <= 1; ++dy) {
for (int dx = -1; dx <= 1; ++dx) {
int nx = (int)x + dx;
if (nx < 0) {
nx = gridSizeInt - 1;
} else if (nx >= gridSizeInt) {
nx = 0;
}
int ny = (int)y + dy;
if (ny < 0) {
ny = gridSizeInt - 1;
} else if (ny >= gridSizeInt) {
ny = 0;
}
int nz = (int)z + dz;
if (nz < 0) {
nz = gridSizeInt - 1;
} else if (nz >= gridSizeInt) {
nz = 0;
}
size_t neighborIndex =
static_cast<size_t>(nz) * gridSizeSquared + static_cast<size_t>(ny) * gridSize + static_cast<size_t>(nx);
neighborIndices.push_back(neighborIndex);
}
}
}
return neighborIndices;
}
/// Calculates the distance between two points, accounting for periodicity.
///
/// @param point1 The first point.
/// @param point2 The second point.
/// @return The distance between the points.
///
double calculateSqrDistance(const IdPoint &point1, const IdPoint &point2) const {
double sx = point2.x - point1.x;
double sy = point2.y - point1.y;
double sz = point2.z - point1.z;
sx -= floor(sx + 0.5);
sy -= floor(sy + 0.5);
sz -= floor(sz + 0.5);
return sx * sx + sy * sy + sz * sz;
}
///
/// Builds the grid and assigns points to grid cells.
///
void buildGrid() {
grid.resize(numCells);
neighborCellIndices.resize(numCells);
for (size_t cellIndex = 0; cellIndex < numCells; ++cellIndex) {
neighborCellIndices[cellIndex] = calculateNeighborCellIndices(cellIndex);
}
for (size_t i = 0; i < points.size(); ++i) {
const IdPoint &point = points[i];
size_t cellIndex = calculateCellIndex(point);
grid[cellIndex].push_back(i);
}
}
std::vector<IdPoint> points;
size_t gridSize;
size_t numCells;
double gridSizeDouble;
int gridSizeInt;
size_t gridSizeSquared;
std::vector<std::vector<size_t>> grid;
std::vector<std::vector<size_t>> neighborCellIndices;
};
#endif /* end of include guard: PERIODIC_NN_HPP */
#if 0
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
int main() {
std::vector<IdPoint> points;
const size_t numPoints = 4 * 4 * 4;
size_t i = 0;
for (size_t iz = 0; iz < 4; ++iz) {
for (size_t iy = 0; iy < 4; ++iy) {
for (size_t ix = 0; ix < 4; ++ix) {
double x = 0.2 + 0.2 * ix;
double y = 0.2 + 0.2 * iy;
double z = 0.2 + 0.2 * iz;
points.push_back({i, x, y, z});
i++;
}
}
}
/*
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dist(0.0, 1.0);
std::vector<IdPoint> points;
const size_t numPoints = 20;
for (size_t i = 0; i < numPoints; ++i) {
double x = dist(gen);
double y = dist(gen);
double z = dist(gen);
points.push_back({i, x, y, z});
}
*/
/*
std::vector<IdPoint> points;
const size_t numPoints = 4;
points.push_back({0, 0.1, 0.1, 0.1});
points.push_back({1, 0.9, 0.9, 0.1});
points.push_back({2, 0.9, 0.1, 0.1});
points.push_back({3, 0.1, 0.9, 0.1});
*/
// Determine the gridSize based on the density of points
double averageDistance = std::pow(1.0 / numPoints, 1.0 / 3.0); // should be in ]0 0.5[
std::cout << "averageDistance: " << averageDistance << std::endl;
size_t gridSize = std::max<size_t>(static_cast<size_t>(std::ceil(1.0 / averageDistance)), 3);
// size_t gridSize = 3;
std::cout << "gridSize: " << gridSize << std::endl;
PeriodicNearestNeighbors perioNN(points, gridSize);
auto start = std::chrono::high_resolution_clock::now();
std::vector<std::vector<size_t>> neighbors = perioNN.getNeighbors(1.7 * averageDistance);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
std::cout << "Execution time: " << duration << " microseconds" << std::endl;
for (size_t i = 0; i < neighbors.size(); ++i) {
std::cout << "Neighbors of point " << i << ": ";
for (size_t neighbor : neighbors[i]) {
std::cout << neighbor << " ";
}
std::cout << std::endl;
}
std::ofstream file("data2.txt");
for (size_t i = 0; i < neighbors.size(); ++i) {
for (size_t j : neighbors[i]) {
file << points[i].x << " " << points[i].y << " " << points[i].z << "\n";
file << points[j].x << " " << points[j].y << " " << points[j].z << "\n\n";
}
}
return 0;
}
#endif