Debian Codegraph @

avogadro/core/neighborperceiver.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "neighborperceiver.h"
7
8 #include <cmath>
9
10 namespace Avogadro::Core {
11
12 NeighborPerceiver::NeighborPerceiver(const Array<Vector3> points,
13 float maxDistance)
14 : m_maxDistance(maxDistance), m_binCount({ 0, 0, 0 }), m_cachedArray(nullptr)
15 {
16 if (!points.size())
17 return;
18
19 if (m_maxDistance <= 0 || !std::isfinite(m_maxDistance))
20 return;
21
22 // find bounding box
23 m_minPos = points[0];
24 m_maxPos = points[0];
25 for (Index i = 1; i < points.size(); i++) {
26 Vector3 ipos = points[i];
27 for (size_t c = 0; c < 3; c++) {
28 m_minPos(c) = std::min(ipos(c), m_minPos(c));
29 m_maxPos(c) = std::max(ipos(c), m_maxPos(c));
30 }
31 }
32
33 // Validate bounding box (NaN/Inf from malformed input)
34 for (size_t c = 0; c < 3; c++) {
35 if (!std::isfinite(m_minPos(c)) || !std::isfinite(m_maxPos(c)))
36 return;
37 }
38
39 // group points into cubic bins so that each point is only checked against
40 // other points inside bins within a 3-dimensional Moore neighborhood
41 for (size_t c = 0; c < 3; c++) {
42 double count =
43 std::floor((m_maxPos(c) + 0.1 - m_minPos(c)) / m_maxDistance) + 1;
44 if (!std::isfinite(count) || count < 1)
45 m_binCount[c] = 1;
46 else if (count > 1000)
47 m_binCount[c] = 1000;
48 else
49 m_binCount[c] = static_cast<int>(count);
50 }
51 const long long totalBins = static_cast<long long>(m_binCount[0])
52 * m_binCount[1] * m_binCount[2];
53 if (totalBins > 10'000'000) // ~240 MB of vector overhead
54 return;
55
56 std::vector<std::vector<std::vector<std::vector<Index>>>> bins(
57 m_binCount[0], std::vector<std::vector<std::vector<Index>>>(
58 m_binCount[1], std::vector<std::vector<Index>>(
59 m_binCount[2], std::vector<Index>())));
60 m_bins = bins;
61 for (Index i = 0; i < points.size(); i++) {
62 std::array<int, 3> bin_index = getBinIndex(points[i]);
63 if (bin_index[0] >= 0 && bin_index[0] < m_binCount[0] &&
64 bin_index[1] >= 0 && bin_index[1] < m_binCount[1] &&
65 bin_index[2] >= 0 && bin_index[2] < m_binCount[2]) {
66 m_bins[bin_index[0]][bin_index[1]][bin_index[2]].push_back(i);
67 }
68 }
69 }
70
71 void NeighborPerceiver::getNeighborsInclusiveInPlace(Array<Index>& out,
72 const Vector3& point) const
73 {
74 if (m_bins.empty()) {
75 out.clear();
76 return;
77 }
78
79 const std::array<int, 3> bin_index = getBinIndex(point);
80 if (&out == m_cachedArray && bin_index == m_cachedIndex)
81 return;
82
83 m_cachedIndex = bin_index;
84 out.clear();
85 for (int xi = std::max(int(1), bin_index[0]) - 1;
86 xi < std::min(m_binCount[0], bin_index[0] + 2); xi++) {
87 for (int yi = std::max(int(1), bin_index[1]) - 1;
88 yi < std::min(m_binCount[1], bin_index[1] + 2); yi++) {
89 for (int zi = std::max(int(1), bin_index[2]) - 1;
90 zi < std::min(m_binCount[2], bin_index[2] + 2); zi++) {
91 const std::vector<Index>& bin = m_bins[xi][yi][zi];
92 out.insert(out.end(), bin.begin(), bin.end());
93 }
94 }
95 }
96 }
97
98 Array<Index> NeighborPerceiver::getNeighborsInclusive(
99 const Vector3& point) const
100 {
101 Array<Index> r;
102 getNeighborsInclusiveInPlace(r, point);
103 return r;
104 }
105
106 std::array<int, 3> NeighborPerceiver::getBinIndex(const Vector3& point) const
107 {
108 std::array<int, 3> r = {};
109 for (size_t c = 0; c < 3; c++) {
110 r[c] = std::floor((point(c) - m_minPos(c)) / m_maxDistance);
111 }
112 return r;
113 }
114
115 } // namespace Avogadro::Core