Incorrect interpolation on non regular grid

I am trying to interpolate a point in 3-dimensional array with arbitrary spacing (distance between each point in real world coordinates). I had used scaled interpolation from Interpolations.jl . However as code below shows interpolated value is lower than any value from the surrounding voxels, and as far as I understand it should be basically weighted mean of them - so i suppose it is an error ?
Secondly given a point 5.5;5.5;5.5 spacing should in principle not affect the value of interpolation as all points on a grid surrounding point I query are in the same distance hence should have the same weights, and a weighted mean should be independent of spacing.

In the code I overwrite scale function as it gives some strange errors on bound checking otherwise (I am sampling far from boundaries - so should not be an issue here).

using Interpolations
function scale(itp::AbstractInterpolation{T,N,IT}, ranges::Vararg{AbstractRange,N}) where {T,N,IT}
    # overwriting this function becouse check_ranges giving error
    # check_ranges(itpflag(itp), axes(itp), ranges)
    ScaledInterpolation{T,N,typeof(itp),IT,typeof(ranges)}(itp, ranges)
end


function interpolate_my(point,input_array,input_array_spacing)

    old_size=size(input_array)
    itp = interpolate(input_array, BSpline(Linear()))
    #we indicate on each axis the spacing from area we are sampling
    A_x1 = 1:input_array_spacing[1]:(old_size[1])
    A_x2 = 1:input_array_spacing[2]:(old_size[2])
    A_x3 = 1:input_array_spacing[3]:(old_size[3])
    
    itp=extrapolate(itp, 0.0)   
    itp = scale(itp, A_x1, A_x2,A_x3)
    return itp(point[1],point[2],point[3])
end#interpolate_my


input_array = reshape(collect(1:1000), (10, 10, 10))
point=[5.5,5.5,5.5]
input_array_spacing=[1.1,1.2,1.5]

input_array[5,5,5]#445
input_array[6,5,5]#446
input_array[5,6,5]#455
input_array[5,5,6]#545
input_array[5,6,6]#555
input_array[6,6,5]#456
input_array[6,5,6]#546
input_array[6,6,6]#556

interpolate_my(point,input_array,input_array_spacing)#324.59


Is there an error in code, library or my reasoning?

I am not following the details closely, but Iā€™d seriously consider other packages if your applications require non-regular grids or general unstructured meshes. The Array representation that Interpolations.jl relies on assumes a certain topology that is not adequate for when vertices are free to move around.

2 Likes