Skip to content

Add field gradient and hessian #14

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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ RegionTrees = "dee08c22-ab7f-5625-9660-a9af2021b33f"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Interpolations = "0.9, 0.10, 0.11"
RegionTrees = "0.2"
Interpolations = "0.9, 0.10, 0.11, 0.12"
RegionTrees = "0.2, 0.3"
julia = "1"

[extras]
Expand Down
14 changes: 14 additions & 0 deletions src/AdaptiveDistanceFields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ using RegionTrees
import RegionTrees: needs_refinement, refine_data
using Interpolations: interpolate!,
extrapolate,
gradient,
hessian,
AbstractInterpolation,
BSpline,
OnGrid,
Linear,
Line

using StaticArrays: SVector

export AdaptiveDistanceField,
evaluate,
fieldgradient,
fieldhessian,
ConvexMesh

include("interpolation.jl")
Expand All @@ -39,4 +44,13 @@ function (field::AdaptiveDistanceField)(point::AbstractArray)
evaluate(field.root, point)
end


function fieldgradient(field::AdaptiveDistanceField,point::AbstractArray)
fieldgradient(field.root, point)
end

function fieldhessian(field::AdaptiveDistanceField,point::AbstractArray)
fieldhessian(field.root, point)
end

end
48 changes: 46 additions & 2 deletions src/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
end

function evaluate(itp::AbstractInterpolation, point::AbstractArray)
itp[point...]
itp(point...)
end

function evaluate(cell::Cell{D}, point::AbstractArray) where {D <: AbstractInterpolation}
Expand All @@ -12,6 +12,50 @@ function evaluate(cell::Cell{D}, point::AbstractArray) where {D <: AbstractInter
end

function evaluate(interp::AbstractInterpolation, boundary::HyperRectangle, point::AbstractArray)
coords = (point - boundary.origin) ./ boundary.widths + 1
coords = (point - boundary.origin) ./ boundary.widths .+ 1
evaluate(interp, coords)
end





@generated function fieldgradient(itp::AbstractInterpolation, point::SVector{N}) where N
Expr(:call, :gradient, :itp, [:(point[$i]) for i in 1:N]...)
end

function fieldgradient(cell::Cell{D}, point::AbstractArray) where {D <: AbstractInterpolation}
leaf = findleaf(cell, point)
fieldgradient(leaf.data, leaf.boundary, point)
end

function fieldgradient(interp::AbstractInterpolation, boundary::HyperRectangle, point::AbstractArray)
coords = (point - boundary.origin) ./ boundary.widths .+ 1
fieldgradient(interp, coords) ./ boundary.widths
end

function fieldgradient(itp::AbstractInterpolation, point::AbstractArray)
gradient(itp, point...)
end





@generated function fieldhessian(itp::AbstractInterpolation, point::SVector{N}) where N
Expr(:call, :hessian, :itp, [:(point[$i]) for i in 1:N]...)
end

function fieldhessian(cell::Cell{D}, point::AbstractArray) where {D <: AbstractInterpolation}
leaf = findleaf(cell, point)
fieldhessian(leaf.data, leaf.boundary, point)
end

function fieldhessian(interp::AbstractInterpolation, boundary::HyperRectangle, point::AbstractArray)
coords = (point - boundary.origin) ./ boundary.widths .+ 1
fieldhessian(interp, coords) ./ (boundary.widths*boundary.widths')
end

function fieldhessian(itp::AbstractInterpolation, point::AbstractArray)
hessian(itp, point...)
end