1987ee64612e2510004fdf08536746c87234d01c1Paul Rohde/* 2987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * Copyright (C) 2014 The Android Open Source Project 3987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * 4987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * Licensed under the Apache License, Version 2.0 (the "License"); 5987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * you may not use this file except in compliance with the License. 6987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * You may obtain a copy of the License at 7987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * 8987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * http://www.apache.org/licenses/LICENSE-2.0 9987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * 10987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * Unless required by applicable law or agreed to in writing, software 11987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * distributed under the License is distributed on an "AS IS" BASIS, 12987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * See the License for the specific language governing permissions and 14987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * limitations under the License. 15987ee64612e2510004fdf08536746c87234d01c1Paul Rohde */ 16987ee64612e2510004fdf08536746c87234d01c1Paul Rohde 17987ee64612e2510004fdf08536746c87234d01c1Paul Rohdepackage com.android.camera.ui.motion; 18987ee64612e2510004fdf08536746c87234d01c1Paul Rohde 19987ee64612e2510004fdf08536746c87234d01c1Paul Rohde/** 20987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * Various static helper functions for interpolating between values. 21987ee64612e2510004fdf08536746c87234d01c1Paul Rohde */ 22987ee64612e2510004fdf08536746c87234d01c1Paul Rohdepublic class InterpolateUtils { 23987ee64612e2510004fdf08536746c87234d01c1Paul Rohde 24987ee64612e2510004fdf08536746c87234d01c1Paul Rohde private InterpolateUtils() { 25987ee64612e2510004fdf08536746c87234d01c1Paul Rohde } 26987ee64612e2510004fdf08536746c87234d01c1Paul Rohde 27987ee64612e2510004fdf08536746c87234d01c1Paul Rohde /** 28987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * Linear interpolation from v0 to v1 as t goes from 0...1 29987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * 30987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @param v0 the value at t=0 31987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @param v1 the value at t=1 32987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @param t value in the range of 0 to 1. 33987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @return the value between v0 and v1 as a ratio between 0 and 1 defined by t. 34987ee64612e2510004fdf08536746c87234d01c1Paul Rohde */ 35987ee64612e2510004fdf08536746c87234d01c1Paul Rohde public static float lerp(float v0, float v1, float t) { 36987ee64612e2510004fdf08536746c87234d01c1Paul Rohde return v0 + t * (v1 - v0); 37987ee64612e2510004fdf08536746c87234d01c1Paul Rohde } 38987ee64612e2510004fdf08536746c87234d01c1Paul Rohde 39987ee64612e2510004fdf08536746c87234d01c1Paul Rohde /** 40987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * Project a value that is within the in(Min/Max) number space into the to(Min/Max) number 41987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * space. 42987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * 43987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @param v value to scale into the 'to' number space. 44987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @param vMin min value of the values number space. 45987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @param vMax max value of the values number space. 46987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @param pMin min value of the projection number space. 47987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @param pMax max value of the projection number space. 48987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * @return the ratio of the value in the source number space as a value in the to(Min/Max) 49987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * number space. 50987ee64612e2510004fdf08536746c87234d01c1Paul Rohde */ 51987ee64612e2510004fdf08536746c87234d01c1Paul Rohde public static float scale(float v, float vMin, float vMax, float pMin, float pMax) { 52987ee64612e2510004fdf08536746c87234d01c1Paul Rohde return (pMax - pMin) * (v - vMin) / (vMax - vMin) + pMin; 53987ee64612e2510004fdf08536746c87234d01c1Paul Rohde } 54987ee64612e2510004fdf08536746c87234d01c1Paul Rohde 55987ee64612e2510004fdf08536746c87234d01c1Paul Rohde /** 56987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * Value between 0 and 1 as a ratio between tBegin over tDuration 57987ee64612e2510004fdf08536746c87234d01c1Paul Rohde * with no upper bound. 58987ee64612e2510004fdf08536746c87234d01c1Paul Rohde */ 59987ee64612e2510004fdf08536746c87234d01c1Paul Rohde public static float unitRatio(long t, long tBegin, float tDuration) { 60987ee64612e2510004fdf08536746c87234d01c1Paul Rohde if (t <= tBegin) { 61987ee64612e2510004fdf08536746c87234d01c1Paul Rohde return 0.0f; 62987ee64612e2510004fdf08536746c87234d01c1Paul Rohde } 63987ee64612e2510004fdf08536746c87234d01c1Paul Rohde 64987ee64612e2510004fdf08536746c87234d01c1Paul Rohde return (t - tBegin) / tDuration; 65987ee64612e2510004fdf08536746c87234d01c1Paul Rohde } 66987ee64612e2510004fdf08536746c87234d01c1Paul Rohde} 67