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
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.io.Serializable;
/** A 3x3 <a href="http://en.wikipedia.org/wiki/Row-major_order">column major</a> matrix; useful for 2D transforms.
*
* @author mzechner */
public class Matrix3 implements Serializable {
private static final long serialVersionUID = 7907569533774959788L;
private final static float DEGREE_TO_RAD = (float)Math.PI / 180;
public static final int M00 = 0;
public static final int M01 = 3;
public static final int M02 = 6;
public static final int M10 = 1;
public static final int M11 = 4;
public static final int M12 = 7;
public static final int M20 = 2;
public static final int M21 = 5;
public static final int M22 = 8;
public float[] val = new float[9];
private float[] tmp = new float[9];
public Matrix3 () {
idt();
}
public Matrix3 (Matrix3 matrix) {
set(matrix);
}
/** Sets this matrix to the identity matrix
* @return This matrix for the purpose of chaining operations. */
public Matrix3 idt () {
val[M00] = 1;
val[M10] = 0;
val[M20] = 0;
val[M01] = 0;
val[M11] = 1;
val[M21] = 0;
val[M02] = 0;
val[M12] = 0;
val[M22] = 1;
return this;
}
/** Multiplies this matrix with the provided matrix and stores the result in this matrix. For example:
*
* <pre>
* A.mul(B) results in A := AB
* </pre>
* @param m Matrix to multiply by.
* @return This matrix for the purpose of chaining operations together. */
public Matrix3 mul (Matrix3 m) {
float v00 = val[M00] * m.val[M00] + val[M01] * m.val[M10] + val[M02] * m.val[M20];
float v01 = val[M00] * m.val[M01] + val[M01] * m.val[M11] + val[M02] * m.val[M21];
float v02 = val[M00] * m.val[M02] + val[M01] * m.val[M12] + val[M02] * m.val[M22];
float v10 = val[M10] * m.val[M00] + val[M11] * m.val[M10] + val[M12] * m.val[M20];
float v11 = val[M10] * m.val[M01] + val[M11] * m.val[M11] + val[M12] * m.val[M21];
float v12 = val[M10] * m.val[M02] + val[M11] * m.val[M12] + val[M12] * m.val[M22];
float v20 = val[M20] * m.val[M00] + val[M21] * m.val[M10] + val[M22] * m.val[M20];
float v21 = val[M20] * m.val[M01] + val[M21] * m.val[M11] + val[M22] * m.val[M21];
float v22 = val[M20] * m.val[M02] + val[M21] * m.val[M12] + val[M22] * m.val[M22];
val[M00] = v00;
val[M10] = v10;
val[M20] = v20;
val[M01] = v01;
val[M11] = v11;
val[M21] = v21;
val[M02] = v02;
val[M12] = v12;
val[M22] = v22;
return this;
}
/** Sets this matrix to a rotation matrix that will rotate any vector in counter-clockwise order around the z-axis.
* @param degrees the angle in degrees.
* @return This matrix for the purpose of chaining operations. */
public Matrix3 setToRotation (float degrees) {
float angle = DEGREE_TO_RAD * degrees;
float cos = (float)Math.cos(angle);
float sin = (float)Math.sin(angle);
this.val[M00] = cos;
this.val[M10] = sin;
this.val[M20] = 0;
this.val[M01] = -sin;
this.val[M11] = cos;
this.val[M21] = 0;
this.val[M02] = 0;
this.val[M12] = 0;
this.val[M22] = 1;
return this;
}
/** Sets this matrix to a translation matrix.
* @param x the translation in x
* @param y the translation in y
* @return This matrix for the purpose of chaining operations. */
public Matrix3 setToTranslation (float x, float y) {
this.val[M00] = 1;
this.val[M10] = 0;
this.val[M20] = 0;
this.val[M01] = 0;
this.val[M11] = 1;
this.val[M21] = 0;
this.val[M02] = x;
this.val[M12] = y;
this.val[M22] = 1;
return this;
}
/** Sets this matrix to a translation matrix.
* @param translation The translation vector.
* @return This matrix for the purpose of chaining operations. */
public Matrix3 setToTranslation (Vector2 translation) {
this.val[M00] = 1;
this.val[M10] = 0;
this.val[M20] = 0;
this.val[M01] = 0;
this.val[M11] = 1;
this.val[M21] = 0;
this.val[M02] = translation.x;
this.val[M12] = translation.y;
this.val[M22] = 1;
return this;
}
/** Sets this matrix to a scaling matrix.
*
* @param scaleX the scale in x
* @param scaleY the scale in y
* @return This matrix for the purpose of chaining operations. */
public Matrix3 setToScaling (float scaleX, float scaleY) {
val[M00] = scaleX;
val[M10] = 0;
val[M20] = 0;
val[M01] = 0;
val[M11] = scaleY;
val[M21] = 0;
val[M02] = 0;
val[M12] = 0;
val[M22] = 1;
return this;
}
public String toString () {
return "[" + val[0] + "|" + val[3] + "|" + val[6] + "]\n" + "[" + val[1] + "|" + val[4] + "|" + val[7] + "]\n" + "["
+ val[2] + "|" + val[5] + "|" + val[8] + "]";
}
/** @return The determinant of this matrix */
public float det () {
return val[M00] * val[M11] * val[M22] + val[M01] * val[M12] * val[M20] + val[M02] * val[M10] * val[M21] - val[M00]
* val[M12] * val[M21] - val[M01] * val[M10] * val[M22] - val[M02] * val[M11] * val[M20];
}
/** Inverts this matrix given that the determinant is != 0.
* @return This matrix for the purpose of chaining operations. */
public Matrix3 inv () {
float det = det();
if (det == 0) throw new Error("Can't invert a singular matrix"); // changed to error from GdxRuntimeException
float inv_det = 1.0f / det;
tmp[M00] = val[M11] * val[M22] - val[M21] * val[M12];
tmp[M10] = val[M20] * val[M12] - val[M10] * val[M22];
tmp[M20] = val[M10] * val[M21] - val[M20] * val[M11];
tmp[M01] = val[M21] * val[M02] - val[M01] * val[M22];
tmp[M11] = val[M00] * val[M22] - val[M20] * val[M02];
tmp[M21] = val[M20] * val[M01] - val[M00] * val[M21];
tmp[M02] = val[M01] * val[M12] - val[M11] * val[M02];
tmp[M12] = val[M10] * val[M02] - val[M00] * val[M12];
tmp[M22] = val[M00] * val[M11] - val[M10] * val[M01];
val[M00] = inv_det * tmp[M00];
val[M10] = inv_det * tmp[M10];
val[M20] = inv_det * tmp[M20];
val[M01] = inv_det * tmp[M01];
val[M11] = inv_det * tmp[M11];
val[M21] = inv_det * tmp[M21];
val[M02] = inv_det * tmp[M02];
val[M12] = inv_det * tmp[M12];
val[M22] = inv_det * tmp[M22];
return this;
}
/** Copies the values from the provided matrix to this matrix.
* @param mat The matrix to copy.
* @return This matrix for the purposes of chaining. */
public Matrix3 set (Matrix3 mat) {
System.arraycopy(mat.val, 0, val, 0, val.length);
return this;
}
/** Sets this 3x3 matrix to the top left 3x3 corner of the provided 4x4 matrix.
* @param mat The matrix whose top left corner will be copied. This matrix will not be modified.
* @return This matrix for the purpose of chaining operations. */
public Matrix3 set (Matrix4 mat) {
val[M00] = mat.val[Matrix4.M00];
val[M10] = mat.val[Matrix4.M10];
val[M20] = mat.val[Matrix4.M20];
val[M01] = mat.val[Matrix4.M01];
val[M11] = mat.val[Matrix4.M11];
val[M21] = mat.val[Matrix4.M21];
val[M02] = mat.val[Matrix4.M02];
val[M12] = mat.val[Matrix4.M12];
val[M22] = mat.val[Matrix4.M22];
return this;
}
/** Adds a translational component to the matrix in the 3rd column. The other columns are untouched.
* @param vector The translation vector.
* @return This matrix for the purpose of chaining. */
public Matrix3 trn (Vector2 vector) {
val[M02] += vector.x;
val[M12] += vector.y;
return this;
}
/** Adds a translational component to the matrix in the 3rd column. The other columns are untouched.
* @param x The x-component of the translation vector.
* @param y The y-component of the translation vector.
* @return This matrix for the purpose of chaining. */
public Matrix3 trn (float x, float y) {
val[M02] += x;
val[M12] += y;
return this;
}
/** Adds a translational component to the matrix in the 3rd column. The other columns are untouched.
* @param vector The translation vector. (The z-component of the vector is ignored because this is a 3x3 matrix)
* @return This matrix for the purpose of chaining. */
public Matrix3 trn (Vector3 vector) {
val[M02] += vector.x;
val[M12] += vector.y;
return this;
}
/** Postmultiplies this matrix by a translation matrix. Postmultiplication is also used by OpenGL ES' 1.x
* glTranslate/glRotate/glScale.
* @param x The x-component of the translation vector.
* @param y The y-component of the translation vector.
* @return This matrix for the purpose of chaining. */
public Matrix3 translate (float x, float y) {
tmp[M00] = 1;
tmp[M10] = 0;
tmp[M20] = 0;
tmp[M01] = 0;
tmp[M11] = 1;
tmp[M21] = 0;
tmp[M02] = x;
tmp[M12] = y;
tmp[M22] = 1;
mul(val, tmp);
return this;
}
/** Postmultiplies this matrix by a translation matrix. Postmultiplication is also used by OpenGL ES' 1.x
* glTranslate/glRotate/glScale.
* @param translation The translation vector.
* @return This matrix for the purpose of chaining. */
public Matrix3 translate (Vector2 translation) {
tmp[M00] = 1;
tmp[M10] = 0;
tmp[M20] = 0;
tmp[M01] = 0;
tmp[M11] = 1;
tmp[M21] = 0;
tmp[M02] = translation.x;
tmp[M12] = translation.y;
tmp[M22] = 1;
mul(val, tmp);
return this;
}
/** Postmultiplies this matrix with a (counter-clockwise) rotation matrix. Postmultiplication is also used by OpenGL ES' 1.x
* glTranslate/glRotate/glScale.
* @param angle The angle in degrees
* @return This matrix for the purpose of chaining. */
public Matrix3 rotate (float angle) {
if (angle == 0) return this;
angle = DEGREE_TO_RAD * angle;
float cos = (float)Math.cos(angle);
float sin = (float)Math.sin(angle);
tmp[M00] = cos;
tmp[M10] = sin;
tmp[M20] = 0;
tmp[M01] = -sin;
tmp[M11] = cos;
tmp[M21] = 0;
tmp[M02] = 0;
tmp[M12] = 0;
tmp[M22] = 1;
mul(val, tmp);
return this;
}
/** Postmultiplies this matrix with a scale matrix. Postmultiplication is also used by OpenGL ES' 1.x
* glTranslate/glRotate/glScale.
* @param scaleX The scale in the x-axis.
* @param scaleY The scale in the y-axis.
* @return This matrix for the purpose of chaining. */
public Matrix3 scale (float scaleX, float scaleY) {
tmp[M00] = scaleX;
tmp[M10] = 0;
tmp[M20] = 0;
tmp[M01] = 0;
tmp[M11] = scaleY;
tmp[M21] = 0;
tmp[M02] = 0;
tmp[M12] = 0;
tmp[M22] = 1;
mul(val, tmp);
return this;
}
/** Postmultiplies this matrix with a scale matrix. Postmultiplication is also used by OpenGL ES' 1.x