1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.constraintlayout.core.motion.utils;
18 
19 /**
20  * Base class for curve fitting / interpolation
21  * Curve fits must be capable of being differentiable and extend beyond the points (extrapolate)
22  *
23  *
24  */
25 
26 public abstract class CurveFit {
27     public static final int SPLINE = 0;
28     public static final int LINEAR = 1;
29     public static final int CONSTANT = 2;
30 
31     // @TODO: add description
get(int type, double[] time, double[][] y)32     public static CurveFit get(int type, double[] time, double[][] y) {
33         if (time.length == 1) {
34             type = CONSTANT;
35         }
36         switch (type) {
37             case SPLINE:
38                 return new MonotonicCurveFit(time, y);
39             case CONSTANT:
40                 return new Constant(time[0], y[0]);
41             default:
42                 return new LinearCurveFit(time, y);
43         }
44     }
45 
46     // @TODO: add description
getArc(int[] arcModes, double[] time, double[][] y)47     public static CurveFit getArc(int[] arcModes, double[] time, double[][] y) {
48         return new ArcCurveFit(arcModes, time, y);
49     }
50 
51     // @TODO: add description
getPos(double t, double[] v)52     public abstract void getPos(double t, double[] v);
53 
54     // @TODO: add description
getPos(double t, float[] v)55     public abstract void getPos(double t, float[] v);
56 
57     // @TODO: add description
getPos(double t, int j)58     public abstract double getPos(double t, int j);
59 
60     // @TODO: add description
getSlope(double t, double[] v)61     public abstract void getSlope(double t, double[] v);
62 
63     // @TODO: add description
getSlope(double t, int j)64     public abstract double getSlope(double t, int j);
65 
66     // @TODO: add description
getTimePoints()67     public abstract double[] getTimePoints();
68 
69     static class Constant extends CurveFit {
70         double mTime;
71         double[] mValue;
72 
Constant(double time, double[] value)73         Constant(double time, double[] value) {
74             mTime = time;
75             mValue = value;
76         }
77 
78         @Override
getPos(double t, double[] v)79         public void getPos(double t, double[] v) {
80             System.arraycopy(mValue, 0, v, 0, mValue.length);
81         }
82 
83         @Override
getPos(double t, float[] v)84         public void getPos(double t, float[] v) {
85             for (int i = 0; i < mValue.length; i++) {
86                 v[i] = (float) mValue[i];
87             }
88         }
89 
90         @Override
getPos(double t, int j)91         public double getPos(double t, int j) {
92             return mValue[j];
93         }
94 
95         @Override
getSlope(double t, double[] v)96         public void getSlope(double t, double[] v) {
97             for (int i = 0; i < mValue.length; i++) {
98                 v[i] = 0;
99             }
100         }
101 
102         @Override
getSlope(double t, int j)103         public double getSlope(double t, int j) {
104             return 0;
105         }
106 
107         @Override
getTimePoints()108         public double[] getTimePoints() {
109             return new double[]{mTime};
110         }
111     }
112 }
113