]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Add edge geometry for detailed rendering in closer zoom levels 54/2754/1
authorjsimomaa <jani.simomaa@gmail.com>
Wed, 6 Mar 2019 19:36:19 +0000 (21:36 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Wed, 6 Mar 2019 19:36:19 +0000 (21:36 +0200)
gitlab #35

Change-Id: I6930dc2b8225647b61beeee0dfa3408e8d1f70c6

org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java
org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java
org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java
org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph
org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java

index 7c660b4c70c9df6605bce156f87a2f041438eba2..93646ded2022e92c9f213ab59054d3c0a60166e2 100644 (file)
@@ -71,6 +71,7 @@ public class CSVImportModel {
     private int valvePositionIndx = -1;
     private int addressIndex;
     private int lengthIndex;
+    private int detailedGeometryIndex;
     
     // Third page
 
@@ -491,4 +492,12 @@ public class CSVImportModel {
     public void setLengthIndex(int lengthIndex) {
         this.lengthIndex = lengthIndex;
     }
+
+    public void detailedGeometryIndex(int detailedGeometryIndex) {
+        this.detailedGeometryIndex = detailedGeometryIndex;
+    }
+    
+    public int getDetailedGeometryIndex() {
+        return detailedGeometryIndex;
+    }
 }
index 54a578ddac5a837d884de0d38a1ca20c4890a6ed..8c087210400459bba772a717fc3c2ec2620bfcb1 100644 (file)
@@ -108,6 +108,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
                         int kReturnIndex = model.getkReturnIndex();
                         int kSupplyIndex = model.getkSupplyIndex();
                         int lengthIndex = model.getLengthIndex();
+                        int detailedGeometryIndex = model.getDetailedGeometryIndex();
                         
                         int mappingColumn = model.getComponentMappingIndex();
                         int idColumn = model.getIdIndex();
@@ -250,6 +251,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
                                             writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
                                             writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
                                             writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength);
+                                            writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform);
                                         }
                                     } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
                                         throw new DatabaseException(e);
@@ -307,4 +309,41 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
             }
         }
     }
+
+    private static void writeDoubleArrayFromString(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation, MathTransform actualTransform) throws DatabaseException, MismatchedDimensionException, TransformException {
+        if (index != -1) {
+            String stringValue = row.get(index);
+            if (!stringValue.isEmpty()) {
+                stringValue = stringValue.substring(1, stringValue.length() - 1);
+                String[] coordPairs = stringValue.split(";");
+                ArrayList<Double> dd = new ArrayList<>(coordPairs.length * 2);
+                for (int i = 0; i < coordPairs.length; i++) {
+                    String coordPair = coordPairs[i];
+                    String[] p = coordPair.split(" ");
+                    double x = Double.parseDouble(p[0]);
+                    double y = Double.parseDouble(p[1]);
+                    if (actualTransform != null) {
+                        DirectPosition2D targetPos = new DirectPosition2D();
+                        DirectPosition2D sourcePos = new DirectPosition2D(y, x);
+                        DirectPosition res = actualTransform.transform(sourcePos, targetPos);
+                        double[] coords = res.getCoordinate();
+                        x = coords[1];
+                        y = coords[0];
+                    }
+                    dd.add(x);
+                    dd.add(y);
+                }
+                double[] detailedGeometryCoords = new double[dd.size()];
+                for (int i = 0; i < dd.size(); i++) {
+                    double d = dd.get(i);
+                    detailedGeometryCoords[i] = d;
+                }
+                try {
+                    graph.claimLiteral(subject, relation, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
+                } catch (NumberFormatException e) {
+                    throw new DatabaseException(e);
+                }
+            }
+        }
+    }
 }
index b7e46475dcec4f00c2b4069476de5084364b8b4e..552f7e60878e5dfb00905e1281181349f41398c9 100644 (file)
@@ -87,6 +87,7 @@ public class CSVImportWizardPage extends WizardPage {
     private DynamicComboFieldEditor endXCoordSelector;
     private DynamicComboFieldEditor endYCoordSelector;
     private DynamicComboFieldEditor endZValueSelector;
+    private DynamicComboFieldEditor detailedGeometrySelector;
 
     private Text edgeConnectionPadding;
     
@@ -750,6 +751,20 @@ public class CSVImportWizardPage extends WizardPage {
                 validatePageComplete();
             }
         });
