ImageLineProfile problems

I am having some problems when using the built-in function ImageLineProfile on large data sets. In some cases, I would like to slice out a two-dimensional wave from a three-dimensional data set along specific directions defined in the 2D plane of each layer. I therefore created a function that allows me to slice along the line between two standard cursors "A" and "B" that I display on an arbitrary layer of the 3D data cube, exploiting the functionality of the ImageLineProfile function. However, I have noticed that the wave M_ImageLineProfile that is generated by the function sometimes comes out "empty" (i.e. a null wave), despite the fact that I provide the function with well-defined input waves xwave, ywave and srcwave. I have consulted the .ihf for the function, but not gotten any wiser from it. Hence I provide a minimum working example here, to see if anyone is able to spot something I have overlooked.

First, I will generate an arbitrary 3D wave of similar dimensions to the ones I normally work with, and display it in the top graph:

Make/O/N=(1024,1024,400) DataCube=gnoise(10,2)
Display; AppendImage DataCube

Next, I place two standard cursors "A" and "B" on the wave displayed, and ask ImageLineprofile to generate a 2D wave along the straight line between the cursors for me. I also specify the variable sliceWidth, so that ImageLineProfile extracts and averages the interpolated values at N equidistant points on the normal to profile line segment defined, with N=2(width+0.5). All of this is done using the following function:

Function cubeSlicer(displayName,cubeWave,sliceWidth)
    String displayName
    Wave cubeWave
    variable sliceWidth

    // Cursor variables
    variable pA = pcsr(A,displayName)
    variable qA = qcsr(A,displayName)
    variable pB = pcsr(B,displayName)
    variable qB = qcsr(B,displayName)
       
    // (1) Calculate line profile between cursors on surface
    variable npx = round(sqrt((pB-pA)^2+(qB-qA)^2 ))+1              //number of pixels
    Make/O/N=(npx) ptmp,qtmp                                                    // Allocate x and y waves for ImageLineProfile
    ptmp[]=pA+(pB-pA)/(npx-1)*p                                                 // x wave of the profile
    qtmp[]=qA+(qB-qA)/(npx-1)*p                                                 // y wave of the profile   
   
    // (2) Extract Line Profile from energy stack
    ImageLineProfile/P=-2 srcWave=cubeWave, xWave=ptmp, yWave=qtmp, width=sliceWidth
    Wave M_ImageLineProfile
    Duplicate/O M_ImageLineProfile, slice                                   // Duplicate the 2D slice profile
       
    // (3) Display slice wave in separate panel
    Display/N=slicePanel
    AppendImage/W=slicePanel slice                                                  // Append new wave to subpanel display

    // (4) cleanup
    KillWaves/Z M_imageLineProfile, W_LineProfileX, W_LineProfileY, ptmp, qtmp

End

The problem is:
For certain lengths npx and widths, M_imageLineProfile comes out empty. If I move the cursors closer together (i.e. reduce npx) or reduce the width of the sampling area, I am able to obtain a well-defined wave from M_imageLineProfile . If I then increase one of these dimensions above a certain threshold again, M_imageLineProfile will come out empty.

Questions:
Is there a definite size limit/maximum allowable data points defined for the output wave M_imageLineProfile? It seems so, since I am able to circumvent the problem if I reduce the overall length and width of the sampling area. If so: is this limit defined somewhere? I could not find anything about this in the Igor manual.

But most importantly:
What would be a possible solution to the problem? Reducing the size of the data sets (waves) is unfortunately not an option here.

An arbitrary 3D test wave showing how the cursors can be placed on one of the layers

You are not really providing examples of widths for which this fails on you.  The obvious situation where this would fail is when the width is such that it corresponds to points outside the domain.  This will return NaNs in the interpolations.  If this is not the case please email an example to support@wavemetrics.com

In reply to by Igor

For the given example (and cursor position), any "width" parameter N larger than 11 px (so that the sampling width becomes 2*11 + 1 px) fails to produce a non-empty wave with finite values in any row+column combination. All empty cells. The code with anything less than N=10, and M_ImageLineProfile is generated with finite/non-empty cells.

In other words, judging by the size of the example wave (1024x1024*400), the domain defined by the width parameter should be well within the dimensions of the wave itself.