]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java
Make optional to calculate elevation for edge during CSV import
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / DNEdgeBuilder.java
1 package org.simantics.district.network.ui;
2
3 import java.awt.geom.Rectangle2D;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.List;
7
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.common.request.ObjectsWithType;
12 import org.simantics.db.exception.BindingException;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
15 import org.simantics.db.exception.ServiceException;
16 import org.simantics.db.layer0.util.Layer0Utils;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
19 import org.simantics.diagram.synchronization.SynchronizationHints;
20 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
21 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
22 import org.simantics.district.network.DistrictNetworkUtil;
23 import org.simantics.district.network.ontology.DistrictNetworkResource;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.layer0.Layer0;
26
27 public class DNEdgeBuilder {
28     
29     private Resource diagramResource;
30     private IDiagram diagram;
31     private GraphLayerManager glm;
32
33     public DNEdgeBuilder(Resource diagramResource, IDiagram diagram) {
34         this.diagramResource = diagramResource;
35         this.diagram = diagram;
36         
37         IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
38         glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
39     }
40
41     public static Resource create(WriteGraph graph, Resource diagramResource, double[] start, double[] end, double padding) throws DatabaseException {
42         Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
43         List<ResourceVertex> vv = new ArrayList<>(vertices.size());
44         for (Resource vertex : vertices) {
45             double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
46             vv.add(new ResourceVertex(vertex, existingCoords));
47         }
48         return create(graph, vv, diagramResource, null, start, end, padding, false);
49     }
50     
51     public static Resource create(WriteGraph graph, Collection<ResourceVertex> vertices, Resource diagramResource, Resource mapping, double[] start, double[] end, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException {
52         
53         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
54         
55         // 1. Get diagram edge to construct
56         Resource edge = getOrCreateEdge(graph, diagramResource, mapping);
57         
58         // 2. Add vertices
59         Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, padding, null);
60         Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, padding, startVertex);
61         if (writeElevationToEdgeFromPoints) {
62             graph.claimLiteral(edge, DN.Edge_HasElevation, calculateElevationFromVertices(graph, startVertex, endVertex), Bindings.DOUBLE);
63         }
64         
65         graph.claim(edge, DN.HasStartVertex, startVertex);
66         graph.claim(edge, DN.HasEndVertex, endVertex);
67         
68         // We need to put GraphLayer to newLayers so...
69 //        for (Resource layer : graph.getObjects(diagramResource, DiagramResource.getInstance(graph).HasLayer)) {
70 //            IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
71 //            
72 //            GraphLayer gl = layerUtil.loadLayer(graph, layer);
73 //            gl.forEachTag(tag -> {
74 //                DiagramGraphUtil.tag(graph, startVertex, tag, true);
75 //                DiagramGraphUtil.tag(graph, endVertex, tag, true);
76 //            });
77 //        }
78 //        
79         return edge;
80     }
81     private static double calculateElevationFromVertices(WriteGraph graph, Resource startVertex, Resource endVertex) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
82         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
83         Double startElevation = graph.getPossibleRelatedValue(startVertex, DN.Vertex_HasElevation, Bindings.DOUBLE);
84         Double endElevation = graph.getPossibleRelatedValue(endVertex, DN.Vertex_HasElevation, Bindings.DOUBLE);
85         if (startElevation != null && endElevation != null) {
86             return (startElevation + endElevation) / 2;
87         }
88         return 0;
89     }
90
91     public void create(WriteGraph graph,  double[] start, double[] end, double padding) throws DatabaseException {
92         
93         Resource edge = create(graph, diagramResource, start, end, padding);
94         // 7. Put the element on all the currently active layers if possible.
95         if (glm != null) {
96             putOnActiveLayer(graph, edge);
97         }
98         
99         Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);
100         graph.markUndoPoint();
101     }
102     
103     private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {
104         glm.removeFromAllLayers(graph, res);
105         glm.putElementOnVisibleLayers(diagram, graph, res);
106     }
107
108     private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Collection<ResourceVertex> vertices, double[] coords, double padding, Resource startVertex) throws DatabaseException {
109         Resource vertex = null;
110         double halfPadding = padding / 2;
111         double maxDistance = Double.MAX_VALUE;
112         for (ResourceVertex vertx : vertices) {
113             Rectangle2D existing = new Rectangle2D.Double(vertx.coords[0] - halfPadding, vertx.coords[1] - halfPadding, padding, padding);
114             Rectangle2D tobecreated = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding);
115             if (existing.intersects(tobecreated)) {
116                 double dist = Math.sqrt((Math.pow(coords[0] - vertx.coords[0], 2) + (Math.pow(coords[1] - vertx.coords[1], 2))));
117                 if (dist <= maxDistance && !vertx.vertex.equals(startVertex)) {
118                     vertex = vertx.vertex;
119                     maxDistance = dist;
120                 }
121             }
122         }
123         if (vertex == null) {
124             vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); 
125             vertices.add(new ResourceVertex(vertex, coords));
126         }
127         return vertex;
128     }
129
130     private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping) throws DatabaseException {
131         return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping);
132     }
133
134     public static class ResourceVertex {
135         
136         final Resource vertex;
137         final double[] coords;
138         
139         public ResourceVertex(Resource vertex, double[] coords) {
140             this.vertex = vertex;
141             this.coords = coords;
142         }
143     }
144 }