|
| 1 | +# [Interpolations extension](@id extension-interpolations) |
| 2 | + |
| 3 | +This extension overloads the `interpolate` function from |
| 4 | +[`Interpolations.jl`](https://juliamath.github.io/Interpolations.jl/latest/) to provide a |
| 5 | +way to construct a global interpolant from the discrete data in a |
| 6 | +[`LevelSet`](@ref) or [`LevelSetEquation`](@ref). This can be useful in situations where you want |
| 7 | +to evaluate the approximate underlying functions at points that are not on the grid. |
| 8 | + |
| 9 | +Here is an example of how to construct such an interpolant: |
| 10 | + |
| 11 | +```@example interpolations |
| 12 | +using LevelSetMethods, Interpolations |
| 13 | +LevelSetMethods.set_makie_theme!() |
| 14 | +a, b = (-2, -2), (2, 2) |
| 15 | +ϕ = LevelSetMethods.star(CartesianGrid(a, b, (50, 50))) |
| 16 | +itp = interpolate(ϕ, BSpline(Cubic())) # create the interpolant |
| 17 | +``` |
| 18 | + |
| 19 | +Once constructed, the interpolant can be used to evaluate the level-set function anywhere |
| 20 | +inside the grid: |
| 21 | + |
| 22 | +```@example interpolations |
| 23 | +itp(0.5, 0.5) |
| 24 | +``` |
| 25 | + |
| 26 | +This can be used e.g. to plot the level-set function using `Makie`: |
| 27 | + |
| 28 | +```@example interpolations |
| 29 | +using CairoMakie |
| 30 | +xx = yy = -2:0.01:2 |
| 31 | +contour(xx, yy, [itp(x,y) for x in xx, y in yy]; levels = [0], linewidth = 2) |
| 32 | +``` |
| 33 | + |
| 34 | +Trying to evaluate it outside the domain will throw an error: |
| 35 | + |
| 36 | +```@example interpolations |
| 37 | +try |
| 38 | + itp(3, 0.1) |
| 39 | +catch e |
| 40 | + println("Error caught") |
| 41 | +end |
| 42 | +``` |
| 43 | + |
| 44 | +Using it on three-dimensional level sets is similar: |
| 45 | + |
| 46 | +```@example interpolations |
| 47 | +using LinearAlgebra |
| 48 | +grid = CartesianGrid((-1.5, -1.5, -1.5), (1.5, 1.5, 1.5), (50, 50, 50)) |
| 49 | +P1, P2 = (-1, 0, 0), (1, 0, 0) |
| 50 | +b = 1.05 |
| 51 | +f = (x) -> norm(x .- P1)*norm(x .- P2) - b^2 |
| 52 | +ϕ = LevelSet(f, grid) |
| 53 | +itp = interpolate(ϕ) # cubic spline by default |
| 54 | +println("ϕ(0.5, 0.5, 0.5) = ", f((0.5, 0.5, 0.5))) |
| 55 | +println("itp(0.5, 0.5, 0.5) = ", itp(0.5, 0.5, 0.5)) |
| 56 | +``` |
0 commit comments