X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=include%2Finterpolator.h;h=c1f454288c055d5f1d3db56384fc77c45b49897d;hp=c6a34d69792b1c43f7e04be8e67a83775225bd83;hb=8a7697ce57b56cdc43698cd1241ad98d49f9b5ac;hpb=c2da697c4203112cf53df1fcd2f8de6f5e385f14 diff --git a/include/interpolator.h b/include/interpolator.h index c6a34d6..c1f4542 100644 --- a/include/interpolator.h +++ b/include/interpolator.h @@ -2,7 +2,7 @@ ** This is part of the CTSim program ** Copyright (c) 1983-2001 Kevin Rosenberg ** -** $Id: interpolator.h,v 1.5 2001/03/30 22:09:09 kevin Exp $ +** $Id$ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -53,8 +53,8 @@ template class BilinearInterpolator { private: T** const m_ppMatrix; - const unsigned int m_nx; - const unsigned int m_ny; + const int m_nx; + const int m_ny; public: BilinearInterpolator (T** ppMatrix, unsigned int nx, unsigned int ny) @@ -62,30 +62,98 @@ public: {} T interpolate (double dXPos, double dYPos) - { - int iFloorX = static_cast(floor(dXPos)); - int iFloorY = static_cast(floor (dYPos)); - double dXFrac = dXPos - iFloorX; - double dYFrac = dYPos - iFloorY; +{ + int iFloorX = static_cast(floor(dXPos)); + int iFloorY = static_cast(floor (dYPos)); + double dXFrac = dXPos - iFloorX; + double dYFrac = dYPos - iFloorY; + + T result = 0; + + if (iFloorX < 0 || iFloorY < 0 || iFloorX > m_nx-1 || iFloorY > m_ny-1) + result = 0; + else if (iFloorX == m_nx - 1 && iFloorY == m_ny - 1) + result = static_cast(m_ppMatrix[m_nx-1][m_ny-1]); + else if (iFloorX == m_nx - 1) + result = static_cast(m_ppMatrix[iFloorX][iFloorY] + dYFrac * (m_ppMatrix[iFloorX][iFloorY+1] - m_ppMatrix[iFloorX][iFloorY])); + else if (iFloorY == m_ny - 1) + result = static_cast(m_ppMatrix[iFloorX][iFloorY] + dXFrac * (m_ppMatrix[iFloorX+1][iFloorY] - m_ppMatrix[iFloorX][iFloorY])); + else + result = static_cast + ((1 - dXFrac) * (1 - dYFrac) * m_ppMatrix[iFloorX][iFloorY] + + dXFrac * (1 - dYFrac) * m_ppMatrix[iFloorX+1][iFloorY] + + dYFrac * (1 - dXFrac) * m_ppMatrix[iFloorX][iFloorY+1] + + dXFrac * dYFrac * m_ppMatrix[iFloorX+1][iFloorY+1]); + + return result; +} + }; - T result = 0; - if (iFloorX < 0 || iFloorY < 0 || iFloorX > m_nx-1 || iFloorY > m_ny-1) - result = 0; - else if (iFloorX == m_nx - 1 && iFloorY == m_ny - 1) - result = m_ppMatrix[m_nx-1][m_ny-1]; - else if (iFloorX == m_nx - 1) - result = m_ppMatrix[iFloorX][iFloorY] + dYFrac * (m_ppMatrix[iFloorX][iFloorY+1] - m_ppMatrix[iFloorX][iFloorY]); - else if (iFloorY == m_ny - 1) - result = m_ppMatrix[iFloorX][iFloorY] + dXFrac * (m_ppMatrix[iFloorX+1][iFloorY] - m_ppMatrix[iFloorX][iFloorY]); +template +class BilinearPolarInterpolator { +private: + T** const m_ppMatrix; + const int m_nAngle; + const int m_nPos; + int m_nCenterPos; + +public: + BilinearPolarInterpolator (T** ppMatrix, unsigned int nAngle, + unsigned int nPos) + : m_ppMatrix(ppMatrix), m_nAngle(nAngle), m_nPos(nPos) + { + if (m_nPos %2) + m_nCenterPos = (m_nPos - 1) / 2; else - result = (1 - dXFrac) * (1 - dYFrac) * m_ppMatrix[iFloorX][iFloorY] + - dXFrac * (1 - dYFrac) * m_ppMatrix[iFloorX+1][iFloorY] + - dYFrac * (1 - dXFrac) * m_ppMatrix[iFloorX][iFloorY+1] + - dXFrac * dYFrac * m_ppMatrix[iFloorX+1][iFloorY+1]; + m_nCenterPos = m_nPos / 2; + } - return result; + T interpolate (double dAngle, double dPos) +{ + int iFloorAngle = static_cast(floor(dAngle)); + int iFloorPos = static_cast(floor (dPos)); + double dAngleFrac = dAngle - iFloorAngle; + double dPosFrac = dPos - iFloorPos; + + T result = 0; + + if (iFloorAngle < -1 || iFloorPos < 0 || iFloorAngle > m_nAngle-1 || iFloorPos > m_nPos-1) + result = 0; + else if (iFloorAngle == -1 && iFloorPos == m_nPos-1) + result = static_cast(m_ppMatrix[0][m_nPos-1] + dAngleFrac * (m_ppMatrix[m_nAngle-1][iFloorPos] - m_ppMatrix[0][iFloorPos])); + else if (iFloorAngle == m_nAngle - 1 && iFloorPos == m_nPos-1) + result = static_cast(m_ppMatrix[m_nAngle-1][m_nPos-1] + dAngleFrac * (m_ppMatrix[0][iFloorPos] - m_ppMatrix[m_nAngle-1][iFloorPos])); + else if (iFloorPos == m_nPos - 1) + result = static_cast(m_ppMatrix[iFloorAngle][iFloorPos] + dAngleFrac * (m_ppMatrix[iFloorAngle+1][iFloorPos] - m_ppMatrix[iFloorAngle][iFloorPos])); + else { + if (iFloorAngle == m_nAngle-1) { + int iUpperAngle = 0; + int iLowerPos = (m_nPos-1) - iFloorPos; + int iUpperPos = (m_nPos-1) - (iFloorPos+1); + result = static_cast + ((1-dAngleFrac) * (1-dPosFrac) * m_ppMatrix[iFloorAngle][iFloorPos] + + dAngleFrac * (1-dPosFrac) * m_ppMatrix[iUpperAngle][iLowerPos] + + dPosFrac * (1-dAngleFrac) * m_ppMatrix[iFloorAngle][iFloorPos+1] + + dAngleFrac * dPosFrac * m_ppMatrix[iUpperAngle][iUpperPos]); + } else if (iFloorAngle == -1) { + int iLowerAngle = m_nAngle - 1; + int iLowerPos = (m_nPos-1) - iFloorPos; + int iUpperPos = (m_nPos-1) - (iFloorPos+1); + result = static_cast + ((1-dAngleFrac) * (1-dPosFrac) * m_ppMatrix[iLowerAngle][iLowerPos] + + dAngleFrac * (1-dPosFrac) * m_ppMatrix[iFloorAngle+1][iFloorPos] + + dPosFrac * (1-dAngleFrac) * m_ppMatrix[iLowerAngle][iUpperPos] + + dAngleFrac * dPosFrac * m_ppMatrix[iFloorAngle+1][iFloorPos+1]); + } else + result = static_cast + ((1-dAngleFrac) * (1-dPosFrac) * m_ppMatrix[iFloorAngle][iFloorPos] + + dAngleFrac * (1-dPosFrac) * m_ppMatrix[iFloorAngle+1][iFloorPos] + + dPosFrac * (1-dAngleFrac) * m_ppMatrix[iFloorAngle][iFloorPos+1] + + dAngleFrac * dPosFrac * m_ppMatrix[iFloorAngle+1][iFloorPos+1]); } + return result; +} }; @@ -103,10 +171,10 @@ public: T interpolate (double dXPos, double dYPos) { - int iFloorX = floor (dXPos); - int iFloorY = floor (dYPos); - double dXFrac = dXPos - iFloorX; - double dYFrac = dYPos - iFloorY; + // int iFloorX = static_cast(floor (dXPos)); + // int iFloorY = static_cast(floor (dYPos)); + // double dXFrac = dXPos - iFloorX; + // double dYFrac = dYPos - iFloorY; T result = 0; @@ -122,7 +190,7 @@ class LinearInterpolator { private: T* const m_pX; T* const m_pY; - const unsigned int m_n; + const int m_n; const bool m_bZeroOutsideRange; public: @@ -154,7 +222,7 @@ public: else result = m_pY[m_n - 1]; } else { - int iFloor = floor(dX); + int iFloor = static_cast(floor(dX)); result = m_pY[iFloor] + (m_pY[iFloor+1] - m_pY[iFloor]) * (dX - iFloor); } } else {