ejmahler / SplineLibrary

A library to collect many useful spline functions into one place.
BSD 2-Clause "Simplified" License
269 stars 50 forks source link

Wrong arcLength result #5

Closed ban-dana closed 7 years ago

ban-dana commented 7 years ago

I'm looking on data like this:

Vector<3, double> points: [0] (0.00000000000000000,0.00000000000000000,0.00000000000000000)
[1] (1.0000000000000000,0.00000000000000000,0.00000000000000000)
[2] (4.0000000000000000,0.00000000000000000,0.00000000000000000)
[3] (9.0000000000000000,0.00000000000000000,0.00000000000000000)
[4] (16.000000000000000,0.00000000000000000,0.00000000000000000)
[5] (25.000000000000000,0.00000000000000000,0.00000000000000000)

NaturalSpline<Vector<3,double>, double> spline(points, true, 0.5); double t = 0.38215637207031250, t0 = 0.47108459472656250;

Then I get this:

    spline.arcLength(t0, t0 +t)  == 0.50000331420977806

but: spline.getPosition(t0) == (1.0000076740967552,0.00000000000000000,0.00000000000000000)

and spline.getPosition(t0 + t) == (2.2312049143150867,0.00000000000000000,0.00000000000000000)

Since all the points are on the same straight line (and looks like there's no wiggles between t0 and t0 + t), I'd expect arcLength to be 1.2312....

ejmahler commented 7 years ago

Hey! So this is caused by the same problem as #4 where several spline types weren't computing the arc length correctly when alpha was nonzero. This particular problem comes up when a and b are inside the same segment and alpha is nonzero.

I designed the unit tests I wrote for #4 to cover this exact special case for each spline type, But there was a flaw in my thinking, so it was setting up scenarios where a and b weren't actually in the same segment, and so the test passed without ever identifying this problem.

So I fixed this problem and modified the test to make sure it's actually testing this special case.

ejmahler commented 7 years ago

Out of curiosity, what use case are you using the library for?

ban-dana commented 7 years ago

Thanks for quick response, I appreciate that! I'm trying to build a simulation for some medical research, and the spline is modelling an elastic probe defined by several points that are entered manually. In fact I need the equidistant split of the spline, so I wrote a (dumb) solver for the spline.arcLength(t) == L equation. BTW, I did some googling and there's quite some demand for this feature, may be you could add it to your lib? I'd be happy to do it myself, if only I was good enough at math... :)

ejmahler commented 7 years ago

Create a new issue for it and I'll look into it. What would the API look like?