diff gnv-artifacts/src/main/java/de/intevation/gnv/math/Interpolation2D.java @ 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 04a242c67fe6
children 0eed5749fd63
line wrap: on
line diff
--- 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);

http://dive4elements.wald.intevation.org