changeset 2611:62e5c6fd2a0c

Join nodes in Q tree if they span a continuous interval and have same Qs. flys-artifacts/trunk@4193 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 03 Apr 2012 17:32:58 +0000
parents 3c907758f0ab
children 49cfa5c66651
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java
diffstat 1 files changed, 33 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java	Tue Apr 03 16:51:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/QRangeTree.java	Tue Apr 03 17:32:58 2012 +0000
@@ -3,6 +3,7 @@
 import java.io.Serializable;
 
 import java.util.List;
+import java.util.ArrayList;
 
 import org.apache.log4j.Logger;
 
@@ -11,6 +12,8 @@
 {
     private static Logger log = Logger.getLogger(QRangeTree.class);
 
+    public static final double EPSILON = 1e-4;
+
     public static class Node
     implements          Serializable
     {
@@ -139,45 +142,60 @@
         int             start,
         int             stop
     ) {
-
         if (stop <= start) {
             return;
         }
 
-        Node [] nodes = new Node[stop-start];
-        for (int i = 0; i < nodes.length; ++i) {
+        int N = stop-start;
+
+        List<Node> nodes = new ArrayList<Node>(N);
+
+        Node last = null;
+
+        for (int i = 0; i < N; ++i) {
             Object [] qRange = qRanges.get(start + i);
             Double q = accessQAB.getQ(qRange);
             Double a = accessQAB.getA(qRange);
             Double b = accessQAB.getB(qRange);
 
-            nodes[i] = new Node(
-                a != null ? a.doubleValue() : -Double.MAX_VALUE,
-                b != null ? b.doubleValue() :  Double.MAX_VALUE,
-                q.doubleValue());
+            double av = a != null ? a.doubleValue() : -Double.MAX_VALUE;
+            double bv = b != null ? b.doubleValue() :  Double.MAX_VALUE;
+            double qv = q.doubleValue();
+
+            // If nodes are directly neighbored and Qs are the same
+            // join them.
+            if (last != null
+            && Math.abs(last.b - av) < EPSILON
+            && Math.abs(last.q - qv) < EPSILON) {
+                last.b = bv;
+            }
+            else {
+                nodes.add(last = new Node(av, bv, qv));
+            }
         }
 
         root = wireTree(nodes);
     }
 
-    protected static Node wireTree(Node [] nodes) {
-        for (int i = 0; i < nodes.length; ++i) {
-            Node node = nodes[i];
-            if (i > 0             ) node.prev = nodes[i-1];
-            if (i < nodes.length-1) node.next = nodes[i+1];
+    protected static Node wireTree(List<Node> nodes) {
+        int N = nodes.size();
+        for (int i = 0; i < N; ++i) {
+            Node node = nodes.get(i);
+            if (i > 0  ) node.prev = nodes.get(i-1);
+            if (i < N-1) node.next = nodes.get(i+1);
         }
 
-        return buildTree(nodes, 0, nodes.length-1);
+        return buildTree(nodes, 0, N-1);
     }
 
-    protected static Node buildTree(Node [] nodes, int lo, int hi) {
+    protected static Node buildTree(List<Node> nodes, int lo, int hi) {
 
         if (lo > hi) {
             return null;
         }
 
         int mid = (lo + hi) >> 1;
-        Node parent = nodes[mid];
+        Node parent = nodes.get(mid);
 
         parent.left  = buildTree(nodes, lo, mid-1);
         parent.right = buildTree(nodes, mid+1, hi);

http://dive4elements.wald.intevation.org