bilinearInterpolator.h
Go to the documentation of this file.00001
00015 #ifndef DLR_NUMERIC_BILINEARINTERPOLATOR_H
00016 #define DLR_NUMERIC_BILINEARINTERPOLATOR_H
00017
00018 #include <cmath>
00019 #include <dlrNumeric/array2D.h>
00020
00021 namespace dlr {
00022
00023 namespace numeric {
00024
00025 template <class TYPE>
00026 class BilinearInterpolator
00027 {
00028 public:
00029
00030 BilinearInterpolator(const Array2D<TYPE>& image0)
00031 : m_array(image0) {}
00032
00033
00034 ~BilinearInterpolator() {}
00035
00036
00037 inline double
00038 operator()(double row, double column);
00039
00040 private:
00041
00042 inline void
00043 checkBounds(double row, double column) const;
00044
00045
00046 Array2D<TYPE> m_array;
00047
00048 };
00049
00050 }
00051
00052 }
00053
00054
00055
00056
00057 namespace dlr {
00058
00059 using numeric::BilinearInterpolator;
00060
00061 }
00062
00063
00064 namespace dlr {
00065
00066 namespace numeric {
00067
00068 template <class TYPE>
00069 inline double
00070 BilinearInterpolator<TYPE>::
00071 operator()(double row, double column)
00072 {
00073 this->checkBounds(row, column);
00074
00075 double tmpDbl;
00076 double x1 = modf(column, &tmpDbl);
00077 size_t i0 = static_cast<size_t>(tmpDbl);
00078 double y1 = modf(row, &tmpDbl);
00079 size_t j0 = static_cast<size_t>(tmpDbl);
00080 double x0 = 1.0 - x1;
00081 double y0 = 1.0 - y1;
00082 size_t index0 = m_array.columns() * j0 + i0;
00083 return (x0 * (y0 * this->m_array(index0)
00084 + y1 * this->m_array(index0 + m_array.columns()))
00085 + x1 * (y0 * this->m_array(index0 + 1)
00086 + y1 * this->m_array(index0 + m_array.columns() + 1)));
00087 }
00088
00089
00090 template <class TYPE>
00091 inline void
00092 BilinearInterpolator<TYPE>::
00093 checkBounds(double row, double column) const
00094 {
00095 #ifdef _DLRNUMERIC_CHECKBOUNDS_
00096 int row0 = static_cast<int>(row);
00097 if(row0 < 0 || (row0 + 1) >= static_cast<int>(m_array.rows())) {
00098 std::ostringstream message;
00099 message << "Row index " << row << " is invalid for a(n) "
00100 << m_array.rows() << " x " << m_array.columns() << " array.";
00101 DLR_THROW(IndexException, "BilinearInterpolator::checkBounds()",
00102 message.str().c_str());
00103 }
00104 int column0 = static_cast<int>(column);
00105 if(column0 < 0 || (column0 + 1) >= static_cast<int>(m_array.columns())) {
00106 std::ostringstream message;
00107 message << "Column index " << column << " is invalid for a(n) "
00108 << m_array.rows() << " x " << m_array.columns() << " array.";
00109 DLR_THROW(IndexException, "BilinearInterpolator::checkBounds()",
00110 message.str().c_str());
00111 }
00112 #endif
00113 }
00114
00115 }
00116
00117 }
00118
00119 #endif