bow-simulation / virtualbow

Software for designing and simulating bows
http://www.virtualbow.org/
GNU General Public License v3.0
23 stars 3 forks source link

Improve monotonic cubic spline interpolation #146

Closed stfnp closed 2 years ago

stfnp commented 5 years ago

Standard cubic splines minimize an integral over the squared curvature to obtain the "smoothest" possible curve through the supplied control points. Things get more complicated when the spline must also preserve the monotonicity of the input data, like the width and height interpolation in VirtualBow. The current implementation (based on the example code in https://en.wikipedia.org/wiki/Monotone_cubic_interpolation) is a compromise that preserves the monotonicity of the input data, while not necessarily leading to the smoothest curve to do so.

There are other methods based on numerical optimization [1] that find the actual minimum curvature (i.e. produce better/smoother results) while still preserving monotonicity. The increased computational cost wouldn't be that important, because those things are only calculated once prior to running the simulation.

[1] Wolfberg, Alfy: Monotonic cubic spline interpolation

stfnp commented 3 years ago

Current state

A first implementation (introduced in 2f72eefefb0244be7763b419b1cb4c3afe1a6eb1 but later reverted in 8edc9d043d32d81b0b37b59a7838097924e381f1 before the 0.8 release) showed that optimization-based splines can work, but the performance of the optimization algorithm (LN_COBYLA from nlopt, a derivative-free algorithm) was not always satisfactory with regards to the produced solutions.

Future plan

The new profile curve (#53, #113) will be based on numerical optimization as well (using LD_SLSQP, an algorithm that utilizes derivatives of the objective and constraint functions). Once the new profile curve is released, see how well this holds up in practice and revisit this issue later. If the results are positive, improve the optimization-based splines with derivatives, use LD_SLSQP and decide again.

stfnp commented 2 years ago

Commit 7ac79b1d45b798a9114138c122e3a17abc9e3e05 removed the custom spline implementations in favor of tk::spline. For monotone interpolation it uses a similar but different method than the Wikipedia example. It first constructs a "normal" cubic spline with minimal curvature and then adjusts the slopes on segments that aren't monotone.

At first glance the results look much better than before, without the need for any numerical optimization or other complicated algorithms. It is still not "exact" like the methods proposed in the paper above, but I think it is good enough now.

Closing this issue as soon as that branch is merged.