1 /* 2 * Copyright (C) 2009 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 com.cooliris.media; 18 19 /** 20 * A static class for some useful operations on Floats and Vectors 21 */ 22 23 public class FloatUtils { 24 private static final float ANIMATION_SPEED = 4.0f; 25 26 /** 27 * This function animates a float value to another float value 28 * 29 * @param prevVal 30 * : The previous value (or the animated value) 31 * @param targetVal 32 * : The target value 33 * @param timeElapsed 34 * Time elapsed since the last time this function was called 35 * @return The new animated value that is closer to the target value and 36 * clamped to the target value 37 */ 38 public static final float animate(float prevVal, float targetVal, float timeElapsed) { 39 timeElapsed = timeElapsed * ANIMATION_SPEED; 40 return animateAfterFactoringSpeed(prevVal, targetVal, timeElapsed); 41 } 42 43 public static final float animateWithMaxSpeed(float prevVal, float targetVal, float timeElapsed, float maxSpeed) { 44 float newTargetVal = targetVal; 45 float delta = newTargetVal - prevVal; 46 if (Math.abs(delta) > maxSpeed) { 47 newTargetVal = prevVal + (Math.signum(delta) * maxSpeed); 48 } 49 timeElapsed = timeElapsed * ANIMATION_SPEED; 50 return animateAfterFactoringSpeed(prevVal, newTargetVal, timeElapsed); 51 } 52 53 /** 54 * This function animates a Tuple3f value to another Tuple3f value 55 * 56 * @param animVal 57 * : The animating Tuple 58 * @param targetVal 59 * : The target value for the Tuple 60 * @param timeElapsed 61 * : Time elapsed since the last time this function was called 62 */ 63 public static final void animate(Vector3f animVal, Vector3f targetVal, float timeElapsed) { 64 timeElapsed = timeElapsed * ANIMATION_SPEED; 65 animVal.x = animateAfterFactoringSpeed(animVal.x, targetVal.x, timeElapsed); 66 animVal.y = animateAfterFactoringSpeed(animVal.y, targetVal.y, timeElapsed); 67 animVal.z = animateAfterFactoringSpeed(animVal.z, targetVal.z, timeElapsed); 68 } 69 70 /** 71 * Clamp a float to a lower bound 72 * 73 * @param val 74 * : the input float value 75 * @param minVal 76 * : the minimum value to use to clamp 77 * @return the clamped value 78 */ 79 public static final float clampMin(float val, float minVal) { 80 if (val < minVal) 81 return minVal; // CR: braces 82 else 83 return val; 84 } 85 86 /** 87 * Clamp a float to an upper bound 88 * 89 * @param val 90 * : the input float value 91 * @param maxVal 92 * : the maximum value to use to clamp 93 * @return the clamped value 94 */ 95 public static final float clampMax(float val, float maxVal) { 96 if (val > maxVal) 97 return maxVal; 98 else 99 return val; 100 } 101 102 // CR: these comments are barely useful. they mostly just fill space. If 103 // anything, a one-liner would be sufficient. 104 /** 105 * Clamp a float to a lower and upper bound 106 * 107 * @param val 108 * : the input float value 109 * @param minVal 110 * : the minimum value to use to clamp 111 * @param maxVal 112 * : the maximum value to use to clamp 113 * @return the clamped value 114 */ 115 public static final float clamp(float val, float minVal, float maxVal) { 116 if (val < minVal) 117 return minVal; 118 else if (val > maxVal) 119 return maxVal; 120 else 121 return val; 122 } 123 124 /** 125 * Clamp an integer to a lower and upper bound 126 * 127 * @param val 128 * : the input float value 129 * @param minVal 130 * : the minimum value to use to clamp 131 * @param maxVal 132 * : the maximum value to use to clamp 133 * @return the clamped value 134 */ 135 public static final int clamp(int val, int minVal, int maxVal) { 136 if (val < minVal) 137 return minVal; 138 else if (val > maxVal) 139 return maxVal; 140 else 141 return val; 142 } 143 144 /** 145 * Function to check whether a point lies inside a rectangle 146 * 147 * @param left 148 * : the x coordinate of the left most point 149 * @param right 150 * : the x coordinate of the right most point 151 * @param top 152 * : the y coordinate of the top most point 153 * @param bottom 154 * : the y coordinate of the bottom most point 155 * @param posX 156 * : the input point's x coordinate 157 * @param posY 158 * : the input point's y coordinate 159 * @return true if point is inside the rectangle else return false 160 */ 161 public static final boolean boundsContainsPoint(float left, float right, float top, float bottom, float posX, float posY) { 162 // CR: return ... (one statement). 163 if (posX < left || posX > right || posY < top || posY > bottom) 164 return false; 165 else 166 return true; 167 } 168 169 private static final float animateAfterFactoringSpeed(float prevVal, float targetVal, float timeElapsed) { 170 if (prevVal == targetVal) 171 return targetVal; 172 float newVal = prevVal + ((targetVal - prevVal) * timeElapsed); 173 if (Math.abs(newVal - prevVal) < 0.0001f) 174 return targetVal; 175 if (newVal == prevVal) { 176 return targetVal; 177 } else { // } else if (...) { ... }; no need for a new level of 178 // indentation. 179 if (prevVal > targetVal && newVal < targetVal) { 180 return targetVal; 181 } else if (prevVal < targetVal && newVal > targetVal) { 182 return targetVal; 183 } else { 184 return newVal; 185 } 186 } 187 } 188 189 public static final float max(float scaleX, float scaleY) { 190 if (scaleX > scaleY) 191 return scaleX; 192 else 193 return scaleY; 194 } 195 } 196