CoastalME (Coastal Modelling Environment)
Simulates the long-term behaviour of complex coastlines
Loading...
Searching...
No Matches
spatial_interpolation.h
Go to the documentation of this file.
1//===============================================================================================================================
35//===============================================================================================================================
36
37#ifndef SPATIAL_INTERPOLATION_H
38#define SPATIAL_INTERPOLATION_H
39
40#include <vector>
41#include <cmath>
42#include <algorithm>
43#include "nanoflann.hpp"
44
45//===============================================================================================================================
47//===============================================================================================================================
48struct Point2D
49{
50 double x, y;
51 Point2D(double x_ = 0, double y_ = 0) : x(x_), y(y_)
52 {
53 }
54};
55
56// Point cloud adaptor for nanoflann
58{
59 std::vector<Point2D> pts;
60
61 inline size_t kdtree_get_point_count() const
62 {
63 return pts.size();
64 }
65
66 inline double kdtree_get_pt(size_t const idx, size_t const dim) const
67 {
68 return (dim == 0) ? pts[idx].x : pts[idx].y;
69 }
70
71 template<class BBOX>
72 bool kdtree_get_bbox(BBOX&) const
73 {
74 return false;
75 }
76};
77
78// Type definitions for nanoflann
82 2 // 2D
83 >;
84
85// Main interpolator class
87{
88 public:
89 SpatialInterpolator(std::vector<Point2D> const& points,
90 std::vector<double> const& values,
91 int k_neighbors = 12,
92 double power = 2.0);
93
95
96 // Interpolate at a single point
97 double Interpolate(double x, double y) const;
98
99 // Interpolate at multiple points (more efficient)
100 void Interpolate(std::vector<Point2D> const& query_points,
101 std::vector<double>& results) const;
102
103 // Get the k-d tree (for sharing between interpolators)
104 KDTree const* GetKDTree() const { return m_kdtree; }
105
106 // Get the point cloud (for sharing between interpolators)
107 PointCloud const& GetPointCloud() const { return m_cloud; }
108
109 private:
111 std::vector<double> m_values;
114 double m_power;
116
117 static constexpr double EPSILON = 1e-10;
118
119 // Private constructor for sharing k-d tree
121 SpatialInterpolator(PointCloud const& cloud,
122 KDTree* kdtree,
123 std::vector<double> const& values,
124 int k_neighbors,
125 double power);
126};
127
128// Optimized dual interpolator for X and Y values sharing same spatial points
130{
131 public:
132 DualSpatialInterpolator(std::vector<Point2D> const& points,
133 std::vector<double> const& values_x,
134 std::vector<double> const& values_y,
135 int k_neighbors = 12,
136 double power = 2.0);
137
139
140 // Interpolate both X and Y at multiple points (parallel optimized)
141 void Interpolate(std::vector<Point2D> const& query_points,
142 std::vector<double>& results_x,
143 std::vector<double>& results_y) const;
144
145 private:
147 std::vector<double> m_values_x;
148 std::vector<double> m_values_y;
151 double m_power;
152
153 static constexpr double EPSILON = 1e-10;
154
155 // Helper for single point interpolation
156 void InterpolatePoint(double x, double y, double& result_x, double& result_y,
157 std::vector<unsigned int>& indices,
158 std::vector<double>& sq_dists) const;
159};
160
161#endif // SPATIAL_INTERPOLATION_H
void Interpolate(std::vector< Point2D > const &query_points, std::vector< double > &results_x, std::vector< double > &results_y) const
void InterpolatePoint(double x, double y, double &result_x, double &result_y, std::vector< unsigned int > &indices, std::vector< double > &sq_dists) const
std::vector< double > m_values_y
DualSpatialInterpolator(std::vector< Point2D > const &points, std::vector< double > const &values_x, std::vector< double > const &values_y, int k_neighbors=12, double power=2.0)
static constexpr double EPSILON
std::vector< double > m_values_x
std::vector< double > m_values
static constexpr double EPSILON
SpatialInterpolator(std::vector< Point2D > const &points, std::vector< double > const &values, int k_neighbors=12, double power=2.0)
double Interpolate(double x, double y) const
KDTree const * GetKDTree() const
PointCloud const & GetPointCloud() const
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< double, PointCloud >, PointCloud, 2 > KDTree
Point2D(double x_=0, double y_=0)
size_t kdtree_get_point_count() const
bool kdtree_get_bbox(BBOX &) const
double kdtree_get_pt(size_t const idx, size_t const dim) const
std::vector< Point2D > pts