changeset 431:422275fc9927

Refactored the XYColumn and Point2d code a bit to be more reusable in 3D. gnv-artifacts/trunk@479 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 23 Dec 2009 06:53:46 +0000
parents 23e9352df09b
children 6a70e8883307
files gnv-artifacts/ChangeLog gnv-artifacts/src/main/java/de/intevation/gnv/math/IJKey.java gnv-artifacts/src/main/java/de/intevation/gnv/math/Interpolation2D.java gnv-artifacts/src/main/java/de/intevation/gnv/math/Point2d.java gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java
diffstat 6 files changed, 132 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Tue Dec 22 20:45:45 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Wed Dec 23 06:53:46 2009 +0000
@@ -1,3 +1,21 @@
+2009-12-23	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/gnv/math/XYColumn.java: Extends from
+	  Point2d now to make some of the code (x/y sorting, envelope, etc.)
+	  reusable in 3D from 2D code.
+
+	* src/main/java/de/intevation/gnv/math/IJKey.java: New: The (i, j) hashing
+	  is now moved to this class because of the new inheritance of XYColumn.
+
+	* src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java: 
+	  Adjusted the (i, j) hashing. 
+
+	* src/main/java/de/intevation/gnv/math/Point2d.java: Added a new constructor
+	  to make it easier to extend from it.
+
+	* src/main/java/de/intevation/gnv/math/Interpolation2D.java: Made the buffer
+	  extend calculation a static method to be reusable in 3D case.
+
 2009-12-22	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* doc/conf/conf.xml: Load salinity palette correctly.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/IJKey.java	Wed Dec 23 06:53:46 2009 +0000
