37 void build(PosAccessor&& get_pos,
int n) {
39 const int total = nx_ * ny_ * nz_;
41 std::fill(count_.begin(), count_.end(), 0);
42 for (
int i = 0; i < n; ++i)
43 ++count_[cell_id_of(get_pos(i))];
46 for (
int c = 0; c < total; ++c)
47 start_[c + 1] = start_[c] + count_[c];
49 std::fill(count_.begin(), count_.end(), 0);
50 for (
int i = 0; i < n; ++i) {
51 const int cid = cell_id_of(get_pos(i));
52 sorted_[start_[cid] + count_[cid]] = i;
59 void query(Scalar px, Scalar py, Scalar pz, F&& f)
const {
60 const int cx = cell_x(px);
61 const int cy = cell_y(py);
62 const int cz = cell_z(pz);
63 for (
int dz = -1; dz <= 1; ++dz) {
64 const int qz = cz + dz;
65 if (qz < 0 || qz >= nz_)
67 for (
int dy = -1; dy <= 1; ++dy) {
68 const int qy = cy + dy;
69 if (qy < 0 || qy >= ny_)
71 for (
int dx = -1; dx <= 1; ++dx) {
72 const int qx = cx + dx;
73 if (qx < 0 || qx >= nx_)
75 const int cid = (qz * ny_ + qy) * nx_ + qx;
76 for (
int k = start_[cid]; k < start_[cid + 1]; ++k)
87 static constexpr int FDX[13] = {-1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, 1};
88 static constexpr int FDY[13] = {-1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0};
89 static constexpr int FDZ[13] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
91 for (
int cz = 0; cz < nz_; ++cz) {
92 for (
int cy = 0; cy < ny_; ++cy) {
93 for (
int cx = 0; cx < nx_; ++cx) {
94 const int cid = (cz * ny_ + cy) * nx_ + cx;
95 const int beg = start_[cid];
96 const int end = start_[cid + 1];
101 for (
int a = beg; a < end; ++a)
102 for (
int b = a + 1; b < end; ++b)
103 f(sorted_[a], sorted_[b]);
106 for (
int d = 0; d < 13; ++d) {
107 const int ncx = cx + FDX[d];
108 const int ncy = cy + FDY[d];
109 const int ncz = cz + FDZ[d];
110 if (ncx < 0 || ncx >= nx_ || ncy < 0 || ncy >= ny_ || ncz < 0
113 const int ncid = (ncz * ny_ + ncy) * nx_ + ncx;
114 const int nbeg = start_[ncid];
115 const int nend = start_[ncid + 1];
118 for (
int a = beg; a < end; ++a)
119 for (
int b = nbeg; b < nend; ++b)
120 f(sorted_[a], sorted_[b]);
133 Scalar cs_ = 0, xmin_ = 0, ymin_ = 0, zmin_ = 0;