Skip to content

Error with smoothing spline for 3D points #106

@Kevin-Mattheus-Moerman

Description

@Kevin-Mattheus-Moerman

@jipolanco I tried testing the new smoothing splines using the below. I essentially create the points for a noisy circle and tried smoothing it. The below code works if V is instead a scalar quantity, but if I let V be a 3D point I get the error. Did you perhaps only implement the smoothing for 1D interpolation? Any help would be appreciated. Thanks.

using Comodo
using Comodo.GeometryBasics
using Comodo.GLMakie
using Comodo.BSplineKit

n = 25
t = collect(range(0.0,(2*pi)-(2*pi)/n,n))
r = 1.0 .+ 0.5.*rand(n)

V = Vector{Point{3,Float64}}(undef,n)
for (i,tt) in enumerate(t)
    V[i] = Point{3,Float64}(r[i]*cos(tt),r[i]*sin(tt),0.0)
end
 
λ = 0.1  # smoothing parameter
# S_fit_natural = BSplineKit.fit(t, V, λ)  # for comparison, compute a natural spline (no implied periodicity)
S_fit_periodic = BSplineKit.fit(t, V, λ, BSplineKit.Periodic(2*pi))

m = 200
t_fit = range(0.0,(2*pi)-(2*pi)/m,m)
V_fit = S_fit_periodic.(t_fit)

# Visualization
fig = Figure(size = (800,800))
ax = Axis3(fig[1, 1],aspect = :data)

hp1 = scatter!(ax, V,markersize=15,color=:red)
hp2 = lines!(ax,V_fit,linewidth=5,color=:black)

Legend(fig[1, 2],[hp1,hp2],["Raw","Smoothing spline"])

fig
ERROR: MethodError: no method matching +(::Float64, ::Point{3, Float64})
For element-wise addition, use broadcasting with dot syntax: scalar .+ array
The function `+` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...)
   @ Base operators.jl:596
  +(::ChainRulesCore.ZeroTangent, ::Any)
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/U6wNx/src/tangent_arithmetic.jl:99
  +(::Any, ::ChainRulesCore.NotImplemented)
   @ ChainRulesCore ~/.julia/packages/ChainRulesCore/U6wNx/src/tangent_arithmetic.jl:25
  ...

Stacktrace:
  [1] _At_or_Ac_mul_B!(tfun::typeof(transpose), C::Vector{…}, A::SparseArrays.SparseMatrixCSC{…}, B::Vector{…}, α::Bool, β::Bool)
    @ SparseArrays ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/SparseArrays/src/linalg.jl:111
  [2] spdensemul!
    @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/SparseArrays/src/linalg.jl:61 [inlined]
  [3] generic_matvecmul!
    @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/SparseArrays/src/linalg.jl:54 [inlined]
  [4] _mul!
    @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/matmul.jl:73 [inlined]
  [5] mul!
    @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/matmul.jl:70 [inlined]
  [6] mul!
    @ ~/.julia/juliaup/julia-1.11.3+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/matmul.jl:253 [inlined]
  [7] fit(xs::Vector{…}, ys::Vector{…}, λ::Float64, order_in::BSplineOrder{…}, bc::Periodic{…}; weights::Nothing)
    @ BSplineKit.SplineInterpolations ~/.julia/packages/BSplineKit/Sxz1p/src/SplineInterpolations/smoothing.jl:178
  [8] fit
    @ ~/.julia/packages/BSplineKit/Sxz1p/src/SplineInterpolations/smoothing.jl:116 [inlined]
  [9] fit(x::Vector{Float64}, y::Vector{Point{3, Float64}}, λ::Float64, bc::Periodic{Float64})
    @ BSplineKit.SplineInterpolations ~/.julia/packages/BSplineKit/Sxz1p/src/SplineInterpolations/smoothing.jl:186
 [10] top-level scope
    @ ~/DATA/Julia/PROJECTS/Comodo_dev/dev/Comodo/examples/demo_smoothing_spline.jl:20
Some type information was truncated. Use `show(err)` to see complete types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions