Commit ca7105b9 authored by Csaba Kertesz's avatar Csaba Kertesz

Add some missing files for the Android build

parent c9f2566c
This diff is collapsed.
package org.kde.kstars.math;
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
/** from libgdx: https://github.com/libgdx/libgdx */
import java.util.Random;
/** Utility and fast math functions.
* <p>
* Thanks to Riven on JavaGaming.org for the basis of sin/cos/atan2/floor/ceil.
* @author Nathan Sweet */
public class MathUtils {
static public final float nanoToSec = 1 / 1000000000f;
// ---
static public final float PI = 3.1415927f;
static private final int SIN_BITS = 13; // Adjust for accuracy.
static private final int SIN_MASK = ~(-1 << SIN_BITS);
static private final int SIN_COUNT = SIN_MASK + 1;
static private final float radFull = PI * 2;
static private final float degFull = 360;
static private final float radToIndex = SIN_COUNT / radFull;
static private final float degToIndex = SIN_COUNT / degFull;
static public final float radiansToDegrees = 180f / PI;
static public final float radDeg = radiansToDegrees;
static public final float degreesToRadians = PI / 180;
static public final float degRad = degreesToRadians;
static private class Sin {
static final float[] table = new float[SIN_COUNT];
static {
for (int i = 0; i < SIN_COUNT; i++)
table[i] = (float)Math.sin((i + 0.5f) / SIN_COUNT * radFull);
for (int i = 0; i < 360; i += 90)
table[(int)(i * degToIndex) & SIN_MASK] = (float)Math.sin(i * degreesToRadians);
}
}
static private class Cos {
static final float[] table = new float[SIN_COUNT];
static {
for (int i = 0; i < SIN_COUNT; i++)
table[i] = (float)Math.cos((i + 0.5f) / SIN_COUNT * radFull);
for (int i = 0; i < 360; i += 90)
table[(int)(i * degToIndex) & SIN_MASK] = (float)Math.cos(i * degreesToRadians);
}
}
/** Returns the sine in radians. */
static public final float sin (float radians) {
return Sin.table[(int)(radians * radToIndex) & SIN_MASK];
}
/** Returns the cosine in radians. */
static public final float cos (float radians) {
return Cos.table[(int)(radians * radToIndex) & SIN_MASK];
}
/** Returns the sine in radians. */
static public final float sinDeg (float degrees) {
return Sin.table[(int)(degrees * degToIndex) & SIN_MASK];
}
/** Returns the cosine in radians. */
static public final float cosDeg (float degrees) {
return Cos.table[(int)(degrees * degToIndex) & SIN_MASK];
}
// ---
static private final int ATAN2_BITS = 7; // Adjust for accuracy.
static private final int ATAN2_BITS2 = ATAN2_BITS << 1;
static private final int ATAN2_MASK = ~(-1 << ATAN2_BITS2);
static private final int ATAN2_COUNT = ATAN2_MASK + 1;
static final int ATAN2_DIM = (int)Math.sqrt(ATAN2_COUNT);
static private final float INV_ATAN2_DIM_MINUS_1 = 1.0f / (ATAN2_DIM - 1);
static private class Atan2 {
static final float[] table = new float[ATAN2_COUNT];
static {
for (int i = 0; i < ATAN2_DIM; i++) {
for (int j = 0; j < ATAN2_DIM; j++) {
float x0 = (float)i / ATAN2_DIM;
float y0 = (float)j / ATAN2_DIM;
table[j * ATAN2_DIM + i] = (float)Math.atan2(y0, x0);
}
}
}
}
/** Returns atan2 in radians from a lookup table. */
static public final float atan2 (float y, float x) {
float add, mul;
if (x < 0) {
if (y < 0) {
y = -y;
mul = 1;
} else
mul = -1;
x = -x;
add = -PI;
} else {
if (y < 0) {
y = -y;
mul = -1;
} else
mul = 1;
add = 0;
}
float invDiv = 1 / ((x < y ? y : x) * INV_ATAN2_DIM_MINUS_1);
int xi = (int)(x * invDiv);
int yi = (int)(y * invDiv);
return (Atan2.table[yi * ATAN2_DIM + xi] + add) * mul;
}
// ---
static public Random random = new Random();
/** Returns a random number between 0 (inclusive) and the specified value (inclusive). */
static public final int random (int range) {
return random.nextInt(range + 1);
}
/** Returns a random number between start (inclusive) and end (inclusive). */
static public final int random (int start, int end) {
return start + random.nextInt(end - start + 1);
}
static public final boolean randomBoolean () {
return random.nextBoolean();
}
static public final float random () {
return random.nextFloat();
}
/** Returns a random number between 0 (inclusive) and the specified value (inclusive). */
static public final float random (float range) {
return random.nextFloat() * range;
}
/** Returns a random number between start (inclusive) and end (inclusive). */
static public final float random (float start, float end) {
return start + random.nextFloat() * (end - start);
}
// ---
/** Returns the next power of two. Returns the specified value if the value is already a power of two. */
static public int nextPowerOfTwo (int value) {
if (value == 0) return 1;
value--;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
return value + 1;
}
static public boolean isPowerOfTwo (int value) {
return value != 0 && (value & value - 1) == 0;
}
// ---
static public int clamp (int value, int min, int max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
static public short clamp (short value, short min, short max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
static public float clamp (float value, float min, float max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
// ---
static private final int BIG_ENOUGH_INT = 16 * 1024;
static private final double BIG_ENOUGH_FLOOR = BIG_ENOUGH_INT;
static private final double CEIL = 0.9999999;
static private final double BIG_ENOUGH_CEIL = NumberUtils
.longBitsToDouble(NumberUtils.doubleToLongBits(BIG_ENOUGH_INT + 1) - 1);
static private final double BIG_ENOUGH_ROUND = BIG_ENOUGH_INT + 0.5f;
/** Returns the largest integer less than or equal to the specified float. This method will only properly floor floats from
* -(2^14) to (Float.MAX_VALUE - 2^14). */
static public int floor (float x) {
return (int)(x + BIG_ENOUGH_FLOOR) - BIG_ENOUGH_INT;
}
/** Returns the largest integer less than or equal to the specified float. This method will only properly floor floats that are
* positive. Note this method simply casts the float to int. */
static public int floorPositive (float x) {
return (int)x;
}
/** Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats from
* -(2^14) to (Float.MAX_VALUE - 2^14). */
static public int ceil (float x) {
return (int)(x + BIG_ENOUGH_CEIL) - BIG_ENOUGH_INT;
}
/** Returns the smallest integer greater than or equal to the specified float. This method will only properly ceil floats that
* are positive. */
static public int ceilPositive (float x) {
return (int)(x + CEIL);
}
/** Returns the closest integer to the specified float. This method will only properly round floats from -(2^14) to
* (Float.MAX_VALUE - 2^14). */
static public int round (float x) {
return (int)(x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT;
}
/** Returns the closest integer to the specified float. This method will only properly round floats that are positive. */
static public int roundPositive (float x) {
return (int)(x + 0.5f);
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
package org.kde.kstars.math;
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
/** from libgdx: https://github.com/libgdx/libgdx */
public class NumberUtils {
public static int floatToIntBits (float value) {
return Float.floatToIntBits(value);
}
public static int floatToRawIntBits (float value) {
return Float.floatToRawIntBits(value);
}
public static int floatToIntColor (float value) {
return Float.floatToRawIntBits(value);
}
/** Encodes the ABGR int color as a float. The high bits are masked to avoid using floats in the NaN range, which unfortunately
* means the full range of alpha cannot be used. See {@link Float#intBitsToFloat(int)} javadocs. */
public static float intToFloatColor (int value) {
return Float.intBitsToFloat(value & 0xfeffffff);
}
public static float intBitsToFloat (int value) {
return Float.intBitsToFloat(value);
}
public static long doubleToLongBits (double value) {
return Double.doubleToLongBits(value);
}
public static double longBitsToDouble (long value) {
return Double.longBitsToDouble(value);
}
}
\ No newline at end of file
This diff is collapsed.
package org.kde.kstars.math;
import android.util.FloatMath;
public class Util {
/**
* Converts a double to a value between 0 and 360
*
* @param x
* @return double in 0-360 range
*/
public static float floatrev(double x) {
return (float) (x - Math.floor(x / 360.0f) * 360.0f);
}
/**
* Derive distance between 3d vector a,b
*
* @param a
* @param b
* @return
*/
public static float calcDistance(Vector3 a, Vector3 b) {
return FloatMath.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z));
}
/**
* calculates the encompassing circle radius for sides a,b,c of a triangle
*
* @param a
* @param b
* @param c
* @return
*/
public static float calcRadius(float a, float b, float c) {
return (a * b * c) / FloatMath.sqrt((a + b + c) * (a - b + c) * (a + b - c) * (b + c - a));
}
/**
* Calculates the angle A given length a and circle radius r, according to
* the law of sines ([a/sin(A) = 2R], thus [A = arcsin(a/2r)])
*
* @param a
* @param r
* @return angle A in radians
*/
public static float calcAngle(float a, float r) {
return (float) Math.asin(a / (2 * r));
}
/**
* Calculates the angle A given length a and circle radius r, according to
* the law of sines ([a/sin(A) = 2R], thus [A = arcsin(a/2r)])
*
* @param a
* @param r
* @return angle A in radians
*/
public static float calcAngleClamp(float a, float r) {
return (float) Math.asin(Math.min(1, Math.max(-1, a / (2 * r))));
}
}
package org.kde.kstars.math;
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
import java.io.Serializable;
/** Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself
* @author badlogicgames@gmail.com */
public class Vector2 implements Serializable {
private static final long serialVersionUID = 913902788239530931L;
/** Static temporary vector. Use with care! Use only when sure other code will not also use this.
* @see #tmp() **/
public final static Vector2 tmp = new Vector2(), tmp2 = new Vector2(), tmp3 = new Vector2();
public final static Vector2 X = new Vector2(1, 0);
public final static Vector2 Y = new Vector2(0, 1);
public final static Vector2 Zero = new Vector2(0, 0);
/** the x-component of this vector **/
public float x;
/** the y-component of this vector **/
public float y;
/** Constructs a new vector at (0,0) */
public Vector2 () {
}
/** Constructs a vector with the given components
* @param x The x-component
* @param y The y-component */
public Vector2 (float x, float y) {
this.x = x;
this.y = y;
}
/** Constructs a vector from the given vector
* @param v The vector */
public Vector2 (Vector2 v) {
set(v);
}
/** @return a copy of this vector */
public Vector2 cpy () {
return new Vector2(this);
}
/** @return The euclidian length */
public float len () {
return (float)Math.sqrt(x * x + y * y);
}
/** @return The squared euclidian length */
public float len2 () {
return x * x + y * y;
}
/** Sets this vector from the given vector
* @param v The vector
* @return This vector for chaining */
public Vector2 set (Vector2 v) {
x = v.x;
y = v.y;
return this;
}
/** Sets the components of this vector
* @param x The x-component
* @param y The y-component
* @return This vector for chaining */
public Vector2 set (float x, float y) {
this.x = x;
this.y = y;
return this;
}
/** Substracts the given vector from this vector.
* @param v The vector
* @return This vector for chaining */
public Vector2 sub (Vector2 v) {
x -= v.x;
y -= v.y;
return this;
}
/** Normalizes this vector
* @return This vector for chaining */
public Vector2 nor () {
float len = len();
if (len != 0) {
x /= len;
y /= len;
}
return this;
}
/** Adds the given vector to this vector
* @param v The vector
* @return This vector for chaining */
public Vector2 add (Vector2 v) {
x += v.x;
y += v.y;
return this;
}
/** Adds the given components to this vector
* @param x The x-component
* @param y The y-component
* @return This vector for chaining */
public Vector2 add (float x, float y) {
this.x += x;
this.y += y;
return this;
}
/** @param v The other vector
* @return The dot product between this and the other vector */
public float dot (Vector2 v) {
return x * v.x + y * v.y;
}
/** Multiplies this vector by a scalar
* @param scalar The scalar
* @return This vector for chaining */
public Vector2 mul (float scalar) {
x *= scalar;
y *= scalar;
return this;
}
/** Multiplies this vector by a scalar
* @return This vector for chaining */
public Vector2 mul (float x, float y) {
this.x *= x;
this.y *= y;
return this;
}
public Vector2 div (float value) {
return this.mul(1/value);
}
public Vector2 div (float vx, float vy) {
return this.mul(1/vx, 1/vy);
}
public Vector2 div (Vector2 other) {
return this.mul(1/other.x, 1/other.y);
}
/** @param v The other vector
* @return the distance between this and the other vector */
public float dst (Vector2 v) {
final float x_d = v.x - x;
final float y_d = v.y - y;
return (float)Math.sqrt(x_d * x_d + y_d * y_d);
}
/** @param x The x-component of the other vector
* @param y The y-component of the other vector
* @return the distance between this and the other vector */
public float dst (float x, float y) {
final float x_d = x - this.x;
final float y_d = y - this.y;
return (float)Math.sqrt(x_d * x_d + y_d * y_d);
}
/** @param v The other vector
* @return the squared distance between this and the other vector */
public float dst2 (Vector2 v) {
final float x_d = v.x - x;
final float y_d = v.y - y;
return x_d * x_d + y_d * y_d;
}
/** @param x The x-component of the other vector
* @param y The y-component of the other vector
* @return the squared distance between this and the other vector */
public float dst2 (float x, float y) {
final float x_d = x - this.x;
final float y_d = y - this.y;
return x_d * x_d + y_d * y_d;
}
public String toString () {
return "[" + x + ":" + y + "]";
}
/** Substracts the other vector from this vector.
* @param x The x-component of the other vector
* @param y The y-component of the other vector
* @return This vector for chaining */
public Vector2 sub (float x, float y) {
this.x -= x;
this.y -= y;
return this;
}
/** NEVER EVER SAVE THIS REFERENCE! Do not use this unless you are aware of the side-effects, e.g. other methods might call this
* as well.
*
* @return a temporary copy of this vector. Use with care as this is backed by a single static Vector2 instance. v1.tmp().add(
* v2.tmp() ) will not work! */
public Vector2 tmp () {
return tmp.set(this);
}
/** Multiplies this vector by the given matrix
* @param mat the matrix
* @return this vector */
public Vector2 mul (Matrix3 mat) {
float x = this.x * mat.val[0] + this.y * mat.val[3] + mat.val[6];
float y = this.x * mat.val[1] + this.y * mat.val[4] + mat.val[7];
this.x = x;
this.y = y;
return this;
}
/** Calculates the 2D cross product between this and the given vector.
* @param v the other vector
* @return the cross product */
public float crs (Vector2 v) {
return this.x * v.y - this.y * v.x;
}
/** Calculates the 2D cross product between this and the given vector.
* @param x the x-coordinate of the other vector
* @param y the y-coordinate of the other vector
* @return the cross product */
public float crs (float x, float y) {
return this.x * y - this.y * x;
}
/** @return the angle in degrees of this vector (point) relative to the x-axis. Angles are counter-clockwise and between 0 and
* 360. */
public float angle () {
float angle = (float)Math.atan2(y, x) * MathUtils.radiansToDegrees;