Advanced Multi-Physics (AMP)
On-Line Documentation
kdtree2.h
Go to the documentation of this file.
1#ifndef included_AMP_kdtree2
2#define included_AMP_kdtree2
3
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <memory>
8#include <tuple>
9#include <vector>
10
11
12namespace AMP {
13
14
26template<uint8_t NDIM, class TYPE>
28{
29public: // Convience typedef
30 using Point = std::array<double, NDIM>;
31
32public:
39 kdtree2( const std::vector<Point> &x, const std::vector<TYPE> &data );
40
42 std::array<double, 2 * NDIM> box() const;
43
45 inline size_t size() const { return d_N; }
46
48 inline bool empty() const { return d_N == 0; }
49
51 std::vector<Point> getPoints() const;
52
54 std::vector<std::pair<Point, TYPE>> getPointsAndData() const;
55
63 void add( const Point &x, const TYPE &data );
64
71 std::tuple<Point, TYPE> findNearest( const Point &x ) const;
72
80 std::vector<std::tuple<Point, TYPE>> findNearest( const Point &x, int N ) const;
81
90 std::vector<std::tuple<Point, TYPE>> findNearest( const Point &x, double dist ) const;
91
100 std::vector<std::tuple<Point, TYPE>>
101 findNearestRay( const Point &x, const Point &dir, double dist ) const;
102
103
104public: // Copy/assignment operators
105 kdtree2() = default;
106 ~kdtree2() = default;
107 kdtree2( const kdtree2 & ) = delete;
108 kdtree2( kdtree2 && ) = default;
109 kdtree2 &operator=( const kdtree2 & ) = delete;
110 kdtree2 &operator=( kdtree2 && ) = default;
111
112
113private: // Internal data
114 // Structure used to store point data in the lowest leaf
115 struct data_struct {
116 data_struct( const data_struct & ) = delete;
117 data_struct &operator=( const data_struct & ) = delete;
118 size_t N = 0;
119 Point *x = nullptr;
120 TYPE *data = nullptr;
121 data_struct( size_t N );
123 void add( const Point &x2, const TYPE &d2 );
124 };
125
126 // Internal data
127 size_t d_N = 0;
128 uint8_t d_split_dim = 0;
129 double d_split = 0;
130 Point d_lb = { 0 };
131 Point d_ub = { 0 };
132 std::unique_ptr<kdtree2> d_left;
133 std::unique_ptr<kdtree2> d_right;
134 std::unique_ptr<data_struct> d_data;
135
136
137private: // Internal functions
138 kdtree2( size_t N, Point *x, TYPE *data );
139 void initialize( size_t N, Point *x, TYPE *data );
140 void splitData( size_t N, Point *x, TYPE *data );
141 bool intersect( const Point &x, double dist2 ) const;
142 void getPoints( std::vector<Point> &x ) const;
143 void getPoints( std::vector<std::pair<Point, TYPE>> &x ) const;
144 void
145 findNearest( const Point &x, size_t N, std::tuple<Point, TYPE> *nearest, double *dist ) const;
146 void findNearest( const Point &x,
147 double dist2,
148 std::vector<std::tuple<Point, TYPE>> &nearest ) const;
149 void findNearestRay( const Point &x,
150 const Point &dir,
151 const Point &n_inv,
152 double dist2,
153 std::vector<std::tuple<Point, TYPE>> &nearest ) const;
154 void
155 checkNearest( const Point &x, size_t N, std::tuple<Point, TYPE> *nearest, double *dist ) const;
156};
157
158
159} // namespace AMP
160
161
162#endif
A class used to to perform kd-tree based operations.
Definition kdtree2.h:28
void initialize(size_t N, Point *x, TYPE *data)
kdtree2(size_t N, Point *x, TYPE *data)
std::vector< std::pair< Point, TYPE > > getPointsAndData() const
Return the points in the tree.
kdtree2 & operator=(const kdtree2 &)=delete
std::vector< Point > getPoints() const
Return the points in the tree.
kdtree2(const std::vector< Point > &x, const std::vector< TYPE > &data)
Constructor.
bool intersect(const Point &x, double dist2) const
void findNearest(const Point &x, size_t N, std::tuple< Point, TYPE > *nearest, double *dist) const
bool empty() const
Return true if the tree is empty.
Definition kdtree2.h:48
kdtree2(const kdtree2 &)=delete
std::array< double, 2 *NDIM > box() const
Return the bounding box for the tree.
std::unique_ptr< data_struct > d_data
Definition kdtree2.h:134
std::vector< std::tuple< Point, TYPE > > findNearestRay(const Point &x, const Point &dir, double dist) const
Search the tree for the nearest points to a ray.
void getPoints(std::vector< Point > &x) const
void checkNearest(const Point &x, size_t N, std::tuple< Point, TYPE > *nearest, double *dist) const
void findNearestRay(const Point &x, const Point &dir, const Point &n_inv, double dist2, std::vector< std::tuple< Point, TYPE > > &nearest) const
size_t d_N
Definition kdtree2.h:127
double d_split
Definition kdtree2.h:129
Point d_ub
Definition kdtree2.h:131
void splitData(size_t N, Point *x, TYPE *data)
void getPoints(std::vector< std::pair< Point, TYPE > > &x) const
size_t size() const
Return the number of entries stored in the tree.
Definition kdtree2.h:45
void findNearest(const Point &x, double dist2, std::vector< std::tuple< Point, TYPE > > &nearest) const
std::unique_ptr< kdtree2 > d_right
Definition kdtree2.h:133
std::vector< std::tuple< Point, TYPE > > findNearest(const Point &x, int N) const
Search the tree for the nearest neighbor points.
void add(const Point &x, const TYPE &data)
Add a point.
std::tuple< Point, TYPE > findNearest(const Point &x) const
Search the tree for the nearest neighbor point.
kdtree2()=default
std::vector< std::tuple< Point, TYPE > > findNearest(const Point &x, double dist) const
Search the tree for the nearest neighbor points.
std::unique_ptr< kdtree2 > d_left
Definition kdtree2.h:132
kdtree2(kdtree2 &&)=default
~kdtree2()=default
kdtree2 & operator=(kdtree2 &&)=default
Point d_lb
Definition kdtree2.h:130
uint8_t d_split_dim
Definition kdtree2.h:128
A class used to store information for a point.
data_struct(const data_struct &)=delete
void add(const Point &x2, const TYPE &d2)
data_struct & operator=(const data_struct &)=delete



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page