Skip to content

Commit ba4b34d

Browse files
timholysglyon
authored andcommitted
Support CartesianIndex indexing when mixed with Real (#184)
Fixes #183
1 parent 3d433df commit ba4b34d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Interpolations.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ eltype(::Type{ITP}) where {ITP<:AbstractInterpolation} = eltype(supertype(ITP))
9494
eltype(itp::AbstractInterpolation) = eltype(typeof(itp))
9595
count_interp_dims(::Type{T}, N) where {T<:AbstractInterpolation} = N
9696

97+
# Generic indexing methods (fixes #183)
98+
Base.to_index(::AbstractInterpolation, i::Real) = i
99+
100+
@inline function Base._getindex(::IndexCartesian, A::AbstractInterpolation{T,N}, I::Vararg{Int,N}) where {T,N} # ambiguity resolution
101+
@inbounds r = getindex(A, I...)
102+
r
103+
end
104+
@inline function Base._getindex(::IndexCartesian, A::AbstractInterpolation{T,N}, I::Vararg{Real,N}) where {T,N}
105+
@inbounds r = getindex(A, I...)
106+
r
107+
end
108+
97109
include("nointerp/nointerp.jl")
98110
include("b-splines/b-splines.jl")
99111
include("gridded/gridded.jl")

test/b-splines/linear.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ for (constructor, copier) in ((interpolate, identity), (interpolate!, copy))
4141
end
4242
end
4343

44+
# Issue #183
45+
x = rand(3,3,3)
46+
itp = interpolate(x, BSpline(Linear()), OnGrid())
47+
@test itp[1.5, CartesianIndex((2, 3))] === itp[1.5, 2, 3]
48+
@test itp[CartesianIndex((1, 2)), 1.5] == itp[1, 2, 1.5]
49+
4450
end

0 commit comments

Comments
 (0)