Public Types | Public Member Functions | Protected Member Functions | Protected Attributes

TRTK::Interpolation3D< ValueType, PointType, DataType > Class Template Reference

Three-dimensional interpolation for irregularly-spaced data. More...

#include <Interpolation3D.hpp>

List of all members.

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

Detailed Description

template<class ValueType = double, class PointType = Coordinate<double>, class DataType = Coordinate<double>>
class TRTK::Interpolation3D< ValueType, PointType, DataType >

Three-dimensional interpolation for irregularly-spaced data.

Template Parameters:
ValueTypeMust be a floating point type.
PointTypeMust provide operator[] for element access.
DataTypeType 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)
Author:
Christoph Haenisch
Version:
0.1.2
Date:
last changed on 2013-08-20

Definition at line 285 of file Interpolation3D.hpp.


Member Enumeration Documentation

template<class ValueType = double, class PointType = Coordinate<double>, class DataType = Coordinate<double>>
enum TRTK::Interpolation3D::InterpolationMethod
Enumerator:
INVERSE_DISTANCE_WEIGHTING 

Interpolation using Shepard's method. Only the k first nearest neighbors are taken into account. Searched is within a circle whose radius can be specified with setRadius.

INVERSE_DISTANCE_WEIGHTING_KNN 

Interpolation using Shepard's method. Only the k first nearest neighbors are taken into account.

NEAREST_NEIGHBOR 

Yields the datum of the nearest neighbor.

Definition at line 295 of file Interpolation3D.hpp.


Constructor & Destructor Documentation

template<class ValueType , class PointType , class DataType >
TRTK::Interpolation3D< ValueType, PointType, DataType >::Interpolation3D ( const DataType &  default_value = DataType(),
InterpolationMethod  interpolation_method = INVERSE_DISTANCE_WEIGHTING_KNN 
)

Constructs an instance of Interpolation3D.

Parameters:
[in]default_valueDefault value for DataType (e.g. null vector).
[in]interpolation_methodInterpolation method to be used.

Definition at line 362 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
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.

Parameters:
[in]data_pointsPositions (in 3D) of the below data.
[in]dataData associated with the above positions (e.g. RGB-values).
[in]default_valueDefault value for DataType (e.g. null vector).
[in]interpolation_methodInterpolation method to be used.

Definition at line 384 of file Interpolation3D.hpp.


Member Function Documentation

template<class ValueType , class PointType , class DataType >
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getData ( const Point point ) const [inline]

Returns an interpolated value at the given position.

Note:
Depending on the currently set interpolation method, an exception might be thrown.

Definition at line 448 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getData ( ValueType  x,
ValueType  y,
ValueType  z 
) const [inline]

Returns an interpolated value at the given position.

Note:
Depending on the currently set interpolation method, an exception might be thrown.

Definition at line 461 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getDataInverseDistanceWeighting ( ValueType  x,
ValueType  y,
ValueType  z 
) const [protected]

Performs an Inverse Distance Weighting.

Parameters:
[in]xx position
[in]yy position
[in]zz 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().

Exceptions:
std::range_erroris thrown, if no data is given.
std::runtime_erroris thrown, if the search radius is too small and no nearest neighbor are found.
See also:
InterpolationMethod

Definition at line 492 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getDataInverseDistanceWeightingKNN ( ValueType  x,
ValueType  y,
ValueType  z 
) const [protected]

Performs an Inverse Distance Weighting.

Parameters:
[in]xx position
[in]yy position
[in]zz 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().

Exceptions:
std::range_erroris thrown, if no data is given.
See also:
InterpolationMethod

Definition at line 563 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::getDataNearestNeighbor ( ValueType  x,
ValueType  y,
ValueType  z 
) const [protected]

Performs a Nearest Neighbor Search.

Parameters:
[in]xx position
[in]yy position
[in]zz position
Returns:
The datum of the nearest neighbor is returned.
Exceptions:
std::range_erroris thrown, if no data is given.
See also:
InterpolationMethod

Definition at line 620 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::operator() ( ValueType  x,
ValueType  y,
ValueType  z 
) const [inline]

Returns an interpolated value at the given position.

Note:
Depending on the currently set interpolation method, an exception might be thrown.

Definition at line 435 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
DataType TRTK::Interpolation3D< ValueType, PointType, DataType >::operator() ( const Point point ) const [inline]

Returns an interpolated value at the given position.

Note:
Depending on the currently set interpolation method, an exception might be thrown.

Definition at line 422 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setData ( const std::vector< Point > &  data_points,
const std::vector< DataType > &  data 
)

Sets the interpolation data.

Parameters:
[in]data_pointsPositions (in 3D) of the below data.
[in]dataData associated with the above positions (e.g. RGB-values).

The data may be irregularly spaced.

Exceptions:
std::logic_erroris thrown, if data_points and data do not have the same size.
std::runtime_erroris thrown, if the input data is emtpy.

Definition at line 649 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setExponent ( ValueType  value = 2.0 )

Sets the exponent for the inverse distance weighting.

Parameters:
[in]valueExponent.

See Detailed Description for more details.

Definition at line 695 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setInterpolationMethod ( InterpolationMethod  interpolation_method )

Sets the interpolation method.

Parameters:
[in]interpolation_methodInterpolation method.
Exceptions:
std::logic_erroris thrown, if interpolation_method is unknown.
See also:
InterpolationMethod

Definition at line 711 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setNumberOfNearestNeighbors ( unsigned  number = 10 )

Sets the maximum number of nearest neighbors to take into account during computations.

Parameters:
[in]numberMust be greater than zero.
Exceptions:
std::logic_erroris thrown, if number is equal to zero.
See also:
getDataInverseDistanceWeighting(), getDataInverseDistanceWeightingKNN(), getDataNearestNeighbor()

Definition at line 752 of file Interpolation3D.hpp.

template<class ValueType , class PointType , class DataType >
void TRTK::Interpolation3D< ValueType, PointType, DataType >::setRadius ( ValueType  radius )

Sets the radius for the nearest neighbor search.

Parameters:
[in]radiusMust be greater than zero.
Exceptions:
std::logic_erroris thrown, if radius is less than or equal to zero.
See also:
getDataInverseDistanceWeighting(), getDataInverseDistanceWeightingKNN()

Definition at line 781 of file Interpolation3D.hpp.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines