Template Class KDTree
Defined in File KDTree.hpp
Nested Relationships
Nested Types
Class Documentation
-
template<typename PointT, unsigned int N = 3>
class KDTree a kd-Tree Implementation for nearest Neighbor searches
Can contain any type of data, as long as it implements <float/double> operator[](uint axis) for axis in [0, N). Specifically, one could create a type like: struct PointWithNormal { Vector3f point; Vector3f normal; float operator[](uint i) const { return point[i]; } }; Or struct PointWithHandle { Vector3f point; VertexHandle handle; float operator[](uint i) const { return point[i]; } }; To have associated metadata with the points, that can then be obtained from the neighbors.
- Template Parameters:
PointT – The type of the data to be stored in the tree
N – The dimensions of PointT
Public Functions
-
virtual ~KDTree() = default
-
template<typename InPointT, typename FloatT>
bool nnSearch(const InPointT &point, PointT *&neighbor, FloatT &distance, double maxDistance = std::numeric_limits<double>::infinity()) const Finds the nearest neighbor of ‘point’ that is within ‘maxDistance’ (defaults to infinity). The resulting neighbor is written into ‘neighbor’ (or nullptr if none is found).
- Parameters:
point – The Point whose neighbor is searched
neighbor – A Pointer that is set to the neighbor or nullptr if none is found
distance – The final distance between point and neighbor
maxDistance – The maximum distance allowed between neighbors. Setting this value significantly speeds up the search.
- Returns:
bool true if a neighbors was found, false otherwise
-
template<typename InPointT, typename FloatT>
size_t knnSearch(const InPointT &point, size_t k, std::vector<PointT*> &neighbors, std::vector<FloatT> &distances, double maxDistance = std::numeric_limits<double>::infinity()) const Finds the ‘k’ nearest neighbor of ‘point’ that are within ‘maxDistance’ (defaults to infinity). The resulting neighbor is written into ‘neighbor’ (or nullptr if none is found).
- Parameters:
point – The Point whose neighbor is searched
k – The number of neighbors to find
neighbors – Will be filled with the neighbors
distances – Will be filled with the distances to the neighbors
maxDistance – The maximum distance allowed between neighbors. Setting this value significantly speeds up the search.
- Returns:
size_t The number of neighbors found (Usually k)
-
template<typename InPointT>
size_t knnSearch(const InPointT &point, size_t k, std::vector<PointT*> &neighbors, double maxDistance = std::numeric_limits<double>::infinity()) const Same as knnSearch, but does not gather distances.
-
inline size_t numPoint() const
Returns the number of points in the tree.
Public Static Functions
Protected Types
-
using Queue = std::priority_queue<DistPoint>
A Priority queue to keep track of the k nearest Points.
-
using KDPtr = std::unique_ptr<KDTreeInternal>
Protected Functions
-
template<typename InPointT>
inline QueryPoint toQueryPoint(const InPointT &point) const
-
void init(size_t maxLeafSize = 20)
-
KDPtr createRecursive(PointT *start, PointT *end, const QueryPoint &min, const QueryPoint &max, size_t maxLeafSize)
-
struct DistPoint
A Point with its squared distance to the query Point for easy comparison.
-
class KDTreeInternal
Public Functions
-
virtual void nnInternal(const QueryPoint &point, DistPoint &neighbor) const = 0
Internal recursive version of nnSearch. Provided by subclasses.
- Parameters:
point – The query Point to search around
neighbor – The current nearest neighbor or nullptr if none is found
worstDistSq – The remaining search radius for new Neighbors (squared)
-
virtual void knnInternal(const QueryPoint &point, size_t k, Queue &neighbors, double &worstDistSq) const = 0
Internal recursive version of knnSearch. Provided by subclasses.
- Parameters:
point – The query Point to search around
neighbors – The Queue to place the Neighbors into
worstDistSq – The remaining search radius for new Neighbors (squared)
k – The number of Neighbors to find
-
virtual ~KDTreeInternal() = default
-
virtual void nnInternal(const QueryPoint &point, DistPoint &neighbor) const = 0