| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.Lists;
|
| 6 | import com.mojang.datafixers.util.Either;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import it.unimi.dsi.fastutil.floats.Float2FloatFunction;
|
| 10 | import it.unimi.dsi.fastutil.floats.FloatArrayList;
|
| 11 | import it.unimi.dsi.fastutil.floats.FloatList;
|
| 12 | import java.util.ArrayList;
|
| 13 | import java.util.Arrays;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Locale;
|
| 16 | import java.util.Objects;
|
| 17 | import java.util.function.Consumer;
|
| 18 | import java.util.function.Function;
|
| 19 | import java.util.stream.Collectors;
|
| 20 | import java.util.stream.IntStream;
|
| 21 |
|
| 22 | public sealed interface CubicSpline<I> permits CubicSpline.Multipoint, CubicSpline.Constant {
|
| 23 | void forEachCoordinate(Consumer<I> consumer);
|
| 24 |
|
| 25 | <R extends BoundedFloatFunction<?>> CubicSpline<R> mapCoordinates(Function<I, R> mapper);
|
| 26 |
|
| 27 | Interval range();
|
| 28 |
|
| 29 | @VisibleForDebug
|
| 30 | String parityString();
|
| 31 |
|
| 32 | static <C, I extends BoundedFloatFunction<C>> float sample(final CubicSpline<I> spline, final C coordinate) {
|
| 33 | return switch (spline) {
|
| 34 | case CubicSpline.Multipoint<I> multipoint -> CubicSpline.Multipoint.sample(multipoint, coordinate);
|
| 35 | case CubicSpline.Constant<I> constant -> constant.value();
|
| 36 | default -> throw new MatchException(null, null);
|
| 37 | };
|
| 38 | }
|
| 39 |
|
| 40 | static <C, I extends BoundedFloatFunction<C>> BoundedFloatFunction<C> asSampler(final CubicSpline<I> spline) {
|
| 41 | return switch (spline) {
|
| 42 | case CubicSpline.Multipoint<I> multipoint -> new BoundedFloatFunction<C>() {
|
| 43 | @Override
|
| 44 | public float apply(final C c) {
|
| 45 | return CubicSpline.Multipoint.sample(multipoint, c);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public Interval range() {
|
| 50 | return multipoint.range();
|
| 51 | }
|
| 52 | };
|
| 53 | case CubicSpline.Constant<I> constant -> BoundedFloatFunction.constant(constant.value());
|
| 54 | default -> throw new MatchException(null, null);
|
| 55 | };
|
| 56 | }
|
| 57 |
|
| 58 | static <I extends BoundedFloatFunction<?>> Codec<CubicSpline<I>> codec(final Codec<I> coordinateCodec) {
|
| 59 | return Codec.recursive(
|
| 60 | "CubicSpline",
|
| 61 | subSplineCodec -> Codec.either(Codec.FLOAT, CubicSpline.Multipoint.codec(coordinateCodec, subSplineCodec))
|
| 62 | .xmap(e -> e.map(CubicSpline.Constant::new, (Function<? super CubicSpline.Multipoint<I>, ? extends CubicSpline.Constant<I>>)(m -> m)), spline -> {
|
| 63 | return switch (spline) {
|
| 64 | case CubicSpline.Constant(float value) -> Either.left(value);
|
| 65 | case CubicSpline.Multipoint<I> multipoint -> Either.right(multipoint);
|
| 66 | default -> throw new MatchException(null, null);
|
| 67 | };
|
| 68 | })
|
| 69 | );
|
| 70 | }
|
| 71 |
|
| 72 | static <I> CubicSpline<I> constant(final float value) {
|
| 73 | return new CubicSpline.Constant<>(value);
|
| 74 | }
|
| 75 |
|
| 76 | static <I extends BoundedFloatFunction<?>> CubicSpline.Builder<I> builder(final I coordinate) {
|
| 77 | return new CubicSpline.Builder<>(coordinate);
|
| 78 | }
|
| 79 |
|
| 80 | static <I extends BoundedFloatFunction<?>> CubicSpline.Builder<I> builder(final I coordinate, final Float2FloatFunction valueTransformer) {
|
| 81 | return new CubicSpline.Builder<>(coordinate, valueTransformer);
|
| 82 | }
|
| 83 |
|
| 84 | final class Builder<I extends BoundedFloatFunction<?>> {
|
| 85 | private final I coordinate;
|
| 86 | private final Float2FloatFunction valueTransformer;
|
| 87 | private final FloatList locations = new FloatArrayList();
|
| 88 | private final List<CubicSpline<I>> values = Lists.newArrayList();
|
| 89 | private final FloatList derivatives = new FloatArrayList();
|
| 90 |
|
| 91 | private Builder(final I coordinate) {
|
| 92 | this(coordinate, Float2FloatFunction.identity());
|
| 93 | }
|
| 94 |
|
| 95 | private Builder(final I coordinate, final Float2FloatFunction valueTransformer) {
|
| 96 | this.coordinate = coordinate;
|
| 97 | this.valueTransformer = valueTransformer;
|
| 98 | }
|
| 99 |
|
| 100 | public CubicSpline.Builder<I> addPoint(final float location, final float value) {
|
| 101 | return this.addPoint(location, new CubicSpline.Constant<>(this.valueTransformer.apply(value)), 0.0F);
|
| 102 | }
|
| 103 |
|
| 104 | public CubicSpline.Builder<I> addPoint(final float location, final float value, final float derivative) {
|
| 105 | return this.addPoint(location, new CubicSpline.Constant<>(this.valueTransformer.apply(value)), derivative);
|
| 106 | }
|
| 107 |
|
| 108 | public CubicSpline.Builder<I> addPoint(final float location, final CubicSpline<I> sampler) {
|
| 109 | return this.addPoint(location, sampler, 0.0F);
|
| 110 | }
|
| 111 |
|
| 112 | private CubicSpline.Builder<I> addPoint(final float location, final CubicSpline<I> sampler, final float derivative) {
|
| 113 | if (!this.locations.isEmpty() && location <= this.locations.getFloat(this.locations.size() - 1)) {
|
| 114 | throw new IllegalArgumentException("Please register points in ascending order");
|
| 115 | }
|
| 116 |
|
| 117 | this.locations.add(location);
|
| 118 | this.values.add(sampler);
|
| 119 | this.derivatives.add(derivative);
|
| 120 | return this;
|
| 121 | }
|
| 122 |
|
| 123 | public CubicSpline<I> build() {
|
| 124 | if (this.locations.isEmpty()) {
|
| 125 | throw new IllegalStateException("No elements added");
|
| 126 | } else {
|
| 127 | return new CubicSpline.Multipoint(this.coordinate, this.locations.toFloatArray(), List.copyOf(this.values), this.derivatives.toFloatArray());
|
| 128 | }
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | @VisibleForDebug
|
| 133 | record Constant<I>(float value) implements CubicSpline<I> {
|
| 134 | @Override
|
| 135 | public String parityString() {
|
| 136 | return String.format(Locale.ROOT, "k=%.3f", this.value);
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public Interval range() {
|
| 141 | return Interval.ofExact(this.value);
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public void forEachCoordinate(final Consumer<I> consumer) {
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public <R extends BoundedFloatFunction<?>> CubicSpline<R> mapCoordinates(final Function<I, R> mapper) {
|
| 150 | return this;
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | @VisibleForDebug
|
| 155 | record Multipoint<I extends BoundedFloatFunction<?>>(I coordinate, float[] locations, List<CubicSpline<I>> values, float[] derivatives)
|
| 156 | implements CubicSpline<I> {
|
| 157 | public Multipoint {
|
| 158 | validateSizes(locations, values, derivatives);
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public Interval range() {
|
| 163 | int lastIndex = this.locations.length - 1;
|
| 164 | float minValue = Float.POSITIVE_INFINITY;
|
| 165 | float maxValue = Float.NEGATIVE_INFINITY;
|
| 166 | Interval inputRange = this.coordinate.range();
|
| 167 | if (inputRange.isNaI()) {
|
| 168 | return inputRange;
|
| 169 | }
|
| 170 |
|
| 171 | if (inputRange.min() < this.locations[0]) {
|
| 172 | Interval firstRange = this.values.getFirst().range();
|
| 173 | float edge1 = linearExtend(inputRange.min(), this.locations, firstRange.min(), this.derivatives, 0);
|
| 174 | float edge2 = linearExtend(inputRange.min(), this.locations, firstRange.max(), this.derivatives, 0);
|
| 175 | minValue = Math.min(minValue, Math.min(edge1, edge2));
|
| 176 | maxValue = Math.max(maxValue, Math.max(edge1, edge2));
|
| 177 | }
|
| 178 |
|
| 179 | if (inputRange.max() > this.locations[lastIndex]) {
|
| 180 | Interval lastRange = this.values.get(lastIndex).range();
|
| 181 | float edge1 = linearExtend(inputRange.max(), this.locations, lastRange.min(), this.derivatives, lastIndex);
|
| 182 | float edge2 = linearExtend(inputRange.max(), this.locations, lastRange.max(), this.derivatives, lastIndex);
|
| 183 | minValue = Math.min(minValue, Math.min(edge1, edge2));
|
| 184 | maxValue = Math.max(maxValue, Math.max(edge1, edge2));
|
| 185 | }
|
| 186 |
|
| 187 | List<Interval> valueRanges = List.copyOf(Lists.transform(this.values, CubicSpline::range));
|
| 188 |
|
| 189 | for (Interval range : valueRanges) {
|
| 190 | minValue = Math.min(minValue, range.min());
|
| 191 | maxValue = Math.max(maxValue, range.max());
|
| 192 | }
|
| 193 |
|
| 194 | for (int i = 0; i < lastIndex; i++) {
|
| 195 | float x1 = this.locations[i];
|
| 196 | float x2 = this.locations[i + 1];
|
| 197 | float xDiff = x2 - x1;
|
| 198 | Interval range1 = valueRanges.get(i);
|
| 199 | Interval range2 = valueRanges.get(i + 1);
|
| 200 | float min1 = range1.min();
|
| 201 | float max1 = range1.max();
|
| 202 | float min2 = range2.min();
|
| 203 | float max2 = range2.max();
|
| 204 | float d1 = this.derivatives[i];
|
| 205 | float d2 = this.derivatives[i + 1];
|
| 206 | if (d1 != 0.0F || d2 != 0.0F) {
|
| 207 | float p1 = d1 * xDiff;
|
| 208 | float p2 = d2 * xDiff;
|
| 209 | float minLerp1 = Math.min(min1, min2);
|
| 210 | float maxLerp1 = Math.max(max1, max2);
|
| 211 | float minA = p1 - max2 + min1;
|
| 212 | float maxA = p1 - min2 + max1;
|
| 213 | float minB = -p2 + min2 - max1;
|
| 214 | float maxB = -p2 + max2 - min1;
|
| 215 | float minLerp2 = Math.min(minA, minB);
|
| 216 | float maxLerp2 = Math.max(maxA, maxB);
|
| 217 | minValue = Math.min(minValue, minLerp1 + 0.25F * minLerp2);
|
| 218 | maxValue = Math.max(maxValue, maxLerp1 + 0.25F * maxLerp2);
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | return Interval.of(minValue, maxValue);
|
| 223 | }
|
| 224 |
|
| 225 | private static float linearExtend(final float input, final float[] locations, final float value, final float[] derivatives, final int index) {
|
| 226 | float derivative = derivatives[index];
|
| 227 | return derivative == 0.0F ? value : value + derivative * (input - locations[index]);
|
| 228 | }
|
| 229 |
|
| 230 | private static <I> void validateSizes(final float[] locations, final List<CubicSpline<I>> values, final float[] derivatives) {
|
| 231 | if (locations.length != values.size() || locations.length != derivatives.length) {
|
| 232 | throw new IllegalArgumentException("All lengths must be equal, got: " + locations.length + " " + values.size() + " " + derivatives.length);
|
| 233 | }
|
| 234 |
|
| 235 | if (locations.length == 0) {
|
| 236 | throw new IllegalArgumentException("Cannot create a multipoint spline with no points");
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | public static <C, I extends BoundedFloatFunction<C>> float sample(final CubicSpline.Multipoint<I> sampler, final C c) {
|
| 241 | return sample(sampler.coordinate, sampler.derivatives, sampler.locations, sampler.values, c);
|
| 242 | }
|
| 243 |
|
| 244 | private static <C, I extends BoundedFloatFunction<C>> float sample(
|
| 245 | final I coordinate, final float[] derivatives, final float[] locations, final List<CubicSpline<I>> values, final C c
|
| 246 | ) {
|
| 247 | float input = coordinate.apply(c);
|
| 248 | int start = findIntervalStart(locations, input);
|
| 249 | int lastIndex = locations.length - 1;
|
| 250 | if (start < 0) {
|
| 251 | return linearExtend(input, locations, CubicSpline.sample(values.getFirst(), c), derivatives, 0);
|
| 252 | }
|
| 253 |
|
| 254 | if (start == lastIndex) {
|
| 255 | return linearExtend(input, locations, CubicSpline.sample(values.get(lastIndex), c), derivatives, lastIndex);
|
| 256 | }
|
| 257 |
|
| 258 | float x1 = locations[start];
|
| 259 | float x2 = locations[start + 1];
|
| 260 | float t = (input - x1) / (x2 - x1);
|
| 261 | CubicSpline<I> f1 = values.get(start);
|
| 262 | CubicSpline<I> f2 = values.get(start + 1);
|
| 263 | float d1 = derivatives[start];
|
| 264 | float d2 = derivatives[start + 1];
|
| 265 | float y1 = CubicSpline.sample(f1, c);
|
| 266 | float y2 = CubicSpline.sample(f2, c);
|
| 267 | float a = d1 * (x2 - x1) - (y2 - y1);
|
| 268 | float b = -d2 * (x2 - x1) + (y2 - y1);
|
| 269 | return Mth.lerp(t, y1, y2) + t * (1.0F - t) * Mth.lerp(t, a, b);
|
| 270 | }
|
| 271 |
|
| 272 | private static int findIntervalStart(final float[] locations, final float input) {
|
| 273 | return Mth.binarySearch(0, locations.length, i -> input < locations[i]) - 1;
|
| 274 | }
|
| 275 |
|
| 276 | @VisibleForTesting
|
| 277 | @Override
|
| 278 | public String parityString() {
|
| 279 | return "Spline{coordinate="
|
| 280 | + this.coordinate
|
| 281 | + ", locations="
|
| 282 | + toString(this.locations)
|
| 283 | + ", derivatives="
|
| 284 | + toString(this.derivatives)
|
| 285 | + ", values="
|
| 286 | + this.values.stream().map(CubicSpline::parityString).collect(Collectors.joining(", ", "[", "]"))
|
| 287 | + "}";
|
| 288 | }
|
| 289 |
|
| 290 | private static String toString(final float[] arr) {
|
| 291 | return "["
|
| 292 | + IntStream.range(0, arr.length).mapToDouble(i -> arr[i]).mapToObj(f -> String.format(Locale.ROOT, "%.3f", f)).collect(Collectors.joining(", "))
|
| 293 | + "]";
|
| 294 | }
|
| 295 |
|
| 296 | @Override
|
| 297 | public void forEachCoordinate(final Consumer<I> consumer) {
|
| 298 | consumer.accept(this.coordinate);
|
| 299 |
|
| 300 | for (CubicSpline<I> spline : this.values) {
|
| 301 | spline.forEachCoordinate(consumer);
|
| 302 | }
|
| 303 | }
|
| 304 |
|
| 305 | @Override
|
| 306 | public <R extends BoundedFloatFunction<?>> CubicSpline<R> mapCoordinates(final Function<I, R> mapper) {
|
| 307 | return new CubicSpline.Multipoint<>(
|
| 308 | mapper.apply(this.coordinate), this.locations, this.values.stream().map(v -> v.mapCoordinates(mapper)).toList(), this.derivatives
|
| 309 | );
|
| 310 | }
|
| 311 |
|
| 312 | @Override
|
| 313 | public boolean equals(final Object obj) {
|
| 314 | return !(obj instanceof CubicSpline.Multipoint<?> multipoint)
|
| 315 | ? false
|
| 316 | : Objects.equals(this.coordinate, multipoint.coordinate)
|
| 317 | && Arrays.equals(this.locations, multipoint.locations)
|
| 318 | && Arrays.equals(this.derivatives, multipoint.derivatives)
|
| 319 | && Objects.equals(this.values, multipoint.values);
|
| 320 | }
|
| 321 |
|
| 322 | @Override
|
| 323 | public int hashCode() {
|
| 324 | return Objects.hash(this.coordinate, Arrays.hashCode(this.locations), this.values, Arrays.hashCode(this.derivatives));
|
| 325 | }
|
| 326 |
|
| 327 | public static <I extends BoundedFloatFunction<?>> Codec<CubicSpline.Multipoint<I>> codec(
|
| 328 | final Codec<I> coordinateCodec, final Codec<CubicSpline<I>> subSplineCodec
|
| 329 | ) {
|
| 330 | return RecordCodecBuilder.create(
|
| 331 | i -> i.group(
|
| 332 | coordinateCodec.fieldOf("coordinate").forGetter(CubicSpline.Multipoint::coordinate),
|
| 333 | ExtraCodecs.<CubicSpline.Multipoint.Point<I>>nonEmptyList(CubicSpline.Multipoint.Point.codec(subSplineCodec).listOf())
|
| 334 | .fieldOf("points")
|
| 335 | .forGetter(CubicSpline.Multipoint::packToPoints)
|
| 336 | )
|
| 337 | .apply(i, CubicSpline.Multipoint::createFromPoints)
|
| 338 | );
|
| 339 | }
|
| 340 |
|
| 341 | private List<CubicSpline.Multipoint.Point<I>> packToPoints() {
|
| 342 | int pointCount = this.locations.length;
|
| 343 | List<CubicSpline.Multipoint.Point<I>> list = new ArrayList<>(pointCount);
|
| 344 |
|
| 345 | for (int p = 0; p < pointCount; p++) {
|
| 346 | list.add(new CubicSpline.Multipoint.Point(this.locations[p], this.values.get(p), this.derivatives[p]));
|
| 347 | }
|
| 348 |
|
| 349 | return list;
|
| 350 | }
|
| 351 |
|
| 352 | private static <I extends BoundedFloatFunction<?>> CubicSpline.Multipoint<I> createFromPoints(
|
| 353 | final I coordinate, final List<CubicSpline.Multipoint.Point<I>> points
|
| 354 | ) {
|
| 355 | int pointCount = points.size();
|
| 356 | float[] locations = new float[pointCount];
|
| 357 | ImmutableList.Builder<CubicSpline<I>> values = ImmutableList.builderWithExpectedSize(pointCount);
|
| 358 | float[] derivatives = new float[pointCount];
|
| 359 |
|
| 360 | for (int p = 0; p < pointCount; p++) {
|
| 361 | CubicSpline.Multipoint.Point<I> point = points.get(p);
|
| 362 | locations[p] = point.location();
|
| 363 | values.add(point.value());
|
| 364 | derivatives[p] = point.derivative();
|
| 365 | }
|
| 366 |
|
| 367 | return new CubicSpline.Multipoint(coordinate, locations, values.build(), derivatives);
|
| 368 | }
|
| 369 |
|
| 370 | private record Point<I extends BoundedFloatFunction<?>>(float location, CubicSpline<I> value, float derivative) {
|
| 371 | public static <I extends BoundedFloatFunction<?>> Codec<CubicSpline.Multipoint.Point<I>> codec(final Codec<CubicSpline<I>> subSplineCodec) {
|
| 372 | return RecordCodecBuilder.create(
|
| 373 | i -> i.group(
|
| 374 | Codec.FLOAT.fieldOf("location").forGetter(CubicSpline.Multipoint.Point::location),
|
| 375 | subSplineCodec.fieldOf("value").forGetter(CubicSpline.Multipoint.Point::value),
|
| 376 | Codec.FLOAT.fieldOf("derivative").forGetter(CubicSpline.Multipoint.Point::derivative)
|
| 377 | )
|
| 378 | .apply(i, CubicSpline.Multipoint.Point::new)
|
| 379 | );
|
| 380 | }
|
| 381 | }
|
| 382 | }
|
| 383 | }
|
| 384 |
|