+        detailedGeometrySelector = new DynamicComboFieldEditor("detailedGeometryValue", "Geometry", parent);
+        detailedGeometrySelector.addComboListener(new SelectionListener() {
+
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                widgetDefaultSelected(e);
+            }
+
+            @Override
+            public void widgetDefaultSelected(SelectionEvent e) {
+                model.detailedGeometryIndex(Integer.parseInt(detailedGeometrySelector.getValue()));
+                validatePageComplete();
+            }
+        });
         diameterSelector = new DynamicComboFieldEditor("diameterValue", "Diameter value", parent);
         diameterSelector.addComboListener(new SelectionListener() {
 
@@ -895,6 +910,7 @@ public class CSVImportWizardPage extends WizardPage {
         endYCoordSelector.updateCombo(namesAndValues);
         startZValueSelector.updateCombo(namesAndValues);
         endZValueSelector.updateCombo(namesAndValues);
+        detailedGeometrySelector.updateCombo(namesAndValues);
         diameterSelector.updateCombo(namesAndValues);
         outerDiameterSelector.updateCombo(namesAndValues);
         nominalMassFlowSelector.updateCombo(namesAndValues);
index c5195291285a76b7c0d4aaed51e452090dd69637..d9ab6d6e842a8fe53c45ddfb6f977ae4c9c694c0 100644 (file)
@@ -85,6 +85,8 @@ DN.Edge <T DN.Element
     >-- DN.Edge.HasLength 
         @defProperty "Length" L0.Double
         L0.readOnly true
+    >-- DN.Edge.HasGeometry
+        @defProperty "Detailed Geometry" L0.DoubleArray
     >-- DN.Edge.HasDiameter
         @defProperty "Diameter" L0.Double
     >-- DN.Edge.HasOuterDiameter
index e853952822a4d76333bb15b942db55f816976e2f..c2b154373ea4c88097f681e308ff2ce435982f7b 100644 (file)
@@ -65,6 +65,8 @@ public class DistrictNetworkResource {
     public final Resource Edge_HasElevation_Inverse;
     public final Resource Edge_HasFlowArea;
     public final Resource Edge_HasFlowArea_Inverse;
+    public final Resource Edge_HasGeometry;
+    public final Resource Edge_HasGeometry_Inverse;
     public final Resource Edge_HasKReturn;
     public final Resource Edge_HasKReturn_Inverse;
     public final Resource Edge_HasKSupply;
@@ -341,6 +343,8 @@ public class DistrictNetworkResource {
         public static final String Edge_HasElevation_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasElevation/Inverse";
         public static final String Edge_HasFlowArea = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasFlowArea";
         public static final String Edge_HasFlowArea_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasFlowArea/Inverse";
+        public static final String Edge_HasGeometry = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasGeometry";
+        public static final String Edge_HasGeometry_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasGeometry/Inverse";
         public static final String Edge_HasKReturn = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasKReturn";
         public static final String Edge_HasKReturn_Inverse = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasKReturn/Inverse";
         public static final String Edge_HasKSupply = "http://www.simantics.org/DistrictNetwork-1.0/Edge/HasKSupply";
@@ -627,6 +631,8 @@ public class DistrictNetworkResource {
         Edge_HasElevation_Inverse = getResourceOrNull(graph, URIs.Edge_HasElevation_Inverse);
         Edge_HasFlowArea = getResourceOrNull(graph, URIs.Edge_HasFlowArea);
         Edge_HasFlowArea_Inverse = getResourceOrNull(graph, URIs.Edge_HasFlowArea_Inverse);
+        Edge_HasGeometry = getResourceOrNull(graph, URIs.Edge_HasGeometry);
+        Edge_HasGeometry_Inverse = getResourceOrNull(graph, URIs.Edge_HasGeometry_Inverse);
         Edge_HasKReturn = getResourceOrNull(graph, URIs.Edge_HasKReturn);
         Edge_HasKReturn_Inverse = getResourceOrNull(graph, URIs.Edge_HasKReturn_Inverse);
         Edge_HasKSupply = getResourceOrNull(graph, URIs.Edge_HasKSupply);
index 28ac19707fe12a7811f51874113e4afb6a19842b..b9dd725000aac88434353a972795b5c3ce30178a 100644 (file)
@@ -4,12 +4,14 @@ import java.awt.geom.Point2D;
 
 public class DistrictNetworkEdge {
 
-    private Point2D startPoint;
-    private Point2D endPoint;
+    private final Point2D startPoint;
+    private final Point2D endPoint;
+    private final double[] geometry;
 
-    public DistrictNetworkEdge(Point2D startPoint, Point2D endPoint) {
+    public DistrictNetworkEdge(Point2D startPoint, Point2D endPoint, double[] geometry) {
         this.startPoint = startPoint;
         this.endPoint = endPoint;
+        this.geometry = geometry;
     }
 
     public Point2D getStartPoint() {
@@ -20,4 +22,7 @@ public class DistrictNetworkEdge {
         return endPoint;
     }
 
+    public double[] getGeometry() {
+        return geometry;
+    }
 }
index ce57e0a21fd7f12b0f8968b74af4899a1092cbaf..34424739eb9a0764dbab3d7338a183b86994ff29 100644 (file)
@@ -105,7 +105,7 @@ public class DistrictNetworkEdgeElement {
             if (size == null)
                 size = new Rectangle2D.Double();
             if (edge != null)
-                size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null).getBounds2D());
+                size.setFrame(DistrictNetworkEdgeNode.calculatePath(edge, null, false).getBounds2D());
             else
                 LOGGER.debug("Element {} does not have edge!", e);
 
@@ -116,7 +116,7 @@ public class DistrictNetworkEdgeElement {
         public Shape getElementShape(IElement e) {
             DistrictNetworkEdge edge = e.getHint(KEY_DN_EDGE);
             if (edge != null) {
-                return DistrictNetworkEdgeNode.calculatePath(edge, null);
+                return DistrictNetworkEdgeNode.calculatePath(edge, null, false);
             } else {
                 return getBounds(e, null);
             }
index 1003a9b22b0a8cd55646c79f9dd4ad38520ed8bb..3b18dc6f0f3481068853253ae5658ed076e59b78 100644 (file)
@@ -2,6 +2,7 @@ package org.simantics.district.network.ui.adapters;
 
 import java.awt.geom.Point2D;
 
+import org.simantics.databoard.Bindings;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
@@ -28,7 +29,8 @@ import org.simantics.maps.MapScalingTransform;
 public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
 
     public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;
-    
+    public static final double[] EMPTY = new double[0];
+
     private DistrictNetworkResource DN;
     private DiagramResource DIA;
     
@@ -46,19 +48,30 @@ public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
         return DN.Edge;
     }
-    
+
     @Override
     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {
         if (!graph.hasStatement(edgeResource))
             return; // already deleted
 
+        if (!graph.hasStatement(edgeResource, DN.HasStartVertex))
+            return; // already deleted
         Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);
+        
+        if (!graph.hasStatement(edgeResource, DN.HasEndVertex))
+            return; // already deleted
         Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);
 
-        // TODO: Find maybe a better way to apply the scaling 
-        double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);
-        double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);
-        DistrictNetworkEdge edge = new DistrictNetworkEdge(new Point2D.Double(startCoords[0], startCoords[1]), new Point2D.Double(endCoords[0], endCoords[1]));
+        double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+        double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+        
+        double[] geometry = EMPTY;
+        try {
+            geometry = graph.getPossibleRelatedValue2(edgeResource, DN.Edge_HasGeometry, Bindings.DOUBLE_ARRAY);
+        } catch (Exception e) {
+            // most likely no geometry available
+        }
+        DistrictNetworkEdge edge = new DistrictNetworkEdge(new Point2D.Double(startCoords[0], startCoords[1]), new Point2D.Double(endCoords[0], endCoords[1]), geometry);
 
         Resource mapping = graph.getSingleObject(edgeResource, DistrictNetworkResource.getInstance(graph).HasMapping);
         element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping);
index b3e7fda3b822401b2d525d51ef7f0c1c0bb70e38..d873835c878bc14916d977153dad4379d09b05aa 100644 (file)
@@ -100,8 +100,12 @@ public class DistrictNetworkVertexElement {
             double counterExpansion = 0.001;
             double x = boundsInLocal.getX() + counterExpansion;
             double y = boundsInLocal.getY() + counterExpansion;
-            double scaledWidth = boundsInLocal.getWidth() / canvasTransform.getScaleX() / 10000.0;
-            double scaledHeight = boundsInLocal.getHeight() / canvasTransform.getScaleY() / 10000.0;
+            double scaledWidth = boundsInLocal.getWidth();
+            double scaledHeight = boundsInLocal.getHeight();
+            if (canvasTransform != null) {
+                scaledWidth = boundsInLocal.getWidth() / canvasTransform.getScaleX() / 10000.0;
+                scaledHeight= boundsInLocal.getHeight() / canvasTransform.getScaleY() / 10000.0;
+            }
             double width = scaledWidth - 2*counterExpansion;
             double height = scaledHeight - 2*counterExpansion;
             size.setFrame(x, y, width, height);
index 00a1427f5f2e6d07f72c6770364f94892afb64c1..a4f348eb11e1314b0b07119a7adf235861b453dc 100644 (file)
@@ -5,13 +5,14 @@ import java.awt.Color;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.awt.geom.AffineTransform;
-import java.awt.geom.Line2D;
 import java.awt.geom.Path2D;
 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 
 import org.simantics.district.network.ModelledCRS;
 import org.simantics.district.network.ui.DistrictNetworkEdge;
+import org.simantics.district.network.ui.adapters.DistrictNetworkEdgeElementFactory;
+import org.simantics.maps.MapScalingTransform;
 import org.simantics.scenegraph.INode;
 import org.simantics.scenegraph.ISelectionPainterNode;
 import org.simantics.scenegraph.g2d.G2DNode;
@@ -29,7 +30,7 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
 
     private DistrictNetworkEdge edge;
     private Rectangle2D bounds;
-    private transient Line2D path;
+    private transient Path2D path;
 
     private boolean scaleStroke = true;
     private Color color;
@@ -76,8 +77,8 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
         } else {
             bs = STROKE;
         }
-
-        path = calculateLine(edge, path);
+        int zoomLevel = MapScalingTransform.zoomLevel(ot);
+        path = calculatePath(edge, path, zoomLevel > 15);
 
         if (isSelected()) {
             g2d.setColor(SELECTION_COLOR);
@@ -126,20 +127,20 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
         return centerPoint;
     }
 
-    public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
-        // Convert to screen coordinates
-        double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
-        double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
-        double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
-        double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
-
-        if (result == null)
-            result = new Line2D.Double();
-        result.setLine(startX, startY, endX, endY);
-        return result;
-    }
-
-    public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result) {
+//    public static Line2D calculateLine(DistrictNetworkEdge edge, Line2D result) {
+//        // Convert to screen coordinates
+//        double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
+//        double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
+//        double endX = ModelledCRS.longitudeToX(edge.getEndPoint().getX());
+//        double endY = ModelledCRS.latitudeToY(-edge.getEndPoint().getY());// Invert for Simantics
+//
+//        if (result == null)
+//            result = new Line2D.Double();
+//        result.setLine(startX, startY, endX, endY);
+//        return result;
+//    }
+
+    public static Path2D calculatePath(DistrictNetworkEdge edge, Path2D result, boolean detailed) {
         // Convert to screen coordinates
         double startX = ModelledCRS.longitudeToX(edge.getStartPoint().getX());
         double startY = ModelledCRS.latitudeToY(-edge.getStartPoint().getY()); // Invert for Simantics
@@ -152,6 +153,18 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
              result.reset();
         }
         result.moveTo(startX, startY);
+        if (detailed) {
+            double[] detailedGeometry = edge.getGeometry();
+            if (detailedGeometry != null && !DistrictNetworkEdgeElementFactory.EMPTY.equals(detailedGeometry)) {
+                // ok, lets do this
+                
+                for (int i = 0; i < detailedGeometry.length; i += 2) {
+                    double x = ModelledCRS.longitudeToX(detailedGeometry[i]);
+                    double y = ModelledCRS.latitudeToY(-detailedGeometry[i+1]);// Invert for Simantics
+                    result.lineTo(x, y);
+                }
+            }
+        }
         result.lineTo(endX, endY);
         return result;
     }
@@ -173,7 +186,7 @@ public class DistrictNetworkEdgeNode extends G2DParentNode implements ISelection
     }
 
     private Rectangle2D calculateBounds(Rectangle2D rect) {
-        return calculatePath(edge, null).getBounds2D();
+        return calculatePath(edge, null, false).getBounds2D();
     }
 
     public void setDNEdge(DistrictNetworkEdge edge) {