net.djnn.addons.interpolation.CubicSplineInterpolation.java Source code

Java tutorial

Introduction

Here is the source code for net.djnn.addons.interpolation.CubicSplineInterpolation.java

Source

/**
 *   djnn java addons
 *
 *   The copyright holders for the contents of this file are:
 *      Ecole Nationale de l'Aviation Civile, France (2012-2014)
 *   See file "license.terms" for the rights and conditions
 *   defined by copyright holders.
 *
 *   Contributors:
 *      Mathieu Magnaudet <mathieu.magnaudet@enac.fr>
 *
 */

package net.djnn.addons.interpolation;

import net.djnn.java.core.Binding;
import net.djnn.java.core.Component;
import net.djnn.java.core.DoubleProperty;
import net.djnn.java.core.Element;
import net.djnn.java.core.NativeAction;

import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;

/**
 * When connected to a {@link ClockControl} or another [0,1] values generator,
 * this component makes evolve a property along a curve specified by a list of
 * couples {t,x}
 * 
 */
public class CubicSplineInterpolation extends Component {
    private DoubleProperty mu;
    private DoubleProperty property;
    private PolynomialSplineFunction psf;

    public CubicSplineInterpolation(Element parent, String name, Element toMove, double[] t, double[] x) {
        super(parent, name);
        mu = new DoubleProperty(this, "mu", 0);
        SplineInterpolator sp = new SplineInterpolator();
        psf = sp.interpolate(x, t);
        this.property = new DoubleProperty(toMove);
        new Binding(this, null, mu, new NativeAction(this, null, null, 1) {
            public void callback(Element e) {
                double muVal = mu.getValue();
                if (muVal > 1)
                    muVal = 1;
                if (muVal < 0)
                    muVal = 0;
                property.setValue(psf.value(muVal));
            }
        });
    }
}