summaryrefslogtreecommitdiff
path: root/src/main/java/ihl/utils/IHLMathUtils.java
blob: 8cab3c9e13cbf6a61a777b0fb08f398be475455c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package ihl.utils;

public class IHLMathUtils {
	private final static int accuracy_level = 1024;
	private final static float[] sqrt_table = new float[accuracy_level];

	public static float sqrt(float value) {
		float value1 = value;
		int multiplier = 1;
		while (value1 >= 1.0f) {
			multiplier=multiplier<<2;
			value1 = value / (multiplier * multiplier);
		}
		return multiplier * sqrt_table[(int) (value1 * accuracy_level)];
	}

	public static float[] vector_vector_multiply(float[] v1, float[] v2) {
		float c_x = v1[1] * v2[2] - v2[1] * v1[2];
		float c_y = v2[0] * v1[2] - v1[0] * v2[2];
		float c_z = v1[0] * v2[1] - v2[0] * v1[1];
		return new float[] { c_x, c_y, c_z };
	}

	public static void normalize_vector(float[] v1) {
		float d = (float) Math.sqrt(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2]);
		if (d == 0) { // Nothing can we do. Create new vector towards up
						// direction.
			v1[0] = 0;
			v1[1] = 1;
			v1[2] = 0;
		} else {
			v1[0] /= d;
			v1[1] /= d;
			v1[2] /= d;
		}
	}

	public static void scale_vector_to_value(float[] v1, float v2) {
		float d = (float) Math.sqrt(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2]);
		if (d == 0) { // Nothing can we do. Create new vector towards up
						// direction.
			v1[0] = 0;
			v1[1] = v2;
			v1[2] = 0;
		} else {
			v1[0] = v1[0] * v2 / d;
			v1[1] = v1[1] * v2 / d;
			v1[2] = v1[2] * v2 / d;
		}
	}

	public static void vector_add(float[] fs, float x, float y, float z) {
		fs[0] += x;
		fs[1] += y;
		fs[2] += z;
	}

	static {
		for (int i = 0; i < accuracy_level; i++) {
			sqrt_table[i] = (float) Math.sqrt((double) i / accuracy_level);
		}
	}

	public static float[] vector_return_difference(double[] v1, double[] v2) {
		return new float[] { (float) (v1[0] - v2[0]), (float) (v1[1] - v2[1]), (float) (v1[2] - v2[2]) };
	}

	public static float[] vector_return_difference(float[] v1, double[] v2) {
		return new float[] { (float) (v1[0] - v2[0]), (float) (v1[1] - v2[1]), (float) (v1[2] - v2[2]) };
	}

	public static float[] vector_return_difference(int[] v1, double[] v2) {
		return new float[] { (float) (v1[0] - v2[0]), (float) (v1[1] - v2[1]), (float) (v1[2] - v2[2]) };
	}

	public static void multiply_vector_to_value(float[] v1, float v2) {
		v1[0] *= v2;
		v1[1] *= v2;
		v1[2] *= v2;
	}

	public static void vector_add(double[] v1, float[] v2) {
		v1[0] += v2[0];
		v1[1] += v2[1];
		v1[2] += v2[2];
	}

	public static float[] get_triangle_normal(double[][] triangle1) {
		float[] v1 = vector_return_difference(triangle1[1], triangle1[0]);
		float[] v2 = vector_return_difference(triangle1[2], triangle1[0]);
		return vector_vector_multiply(v1, v2);
	}

	public static int sign(int value) {
		if (value > 0)
			return 1;
		else if (value < 0)
			return -1;
		else
			return 0;
	}

}