VAPOR3 3.9.4
KDTreeRG.h
Go to the documentation of this file.
1#ifndef _KDTreeRG_
2#define _KDTreeRG_
3
4#include <ostream>
5#include <vector>
6#include <vapor/Grid.h>
7
8#include "nanoflann.hpp"
9
10struct kdtree;
11
12namespace VAPoR {
13//
21//
23public:
40 //
41 KDTreeRG(const Grid &xg, const Grid &yg);
42
57 //
58 // KDTreeRG( const Grid &xg, const Grid &yg, const Grid &zg );
59
60 virtual ~KDTreeRG();
61
74 //
75 void Nearest(const std::vector<float> &coordu, std::vector<size_t> &index) const;
76
77 void Nearest(const std::vector<double> &coordu, std::vector<size_t> &index) const
78 {
79 std::vector<float> coordu_f;
80 for (int i = 0; i < coordu.size(); i++) coordu_f.push_back(coordu[i]);
81 this->Nearest(coordu_f, index);
82 }
83
92 std::vector<size_t> GetDimensions() const { return (_dims); }
93
94private:
95 class PointCloud2D {
96 public:
97 // Constructor
98 PointCloud2D(const Grid &xg, const Grid &yg)
99 {
100 VAssert(xg.GetDimensions() == yg.GetDimensions());
101 VAssert(xg.GetNumDimensions() <= 2);
102
103 // number of elements
104 auto dims = xg.GetDimensions();
105 size_t nelem = 1;
106 for (int i = 0; i < dims.size(); i++) nelem *= dims[i];
107 this->X.resize(nelem);
108 this->Y.resize(nelem);
109
110 // Store the point coordinates in the k-d tree
111 Grid::ConstIterator xitr = xg.cbegin();
112 Grid::ConstIterator yitr = yg.cbegin();
113
114 for (size_t i = 0; i < nelem; ++i, ++xitr, ++yitr) {
115 this->X[i] = *xitr;
116 this->Y[i] = *yitr;
117 }
118 } // end of the Constructor
119
120 // Must return the number of data points
121 inline size_t kdtree_get_point_count() const
122 {
123 VAssert(X.size() == Y.size());
124 return X.size();
125 }
126
127 // Returns the dim'th component of the idx'th point in the class:
128 // Since this is inlined and the "dim" argument is typically an immediate value, the
129 // "if/else's" are actually solved at compile time.
130 inline float kdtree_get_pt(const size_t idx, int dim) const
131 {
132 if (dim == 0)
133 return X[idx];
134 else
135 return Y[idx];
136 }
137
138 // Optional bounding-box computation: return false to default to a standard bbox computation loop.
139 // Return true if the BBOX was already computed by the class and returned in "bb"
140 // so it can be avoided to redo it again.
141 // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds)
142 template<class BBOX> bool kdtree_get_bbox(BBOX & /* bb */) const { return false; }
143
144 private:
145 std::vector<float> X, Y;
146
147 }; // end of class PointCloud2D
148
149 typedef nanoflann::KDTreeSingleIndexAdaptor<nanoflann::L2_Simple_Adaptor<float, PointCloud2D>, PointCloud2D, 2 /* dimension */> KDTreeType;
150
151 PointCloud2D _points;
152 KDTreeType _kdtree;
153 std::vector<size_t> _dims;
154}; // end of class KDTreeRG.
155
169public:
171
188 //
189 KDTreeRGSubset(const KDTreeRG *kdtreerg, const std::vector<size_t> &min, const std::vector<size_t> &max);
191
205 //
206 void Nearest(const std::vector<float> &coordu, std::vector<size_t> &coord) const;
207
208 void Nearest(const std::vector<double> &coordu, std::vector<size_t> &index) const
209 {
210 std::vector<float> coordu_f;
211 for (int i = 0; i < coordu.size(); i++) coordu_f.push_back(coordu[i]);
212 Nearest(coordu_f, index);
213 }
214
215 std::vector<size_t> GetDimensions() const
216 {
217 std::vector<std::size_t> dims;
218 for (int i = 0; i < _min.size(); i++) { dims.push_back(_max[i] - _min[i] + 1); }
219 return (dims);
220 }
221
222private:
223 const KDTreeRG * _kdtree;
224 std::vector<size_t> _min;
225 std::vector<size_t> _max;
226};
227
228}; // namespace VAPoR
229
230#endif
#define VAssert(expr)
Definition: VAssert.h:9
Abstract base class for a 2D or 3D structured or unstructured grid.
Definition: Grid.h:56
size_t GetNumDimensions() const
Definition: Grid.h:107
const DimsType & GetDimensions() const
Definition: Grid.h:100
ConstIterator cbegin(const CoordType &minu, const CoordType &maxu) const
Definition: Grid.h:1203
This class implements a k-d tree for a structured grid over a reduced region-of-interest (ROI)
Definition: KDTreeRG.h:168
void Nearest(const std::vector< double > &coordu, std::vector< size_t > &index) const
Definition: KDTreeRG.h:208
void Nearest(const std::vector< float > &coordu, std::vector< size_t > &coord) const
KDTreeRGSubset(const KDTreeRG *kdtreerg, const std::vector< size_t > &min, const std::vector< size_t > &max)
std::vector< size_t > GetDimensions() const
Definition: KDTreeRG.h:215
This class implements a k-d space partitioning tree.
Definition: KDTreeRG.h:22
std::vector< size_t > GetDimensions() const
Definition: KDTreeRG.h:92
void Nearest(const std::vector< double > &coordu, std::vector< size_t > &index) const
Definition: KDTreeRG.h:77
KDTreeRG(const Grid &xg, const Grid &yg)
virtual ~KDTreeRG()
void Nearest(const std::vector< float > &coordu, std::vector< size_t > &index) const
#define VDF_API
Definition: common.h:73