Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/util/LinearInterpolator.java @ 8964:45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
author | gernotbelger |
---|---|
date | Thu, 29 Mar 2018 15:48:17 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
8963:b98fbd91f64a | 8964:45f1ad66560e |
---|---|
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by | |
3 * Björnsen Beratende Ingenieure GmbH | |
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt | |
5 * | |
6 * This file is Free Software under the GNU AGPL (>=v3) | |
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
8 * documentation coming with Dive4Elements River for details. | |
9 */ | |
10 package org.dive4elements.river.artifacts.sinfo.util; | |
11 | |
12 import java.util.Map.Entry; | |
13 import java.util.NavigableMap; | |
14 import java.util.TreeMap; | |
15 | |
16 import org.dive4elements.river.artifacts.math.Linear; | |
17 import org.dive4elements.river.artifacts.model.Calculation; | |
18 | |
19 import gnu.trove.TDoubleArrayList; | |
20 | |
21 /** | |
22 * Helper for interpolating values from a piecewise linear function defined by discrete values. | |
23 * | |
24 * @author Gernot Belger | |
25 */ | |
26 public final class LinearInterpolator { | |
27 | |
28 private final NavigableMap<Double, Double> data; | |
29 private final double maxDistance; | |
30 | |
31 private Calculation problems; | |
32 | |
33 public static LinearInterpolator create(final Calculation problems, final TDoubleArrayList xs, final TDoubleArrayList ys, final double maxDistance) { | |
34 if (xs.size() != ys.size()) | |
35 throw new IllegalArgumentException("Array sizes must be equal"); | |
36 | |
37 if (xs.size() < 2) | |
38 throw new IllegalArgumentException("Array must have at least 2 values"); | |
39 | |
40 final NavigableMap<Double, Double> data = new TreeMap<>(); | |
41 | |
42 for (int i = 0; i < xs.size(); i++) { | |
43 final double x = xs.getQuick(i); | |
44 final double y = ys.getQuick(i); | |
45 data.put(x, y); | |
46 } | |
47 | |
48 return new LinearInterpolator(problems, data, maxDistance); | |
49 } | |
50 | |
51 private LinearInterpolator(final Calculation problems, final NavigableMap<Double, Double> data, final double maxDistance) { | |
52 this.problems = problems; | |
53 this.data = data; | |
54 this.maxDistance = maxDistance; | |
55 } | |
56 | |
57 public double value(final double value) { | |
58 | |
59 final Entry<Double, Double> floorEntry = this.data.floorEntry(value); | |
60 final Entry<Double, Double> ceilingEntry = this.data.ceilingEntry(value); | |
61 | |
62 if (floorEntry == null || ceilingEntry == null) | |
63 return Double.NaN; | |
64 | |
65 final double floorKey = floorEntry.getKey(); | |
66 final double floorValue = floorEntry.getValue(); | |
67 final double ceilingKey = ceilingEntry.getKey(); | |
68 final double ceilingValue = ceilingEntry.getValue(); | |
69 | |
70 if (Math.abs(floorKey - ceilingKey) > this.maxDistance && this.problems != null) { | |
71 this.problems.addProblem(value, "linearInterpolator.maxdistance", this.maxDistance * 1000); | |
72 this.problems = null; | |
73 } | |
74 | |
75 return Linear.linear(value, floorKey, ceilingKey, floorValue, ceilingValue); | |
76 } | |
77 } |