| 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 |
#ifndef AVOGADRO_CORE_NEIGHBORPERCEIVER_H |
| 7 |
#define AVOGADRO_CORE_NEIGHBORPERCEIVER_H |
| 8 |
|
| 9 |
#include "avogadrocoreexport.h" |
| 10 |
|
| 11 |
#include "avogadrocore.h" |
| 12 |
|
| 13 |
#include "array.h" |
| 14 |
#include "vector.h" |
| 15 |
|
| 16 |
#include <array> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
namespace Avogadro::Core { |
| 20 |
|
| 21 |
/** |
| 22 |
* @class NeighborPerceiver neighborperceiver.h |
| 23 |
* <avogadro/core/neighborperceiver.h> |
| 24 |
* @brief This class can be used to find physically neighboring points in linear |
| 25 |
* average time. |
| 26 |
*/ |
| 27 |
class AVOGADROCORE_EXPORT NeighborPerceiver |
| 28 |
{ |
| 29 |
public: |
| 30 |
/** |
| 31 |
* Creates a NeighborPerceiver that detects neighbors up to at least some |
| 32 |
* distance. |
| 33 |
* |
| 34 |
* @param points Positions in 3D space to detect neighbors among. |
| 35 |
* @param maxDistance All neighbors strictly within this distance will be |
| 36 |
* detected. Should be as low as possible for best |
| 37 |
* performance. |
| 38 |
*/ |
| 39 |
NeighborPerceiver(const Array<Vector3> points, float maxDistance); |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns a list of neighboring points. Linear time to number of neighbors. |
| 43 |
* Can include some neighbors up to 2*sqrt(3) times the maximum distance. |
| 44 |
* The list is newly allocated on every call; if performance/fragmentation |
| 45 |
* is a concern, prefer NeighborPerceiver::getNeighborsInclusiveInPlace(). |
| 46 |
* |
| 47 |
* @param point Position to return neighbors of, can be located anywhere. |
| 48 |
*/ |
| 49 |
Array<Index> getNeighborsInclusive(const Vector3& point) const; |
| 50 |
|
| 51 |
/** |
| 52 |
* Fills an array with all neighboring points. Linear time to number of |
| 53 |
* neighbors. Can include some neighbors up to 2*sqrt(3) times the maximum |
| 54 |
* distance. |
| 55 |
* |
| 56 |
* @param out Array to output neighbor indices in. |
| 57 |
* @param point Position to return neighbors of, can be located anywhere. |
| 58 |
*/ |
| 59 |
void getNeighborsInclusiveInPlace(Array<Index>& out, |
| 60 |
const Vector3& point) const; |
| 61 |
|
| 62 |
private: |
| 63 |
std::array<int, 3> getBinIndex(const Vector3& point) const; |
| 64 |
|
| 65 |
protected: |
| 66 |
float m_maxDistance; |
| 67 |
std::array<int, 3> m_binCount; |
| 68 |
std::vector<std::vector<std::vector<std::vector<Index>>>> m_bins; |
| 69 |
Vector3 m_minPos = Vector3::Zero(); |
| 70 |
Vector3 m_maxPos = Vector3::Zero(); |
| 71 |
mutable Array<Index>* m_cachedArray; |
| 72 |
mutable std::array<int, 3> m_cachedIndex; |
| 73 |
}; |
| 74 |
|
| 75 |
} // namespace Avogadro::Core |
| 76 |
|
| 77 |
#endif // AVOGADRO_CORE_NEIGHBORPERCEIVER_H |