diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java @ 655:913b52064449

Refactored version of "Berechnung 4" flys-artifacts/trunk@2053 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 05 Jun 2011 18:24:46 +0000
parents 44175d4720f8
children c501f27c1f71 3dc61e00385e
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Fri Jun 03 10:21:13 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WstValueTable.java	Sun Jun 05 18:24:46 2011 +0000
@@ -2,8 +2,8 @@
 
 import java.io.Serializable;
 
-
-import de.intevation.flys.artifacts.math.LinearRemap;
+import de.intevation.flys.artifacts.math.Linear;
+import de.intevation.flys.artifacts.math.Function;
 
 import java.util.Arrays;
 import java.util.ArrayList;
@@ -109,7 +109,7 @@
             double []     ows,
             WstValueTable table
         ) {
-            double kmWeight = factor(km, this.km, other.km);
+            double kmWeight = Linear.factor(km, this.km, other.km);
 
             QPosition qPosition = new QPosition();
 
@@ -117,7 +117,7 @@
                 if (table.getQPosition(km, iqs[i], qPosition) != null) {
                     double wt =       getW(qPosition);
                     double wo = other.getW(qPosition);
-                    ows[i] = weight(kmWeight, wt, wo);
+                    ows[i] = Linear.weight(kmWeight, wt, wo);
                 }
                 else {
                     ows[i] = Double.NaN;
@@ -137,7 +137,7 @@
                 return new double[2][0];
             }
 
-            double factor = factor(km, this.km, other.km);
+            double factor = Linear.factor(km, this.km, other.km);
 
             double [] splineQ = new double[W];
             double [] splineW = new double[W];
@@ -146,8 +146,8 @@
             double maxQ = -Double.MAX_VALUE;
 
             for (int i = 0; i < W; ++i) {
-                double wws = weight(factor, ws[i], other.ws[i]);
-                double wqs = weight(
+                double wws = Linear.weight(factor, ws[i], other.ws[i]);
+                double wqs = Linear.weight(
                     factor,
                     table.getQIndex(i, km),
                     table.getQIndex(i, other.km));
@@ -231,7 +231,7 @@
 
             return weight == 1.0
                 ? ws[index]
-                : weight(weight, ws[index-1], ws[index]);
+                : Linear.weight(weight, ws[index-1], ws[index]);
         }
 
         public double getW(
@@ -239,7 +239,7 @@
             double    km,
             QPosition qPosition
         ) {
-            double kmWeight = factor(km, this.km, other.km);
+            double kmWeight = Linear.factor(km, this.km, other.km);
 
             int    index  = qPosition.index;
             double weight = qPosition.weight;
@@ -251,11 +251,11 @@
                 ow = other.ws[index];
             }
             else {
-                tw = weight(weight, ws[index-1], ws[index]); 
-                ow = weight(weight, other.ws[index-1], other.ws[index]); 
+                tw = Linear.weight(weight, ws[index-1], ws[index]); 
+                ow = Linear.weight(weight, other.ws[index-1], other.ws[index]); 
             }
 
-            return weight(kmWeight, tw, ow);
+            return Linear.weight(kmWeight, tw, ow);
         }
     } // class Row
 
@@ -338,60 +338,45 @@
         return r1.interpolateWQ(r2, km, steps, this);
     }
 
