Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interpolate2D to CtlLookupTable.cpp #8

Closed
scottdyer opened this issue Jan 31, 2012 · 1 comment
Closed

Add interpolate2D to CtlLookupTable.cpp #8

scottdyer opened this issue Jan 31, 2012 · 1 comment

Comments

@scottdyer
Copy link
Contributor

The function should interpolate to find ZI, the values of the underlying 2-D function Z at the points XI and YI. Vectors X and Y specify the points at which the data Z is given.
Similarly to interpolate1D, this function should use linear interpolation.

Inputs: X,Y,Z,XI,YI
Outputs: ZI

Here is some rough code that should, I think, do the job. I'm not sure if the declaration, or later the calculation, of the size of the input vectors is valid.

float   
interpolate2D
    (const float X[],
     const float Y[],
     const float Z[],
     float XI,
     float YI)
{

    // Check sizes of inputs
    size_t n = sizeof(X);
    size_t m = sizeof(Y);
    size_t nZ = sizeof(Z[0]);
    size_t mZ = sizeof(Z);

    if (n != nZ)
    return 0;

    if (m != mZ)
    return 0;

    if ( (n < 1) || (m < 1) )
    return 0;

    // Find xLow and xHigh
    float xMin = X[0];      float xMax = X[n-1];
    int xLowIndex;          int xHighIndex;
    float xPercent;

    if (XI > xMin && XI < xMax) // XI is within vector X
    {
        int j = 0;
        while (XI > X[j])
        {
            j = j + 1;
            xLowIndex = j-1;
            xHighIndex = j;
        }
        xPercent = (XI - X[xLowIndex]) / (X[xHighIndex] - X[xLowIndex]);        
    }
    else if (XI <= xMin) // XI is lower than minimum of vector X, use low edge
    {
        xLowIndex = 0;
        xHighIndex = 0; 
        xPercent = 1.0;
    }
    else if (XI >= xMax) // XI higher than maximum of vector X, use high edge
    {
        xLowIndex = n-1;
        xHighIndex = n-1;   
        xPercent = 1.0;
    }
    else // XI is a NaN
    {
        return 0;
    }

    float yMin = Y[0];      float yMax = Y[m-1];
    int yLowIndex;          int yHighIndex;
    float yPercent;

    if (YI > yMin && YI < yMax) // YI is within vector Y
    {
        int j = 0;
        while (YI > Y[j]) 
        {
            j = j + 1;
            yLowIndex = j-1;
            yHighIndex = j;
        }
        yPercent = (YI - Y[yLowIndex]) / (Y[yHighIndex] - Y[yLowIndex]);
    }
    else if (YI <= yMin) // YI is lower than minimum of vector Y, use low edge
    {
        yLowIndex = 0;
        yHighIndex = 0; 
        yPercent = 1.0;
    }
    else if (YI >= yMax) // YI higher than maximum of vector Y, use high edge
    {
        yLowIndex = m-1;
        yHighIndex = m-1;   
        yPercent = 1.0;
    }
    else // YI is a NaN
    {
        return 0;
    }

float val1 = (Z[yLowIndex][xHighIndex] - Z[yLowIndex][xLowIndex]) * xPercent + Z[yLowIndex][xLowIndex];
float val2 = (Z[yHighIndex][xHighIndex] - Z[yHighIndex][xLowIndex]) * xPercent + Z[yHighIndex][xLowIndex];

return (val2 - val1) * yPercent + val1;

}
@scottdyer
Copy link
Contributor Author

No longer relevant. Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant