From: jsimomaa Date: Wed, 6 Mar 2019 19:36:19 +0000 (+0200) Subject: Add edge geometry for detailed rendering in closer zoom levels X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=830edf8e15f1608c13a869efe52d61f7da6bd9ae;p=simantics%2Fdistrict.git Add edge geometry for detailed rendering in closer zoom levels gitlab #35 Change-Id: I6930dc2b8225647b61beeee0dfa3408e8d1f70c6 --- diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java index 7c660b4c..93646ded 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java @@ -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; + } } diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java index 54a578dd..8c087210 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java @@ -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 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); + } + } + } + } } diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java index b7e46475..552f7e60 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java @@ -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); diff --git a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph index c5195291..d9ab6d6e 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph @@ -85,6 +85,8 @@ DN.Edge -- 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 diff --git a/org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java b/org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java index e8539528..c2b15437 100644 --- a/org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java +++ b/org.simantics.district.network.ontology/src/org/simantics/district/network/ontology/DistrictNetworkResource.java @@ -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); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java index 28ac1970..b9dd7250 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DistrictNetworkEdge.java @@ -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; + } } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java index ce57e0a2..34424739 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElement.java @@ -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); } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java index 1003a9b2..3b18dc6f 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java @@ -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); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java index b3e7fda3..d873835c 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkVertexElement.java @@ -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); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java index 00a1427f..a4f348eb 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkEdgeNode.java @@ -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) {