changeset 6153:12af732c9d0f

LinearInterpolated#apply(): Added simple test if the two datasets intersect at all.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 02 Jun 2013 18:13:29 +0200
parents 0587819960c3
children 25925b87a74b
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/LinearInterpolated.java
diffstat 1 files changed, 21 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/LinearInterpolated.java	Sun Jun 02 17:52:53 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/LinearInterpolated.java	Sun Jun 02 18:13:29 2013 +0200
@@ -176,12 +176,33 @@
         return apply(MAX, other, from, to);
     }
 
+    public boolean intersect(LinearInterpolated other) {
+        if (xs.isEmpty() || other.xs.isEmpty()) {
+            return false;
+        }
+
+        double tMax = xs.max();
+        double oMin = other.xs.min();
+        if (tMax < oMin) {
+            return false;
+        }
+
+        double tMin = xs.min();
+        double oMax = other.xs.max();
+        return !(tMin > oMax);
+    }
+
     public LinearInterpolated apply(
         Operator           operator,
         LinearInterpolated other,
         double             from,
         double             to
     ) {
+        LinearInterpolated result = new LinearInterpolated();
+        if (!intersect(other)) {
+            return result;
+        }
+
         Set<Double> points = new TreeSet<Double>(CMP);
         points.add(from);
         points.add(to);
@@ -189,7 +210,6 @@
         this .pointsInRange(from, to, points);
         other.pointsInRange(from, to, points);
 
-        LinearInterpolated result = new LinearInterpolated();
 
         for (double x: points) {
             if (!inGap(x) && !other.inGap(x)) {

http://dive4elements.wald.intevation.org