]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java
Make vertices smaller on map UI & CSV import performance improvements
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / DNEdgeBuilder.java
index 0df72c517f533f79e89dbee085b64819ec76cb9f..7ed99c363be31a83dd139534f08b71be812b33d3 100644 (file)
@@ -4,6 +4,7 @@ import java.awt.geom.Rectangle2D;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Optional;
 
 import org.simantics.databoard.Bindings;
 import org.simantics.db.Resource;
@@ -23,9 +24,16 @@ import org.simantics.district.network.DistrictNetworkUtil;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.g2d.diagram.IDiagram;
 import org.simantics.layer0.Layer0;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.index.quadtree.Quadtree;
 
 public class DNEdgeBuilder {
-    
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(DNEdgeBuilder.class);
+
     private Resource diagramResource;
     private IDiagram diagram;
     private GraphLayerManager glm;
@@ -38,26 +46,38 @@ public class DNEdgeBuilder {
         glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
     }
 
-    public static Resource create(WriteGraph graph, Resource diagramResource, double[] start, double[] end, double padding) throws DatabaseException {
+    public static Optional<Resource> create(WriteGraph graph, Resource diagramResource, double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException {
         Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
-        List<ResourceVertex> vv = new ArrayList<>(vertices.size());
+        double halfPadding = padding / 2;
+
+        Quadtree vv = new Quadtree();
         for (Resource vertex : vertices) {
-            double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
-            vv.add(new ResourceVertex(vertex, existingCoords));
+            double[] coords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
+            double x1 = coords[0] - halfPadding;
+            double y1= coords[1] - halfPadding;
+            double x2 = coords[0] + halfPadding;
+            double y2= coords[1] + halfPadding;
+            Envelope e = new Envelope(x1, x2, y1, y2);
+            vv.insert(e, new ResourceVertex(vertex, coords, false));
         }
-        return create(graph, vv, diagramResource, null, start, end, padding, false);
+        return create(graph, vv, diagramResource, null, start, startElevation, end, endElevation, padding, false);
     }
     
-    public static Resource create(WriteGraph graph, Collection<ResourceVertex> vertices, Resource diagramResource, Resource mapping, double[] start, double[] end, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException {
+    public static Optional<Resource> create(WriteGraph graph, Quadtree vertices, Resource diagramResource, Resource mapping, double[] start, double startElevation, double[] end, double endElevation, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException {
         
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+
+     // 2. Add vertices
+        Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, startElevation, padding);
+        Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, endElevation, padding);
+        if (startVertex.equals(endVertex)) {
+            LOGGER.info("Circular edges are not supported, startVertex: {}, endVertex: {}", startVertex, endVertex);
+            return Optional.empty();
+        }
         
         // 1. Get diagram edge to construct
         Resource edge = getOrCreateEdge(graph, diagramResource, mapping);
         
-        // 2. Add vertices
-        Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, padding, null);
-        Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, padding, startVertex);
         if (writeElevationToEdgeFromPoints) {
             graph.claimLiteral(edge, DN.Edge_HasElevation, calculateElevationFromVertices(graph, startVertex, endVertex), Bindings.DOUBLE);
         }
@@ -76,7 +96,7 @@ public class DNEdgeBuilder {
 //            });
 //        }
 //        
-        return edge;
+        return Optional.of(edge);
     }
     private static double calculateElevationFromVertices(WriteGraph graph, Resource startVertex, Resource endVertex) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
@@ -88,15 +108,15 @@ public class DNEdgeBuilder {
         return 0;
     }
 
-    public void create(WriteGraph graph,  double[] start, double[] end, double padding) throws DatabaseException {
+    public void create(WriteGraph graph,  double[] start, double startElevation, double[] end, double endElevation, double padding) throws DatabaseException {
         
-        Resource edge = create(graph, diagramResource, start, end, padding);
+        Optional<Resource> edge = create(graph, diagramResource, start, startElevation, end, endElevation, padding);
         // 7. Put the element on all the currently active layers if possible.
         if (glm != null) {
-            putOnActiveLayer(graph, edge);
+            putOnActiveLayer(graph, edge.get());
         }
         
-        Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);
+        Layer0Utils.addCommentMetadata(graph, "Added edge " + edge.get());
         graph.markUndoPoint();
     }
     
@@ -105,24 +125,32 @@ public class DNEdgeBuilder {
         glm.putElementOnVisibleLayers(diagram, graph, res);
     }
 
-    private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Collection<ResourceVertex> vertices, double[] coords, double padding, Resource startVertex) throws DatabaseException {
+    private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Quadtree qtree, double[] coords, double elevation, double padding) throws DatabaseException {
         Resource vertex = null;
         double halfPadding = padding / 2;
         double maxDistance = Double.MAX_VALUE;
+        double x1 = coords[0] - halfPadding;
+        double y1= coords[1] - halfPadding;
+        double x2 = coords[0] + halfPadding;
+        double y2= coords[1] + halfPadding;
+        Envelope e = new Envelope(x1, x2, y1, y2);
+        
+        List result = qtree.query(e);
+        List<ResourceVertex> vertices = (List<ResourceVertex>) result;
         for (ResourceVertex vertx : vertices) {
             Rectangle2D existing = new Rectangle2D.Double(vertx.coords[0] - halfPadding, vertx.coords[1] - halfPadding, padding, padding);
-            Rectangle2D tobecreated = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding);
+            Rectangle2D tobecreated = new Rectangle2D.Double(x1, y1, padding, padding);
             if (existing.intersects(tobecreated)) {
                 double dist = Math.sqrt((Math.pow(coords[0] - vertx.coords[0], 2) + (Math.pow(coords[1] - vertx.coords[1], 2))));
-                if (dist <= maxDistance && !vertx.vertex.equals(startVertex)) {
+                if (dist <= maxDistance) {
                     vertex = vertx.vertex;
                     maxDistance = dist;
                 }
             }
         }
         if (vertex == null) {
-            vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); 
-            vertices.add(new ResourceVertex(vertex, coords));
+            vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords, elevation); 
+            qtree.insert(e, new ResourceVertex(vertex, coords, false));
         }
         return vertex;
     }
@@ -133,12 +161,14 @@ public class DNEdgeBuilder {
 
     public static class ResourceVertex {
         
+        final boolean isConsumer;
         final Resource vertex;
         final double[] coords;
         
-        public ResourceVertex(Resource vertex, double[] coords) {
+        public ResourceVertex(Resource vertex, double[] coords, boolean isConsumer) {
             this.vertex = vertex;
             this.coords = coords;
+            this.isConsumer = isConsumer;
         }
     }
 }