|
8 | 8 |
|
9 | 9 | class ESE_Math |
10 | 10 | { |
| 11 | + // #ESE_ADD_DOCUMENTATION |
| 12 | + static float CeilToNearestMultiple(float number, float multiple) |
| 13 | + { |
| 14 | + if (multiple != 0 && number != 0) |
| 15 | + { |
| 16 | + float sign; |
| 17 | + if (number > 0) {sign = 1;} |
| 18 | + else {sign = -1;} |
| 19 | + number *= sign; |
| 20 | + number /= multiple; |
| 21 | + int fixedPoint = Math.Ceil(number); |
| 22 | + number = fixedPoint * multiple; |
| 23 | + number *= sign; |
| 24 | + } |
| 25 | + return number; |
| 26 | + } |
| 27 | + |
| 28 | + |
11 | 29 | /** |
12 | 30 | Fixes given value to modulo of 360 and then within -180 and 180, used mostly for rotation values |
13 | 31 | @code |
@@ -38,12 +56,41 @@ class ESE_Math |
38 | 56 | // See FixInt180(), same but for vectors |
39 | 57 | static vector FixVector180(vector vec) |
40 | 58 | { |
41 | | - for (int i = 0; i < vec.Length(); i++) |
| 59 | + for (int i = 0; i < vec.Length() - 1; i++) |
42 | 60 | { |
43 | 61 | vec[i] = FixFloat180(vec[i]); |
44 | 62 | } |
45 | 63 | return vec; |
46 | 64 | } |
| 65 | + //----------------------------------------------------------------------------------------------------------- |
| 66 | + // Fast versions for when input is known to be between -360 & 360 |
| 67 | + static float FastFixFloat180(float val) |
| 68 | + { |
| 69 | + if (val > 180) {val -= 360;} |
| 70 | + else if (val < -180) {val += 360;} |
| 71 | + return val; |
| 72 | + } |
| 73 | + |
| 74 | + static int FastFixInt180(int val) |
| 75 | + { |
| 76 | + if (val > 180) {val -= 360;} |
| 77 | + else if (val < -180) {val += 360;} |
| 78 | + return val; |
| 79 | + } |
| 80 | + |
| 81 | + static vector FastFixVector180(vector vec) |
| 82 | + { |
| 83 | + for (int a = 0; a < 3; a++) |
| 84 | + { |
| 85 | + float v = vec[a]; |
| 86 | + while (v > 180) |
| 87 | + v -= 360; |
| 88 | + while (v < -180) |
| 89 | + v += 360; |
| 90 | + vec[a] = v; |
| 91 | + } |
| 92 | + return vec; |
| 93 | + } |
47 | 94 | // ----------------------------------------------------------------------------------------------------------- |
48 | 95 |
|
49 | 96 |
|
|
0 commit comments