1 /*
2 * jcurl java curling software framework https://JCurl.mro.name Copyright (C)
3 * 2005-2009 M. Rohrmoser
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package org.jcurl.math;
21
22 /**
23 * Provides a number of built-in implementations of the {@link Interpolator}
24 * interface.
25 *
26 * @author <a href="mailto:JCurl@mro.name">M. Rohrmoser </a>
27 * @version $Id$
28 */
29 public class Interpolators {
30
31 protected static class LinearInterpolator implements Interpolator {
32 public float interpolate(final float fraction) {
33 if (Float.isNaN(fraction) || fraction < 0.0F || fraction > 1.0F)
34 return Float.NaN;
35 return fraction;
36 }
37 }
38
39 private static final Interpolator exponential = new LinearInterpolator() {
40 @Override
41 public float interpolate(final float fraction) {
42 if (Float.isNaN(super.interpolate(fraction)))
43 return Float.NaN;
44 if (0.0F == fraction || 1.0F == fraction)
45 return fraction;
46 return 1.0F - (float) (Math.exp(1.0 - fraction) / Math.exp(1.0));
47 }
48 };
49 private static final Interpolator linear = new LinearInterpolator();
50
51 private static final Interpolator quadratic = new LinearInterpolator() {
52 @Override
53 public float interpolate(final float fraction) {
54 if (Float.isNaN(super.interpolate(fraction)))
55 return Float.NaN;
56 if (0.0F == fraction || 1.0F == fraction)
57 return fraction;
58 float t = fraction - 1.0F;
59 return 1.0F - t * t;
60 }
61 };
62
63 /** Getting exponentially denser towards <code>1.0F</code>. */
64 private static Interpolator getExponentialInstance() {
65 return exponential;
66 }
67
68 /**
69 * Returns a trivial implementation of Interpolator that provides linear
70 * time interpolation (the input "t" value is simply returned unmodified).
71 *
72 * @return an instance of Interpolator that provides simple linear time
73 * interpolation
74 */
75 public static Interpolator getLinearInstance() {
76 return linear;
77 }
78
79 /** Getting quadratically denser towards <code>1.0F</code>. */
80 public static Interpolator getQuadraticInstance() {
81 return quadratic;
82 }
83 }