10#include <Math/Vector3D.h>
15namespace Belle2::GNNFinder::Utils {
37 double dx =
x - other.
x;
38 double dy =
y - other.
y;
39 return dx * dx + dy * dy;
139 static KDTNode*
buildKDTree(std::vector<KDTHit>::iterator begin, std::vector<KDTHit>::iterator end,
const int depth,
140 KDTNodePool& pool,
const size_t insertionSortThreshold = 10);
151 template<
typename Iterator,
typename Compare>
198 static std::vector<int>
orderHits(
const double startingX,
const double startingY,
199 std::vector<KDTHit> kdtHits);
225 std::pair<double, double> intersectCylinderXY(
const ROOT::Math::XYZVector& pos,
226 const ROOT::Math::XYZVector& mom,
227 const double targetR);
static void nearestNeighbor(const KDTNode *node, const KDTHit &query, KDTHit &best, double &bestDist)
Finds the closest unused neighbor to a query hit in the KD-tree.
HitOrderer()=default
Constructor.
static bool markUsed(KDTNode *node, const KDTHit &hit)
Marks a KD-tree node as used if its hit index matches the given hit.
static void freeKDTree(KDTNode *node)
Recursively frees all nodes in a KD-tree.
~HitOrderer()=default
Destructor.
static KDTNode * buildKDTree(std::vector< KDTHit >::iterator begin, std::vector< KDTHit >::iterator end, const int depth, KDTNodePool &pool, const size_t insertionSortThreshold=10)
Recursively builds a balanced KD-tree from a range of KDTHits.
static void insertionSort(Iterator begin, Iterator end, Compare cmp)
Sorts a small range using insertion sort for performance.
static std::vector< int > orderHits(const double startingX, const double startingY, std::vector< KDTHit > kdtHits)
Sort hits spatially based on proximity to a starting position.
Memory pool for pre-allocating and reusing KDTNode objects.
size_t m_index
Current index for the next available node in the pool.
KDTNodePool(size_t capacity)
Constructs a pool with the specified capacity.
KDTNodePool & operator=(const KDTNodePool &)=delete
Assignment operator.
std::vector< KDTNode * > m_pool
Internal storage for preallocated KD-tree nodes.
~KDTNodePool()
Destructor.
KDTNodePool(const KDTNodePool &)=delete
Copy constructor.
void reset()
Resets the pool index, making all nodes available for reuse.
KDTNode * allocate()
Allocates and returns the next available KDTNode from the pool.
map< unsigned, size_t >::const_iterator Iterator
Iterator for m_map.
Lightweight 2D representation of a CDC hit for KD-tree sorting.
double squaredDistanceTo(const KDTHit &other) const
Compute squared distance to another KDTHit.
double y
Y coordinate of the hit.
int hitIndex
Index of the hit in the original input collection.
double x
X coordinate of the hit.
Node structure for a 2D KD-tree used in spatial hit sorting.
KDTHit hit
The hit associated with this node (contains x, y, and index).
int dim
Splitting dimension: 0 for x-axis, 1 for y-axis.
KDTNode * left
Pointer to the left subtree (for values less than split).
KDTNode * right
Pointer to the right subtree (for values greater than split).
bool used
Flag indicating whether the node has already been used in traversal.