@@ -0,0 +1,31 @@
+package de.intevation.gnv.math;
+
+import java.io.Serializable;
+
+/**
+ * @author Sascha L. Teichmann <sascha.teichmann@intevation.de>
+ */
+public class IJKey
+implements   Serializable
+{
+    public int i;
+    public int j;
+
+    public IJKey() {
+    }
+
+    public IJKey(int i, int j) {
+        this.i = i;
+        this.j = j;
+    }
+
+    public int hashCode() {
+        return (i << 16) | j;
+    }
+
+    public boolean equals(Object obj) {
+        IJKey other = (IJKey)obj;
+        return i == other.i && j == other.j;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/math/Interpolation2D.java	Tue Dec 22 20:45:45 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/Interpolation2D.java	Wed Dec 23 06:53:46 2009 +0000
@@ -26,6 +26,62 @@
     private Interpolation2D() {
     }
 
+    public static final double [] calculateBuffer(
+        List <? extends Point2d> points
+    ) {
+        HashMap<Integer, ArrayList<Point2d>> iMap = 
+            new HashMap<Integer, ArrayList<Point2d>>();
+
+        HashMap<Integer, ArrayList<Point2d>> jMap = 
+            new HashMap<Integer, ArrayList<Point2d>>();
+
+        for (int k = points.size()-1; k >= 0; --k) {
+            Point2d p = points.get(k);
+
+            ArrayList<Point2d> jList = jMap.get(p.j);
+            ArrayList<Point2d> iList = jMap.get(p.i);
+
+            if (jList == null) {
+                iMap.put(p.j, jList = new ArrayList<Point2d>());
+            }
+            jList.add(p);
+
+            if (iList == null) {
+                jMap.put(p.i, iList = new ArrayList<Point2d>());
+            }
+            iList.add(p);
+        }
+
+        double dxMax = -Double.MAX_VALUE;
+        double dyMax = -Double.MAX_VALUE;
+
+        for (ArrayList<Point2d> v: jMap.values()) {
+            Collections.sort(v, Point2d.Y_COMPARATOR);
+            for (int i = 1, L = v.size(); i < L; ++i) {
+                double dy = Math.abs(v.get(i).x - v.get(i-1).x);
+                if (dy > dyMax) {
+                    dyMax = dy;
+                }
+            }
+        }
+
+        dyMax += 1e-5d;
+
+        for (ArrayList<Point2d> v: iMap.values()) {
+            Collections.sort(v, Point2d.X_COMPARATOR);
+            for (int i = 1, L = v.size(); i < L; ++i) {
+                double dx = Math.abs(v.get(i).x - v.get(i-1).x);
+                if (dx > dxMax) {
+                    dxMax = dx;
+                }
+            }
+        }
+
+        dxMax += 1e-5d;
+
+        return new double [] { dxMax, dyMax };
+    }
+
     public static void interpolate(
         List<? extends Coordinate> path,
         List<? extends Point2d>    points,
@@ -47,61 +103,9 @@
             return;
         }
 
-        HashMap<Integer, ArrayList<Point2d>> map = new HashMap<Integer, ArrayList<Point2d>>();
-
-        for (int k = M-1; k >= 0; --k) {
-            Point2d p = points.get(k);
-
-            ArrayList<Point2d> list = map.get(p.j);
-
-            if (list == null) {
-                map.put(p.j, list = new ArrayList<Point2d>());
-            }
-            list.add(p);
-        }
-
-        double dxMax = -Double.MAX_VALUE;
-
-        for (ArrayList<Point2d> v: map.values()) {
-            Collections.sort(v, Point2d.X_COMPARATOR);
-            for (int i = 1, L = v.size(); i < L; ++i) {
-                double dx = Math.abs(v.get(i).x - v.get(i-1).x);
-                if (dx > dxMax) {
-                    dxMax = dx;
-                }
-            }
-        }
-
-        dxMax += 1e-5d;
-
-        map.clear();
-
-        for (int k = M-1; k >= 0; --k) {
-            Point2d p = points.get(k);
-
-            ArrayList<Point2d> list = map.get(p.i);
-
-            if (list == null) {
-                map.put(p.i, list = new ArrayList<Point2d>());
-            }
-            list.add(p);
-        }
-
-        double dyMax = -Double.MAX_VALUE;
-
-        for (ArrayList<Point2d> v: map.values()) {
-            Collections.sort(v, Point2d.Y_COMPARATOR);
-            for (int i = 1, L = v.size(); i < L; ++i) {
-                double dy = Math.abs(v.get(i).y - v.get(i-1).y);
-                if (dy > dyMax) {
-                    dyMax = dy;
-                }
-            }
-        }
-
-        dyMax += 1e-5d;
-
-        map = null;
+        double [] buffer = calculateBuffer(points);
+        double dxMax = buffer[0];
+        double dyMax = buffer[1];
 
         if (log.isDebugEnabled()) {
             log.debug("buffer size: " + dxMax + " / " + dyMax);
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/math/Point2d.java	Tue Dec 22 20:45:45 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/Point2d.java	Wed Dec 23 06:53:46 2009 +0000
@@ -59,6 +59,12 @@
     public Point2d() {
     }
 
+    public Point2d(double x, double y, int i, int j) {
+        super(x, y);
+        this.i = i;
+        this.j = j;
+    }
+
     public Point2d(double x, double y, double z, int i, int j) {
         super(x, y, z);
         this.i = i;
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java	Tue Dec 22 20:45:45 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java	Wed Dec 23 06:53:46 2009 +0000
@@ -1,32 +1,24 @@
 package de.intevation.gnv.math;
 
-import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
+ * @author Sascha L. Teichmann <sascha.teichmann@intevation.de>
  */
 public class XYColumn
-implements   Serializable
+extends      Point2d
 {
+    protected List values;
 
-    private double x;
-    private double y;
-
-    private int    i;
-    private int    j;
-
-    private List   values;
+    public XYColumn() {
+    }
 
     public XYColumn(double x, double y, int i, int j) {
-        this.x = x;
-        this.y = y;
-        this.i = i;
-        this.j = j;
+        super(x, y, i, j);
     }
 
-
     public void add(HeightValue value) {
         if (values == null) {
             values = new ArrayList();
@@ -34,20 +26,8 @@
         values.add(value);
     }
 
-
     public HeightValue[] getValues() {
         return (HeightValue[]) values.toArray(new HeightValue[values.size()]);
     }
-
-
-    public int hashCode() {
-        return (i << 16) | j;
-    }
-
-
-    public boolean equals(Object obj) {
-        XYColumn other = (XYColumn) obj;
-
-        return i == other.i && j == other.j && x == other.x && y == other.y;
-    }
 }
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java	Tue Dec 22 20:45:45 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/profile/verticalcrosssection/VerticalCrossSectionOutputState.java	Wed Dec 23 06:53:46 2009 +0000
@@ -40,9 +40,12 @@
 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
+
 import de.intevation.gnv.math.AttributedXYColumns;
 import de.intevation.gnv.math.HeightValue;
 import de.intevation.gnv.math.XYColumn;
+import de.intevation.gnv.math.IJKey;
+
 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
 import de.intevation.gnv.state.exception.StateException;
 import de.intevation.gnv.state.timeseries.TimeSeriesOutputState;
@@ -156,7 +159,7 @@
 
     protected AttributedXYColumns preProcess(Collection results) {
         AttributedXYColumns attColumns = new AttributedXYColumns();
-        Map        map                 = new HashMap();
+        Map        map                 = new HashMap(1013);
         Iterator   iter                = results.iterator();
 
         int sIdx = -1;
@@ -200,14 +203,16 @@
                 int k        = result.getInteger(kIdx);
                 int z        = result.getInteger(zIdx);
 
-                XYColumn col = new XYColumn(point.getX(), point.getY(), i, j);
-                XYColumn old = (XYColumn)map.get(col);
+                IJKey key = new IJKey(i, j);
 
-                if (old == null) {
-                    map.put(old = col, col);
+                XYColumn col = (XYColumn)map.get(key);
+
+                if (col == null) {
+                    col = new XYColumn(point.getX(), point.getY(), i, j);
+                    map.put(key, col);
                 }
 
-                old.add(new HeightValue(z, v, k));
+                col.add(new HeightValue(z, v, k));
             }
             catch (ParseException pe) {
                 log.warn("Error while parsing geometry.", pe);

http://dive4elements.wald.intevation.org