I cannot understand Finite Difference Method exactly

359 views Asked by At

I am a student who studies DirectX 11. To calculate the normal vector of the surface, I know I need to get two tangent vectors to calculate the cross product. But I can do it if there is the equation of surface.

If I don't know the equation/formula of surface, I have to get the approximation of normal vector using Finite Difference Method. Below code is from the book I am reading.

for(UINT i = 1; i < mNumRows-1; ++i)
{
    for(UINT j = 1; j < mNumCols-1; ++j)
    {
        float l = mCurrSolution[i*mNumCols+j-1].y;
        float r = mCurrSolution[i*mNumCols+j+1].y;
        float t = mCurrSolution[(i-1)*mNumCols+j].y;
        float b = mCurrSolution[(i+1)*mNumCols+j].y;
        mNormals[i*mNumCols+j].x = -r+l;
        mNormals[i*mNumCols+j].y = 2.0f*mSpatialStep;
        mNormals[i*mNumCols+j].z = b-t;

        XMVECTOR n = XMVector3Normalize(XMLoadFloat3(&mNormals[i*mNumCols+j]));
        XMStoreFloat3(&mNormals[i*mNumCols+j], n);
    }
}

I cannot understand why the values -r+l and b-t (the difference of y value) become the normal vector's x value and z value, respectively. I wonder why 2.0f * mSpatialStep is the y value, too.

1

There are 1 answers

5
AudioBubble On

A derivative is numerically estimated by Ft ~ (F(t+step) - F(t-step))/ (2 step). Observe how the given normal vector is proportional to the estimate of the gradient of y = - F(x, z), i.e. (Fx, 1, Fz).