-    public void interpolate(
-        double []   kms,
-        double []   ws,
-        double []   qs,
-        QPosition   qPosition,
-        LinearRemap remap
-    ) {
-        interpolate(kms, ws, qs, qPosition, remap, 0, kms.length);
-    }
-
-    public void interpolate(
-        double []   kms,
-        double []   ws,
-        double []   qs,
-        QPosition   qPosition,
-        LinearRemap remap,
-        int         startIndex,
-        int         length
+    public boolean interpolate(
+        double    km,
+        double [] out,
+        QPosition qPosition,
+        Function  qFunction
     ) {
         int R1 = rows.size()-1;
 
-        Row kmKey = new Row();
+        out[1] = qFunction.value(getQ(qPosition, km));
+
+        if (Double.isNaN(out[1])) {
+            return false;
+        }
 
         QPosition nPosition = new QPosition();
-
-        for (int i = startIndex, end = startIndex+length; i < end; ++i) {
-            kmKey.km = kms[i];
-
-            qs[i] = remap.eval(kms[i], getQ(qPosition, kms[i]));
-
-            if (getQPosition(kms[i], qs[i], nPosition) == null) {
-                ws[i] = Double.NaN;
-                continue;
-            }
-
-            int rowIndex = Collections.binarySearch(rows, kmKey);
+        if (getQPosition(km, out[1], nPosition) == null) {
+            return false;
+        }
 
-            if (rowIndex >= 0) {
-                // direct row match
-                ws[i] = rows.get(rowIndex).getW(nPosition);
-                continue;
-            }
-
-            rowIndex = -rowIndex -1;
+        int rowIndex = Collections.binarySearch(rows, new Row(km));
 
-            if (rowIndex < 1 || rowIndex >= R1) {
-                // do not extrapolate
-                ws[i] = Double.NaN;
-                continue;
-            }
-            Row r1 = rows.get(rowIndex-1);
-            Row r2 = rows.get(rowIndex);
-            ws[i] = r1.getW(r2, kms[i], nPosition);
+        if (rowIndex >= 0) {
+            // direct row match
+            out[0] = rows.get(rowIndex).getW(nPosition);
+            return !Double.isNaN(out[0]);
         }
+
+        rowIndex = -rowIndex -1;
+
+        if (rowIndex < 1 || rowIndex >= R1) {
+            // do not extrapolate
+            return false;
+        }
+
+        Row r1 = rows.get(rowIndex-1);
+        Row r2 = rows.get(rowIndex);
+        out[0] = r1.getW(r2, km, nPosition);
+
+        return !Double.isNaN(out[0]);
     }
 
     public QPosition interpolate(
@@ -480,7 +465,7 @@
             else                  { qMin = qCurrent; qMax = qLast; }
 
             if (q > qMin && q < qMax) {
-                double weight = factor(q, qLast, qCurrent);
+                double weight = Linear.factor(q, qLast, qCurrent);
                 return qPosition.set(i, weight);
             }
             qLast = qCurrent;
@@ -502,41 +487,8 @@
         }
         double q1 = columns[index-1].getQRangeTree().findQ(km);
         double q2 = columns[index  ].getQRangeTree().findQ(km);
-        return weight(weight, q1, q2);
+        return Linear.weight(weight, q1, q2);
     }
 
-    public static final double linear(
-        double x,
-        double x1, double x2,
-        double y1, double y2
-    ) {
-        // y1 = m*x1 + b
-        // y2 = m*x2 + b
-        // y2 - y1 = m*(x2 - x1)
-        // m = (y2 - y1)/(x2 - x1) # x2 != x1
-        // b = y1 - m*x1
-
-        if (x1 == x2) {
-            return 0.5*(y1 + y2);
-        }
-        double m = (y2 - y1)/(x2 - x1);
-        double b = y1 - m*x1;
-        return x*m + b;
-    }
-
-    public static final double factor(double x, double p1, double p2) {
-        // 0 = m*p1 + b <=> b = -m*p1
-        // 1 = m*p2 + b
-        // 1 = m*(p2 - p1)
-        // m = 1/(p2 - p1) # p1 != p2
-        // f(x) = x/(p2-p1) - p1/(p2-p1) <=> (x-p1)/(p2-p1)
-
-        return p1 == p2 ? 0.0 : (x-p1)/(p2-p1);
-    }
-
-    public static final double weight(double factor, double a, double b) {
-        //return (1.0-factor)*a + factor*b;
-        return a + factor*(b-a);
-    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org