From 2fe49b3c40d08dccf2dc7d3df0dce8809c85b97e Mon Sep 17 00:00:00 2001 From: Sathvik Bhagavan Date: Sun, 7 Jul 2024 07:01:52 +0000 Subject: [PATCH 1/2] refactor: error in BSpline when number of data/control is less than or equal to degree --- src/interpolation_caches.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/interpolation_caches.jl b/src/interpolation_caches.jl index 83e04fe5..c7274471 100644 --- a/src/interpolation_caches.jl +++ b/src/interpolation_caches.jl @@ -481,6 +481,7 @@ function BSplineInterpolation( u, t, d, pVecType, knotVecType; extrapolate = false, safetycopy = true) u, t = munge_data(u, t, safetycopy) n = length(t) + n < d + 1 && error("BSplineInterpolation needs at least d + 1, i.e. $(d+1) points.") s = zero(eltype(u)) p = zero(t) k = zeros(eltype(t), n + d + 1) @@ -615,6 +616,7 @@ function BSplineApprox( u, t, d, h, pVecType, knotVecType; extrapolate = false, safetycopy = true) u, t = munge_data(u, t, safetycopy) n = length(t) + h < d + 1 && error("BSplineApprox needs at least d + 1, i.e. $(d+1) control points.") s = zero(eltype(u)) p = zero(t) k = zeros(eltype(t), h + d + 1) From 19b1db9855298e7a4e69f275fa9a9684039d99e5 Mon Sep 17 00:00:00 2001 From: Sathvik Bhagavan Date: Sun, 7 Jul 2024 07:05:01 +0000 Subject: [PATCH 2/2] test: add tests for checking error with bspline wrt to degree and data/control points --- test/interpolation_tests.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/interpolation_tests.jl b/test/interpolation_tests.jl index 779c5bd8..9549038c 100644 --- a/test/interpolation_tests.jl +++ b/test/interpolation_tests.jl @@ -615,6 +615,12 @@ end @test [A(190.0), A(225.0)] == [13.437481084762863, 11.367034741256463] @test [A(t[1]), A(t[end])] == [u[1], u[end]] + @test_throws ErrorException("BSplineInterpolation needs at least d + 1, i.e. 4 points.") BSplineInterpolation( + u[1:3], t[1:3], 3, :Uniform, :Uniform) + @test_throws ErrorException("BSplineInterpolation needs at least d + 1, i.e. 5 points.") BSplineInterpolation( + u[1:4], t[1:4], 4, :ArcLen, :Average) + @test_nowarn BSplineInterpolation(u[1:3], t[1:3], 2, :Uniform, :Uniform) + # Test extrapolation A = BSplineInterpolation(u, t, 2, :ArcLen, :Average; extrapolate = true) @test A(-1.0) == u[1] @@ -633,6 +639,12 @@ end @test [A(t[1]), A(t[end])] ≈ [u[1], u[end]] test_cached_index(A) + @test_throws ErrorException("BSplineApprox needs at least d + 1, i.e. 3 control points.") BSplineApprox( + u, t, 2, 2, :Uniform, :Uniform) + @test_throws ErrorException("BSplineApprox needs at least d + 1, i.e. 4 control points.") BSplineApprox( + u, t, 3, 3, :ArcLen, :Average) + @test_nowarn BSplineApprox(u, t, 2, 3, :Uniform, :Uniform) + # Test extrapolation A = BSplineApprox(u, t, 2, 4, :Uniform, :Uniform; extrapolate = true) @test A(-1.0) == u[1]