• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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.replica.replicaisland;
18 
19 /** A collection of miscellaneous utility functions. */
20 public class Utils {
21     private static final float EPSILON = 0.0001f;
22 
close(float a, float b)23     public final static boolean close(float a, float b) {
24         return close(a, b, EPSILON);
25     }
26 
close(float a, float b, float epsilon)27     public final static boolean close(float a, float b, float epsilon) {
28         return Math.abs(a - b) < epsilon;
29     }
30 
sign(float a)31     public final static int sign(float a) {
32         if (a >= 0.0f) {
33             return 1;
34         } else {
35             return -1;
36         }
37     }
38 
clamp(int value, int min, int max)39     public final static int clamp(int value, int min, int max) {
40         int result = value;
41         if (min == max) {
42             if (value != min) {
43                 result = min;
44             }
45         } else if (min < max) {
46             if (value < min) {
47                 result = min;
48             } else if (value > max) {
49                 result = max;
50             }
51         } else {
52             result = clamp(value, max, min);
53         }
54 
55         return result;
56     }
57 
58 
byteArrayToInt(byte[] b)59     public final static int byteArrayToInt(byte[] b) {
60         if (b.length != 4) {
61             return 0;
62         }
63 
64         // Same as DataInputStream's 'readInt' method
65         /*int i = (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8)
66                 | (b[3] & 0xff));*/
67 
68         // little endian
69         int i = (((b[3] & 0xff) << 24) | ((b[2] & 0xff) << 16) | ((b[1] & 0xff) << 8)
70                 | (b[0] & 0xff));
71 
72         return i;
73     }
74 
byteArrayToFloat(byte[] b)75     public final static float byteArrayToFloat(byte[] b) {
76 
77         // intBitsToFloat() converts bits as follows:
78         /*
79         int s = ((i >> 31) == 0) ? 1 : -1;
80         int e = ((i >> 23) & 0xff);
81         int m = (e == 0) ? (i & 0x7fffff) << 1 : (i & 0x7fffff) | 0x800000;
82         */
83 
84         return Float.intBitsToFloat(byteArrayToInt(b));
85     }
86 
framesToTime(int framesPerSecond, int frameCount)87     public final static float framesToTime(int framesPerSecond, int frameCount) {
88         return (1.0f / framesPerSecond) * frameCount;
89     }
90 
91 }
92