-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.java
109 lines (100 loc) · 3.78 KB
/
Utilities.java
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
104
105
106
107
108
109
/**
* @author jakemingolla
* @since 1.0
*
* Collection of utilities for mathematical operations common
* to the Force Directed Graph.
*/
public class Utilities {
/* Epsilon value for all mathematical calculations. This
* simplifies all math related to figuring out double
* precision operations in relation to 0 -- due to the
* nature of double precision and floating point operations,
* simply checking if a value x is greater than 0 does not
* mean it is positive. Instead, the epsilon is used
* to represent the smallest possible positive value. */
public static final Double EPSILON = 0.000000000001;
/* The unit value within a scene. For Forces, this is the
* maximum absolute value any of its x, y, or z portions
* may be. In other words, all values must be between
* -1.0 and 1.0 (within an epsilon value, that is) */
public static final Double UNIT = 1.0;
/**
* @author jakemingolla
*
* Gets the absolute value of a given double precision value.
* For example, if the value is negative it will return the positive
* value.
*
* @param a Value to get the absolute value of it.
* @return The absolute value.
*/
public static Double abs(Double a) {
if (a > EPSILON) {
return a;
} else if (a < EPSILON) {
return -1 * a;
} else {
return 0.0;
}
}
/**
* @author jakemingolla
*
* Returns whether the two given double precision values are
* within {@link EPSILON} of eachother.
*
* @param a First value to check if it is in range of the second.
* @param b Second value to check if it is in range of the first.
* @return The Boolean value of whether or not a and b are within
* EPSILON of eachother.
*/
public static Boolean inRange(Double a, Double b) {
return abs(a - b) < EPSILON;
}
/**
* @author jakemingolla
*
* Returns the enumerated {@link UNIT} value whether or not
* the argument is positive or negative. Uses {@link EPSILON}
* to determine the sign.
*
* @param a The double precision value to check the sign of.
* @return The signed {@link UNIT} value.
*/
public static Double getSign(Double a) {
if (inRange(a, 0.0)) {
return UNIT;
} else if (a > EPSILON) {
return UNIT;
} else {
return -1 * UNIT;
}
}
/**
* @author jakemingolla
*
* Bounds a given value close to {@link EPSILON}. If the
* absolute value of the value is within -1 * EPSILON and
* EPSILON, it will be rounded to EPSILON. Otherwise it
* remains unchanged.
*
* @param val The double precision value to get the
* bounded value of.
* @return The bounded value.
*/
public static Double bound(Double val) {
if (inRange(val, EPSILON)) {
return EPSILON;
} else {
return val;
}
}
public static Boolean inUnit(Double a) {
Double val = abs(a);
return (val < UNIT) || (inRange(val, UNIT));
}
public static Boolean isPositive(Double a) {
return (inRange(a, EPSILON) || a > EPSILON);
}
}