Skip to content

Commit 89a194d

Browse files
committed
Document KdTree class and its methods
1 parent 6c49640 commit 89a194d

File tree

1 file changed

+104
-15
lines changed

1 file changed

+104
-15
lines changed

source/pbat/geometry/KdTree.h

Lines changed: 104 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* @file KdTree.h
3+
* @author Quoc-Minh Ton-That ([email protected])
4+
* @brief This file contains the KdTree class.
5+
* @date 2025-02-12
6+
*
7+
* @copyright Copyright (c) 2025
8+
*
9+
*/
10+
111
#ifndef PBAT_GEOMETRY_KD_TREE_H
212
#define PBAT_GEOMETRY_KD_TREE_H
313

@@ -14,6 +24,9 @@
1424
namespace pbat {
1525
namespace geometry {
1626

27+
/**
28+
* @brief Node of a KDTree
29+
*/
1730
struct KdTreeNode
1831
{
1932
Index lc{-1}; ///< Index of left child of this node in the KDTree. -1 if no left child.
@@ -24,23 +37,48 @@ struct KdTreeNode
2437
std::size_t n{0}; ///< Number of points encapsulated in this node's AABB starting from begin and
2538
///< continuing in contiguous memory in the permutation list until begin + n.
2639

27-
Index depth{0};
28-
40+
Index depth{0}; ///< Depth of this node in the KDTree.
41+
/**
42+
* @brief Returns true if this node has a left child, false otherwise
43+
* @return true if this node has a left child, false otherwise
44+
*/
2945
bool HasLeftChild() const { return lc > -1; }
46+
/**
47+
* @brief Returns true if this node has a right child, false otherwise
48+
* @return true if this node has a right child, false otherwise
49+
*/
3050
bool HasRightChild() const { return rc > -1; }
51+
/**
52+
* @brief Returns true if this node is a leaf node, false otherwise
53+
* @return true if this node is a leaf node, false otherwise
54+
*/
3155
[[maybe_unused]] bool IsLeafNode() const
3256
{
3357
return (not HasLeftChild()) and (not HasRightChild());
3458
}
59+
/**
60+
* @brief Returns true if this node is an internal node, false otherwise
61+
* @return true if this node is an internal node, false otherwise
62+
*/
3563
[[maybe_unused]] bool IsInternalNode() const { return HasLeftChild() or HasRightChild(); }
3664
};
3765

66+
/**
67+
* @brief KDTree class
68+
* @tparam Dims Number of dimensions of the points' coordinate system in the k-D tree
69+
*/
3870
template <int Dims>
3971
class KdTree
4072
{
4173
public:
4274
KdTree() = default;
4375

76+
/**
77+
* @brief Construct a k-D tree from a set of points
78+
* @tparam TDerivedP Eigen dense expression type
79+
* @param P Points to construct the k-D tree from
80+
* @param maxPointsInLeaf Maximum number of points in a leaf node
81+
*/
4482
template <class TDerivedP>
4583
KdTree(Eigen::DenseBase<TDerivedP> const& P, std::size_t maxPointsInLeaf = 10);
4684

@@ -51,35 +89,77 @@ class KdTree
5189

5290
public:
5391
/**
54-
* @brief
55-
* @param visit Returns true if the node's children should be visited, false otherwise
56-
* @param stop Returns true if the search should stop
57-
* @param root
92+
* @brief Breadth-first search over the k-D tree
93+
* @tparam FVisit Callable with signature `bool(Index, KdTreeNode const&)`
94+
* @tparam FStop Callable with signature `bool(Index, KdTreeNode const&)`
95+
* @param visit Callback invoked when visiting a node. Returns true if the node's children
96+
* should be visited, false otherwise
97+
* @param stop Callback invoked when visiting a node. Returns true if the search should stop,
98+
* false otherwise
99+
* @param root Index of the root node to start the search from
58100
*/
59101
template <class FVisit, class FStop = decltype(fStopDefault)>
60102
void BreadthFirstSearch(FVisit visit, FStop stop = fStopDefault, Index root = 0) const;
61103

62104
/**
63-
* @brief
64-
* @param visit Returns true if the node's children should be visited, false otherwise
65-
* @param stop Returns true if the search should stop
66-
* @param root
105+
* @brief Depth-first search over the k-D tree
106+
* @tparam FVisit Callable with signature `bool(Index, KdTreeNode const&)`
107+
* @tparam FStop Callable with signature `bool(Index, KdTreeNode const&)`
108+
* @param visit Callback invoked when visiting a node. Returns true if the node's children
109+
* @param stop Callback invoked when visiting a node. Returns true if the search should stop,
110+
* @param root Index of the root node to start the search from
67111
*/
68112
template <class FVisit, class FStop = decltype(fStopDefault)>
69113
void DepthFirstSearch(FVisit visit, FStop stop = fStopDefault, Index root = 0) const;
70114

115+
/**
116+
* @brief Construct a k-D tree from a set of points
117+
* @tparam TDerivedP Eigen dense expression type
118+
* @param P Points to construct the k-D tree from
119+
* @param maxPointsInLeaf Maximum number of points in a leaf node
120+
*/
71121
template <class TDerivedP>
72122
void Construct(Eigen::DenseBase<TDerivedP> const& P, std::size_t maxPointsInLeaf);
73123

124+
/**
125+
* @brief Returns the nodes of the k-D tree
126+
* @return Nodes of the k-D tree
127+
*/
74128
std::vector<KdTreeNode> const& Nodes() const { return mNodes; }
75-
129+
/**
130+
* @brief Returns the permutation of the points in the k-D tree
131+
*
132+
* The permutation is such that mPermutation[i] gives the index of point i in the original point
133+
* set given to Construct().
134+
*
135+
* @return Permutation of the points in the k-D tree
136+
*/
76137
std::vector<Index> const& Permutation() const { return mPermutation; }
77-
138+
/**
139+
* @brief Returns the points in a node
140+
* @param nodeIdx Index of the node
141+
* @return Range of points in the node
142+
*/
78143
auto PointsInNode(Index const nodeIdx) const;
79-
144+
/**
145+
* @brief Returns the points in a node
146+
* @param node Reference to k-D tree node
147+
* @return Range of points in the node
148+
*/
80149
auto PointsInNode(KdTreeNode const& node) const;
81150

82151
protected:
152+
/**
153+
* @brief Recursive construct call that constructs a sub-tree of the full k-D tree
154+
* @tparam TDerivedP Eigen dense expression type
155+
* @param nodeIdx Index of the sub-tree root node
156+
* @param P Points to construct the k-D tree from
157+
* @param aabb Axis-aligned bounding box of the points contained in the sub-tree
158+
* @param begin Index of the first point in the permutation list contained in the sub-tree
159+
* @param n Number of points in the permutation list starting from begin contained in the
160+
* sub-tree
161+
* @param maxPointsInLeaf Maximum number of points in a leaf node
162+
*/
83163
template <class TDerivedP>
84164
void DoConstruct(
85165
Index const nodeIdx,
@@ -88,11 +168,20 @@ class KdTree
88168
Index const begin,
89169
std::size_t const n,
90170
std::size_t maxPointsInLeaf);
91-
171+
/**
172+
* @brief Adds a node to the k-D tree's node list
173+
*
174+
* @param begin Index of the first point in the permutation list contained in the sub-tree
175+
* rooted in the added node
176+
* @param n Number of points in the permutation list starting from begin contained in the
177+
* sub-tree rooted in the added node
178+
* @param depth Depth of the added node in the k-D tree
179+
* @return Index of the added node in the k-D tree's node list
180+
*/
92181
Index AddNode(Index begin, std::size_t n, Index depth);
93182

94183
private:
95-
std::vector<Index> mPermutation; ///< permutation_[i] gives the index of point i in
184+
std::vector<Index> mPermutation; ///< mPermutation[i] gives the index of point i in
96185
///< the original points list given to
97186
///< construct(std::vector<Vector3> const& points).
98187
std::vector<KdTreeNode> mNodes; ///< KDTree nodes.

0 commit comments

Comments
 (0)