01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.motion;
21 
22 import js.ArrayLike;
23 import js.Vars;
24 import jsx.core.ArrayLikes;
25 
26 /**
27  * <p>Defines motions by approximating sample data with a cubic spline interpolation.</p>
28  * 
29  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
30  */
31 public final class Spline extends Interpolator
32 {
33     private final ArrayLike<Double> d = new Vars<Double>().var();
34 
35     /**
36      * <p>The typical constructor to construct a motion of this type.</p>
37      * @param values An array of sample data to be held by this object.
38      * @since 1.0
39      */
40     public Spline(ArrayLike<Number> values) {
41         super(values);
42         int n = ArrayLikes.length(values) - 1;
43         for (int i = 0; i < n; i++) {
44             d.set(i, values.get(i + 1).doubleValue() - values.get(i).doubleValue());
45         }
46     }
47 
48     /**
49      * <p>Gets the datum of a float number for the specified step.</p>
50      * @param i The step number. It must be within the number of steps previously initialized 
51      * with the method {@link Motion#init(int)}.
52      * @return The float datum for the <tt>i</tt>th step.
53      * @since 1.0
54      */
55     @Override
56     public final double get(int i) {
57         int n = ArrayLikes.length(values) - 1;
58         double x = ((double)n * i) / steps(this);
59         int j = x >= n ? n - 1 : x < 0 ? 0 : (int)x;
60         x = x - j;
61         double d0 = d.get(j);
62         double d1 = d0;
63         if (j > 0) {
64             d1 = (d1 + d.get(j - 1)) / 2;
65         }
66         double d2 = d0;
67         if (j < n - 1) {
68             d2 = (d2 + d.get(j + 1)) / 2;
69         }
70         d2 = d2 + d1;
71         double y = (d2 - 2 * d0) * x;
72         y = (y + 3 * d0 - d1 - d2) * x;
73         return (y + d1) * x + values.get(j).doubleValue();
74     }
75 }