ArViewer  Android Version
Arvos - Augmented reality viewer open source
 All Classes Namespaces Files Functions Variables
FixedPointUtils.java
Go to the documentation of this file.
1 /*
2  * Taken from android-gl, Making OpenGL Programming in Android Easier
3  * URL: https://code.google.com/p/android-gl/source/browse/trunk/AndroidGL/src/edu/union/
4  *
5  * License: GNU Lesser General Public License
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 package com.mission_base.arviewer_android.viewer.utilities;
22 
23 //Much of this is adapted from the beartronics FP lib
24 public class FixedPointUtils
25 {
26  public static final int ONE = 0x10000;
27 
35  public static int toFixed(float val)
36  {
37  return (int) (val * 65536F);
38  }
39 
47  public static int[] toFixed(float[] arr)
48  {
49  int[] res = new int[arr.length];
50  toFixed(arr, res);
51  return res;
52  }
53 
62  public static void toFixed(float[] arr, int[] storage)
63  {
64  for (int i = 0; i < storage.length; i++)
65  {
66  storage[i] = toFixed(arr[i]);
67  }
68  }
69 
77  public static float toFloat(int val)
78  {
79  return ((float) val) / 65536.0f;
80  }
81 
89  public static float[] toFloat(int[] arr)
90  {
91  float[] res = new float[arr.length];
92  toFloat(arr, res);
93  return res;
94  }
95 
104  public static void toFloat(int[] arr, float[] storage)
105  {
106  for (int i = 0; i < storage.length; i++)
107  {
108  storage[i] = toFloat(arr[i]);
109  }
110  }
111 
119  public static int multiply(int x, int y)
120  {
121  long z = (long) x * (long) y;
122  return ((int) (z >> 16));
123  }
124 
132  public static int divide(int x, int y)
133  {
134  long z = (((long) x) << 32);
135  return (int) ((z / y) >> 16);
136  }
137 
144  public static int sqrt(int n)
145  {
146  int s = (n + 65536) >> 1;
147  for (int i = 0; i < 8; i++)
148  {
149  // converge six times
150  s = (s + divide(n, s)) >> 1;
151  }
152  return s;
153  }
154 }