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 import jsx.core.Maths;
26 
27 /**
28  * <p>Defines motions by approximating sample data with a Fourier interpolation.</p>
29  * 
30  * @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>
31  */
32 public final class Fourier extends Interpolator
33 {
34     private final static double PI2 = Maths.PI * 2;
35     private final ArrayLike<Double> a = new Vars<Double>().var();
36     private final ArrayLike<Double> b = new Vars<Double>().var();
37     private int n, m;
38 
39     /**
40      * <p>The typical constructor to construct a motion of this type.</p>
41      * @param values An array of sample data to be held by this object.
42      * @since 1.0
43      */
44     public Fourier(ArrayLike<Number> values) {
45         super(values);
46         n = (ArrayLikes.length(values) - 1) / 2;
47         m = 2 * n + 1;
48         double t = PI2 / m;
49         for (int j = 0; j <= n; j++) {
50             a.set(j, (double)0);
51             b.set(j, (double)0);
52             for (int k = 0; k < m; k++) {
53                 double jtk = j * t * k;
54                 double vk = values.get(k).doubleValue();
55                 a.set(j, a.get(j) + vk * Maths.cos(jtk));
56                 b.set(j, b.get(j) + vk * Maths.sin(jtk));
57             }
58             a.set(j, a.get(j) * 2 / m);
59             b.set(j, b.get(j) * 2 / m);
60         }
61     }
62 
63     /**
64      * <p>Gets the datum of a float number for the specified step.</p>
65      * @param i The step number. It must be within the number of steps previously initialized 
66      * with the method {@link #init(int)}.
67      * @return The float datum for the <tt>i</tt>th step.
68      * @since 1.0
69      */
70     @Override
71     public final double get(int i) {
72         double t = PI2 / steps(this) * i;
73         double p = a.get(0) / 2;
74         for (int j = 1; j <= n; j++) {
75             double jt = j * t;
76             p = p + a.get(j) * Maths.cos(jt)
77                   + b.get(j) * Maths.sin(jt);
78         }
79         return p;
80     }
81 
82     /**
83      * <p>Resets the motion object to provide data for a specified number of steps.</p>
84      * @param steps The total number of steps in which this motion object will provide 
85      * stepping data.
86      * @since 1.0
87      */
88     @Override
89     public final void init(int steps) {
90         super.init(steps * (m + 1) / m);
91     }
92 }