| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import java.util.Locale;
|
| 4 | import java.util.UUID;
|
| 5 | import java.util.function.IntPredicate;
|
| 6 | import java.util.stream.IntStream;
|
| 7 | import net.minecraft.core.Vec3i;
|
| 8 | import net.minecraft.world.phys.AABB;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.apache.commons.lang3.math.Fraction;
|
| 11 | import org.apache.commons.lang3.math.NumberUtils;
|
| 12 | import org.joml.Quaternionf;
|
| 13 | import org.joml.Vector3f;
|
| 14 | import org.joml.Vector3fc;
|
| 15 |
|
| 16 | public class Mth {
|
| 17 | private static final long UUID_VERSION = 61440L;
|
| 18 | private static final long UUID_VERSION_TYPE_4 = 16384L;
|
| 19 | private static final long UUID_VARIANT = -4611686018427387904L;
|
| 20 | private static final long UUID_VARIANT_2 = Long.MIN_VALUE;
|
| 21 | public static final float PI = (float) Math.PI;
|
| 22 | public static final float HALF_PI = (float) (Math.PI / 2);
|
| 23 | public static final float TWO_PI = (float) (Math.PI * 2);
|
| 24 | public static final float DEG_TO_RAD = (float) (Math.PI / 180.0);
|
| 25 | public static final float RAD_TO_DEG = 180.0F / (float)Math.PI;
|
| 26 | public static final float EPSILON = 1.0E-5F;
|
| 27 | public static final float SQRT_OF_TWO = sqrt(2.0F);
|
| 28 | public static final Vector3fc Y_AXIS = new Vector3f(0.0F, 1.0F, 0.0F);
|
| 29 | public static final Vector3fc X_AXIS = new Vector3f(1.0F, 0.0F, 0.0F);
|
| 30 | public static final Vector3fc Z_AXIS = new Vector3f(0.0F, 0.0F, 1.0F);
|
| 31 | private static final int SIN_QUANTIZATION = 65536;
|
| 32 | private static final int SIN_MASK = 65535;
|
| 33 | private static final int COS_OFFSET = 16384;
|
| 34 | private static final double SIN_SCALE = 10430.378350470453;
|
| 35 | private static final float[] SIN = Util.make(new float[65536], sin -> {
|
| 36 | for (int i = 0; i < sin.length; i++) {
|
| 37 | sin[i] = (float)Math.sin(i / 10430.378350470453);
|
| 38 | }
|
| 39 | });
|
| 40 | private static final int[] MULTIPLY_DE_BRUIJN_BIT_POSITION = new int[]{
|
| 41 | 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
|
| 42 | };
|
| 43 | private static final double ONE_SIXTH = 0.16666666666666666;
|
| 44 | private static final int FRAC_EXP = 8;
|
| 45 | private static final int LUT_SIZE = 257;
|
| 46 | private static final double FRAC_BIAS = Double.longBitsToDouble(4805340802404319232L);
|
| 47 | private static final double[] ASIN_TAB = new double[257];
|
| 48 | private static final double[] COS_TAB = new double[257];
|
| 49 |
|
| 50 | public static float sin(final double i) {
|
| 51 | return SIN[(int)((long)(i * 10430.378350470453) & 65535L)];
|
| 52 | }
|
| 53 |
|
| 54 | public static float cos(final double i) {
|
| 55 | return SIN[(int)((long)(i * 10430.378350470453 + 16384.0) & 65535L)];
|
| 56 | }
|
| 57 |
|
| 58 | public static float sqrt(final float x) {
|
| 59 | return (float)Math.sqrt(x);
|
| 60 | }
|
| 61 |
|
| 62 | public static int floor(final float v) {
|
| 63 | return (int)Math.floor(v);
|
| 64 | }
|
| 65 |
|
| 66 | public static int floor(final double v) {
|
| 67 | return (int)Math.floor(v);
|
| 68 | }
|
| 69 |
|
| 70 | public static long lfloor(final double v) {
|
| 71 | return (long)Math.floor(v);
|
| 72 | }
|
| 73 |
|
| 74 | public static float abs(final float v) {
|
| 75 | return Math.abs(v);
|
| 76 | }
|
| 77 |
|
| 78 | public static int abs(final int v) {
|
| 79 | return Math.abs(v);
|
| 80 | }
|
| 81 |
|
| 82 | public static int ceil(final float v) {
|
| 83 | return (int)Math.ceil(v);
|
| 84 | }
|
| 85 |
|
| 86 | public static int ceil(final double v) {
|
| 87 | return (int)Math.ceil(v);
|
| 88 | }
|
| 89 |
|
| 90 | public static long ceilLong(final double v) {
|
| 91 | return (long)Math.ceil(v);
|
| 92 | }
|
| 93 |
|
| 94 | public static int clamp(final int value, final int min, final int max) {
|
| 95 | return Math.min(Math.max(value, min), max);
|
| 96 | }
|
| 97 |
|
| 98 | public static long clamp(final long value, final long min, final long max) {
|
| 99 | return Math.min(Math.max(value, min), max);
|
| 100 | }
|
| 101 |
|
| 102 | public static float clamp(final float value, final float min, final float max) {
|
| 103 | return value < min ? min : Math.min(value, max);
|
| 104 | }
|
| 105 |
|
| 106 | public static double clamp(final double value, final double min, final double max) {
|
| 107 | return value < min ? min : Math.min(value, max);
|
| 108 | }
|
| 109 |
|
| 110 | public static double clampedLerp(final double factor, final double min, final double max) {
|
| 111 | if (factor < 0.0) {
|
| 112 | return min;
|
| 113 | } else {
|
| 114 | return factor > 1.0 ? max : lerp(factor, min, max);
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | public static float clampedLerp(final float factor, final float min, final float max) {
|
| 119 | if (factor < 0.0F) {
|
| 120 | return min;
|
| 121 | } else {
|
| 122 | return factor > 1.0F ? max : lerp(factor, min, max);
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | public static int absMax(final int a, final int b) {
|
| 127 | return Math.max(Math.abs(a), Math.abs(b));
|
| 128 | }
|
| 129 |
|
| 130 | public static float absMax(final float a, final float b) {
|
| 131 | return Math.max(Math.abs(a), Math.abs(b));
|
| 132 | }
|
| 133 |
|
| 134 | public static double absMax(final double a, final double b) {
|
| 135 | return Math.max(Math.abs(a), Math.abs(b));
|
| 136 | }
|
| 137 |
|
| 138 | public static int chessboardDistance(final int x0, final int z0, final int x1, final int z1) {
|
| 139 | return absMax(x1 - x0, z1 - z0);
|
| 140 | }
|
| 141 |
|
| 142 | public static int floorDiv(final int a, final int b) {
|
| 143 | return Math.floorDiv(a, b);
|
| 144 | }
|
| 145 |
|
| 146 | public static int nextInt(final RandomSource random, final int minInclusive, final int maxInclusive) {
|
| 147 | return minInclusive >= maxInclusive ? minInclusive : random.nextInt(maxInclusive - minInclusive + 1) + minInclusive;
|
| 148 | }
|
| 149 |
|
| 150 | public static float nextFloat(final RandomSource random, final float min, final float max) {
|
| 151 | return min >= max ? min : random.nextFloat() * (max - min) + min;
|
| 152 | }
|
| 153 |
|
| 154 | public static double nextDouble(final RandomSource random, final double min, final double max) {
|
| 155 | return min >= max ? min : random.nextDouble() * (max - min) + min;
|
| 156 | }
|
| 157 |
|
| 158 | public static boolean equal(final float a, final float b) {
|
| 159 | return Math.abs(b - a) < 1.0E-5F;
|
| 160 | }
|
| 161 |
|
| 162 | public static boolean equal(final double a, final double b) {
|
| 163 | return Math.abs(b - a) < 1.0E-5F;
|
| 164 | }
|
| 165 |
|
| 166 | public static int positiveModulo(final int input, final int mod) {
|
| 167 | return Math.floorMod(input, mod);
|
| 168 | }
|
| 169 |
|
| 170 | public static float positiveModulo(final float input, final float mod) {
|
| 171 | return (input % mod + mod) % mod;
|
| 172 | }
|
| 173 |
|
| 174 | public static double positiveModulo(final double input, final double mod) {
|
| 175 | return (input % mod + mod) % mod;
|
| 176 | }
|
| 177 |
|
| 178 | public static boolean isMultipleOf(final int dividend, final int divisor) {
|
| 179 | return dividend % divisor == 0;
|
| 180 | }
|
| 181 |
|
| 182 | public static byte packDegrees(final float angle) {
|
| 183 | return (byte)floor(angle * 256.0F / 360.0F);
|
| 184 | }
|
| 185 |
|
| 186 | public static float unpackDegrees(final byte rot) {
|
| 187 | return rot * 360 / 256.0F;
|
| 188 | }
|
| 189 |
|
| 190 | public static int wrapDegrees(final int angle) {
|
| 191 | int normalizedAngle = angle % 360;
|
| 192 | if (normalizedAngle >= 180) {
|
| 193 | normalizedAngle -= 360;
|
| 194 | }
|
| 195 |
|
| 196 | if (normalizedAngle < -180) {
|
| 197 | normalizedAngle += 360;
|
| 198 | }
|
| 199 |
|
| 200 | return normalizedAngle;
|
| 201 | }
|
| 202 |
|
| 203 | public static float wrapDegrees(final long angle) {
|
| 204 | float normalizedAngle = (float)(angle % 360L);
|
| 205 | if (normalizedAngle >= 180.0F) {
|
| 206 | normalizedAngle -= 360.0F;
|
| 207 | }
|
| 208 |
|
| 209 | if (normalizedAngle < -180.0F) {
|
| 210 | normalizedAngle += 360.0F;
|
| 211 | }
|
| 212 |
|
| 213 | return normalizedAngle;
|
| 214 | }
|
| 215 |
|
| 216 | public static float wrapDegrees(final float angle) {
|
| 217 | float normalizedAngle = angle % 360.0F;
|
| 218 | if (normalizedAngle >= 180.0F) {
|
| 219 | normalizedAngle -= 360.0F;
|
| 220 | }
|
| 221 |
|
| 222 | if (normalizedAngle < -180.0F) {
|
| 223 | normalizedAngle += 360.0F;
|
| 224 | }
|
| 225 |
|
| 226 | return normalizedAngle;
|
| 227 | }
|
| 228 |
|
| 229 | public static double wrapDegrees(final double angle) {
|
| 230 | double normalizedAngle = angle % 360.0;
|
| 231 | if (normalizedAngle >= 180.0) {
|
| 232 | normalizedAngle -= 360.0;
|
| 233 | }
|
| 234 |
|
| 235 | if (normalizedAngle < -180.0) {
|
| 236 | normalizedAngle += 360.0;
|
| 237 | }
|
| 238 |
|
| 239 | return normalizedAngle;
|
| 240 | }
|
| 241 |
|
| 242 | public static float wrapDegrees90(final float angle) {
|
| 243 | float normalizedAngle = angle % 90.0F;
|
| 244 | if (normalizedAngle >= 45.0F) {
|
| 245 | normalizedAngle -= 90.0F;
|
| 246 | }
|
| 247 |
|
| 248 | if (normalizedAngle < -45.0F) {
|
| 249 | normalizedAngle += 90.0F;
|
| 250 | }
|
| 251 |
|
| 252 | return normalizedAngle;
|
| 253 | }
|
| 254 |
|
| 255 | public static float degreesDifference(final float fromAngle, final float toAngle) {
|
| 256 | return wrapDegrees(toAngle - fromAngle);
|
| 257 | }
|
| 258 |
|
| 259 | public static float degreesDifferenceAbs(final float angleA, final float angleB) {
|
| 260 | return abs(degreesDifference(angleA, angleB));
|
| 261 | }
|
| 262 |
|
| 263 | public static float rotateIfNecessary(final float baseAngle, final float targetAngle, final float maxAngleDiff) {
|
| 264 | float deltaAngle = degreesDifference(baseAngle, targetAngle);
|
| 265 | float deltaAngleClamped = clamp(deltaAngle, -maxAngleDiff, maxAngleDiff);
|
| 266 | return targetAngle - deltaAngleClamped;
|
| 267 | }
|
| 268 |
|
| 269 | public static float approach(final float current, final float target, float increment) {
|
| 270 | increment = abs(increment);
|
| 271 | return current < target ? clamp(current + increment, current, target) : clamp(current - increment, target, current);
|
| 272 | }
|
| 273 |
|
| 274 | public static float approachDegrees(final float current, final float target, final float increment) {
|
| 275 | float difference = degreesDifference(current, target);
|
| 276 | return approach(current, current + difference, increment);
|
| 277 | }
|
| 278 |
|
| 279 | public static int getInt(final String input, final int def) {
|
| 280 | return NumberUtils.toInt(input, def);
|
| 281 | }
|
| 282 |
|
| 283 | public static int smallestEncompassingPowerOfTwo(final int input) {
|
| 284 | int result = input - 1;
|
| 285 | result |= result >> 1;
|
| 286 | result |= result >> 2;
|
| 287 | result |= result >> 4;
|
| 288 | result |= result >> 8;
|
| 289 | result |= result >> 16;
|
| 290 | return result + 1;
|
| 291 | }
|
| 292 |
|
| 293 | public static int smallestSquareSide(final int itemCount) {
|
| 294 | if (itemCount < 0) {
|
| 295 | throw new IllegalArgumentException("itemCount must be greater than or equal to zero");
|
| 296 | } else {
|
| 297 | return ceil(Math.sqrt(itemCount));
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | public static boolean isPowerOfTwo(final int input) {
|
| 302 | return input != 0 && (input & input - 1) == 0;
|
| 303 | }
|
| 304 |
|
| 305 | public static boolean isPowerOfTwo(final long input) {
|
| 306 | return input != 0L && (input & input - 1L) == 0L;
|
| 307 | }
|
| 308 |
|
| 309 | public static int ceillog2(int input) {
|
| 310 | input = isPowerOfTwo(input) ? input : smallestEncompassingPowerOfTwo(input);
|
| 311 | return MULTIPLY_DE_BRUIJN_BIT_POSITION[(int)(input * 125613361L >> 27) & 31];
|
| 312 | }
|
| 313 |
|
| 314 | public static int log2(final int input) {
|
| 315 | return ceillog2(input) - (isPowerOfTwo(input) ? 0 : 1);
|
| 316 | }
|
| 317 |
|
| 318 | public static float frac(final float num) {
|
| 319 | return num - floor(num);
|
| 320 | }
|
| 321 |
|
| 322 | public static double frac(final double num) {
|
| 323 | return num - lfloor(num);
|
| 324 | }
|
| 325 |
|
| 326 | @Deprecated
|
| 327 | public static long getSeed(final Vec3i vec) {
|
| 328 | return getSeed(vec.getX(), vec.getY(), vec.getZ());
|
| 329 | }
|
| 330 |
|
| 331 | @Deprecated
|
| 332 | public static long getSeed(final int x, final int y, final int z) {
|
| 333 | long seed = x * 3129871 ^ z * 116129781L ^ y;
|
| 334 | seed = seed * seed * 42317861L + seed * 11L;
|
| 335 | return seed >> 16;
|
| 336 | }
|
| 337 |
|
| 338 | public static UUID createInsecureUUID(final RandomSource random) {
|
| 339 | long most = random.nextLong() & -61441L | 16384L;
|
| 340 | long least = random.nextLong() & 4611686018427387903L | Long.MIN_VALUE;
|
| 341 | return new UUID(most, least);
|
| 342 | }
|
| 343 |
|
| 344 | public static double inverseLerp(final double value, final double min, final double max) {
|
| 345 | return (value - min) / (max - min);
|
| 346 | }
|
| 347 |
|
| 348 | public static float inverseLerp(final float value, final float min, final float max) {
|
| 349 | return (value - min) / (max - min);
|
| 350 | }
|
| 351 |
|
| 352 | public static boolean rayIntersectsAABB(final Vec3 rayStart, final Vec3 rayDir, final AABB aabb) {
|
| 353 | double centerX = (aabb.minX + aabb.maxX) * 0.5;
|
| 354 | double boxExtentX = (aabb.maxX - aabb.minX) * 0.5;
|
| 355 | double diffX = rayStart.x - centerX;
|
| 356 | if (Math.abs(diffX) > boxExtentX && diffX * rayDir.x >= 0.0) {
|
| 357 | return false;
|
| 358 | }
|
| 359 |
|
| 360 | double centerY = (aabb.minY + aabb.maxY) * 0.5;
|
| 361 | double boxExtentY = (aabb.maxY - aabb.minY) * 0.5;
|
| 362 | double diffY = rayStart.y - centerY;
|
| 363 | if (Math.abs(diffY) > boxExtentY && diffY * rayDir.y >= 0.0) {
|
| 364 | return false;
|
| 365 | }
|
| 366 |
|
| 367 | double centerZ = (aabb.minZ + aabb.maxZ) * 0.5;
|
| 368 | double boxExtentZ = (aabb.maxZ - aabb.minZ) * 0.5;
|
| 369 | double diffZ = rayStart.z - centerZ;
|
| 370 | if (Math.abs(diffZ) > boxExtentZ && diffZ * rayDir.z >= 0.0) {
|
| 371 | return false;
|
| 372 | }
|
| 373 |
|
| 374 | double andrewWooDiffX = Math.abs(rayDir.x);
|
| 375 | double andrewWooDiffY = Math.abs(rayDir.y);
|
| 376 | double andrewWooDiffZ = Math.abs(rayDir.z);
|
| 377 | double f = rayDir.y * diffZ - rayDir.z * diffY;
|
| 378 | if (Math.abs(f) > boxExtentY * andrewWooDiffZ + boxExtentZ * andrewWooDiffY) {
|
| 379 | return false;
|
| 380 | }
|
| 381 |
|
| 382 | f = rayDir.z * diffX - rayDir.x * diffZ;
|
| 383 | if (Math.abs(f) > boxExtentX * andrewWooDiffZ + boxExtentZ * andrewWooDiffX) {
|
| 384 | return false;
|
| 385 | }
|
| 386 |
|
| 387 | f = rayDir.x * diffY - rayDir.y * diffX;
|
| 388 | return Math.abs(f) < boxExtentX * andrewWooDiffY + boxExtentY * andrewWooDiffX;
|
| 389 | }
|
| 390 |
|
| 391 | public static double atan2(double y, double x) {
|
| 392 | double d2 = x * x + y * y;
|
| 393 | if (Double.isNaN(d2)) {
|
| 394 | return Double.NaN;
|
| 395 | }
|
| 396 |
|
| 397 | boolean negY = y < 0.0;
|
| 398 | if (negY) {
|
| 399 | y = -y;
|
| 400 | }
|
| 401 |
|
| 402 | boolean negX = x < 0.0;
|
| 403 | if (negX) {
|
| 404 | x = -x;
|
| 405 | }
|
| 406 |
|
| 407 | boolean steep = y > x;
|
| 408 | if (steep) {
|
| 409 | double t = x;
|
| 410 | x = y;
|
| 411 | y = t;
|
| 412 | }
|
| 413 |
|
| 414 | double rinv = fastInvSqrt(d2);
|
| 415 | x *= rinv;
|
| 416 | y *= rinv;
|
| 417 | double yp = FRAC_BIAS + y;
|
| 418 | int index = (int)Double.doubleToRawLongBits(yp);
|
| 419 | double phi = ASIN_TAB[index];
|
| 420 | double cPhi = COS_TAB[index];
|
| 421 | double sPhi = yp - FRAC_BIAS;
|
| 422 | double sd = y * cPhi - x * sPhi;
|
| 423 | double d = (6.0 + sd * sd) * sd * 0.16666666666666666;
|
| 424 | double theta = phi + d;
|
| 425 | if (steep) {
|
| 426 | theta = (Math.PI / 2) - theta;
|
| 427 | }
|
| 428 |
|
| 429 | if (negX) {
|
| 430 | theta = Math.PI - theta;
|
| 431 | }
|
| 432 |
|
| 433 | if (negY) {
|
| 434 | theta = -theta;
|
| 435 | }
|
| 436 |
|
| 437 | return theta;
|
| 438 | }
|
| 439 |
|
| 440 | public static float invSqrt(final float x) {
|
| 441 | return org.joml.Math.invsqrt(x);
|
| 442 | }
|
| 443 |
|
| 444 | public static double invSqrt(final double x) {
|
| 445 | return org.joml.Math.invsqrt(x);
|
| 446 | }
|
| 447 |
|
| 448 | @Deprecated
|
| 449 | public static double fastInvSqrt(double x) {
|
| 450 | double xhalf = 0.5 * x;
|
| 451 | long i = Double.doubleToRawLongBits(x);
|
| 452 | i = 6910469410427058090L - (i >> 1);
|
| 453 | x = Double.longBitsToDouble(i);
|
| 454 | return x * (1.5 - xhalf * x * x);
|
| 455 | }
|
| 456 |
|
| 457 | public static float fastInvCubeRoot(final float x) {
|
| 458 | int i = Float.floatToIntBits(x);
|
| 459 | i = 1419967116 - i / 3;
|
| 460 | float y = Float.intBitsToFloat(i);
|
| 461 | y = 0.6666667F * y + 1.0F / (3.0F * y * y * x);
|
| 462 | return 0.6666667F * y + 1.0F / (3.0F * y * y * x);
|
| 463 | }
|
| 464 |
|
| 465 | public static int hsvToRgb(final float hue, final float saturation, final float value) {
|
| 466 | return hsvToArgb(hue, saturation, value, 0);
|
| 467 | }
|
| 468 |
|
| 469 | public static int hsvToArgb(final float hue, final float saturation, final float value, final int alpha) {
|
| 470 | int h = (int)(hue * 6.0F) % 6;
|
| 471 | float f = hue * 6.0F - h;
|
| 472 | float p = value * (1.0F - saturation);
|
| 473 | float q = value * (1.0F - f * saturation);
|
| 474 | float t = value * (1.0F - (1.0F - f) * saturation);
|
| 475 | float red;
|
| 476 | float green;
|
| 477 | float blue;
|
| 478 | switch (h) {
|
| 479 | case 0:
|
| 480 | red = value;
|
| 481 | green = t;
|
| 482 | blue = p;
|
| 483 | break;
|
| 484 | case 1:
|
| 485 | red = q;
|
| 486 | green = value;
|
| 487 | blue = p;
|
| 488 | break;
|
| 489 | case 2:
|
| 490 | red = p;
|
| 491 | green = value;
|
| 492 | blue = t;
|
| 493 | break;
|
| 494 | case 3:
|
| 495 | red = p;
|
| 496 | green = q;
|
| 497 | blue = value;
|
| 498 | break;
|
| 499 | case 4:
|
| 500 | red = t;
|
| 501 | green = p;
|
| 502 | blue = value;
|
| 503 | break;
|
| 504 | case 5:
|
| 505 | red = value;
|
| 506 | green = p;
|
| 507 | blue = q;
|
| 508 | break;
|
| 509 | default:
|
| 510 | throw new RuntimeException("Something went wrong when converting from HSV to RGB. Input was " + hue + ", " + saturation + ", " + value);
|
| 511 | }
|
| 512 |
|
| 513 | return ARGB.color(alpha, clamp((int)(red * 255.0F), 0, 255), clamp((int)(green * 255.0F), 0, 255), clamp((int)(blue * 255.0F), 0, 255));
|
| 514 | }
|
| 515 |
|
| 516 | public static int murmurHash3Mixer(int hash) {
|
| 517 | hash ^= hash >>> 16;
|
| 518 | hash *= -2048144789;
|
| 519 | hash ^= hash >>> 13;
|
| 520 | hash *= -1028477387;
|
| 521 | return hash ^ hash >>> 16;
|
| 522 | }
|
| 523 |
|
| 524 | public static int binarySearch(int from, final int to, final IntPredicate condition) {
|
| 525 | int len = to - from;
|
| 526 |
|
| 527 | while (len > 0) {
|
| 528 | int half = len / 2;
|
| 529 | int middle = from + half;
|
| 530 | if (condition.test(middle)) {
|
| 531 | len = half;
|
| 532 | } else {
|
| 533 | from = middle + 1;
|
| 534 | len -= half + 1;
|
| 535 | }
|
| 536 | }
|
| 537 |
|
| 538 | return from;
|
| 539 | }
|
| 540 |
|
| 541 | public static int lerpInt(final float alpha1, final int p0, final int p1) {
|
| 542 | return p0 + floor(alpha1 * (p1 - p0));
|
| 543 | }
|
| 544 |
|
| 545 | public static int lerpDiscrete(final float alpha1, final int p0, final int p1) {
|
| 546 | int delta = p1 - p0;
|
| 547 | return p0 + floor(alpha1 * (delta - 1)) + (alpha1 > 0.0F ? 1 : 0);
|
| 548 | }
|
| 549 |
|
| 550 | public static float lerp(final float alpha1, final float p0, final float p1) {
|
| 551 | return p0 + alpha1 * (p1 - p0);
|
| 552 | }
|
| 553 |
|
| 554 | public static float lerp2(final float alpha1, final float alpha2, final float x00, final float x10, final float x01, final float x11) {
|
| 555 | return lerp(alpha2, lerp(alpha1, x00, x10), lerp(alpha1, x01, x11));
|
| 556 | }
|
| 557 |
|
| 558 | public static float lerp3(
|
| 559 | final float alpha1,
|
| 560 | final float alpha2,
|
| 561 | final float alpha3,
|
| 562 | final float x000,
|
| 563 | final float x100,
|
| 564 | final float x010,
|
| 565 | final float x110,
|
| 566 | final float x001,
|
| 567 | final float x101,
|
| 568 | final float x011,
|
| 569 | final float x111
|
| 570 | ) {
|
| 571 | return lerp(alpha3, lerp2(alpha1, alpha2, x000, x100, x010, x110), lerp2(alpha1, alpha2, x001, x101, x011, x111));
|
| 572 | }
|
| 573 |
|
| 574 | public static Vec3 lerp(final double alpha, final Vec3 p1, final Vec3 p2) {
|
| 575 | return new Vec3(lerp(alpha, p1.x, p2.x), lerp(alpha, p1.y, p2.y), lerp(alpha, p1.z, p2.z));
|
| 576 | }
|
| 577 |
|
| 578 | public static double lerp(final double alpha1, final double p0, final double p1) {
|
| 579 | return p0 + alpha1 * (p1 - p0);
|
| 580 | }
|
| 581 |
|
| 582 | public static double lerp2(final double alpha1, final double alpha2, final double x00, final double x10, final double x01, final double x11) {
|
| 583 | return lerp(alpha2, lerp(alpha1, x00, x10), lerp(alpha1, x01, x11));
|
| 584 | }
|
| 585 |
|
| 586 | public static double lerp3(
|
| 587 | final double alpha1,
|
| 588 | final double alpha2,
|
| 589 | final double alpha3,
|
| 590 | final double x000,
|
| 591 | final double x100,
|
| 592 | final double x010,
|
| 593 | final double x110,
|
| 594 | final double x001,
|
| 595 | final double x101,
|
| 596 | final double x011,
|
| 597 | final double x111
|
| 598 | ) {
|
| 599 | return lerp(alpha3, lerp2(alpha1, alpha2, x000, x100, x010, x110), lerp2(alpha1, alpha2, x001, x101, x011, x111));
|
| 600 | }
|
| 601 |
|
| 602 | public static float catmullrom(final float alpha, final float p0, final float p1, final float p2, final float p3) {
|
| 603 | return 0.5F
|
| 604 | * (2.0F * p1 + (p2 - p0) * alpha + (2.0F * p0 - 5.0F * p1 + 4.0F * p2 - p3) * alpha * alpha + (3.0F * p1 - p0 - 3.0F * p2 + p3) * alpha * alpha * alpha);
|
| 605 | }
|
| 606 |
|
| 607 | public static float smoothstep(final float x) {
|
| 608 | return x * x * x * (x * (x * 6.0F - 15.0F) + 10.0F);
|
| 609 | }
|
| 610 |
|
| 611 | public static float smoothstepDerivative(final float x) {
|
| 612 | return 30.0F * x * x * (x - 1.0F) * (x - 1.0F);
|
| 613 | }
|
| 614 |
|
| 615 | public static int sign(final double number) {
|
| 616 | if (number == 0.0) {
|
| 617 | return 0;
|
| 618 | } else {
|
| 619 | return number > 0.0 ? 1 : -1;
|
| 620 | }
|
| 621 | }
|
| 622 |
|
| 623 | public static float rotLerp(final float a, final float from, final float to) {
|
| 624 | return from + a * wrapDegrees(to - from);
|
| 625 | }
|
| 626 |
|
| 627 | public static double rotLerp(final double a, final double from, final double to) {
|
| 628 | return from + a * wrapDegrees(to - from);
|
| 629 | }
|
| 630 |
|
| 631 | public static float rotLerpRad(final float a, final float from, final float to) {
|
| 632 | float diff = to - from;
|
| 633 |
|
| 634 | while (diff < (float) -Math.PI) {
|
| 635 | diff += (float) (Math.PI * 2);
|
| 636 | }
|
| 637 |
|
| 638 | while (diff >= (float) Math.PI) {
|
| 639 | diff -= (float) (Math.PI * 2);
|
| 640 | }
|
| 641 |
|
| 642 | return from + a * diff;
|
| 643 | }
|
| 644 |
|
| 645 | public static float triangleWave(final float index, final float period) {
|
| 646 | return (Math.abs(index % period - period * 0.5F) - period * 0.25F) / (period * 0.25F);
|
| 647 | }
|
| 648 |
|
| 649 | public static float square(final float x) {
|
| 650 | return x * x;
|
| 651 | }
|
| 652 |
|
| 653 | public static float cube(final float x) {
|
| 654 | return x * x * x;
|
| 655 | }
|
| 656 |
|
| 657 | public static double square(final double x) {
|
| 658 | return x * x;
|
| 659 | }
|
| 660 |
|
| 661 | public static int square(final int x) {
|
| 662 | return x * x;
|
| 663 | }
|
| 664 |
|
| 665 | public static long square(final long x) {
|
| 666 | return x * x;
|
| 667 | }
|
| 668 |
|
| 669 | public static double clampedMap(final double value, final double fromMin, final double fromMax, final double toMin, final double toMax) {
|
| 670 | return clampedLerp(inverseLerp(value, fromMin, fromMax), toMin, toMax);
|
| 671 | }
|
| 672 |
|
| 673 | public static float clampedMap(final float value, final float fromMin, final float fromMax, final float toMin, final float toMax) {
|
| 674 | return clampedLerp(inverseLerp(value, fromMin, fromMax), toMin, toMax);
|
| 675 | }
|
| 676 |
|
| 677 | public static double map(final double value, final double fromMin, final double fromMax, final double toMin, final double toMax) {
|
| 678 | return lerp(inverseLerp(value, fromMin, fromMax), toMin, toMax);
|
| 679 | }
|
| 680 |
|
| 681 | public static float map(final float value, final float fromMin, final float fromMax, final float toMin, final float toMax) {
|
| 682 | return lerp(inverseLerp(value, fromMin, fromMax), toMin, toMax);
|
| 683 | }
|
| 684 |
|
| 685 | public static double wobble(final double coord) {
|
| 686 | return coord + (2.0 * RandomSource.createThreadLocalInstance(floor(coord * 3000.0)).nextDouble() - 1.0) * 1.0E-7 / 2.0;
|
| 687 | }
|
| 688 |
|
| 689 | public static int roundToward(final int input, final int multiple) {
|
| 690 | return positiveCeilDiv(input, multiple) * multiple;
|
| 691 | }
|
| 692 |
|
| 693 | public static long roundToward(final long input, final long multiple) {
|
| 694 | return positiveCeilDiv(input, multiple) * multiple;
|
| 695 | }
|
| 696 |
|
| 697 | public static int positiveCeilDiv(final int input, final int divisor) {
|
| 698 | return -Math.floorDiv(-input, divisor);
|
| 699 | }
|
| 700 |
|
| 701 | public static long positiveCeilDiv(final long input, final long divisor) {
|
| 702 | return -Math.floorDiv(-input, divisor);
|
| 703 | }
|
| 704 |
|
| 705 | public static int randomBetweenInclusive(final RandomSource random, final int min, final int maxInclusive) {
|
| 706 | return random.nextInt(maxInclusive - min + 1) + min;
|
| 707 | }
|
| 708 |
|
| 709 | public static float randomBetween(final RandomSource random, final float min, final float maxExclusive) {
|
| 710 | return random.nextFloat() * (maxExclusive - min) + min;
|
| 711 | }
|
| 712 |
|
| 713 | public static float normal(final RandomSource random, final float mean, final float deviation) {
|
| 714 | return mean + (float)random.nextGaussian() * deviation;
|
| 715 | }
|
| 716 |
|
| 717 | public static double lengthSquared(final double x, final double y) {
|
| 718 | return x * x + y * y;
|
| 719 | }
|
| 720 |
|
| 721 | public static float lengthSquared(final float x, final float y) {
|
| 722 | return x * x + y * y;
|
| 723 | }
|
| 724 |
|
| 725 | public static double length(final double x, final double y) {
|
| 726 | return Math.sqrt(lengthSquared(x, y));
|
| 727 | }
|
| 728 |
|
| 729 | public static float length(final float x, final float y) {
|
| 730 | return (float)Math.sqrt(lengthSquared(x, y));
|
| 731 | }
|
| 732 |
|
| 733 | public static double lengthSquared(final double x, final double y, final double z) {
|
| 734 | return x * x + y * y + z * z;
|
| 735 | }
|
| 736 |
|
| 737 | public static double length(final double x, final double y, final double z) {
|
| 738 | return Math.sqrt(lengthSquared(x, y, z));
|
| 739 | }
|
| 740 |
|
| 741 | public static float length(final float x, final float y, final float z) {
|
| 742 | return sqrt(lengthSquared(x, y, z));
|
| 743 | }
|
| 744 |
|
| 745 | public static float lengthSquared(final float x, final float y, final float z) {
|
| 746 | return x * x + y * y + z * z;
|
| 747 | }
|
| 748 |
|
| 749 | public static int quantize(final double value, final int quantizeResolution) {
|
| 750 | return floor(value / quantizeResolution) * quantizeResolution;
|
| 751 | }
|
| 752 |
|
| 753 | public static IntStream outFromOrigin(final int origin, final int lowerBound, final int upperBound) {
|
| 754 | return outFromOrigin(origin, lowerBound, upperBound, 1);
|
| 755 | }
|
| 756 |
|
| 757 | public static IntStream outFromOrigin(final int origin, final int lowerBound, final int upperBound, final int stepSize) {
|
| 758 | if (lowerBound > upperBound) {
|
| 759 | throw new IllegalArgumentException(String.format(Locale.ROOT, "upperBound %d expected to be > lowerBound %d", upperBound, lowerBound));
|
| 760 | }
|
| 761 |
|
| 762 | if (stepSize < 1) {
|
| 763 | throw new IllegalArgumentException(String.format(Locale.ROOT, "step size expected to be >= 1, was %d", stepSize));
|
| 764 | }
|
| 765 |
|
| 766 | int clampedOrigin = clamp(origin, lowerBound, upperBound);
|
| 767 | return IntStream.iterate(clampedOrigin, cursor -> {
|
| 768 | int currentDistance = Math.abs(clampedOrigin - cursor);
|
| 769 | return clampedOrigin - currentDistance >= lowerBound || clampedOrigin + currentDistance <= upperBound;
|
| 770 | }, cursor -> {
|
| 771 | boolean previousWasNegative = cursor <= clampedOrigin;
|
| 772 | int currentDistance = Math.abs(clampedOrigin - cursor);
|
| 773 | boolean canMovePositive = clampedOrigin + currentDistance + stepSize <= upperBound;
|
| 774 | if (!previousWasNegative || !canMovePositive) {
|
| 775 | int attemptedStep = clampedOrigin - currentDistance - (previousWasNegative ? stepSize : 0);
|
| 776 | if (attemptedStep >= lowerBound) {
|
| 777 | return attemptedStep;
|
| 778 | }
|
| 779 | }
|
| 780 |
|
| 781 | return clampedOrigin + currentDistance + stepSize;
|
| 782 | });
|
| 783 | }
|
| 784 |
|
| 785 | public static Quaternionf rotationAroundAxis(final Vector3fc axis, final Quaternionf rotation, final Quaternionf result) {
|
| 786 | float projectedLength = axis.dot(rotation.x, rotation.y, rotation.z);
|
| 787 | return result.set(axis.x() * projectedLength, axis.y() * projectedLength, axis.z() * projectedLength, rotation.w).normalize();
|
| 788 | }
|
| 789 |
|
| 790 | public static int mulAndTruncate(final Fraction fraction, final int factor) {
|
| 791 | return fraction.getNumerator() * factor / fraction.getDenominator();
|
| 792 | }
|
| 793 |
|
| 794 | static {
|
| 795 | for (int ind = 0; ind < 257; ind++) {
|
| 796 | double v = ind / 256.0;
|
| 797 | double asinv = Math.asin(v);
|
| 798 | COS_TAB[ind] = Math.cos(asinv);
|
| 799 | ASIN_TAB[ind] = asinv;
|
| 800 | }
|
| 801 | }
|
| 802 | }
|
| 803 |
|