Three-dimensional interpolation for irregularly-spaced data. More...
#include <Interpolation3D.hpp>
Public Types | |
enum | InterpolationMethod { INVERSE_DISTANCE_WEIGHTING, INVERSE_DISTANCE_WEIGHTING_KNN, NEAREST_NEIGHBOR } |
typedef ValueType | value_type |
typedef PointType | point_type |
typedef DataType | data_type |
typedef PointType | Point |
Alias for point_type. | |
Public Member Functions | |
Interpolation3D (const DataType &default_value=DataType(), InterpolationMethod interpolation_method=INVERSE_DISTANCE_WEIGHTING_KNN) | |
Constructs an instance of Interpolation3D. | |
Interpolation3D (const std::vector< Point > &data_points, const std::vector< DataType > &data, const DataType &default_value=DataType(), InterpolationMethod interpolation_method=INVERSE_DISTANCE_WEIGHTING_KNN) | |
Constructs an instance of Interpolation3D. | |
~Interpolation3D () | |
Destructs the instance of Interpolation3D. | |
DataType | operator() (const Point &point) const |
Returns an interpolated value at the given position. | |
DataType | operator() (ValueType x, ValueType y, ValueType z) const |
Returns an interpolated value at the given position. | |
DataType | getData (const Point &point) const |
Returns an interpolated value at the given position. | |
DataType | getData (ValueType x, ValueType y, ValueType z) const |
Returns an interpolated value at the given position. | |
void | setData (const std::vector< Point > &data_points, const std::vector< DataType > &data) |
Sets the interpolation data. | |
void | setExponent (ValueType value=2.0) |
Sets the exponent for the inverse distance weighting. | |
void | setInterpolationMethod (InterpolationMethod interpolation_method) |
Sets the interpolation method. | |
void | setNumberOfNearestNeighbors (unsigned number=10) |
Sets the maximum number of nearest neighbors to take into account during computations. | |
void | setRadius (ValueType radius) |
Sets the radius for the nearest neighbor search. | |
Protected Member Functions | |
DataType | getDataInverseDistanceWeighting (ValueType x, ValueType y, ValueType z) const |
Performs an Inverse Distance Weighting. | |
DataType | getDataInverseDistanceWeightingKNN (ValueType x, ValueType y, ValueType z) const |
Performs an Inverse Distance Weighting. | |
DataType | getDataNearestNeighbor (ValueType x, ValueType y, ValueType z) const |
Performs a Nearest Neighbor Search. | |
Protected Attributes | |
DataType(Interpolation3D::* | interpolation_function )(ValueType, ValueType, ValueType) const |
ValueType | exponent |
ValueType | radius |
unsigned | max_number_of_nearest_neighbors |
flann::Index< flann::L2 < ValueType > > * | tree |
const DataType | data_type_default_value |
std::vector< DataType > | data |
flann::Matrix< ValueType > | data_points |
flann::Matrix< ValueType > | query_point |
flann::Matrix< ValueType > | distances |
flann::Matrix< int > | indices |
Three-dimensional interpolation for irregularly-spaced data.
ValueType | Must be a floating point type. |
PointType | Must provide operator [] for element access. |
DataType | Type of the data associated with each point (e.g. intensity, RGB-value, etc.). DataType must support operator+= as well as the multiplication with a scalar of type ValueType . |
This class provides a three-dimensional interpolation for irregularly-spaced data. The data types can be arbitrary and must be specified as templates parameters.
The following interpolation methods are available:
Here is an example of how to use the class:
#include <iostream> #include <TRTK/Interpolation3D.hpp> using namespace TRTK; int main() { try { // Setup data. typedef Interpolation3D<double, Coordinate<double>, double> Interpolation3D; typedef Interpolation3D::Point Point; std::vector<Point> data_points; std::vector<Interpolation3D::data_type> data; data_points.push_back(Point(1 ,1, 2)); data.push_back(1); data_points.push_back(Point(2, -1, 1)); data.push_back(2); data_points.push_back(Point(0, 0, 0)); data.push_back(3); data_points.push_back(Point(1, 0, -1)); data.push_back(4); data_points.push_back(Point(-1, 1, 3)); data.push_back(5); // Setup interpolation class. Interpolation3D interpolation(data_points, data, 0); interpolation.setInterpolationMethod(Interpolation3D::INVERSE_DISTANCE_WEIGHTING); interpolation.setNumberOfNearestNeighbors(10); interpolation.setRadius(10); // Get interpolated values. std::cout << interpolation(1.2, 1.0, 2.0) << std::endl; } catch (std::exception & error) { std::cout << error.what() << std::endl; return -1; } return 0; }
Output:
1.00032
To process higher-dimensional data, you might want to use the Coordinate class...:
// Setup data. typedef Interpolation3D<double> Interpolation3D; typedef Interpolation3D::Point Point; std::vector<Point> data_points; std::vector<Interpolation3D::data_type> data; data_points.push_back(Point(0, 0, 0)); data.push_back(Point(100, 100, 0)); data_points.push_back(Point(1 ,1, 1)); data.push_back(Point(255, 255, 255)); data_points.push_back(Point(2, -1, 3)); data.push_back(Point(0, 0, 0)); // Setup interpolation class. Interpolation3D interpolation(data_points, data, Point(0, 0, 0)); // Get interpolated values. std::cout << interpolation(1.2, 0.1, 2) << std::endl;
Output:
(173.451, 173.451, 165.952)
... or a self-defined class as below:
#include <iostream> #include <TRTK/Interpolation3D.hpp> using namespace TRTK; class RGB { public: RGB(unsigned short red = 0, unsigned short green = 0, unsigned short blue = 0) { data[0] = red; data[1] = green; data[2] = blue; } RGB & operator+=(const RGB & other) { this->data[0] += other.data[0]; this->data[1] += other.data[1]; this->data[2] += other.data[2]; return *this; } unsigned short operator[](size_t n) const { return data[n]; }; protected: unsigned short data[3]; }; RGB operator*(const RGB & rgb, float factor) { return RGB(rgb[0] * factor, rgb[1] * factor, rgb[2] * factor); } int main() { try { // Setup data. typedef Interpolation3D<double, Coordinate<double>, RGB> Interpolation3D; typedef Interpolation3D::Point Point; std::vector<Point> data_points; std::vector<Interpolation3D::data_type> data; data_points.push_back(Point(0, 0, 0)); data.push_back(RGB(100, 100, 0)); data_points.push_back(Point(1 ,1, 1)); data.push_back(RGB(255, 255, 255)); data_points.push_back(Point(2, -1, 3)); data.push_back(RGB(0, 0, 0)); // Setup interpolation class. Interpolation3D interpolation(data_points, data); interpolation.setInterpolationMethod(Interpolation3D::INVERSE_DISTANCE_WEIGHTING); interpolation.setRadius(10); // Get interpolated values. RGB value = interpolation(1.2, 0.1, 2); std::cout << "(" << value[0] << ", " << value[1] << ", " << value[2] << ")" << std::endl; } catch (std::exception & error) { std::cout << error.what() << std::endl; return -1; } return 0; }
Output:
(171, 171, 164)
Definition at line 285 of file Interpolation3D.hpp.
enum TRTK::Interpolation3D::InterpolationMethod |
Definition at line 295 of file Interpolation3D.hpp.
TRTK::Interpolation3D< ValueType, PointType, DataType >::Interpolation3D | ( | const DataType & | default_value = DataType() , |
InterpolationMethod | interpolation_method = INVERSE_DISTANCE_WEIGHTING_KNN |
||
) |
Constructs an instance of Interpolation3D.
[in] | default_value | Default value for DataType (e.g. null vector). |
[in] | interpolation_method | Interpolation method to be used. |
Definition at line 362 of file Interpolation3D.hpp.
TRTK::Interpolation3D< ValueType, PointType, DataType >::Interpolation3D | ( | const std::vector< Point > & | data_points, |
const std::vector< DataType > & | data, | ||
const DataType & | default_value = DataType() , |
||
InterpolationMethod | interpolation_method = INVERSE_DISTANCE_WEIGHTING_KNN |
||
) |
Constructs an instance of Interpolation3D.
[in] | data_points | Positions (in 3D) of the below data. |
[in] | data | Data associated with the above positions (e.g. RGB-values). |
[in] | default_value | Default value for DataType (e.g. null vector). |
[in] | interpolation_method | Interpolation method to be used. |
Definition at line 384 of file Interpolation3D.hpp.
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getData | ( | const Point & | point ) | const [inline] |
Returns an interpolated value at the given position.
Definition at line 448 of file Interpolation3D.hpp.
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getData | ( | ValueType | x, |
ValueType | y, | ||
ValueType | z | ||
) | const [inline] |
Returns an interpolated value at the given position.
Definition at line 461 of file Interpolation3D.hpp.
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getDataInverseDistanceWeighting | ( | ValueType | x, |
ValueType | y, | ||
ValueType | z | ||
) | const [protected] |
Performs an Inverse Distance Weighting.
[in] | x | x position |
[in] | y | y position |
[in] | z | z position |
An interpolated value \( data' \) at postion \( (x, y, z) \) is computed from its surrounding neighborhood \( \mathcal{N} \) by
\[ data' = \left( \sum_{i \in \mathcal{N}} {|d_i|}^{-p} \right)^{-1} \sum_{i \in \mathcal{N}} {|d_i|}^{-p} data_i \]
where \( d_i \) is the distance between the given point \( (x, y, z) \) and the location of the i-th data point. The Neighborhood is a circle whose radius can be specified by setRadius()
. At most max_number_of_nearest_neighbors
are used to compute the interpolant, which can be set by setNumberOfNearestNeighbors()
. The exponent \( p \) can be set by setExponent()
.
std::range_error | is thrown, if no data is given. |
std::runtime_error | is thrown, if the search radius is too small and no nearest neighbor are found. |
Definition at line 492 of file Interpolation3D.hpp.
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getDataInverseDistanceWeightingKNN | ( | ValueType | x, |
ValueType | y, | ||
ValueType | z | ||
) | const [protected] |
Performs an Inverse Distance Weighting.
[in] | x | x position |
[in] | y | y position |
[in] | z | z position |
An interpolated value \( data' \) at postion \( (x, y, z) \) is computed from its surrounding neighborhood \( \mathcal{N} \) by
\[ data' = \left( \sum_{i \in \mathcal{N}} {|d_i|}^{-p} \right)^{-1} \sum_{i \in \mathcal{N}} {|d_i|}^{-p} data_i \]
where \( d_i \) is the distance between the given point \( (x, y, z) \) and the location of the i-th data point. At most max_number_of_nearest_neighbors
are used to compute the interpolant, which can be set by setNumberOfNearestNeighbors()
. The exponent \( p \) can be set by setExponent()
.
std::range_error | is thrown, if no data is given. |
Definition at line 563 of file Interpolation3D.hpp.
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getDataNearestNeighbor | ( | ValueType | x, |
ValueType | y, | ||
ValueType | z | ||
) | const [protected] |
Performs a Nearest Neighbor Search.
[in] | x | x position |
[in] | y | y position |
[in] | z | z position |
std::range_error | is thrown, if no data is given. |
Definition at line 620 of file Interpolation3D.hpp.
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::operator() | ( | ValueType | x, |
ValueType | y, | ||
ValueType | z | ||
) | const [inline] |
Returns an interpolated value at the given position.
Definition at line 435 of file Interpolation3D.hpp.
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::operator() | ( | const Point & | point ) | const [inline] |
Returns an interpolated value at the given position.
Definition at line 422 of file Interpolation3D.hpp.
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setData | ( | const std::vector< Point > & | data_points, |
const std::vector< DataType > & | data | ||
) |
Sets the interpolation data.
[in] | data_points | Positions (in 3D) of the below data. |
[in] | data | Data associated with the above positions (e.g. RGB-values). |
The data may be irregularly spaced.
std::logic_error | is thrown, if data_points and data do not have the same size. |
std::runtime_error | is thrown, if the input data is emtpy. |
Definition at line 649 of file Interpolation3D.hpp.
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setExponent | ( | ValueType | value = 2.0 ) |
Sets the exponent for the inverse distance weighting.
[in] | value | Exponent. |
See Detailed Description for more details.
Definition at line 695 of file Interpolation3D.hpp.
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setInterpolationMethod | ( | InterpolationMethod | interpolation_method ) |
Sets the interpolation method.
[in] | interpolation_method | Interpolation method. |
std::logic_error | is thrown, if interpolation_method is unknown. |
Definition at line 711 of file Interpolation3D.hpp.
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setNumberOfNearestNeighbors | ( | unsigned | number = 10 ) |
Sets the maximum number of nearest neighbors to take into account during computations.
[in] | number | Must be greater than zero. |
std::logic_error | is thrown, if number is equal to zero. |
Definition at line 752 of file Interpolation3D.hpp.
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setRadius | ( | ValueType | radius ) |
Sets the radius for the nearest neighbor search.
[in] | radius | Must be greater than zero. |
std::logic_error | is thrown, if radius is less than or equal to zero. |
Definition at line 781 of file Interpolation3D.hpp.
Documentation generated by Doxygen