Ordering of RouteGraphs RouteLines is nondeterministic 52/652/1
authorjsimomaa <jani.simomaa@gmail.com>
Sat, 24 Jun 2017 19:30:24 +0000 (22:30 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Sat, 24 Jun 2017 19:30:24 +0000 (22:30 +0300)
refs #7320

Change-Id: Ibac54bc6d16578e812542360573cb0836db7deab

bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/RouteGraph.java

index feaefeb69d15a251e46f8b2a7eb31dfd4460206b..62d3de68fae5f110a6b08eb80dcc7558ca34df80 100644 (file)
@@ -24,7 +24,10 @@ import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Iterator;
+import java.util.Map;
+import java.util.TreeMap;
 
 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
 import org.simantics.diagram.connection.rendering.arrows.PlainLineEndStyle;
@@ -129,7 +132,7 @@ public class RouteGraph implements Serializable {
             double minX, double minY,
             double maxX, double maxY,
             ILineEndStyle style) {
-       return addBigTerminal(minX, minY, maxX, maxY, style, null);
+        return addBigTerminal(minX, minY, maxX, maxY, style, null);
     }
     public RouteTerminal addBigTerminal( 
             double minX, double minY,
@@ -509,9 +512,9 @@ public class RouteGraph implements Serializable {
         for(RouteTerminal terminal : terminals)
             if((terminal.getAllowedDirections()&0x10) != 0) {     
                 try {
-                       RouteLine line = terminal.getLine();
-                       if(line == null)
-                               continue;
+                    RouteLine line = terminal.getLine();
+                    if(line == null)
+                        continue;
                     RoutePoint b = line.getBegin();
                     double distSq = Line2D.ptSegDistSq(terminal.x, terminal.y, b.x, b.y, x, y); 
                     if(distSq <= toleranceSq) {
@@ -642,7 +645,7 @@ public class RouteGraph implements Serializable {
         if((mask & PICK_POINTS) != 0) {
             Object point = pickPoint(x, y, tolerance, mask);
             if(point != null)
-               return point;
+                return point;
         }
         /*if((mask & PICK_DIRECT_LINES) != 0) {
             RouteTerminal terminal = pickDirectLine(x, y, tolerance);
@@ -917,12 +920,12 @@ public class RouteGraph implements Serializable {
              * 
              * Update 14.2.2014: With Sulca, there is a constant issue when line.terminal == null but point is RouteTerminal. 
              */
-               if (point instanceof RouteLink) {
-                   RouteLine l = ((RouteLink)point).getOther(line);
-                   l.points.remove(point);
-                   if(!l.isTransient())
-                       nLines.add(l);
-               }
+            if (point instanceof RouteLink) {
+                RouteLine l = ((RouteLink)point).getOther(line);
+                l.points.remove(point);
+                if(!l.isTransient())
+                    nLines.add(l);
+            }
         }
         
         if(nLines.isEmpty())
@@ -936,10 +939,10 @@ public class RouteGraph implements Serializable {
                  * 
                  * Update 16.10.2014: With Sulca, there is a constant issue when line.terminal == null but point is RouteTerminal. 
                  */
-               if (point instanceof RouteLink) {
-                       RouteLink link = (RouteLink)point;
-                       link.replace(l, merged);
-               }
+                if (point instanceof RouteLink) {
+                    RouteLink link = (RouteLink)point;
+                    link.replace(l, merged);
+                }
             }
         }
         THashSet<RouteLine> removedLines = new THashSet<RouteLine>();
@@ -1345,6 +1348,18 @@ public class RouteGraph implements Serializable {
         path.moveTo(x, y);
     }
     
+    private static final Comparator<RoutePoint> RG_COMP = (o1, o2) -> {
+        if (o1.getX() < o2.getX())
+            return -1;
+        else if (o1.getX() > o2.getX())
+            return 1;
+        if (o1.getY() < o2.getY())
+            return -1;
+        else if (o1.getY() > o2.getY())
+            return 1;
+        return 0;
+    };
+    
     private static void addPathEnd(Path2D path, RoutePoint cur, RouteLine line) {
         double x = cur.x, y = cur.y;
         if(cur instanceof RouteTerminal) {
@@ -1382,8 +1397,9 @@ public class RouteGraph implements Serializable {
         }
 
         // Analyze graph
-        THashMap<RoutePoint, RouteLine> begins = 
-                new THashMap<RoutePoint, RouteLine>();
+        Map<RoutePoint, RouteLine> begins = 
+//                new THashMap<RoutePoint, RouteLine>(); //The ordering of the coordinates in the path should be deterministic between scenegraphs
+                new TreeMap<RoutePoint, RouteLine>(RG_COMP);
         for(RouteLine line : lines) {
             add(begins, line);
         }
@@ -1403,7 +1419,7 @@ public class RouteGraph implements Serializable {
         }
     }
     
-    private void drawContinuousPath(Path2D path, THashMap<RoutePoint, RouteLine> begins,
+    private void drawContinuousPath(Path2D path, Map<RoutePoint, RouteLine> begins,
             RoutePoint cur, RouteLine curLine) {
         if(curLine == null)
             return;
@@ -1432,7 +1448,7 @@ public class RouteGraph implements Serializable {
         }
     }
 
-    private static void add(THashMap<RoutePoint, RouteLine> begins,
+    private static void add(Map<RoutePoint, RouteLine> begins,
             RouteLine line) { 
         if(line.points.size() > 1) {
             {
@@ -1558,8 +1574,8 @@ public class RouteGraph implements Serializable {
     }
     
     public void reclaimTransientMemory() {
-       removeTransientRouteLines();
-       needsUpdate = true;
+        removeTransientRouteLines();
+        needsUpdate = true;
     }
 
 }