From 639e8c29ade0df44b35fb2e351794f1913edaa5e Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Tue, 25 Jan 2022 17:34:59 +0100 Subject: [PATCH 1/2] Point parameters x and y are now final Signed-off-by: Florian Dupuy --- src/main/java/com/powsybl/nad/model/Point.java | 12 ++---------- src/main/java/com/powsybl/nad/svg/SvgWriter.java | 4 +--- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/powsybl/nad/model/Point.java b/src/main/java/com/powsybl/nad/model/Point.java index 9d754005..e6d86cab 100644 --- a/src/main/java/com/powsybl/nad/model/Point.java +++ b/src/main/java/com/powsybl/nad/model/Point.java @@ -13,8 +13,8 @@ */ public class Point { - private double x; - private double y; + private final double x; + private final double y; public Point(double x, double y) { this.x = x; @@ -56,18 +56,10 @@ public double getY() { return y; } - public void setY(double y) { - this.y = y; - } - public double getX() { return x; } - public void setX(double x) { - this.x = x; - } - public Point atDistance(double dist, Point direction) { double r = dist / distance(direction); return new Point(x + r * (direction.x - x), diff --git a/src/main/java/com/powsybl/nad/svg/SvgWriter.java b/src/main/java/com/powsybl/nad/svg/SvgWriter.java index b5a9e8c3..b24ed188 100644 --- a/src/main/java/com/powsybl/nad/svg/SvgWriter.java +++ b/src/main/java/com/powsybl/nad/svg/SvgWriter.java @@ -630,9 +630,7 @@ private void addStylesIfAny(XMLStreamWriter writer, List edgeStyleClasse } private void shiftEdgeStart(List points) { - Point point0 = points.get(0).atDistance(svgParameters.getVoltageLevelCircleRadius(), points.get(1)); - points.get(0).setX(point0.getX()); - points.get(0).setY(point0.getY()); + points.set(0, points.get(0).atDistance(svgParameters.getVoltageLevelCircleRadius(), points.get(1))); } private void addSvgRoot(Graph graph, XMLStreamWriter writer) throws XMLStreamException { From 9c553794911275df7d0fa7c99e3c0c7e4f098e93 Mon Sep 17 00:00:00 2001 From: Florian Dupuy Date: Tue, 25 Jan 2022 17:35:26 +0100 Subject: [PATCH 2/2] Adding Node::setPosition(point) and Node::getPosition Signed-off-by: Florian Dupuy --- .../com/powsybl/nad/layout/BasicForceLayout.java | 6 +++--- .../java/com/powsybl/nad/model/AbstractNode.java | 10 ++++++++++ src/main/java/com/powsybl/nad/model/Node.java | 4 ++++ src/main/java/com/powsybl/nad/svg/SvgWriter.java | 16 ++++++---------- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/powsybl/nad/layout/BasicForceLayout.java b/src/main/java/com/powsybl/nad/layout/BasicForceLayout.java index 04a2f6fb..49638826 100644 --- a/src/main/java/com/powsybl/nad/layout/BasicForceLayout.java +++ b/src/main/java/com/powsybl/nad/layout/BasicForceLayout.java @@ -44,15 +44,15 @@ protected void busNodesLayout(Graph graph, LayoutParameters layoutParameters) { BusNode busNode = sortedNodes.get(i); busNode.setIndex(i); busNode.setNbNeighbouringBusNodes(sortedNodes.size() - 1); - busNode.setPosition(n.getX(), n.getY()); + busNode.setPosition(n.getPosition()); } }); } private void fixedTextNodeLayout(TextEdge textEdge, Pair nodes) { - VoltageLevelNode vlNode = nodes.getFirst(); Point fixedShift = getTextNodeFixedShift(); - nodes.getSecond().setPosition(vlNode.getX() + fixedShift.getX(), vlNode.getY() + fixedShift.getY()); + Point textPos = nodes.getFirst().getPosition().shift(fixedShift.getX(), fixedShift.getY()); + nodes.getSecond().setPosition(textPos); } protected Point getTextNodeFixedShift() { diff --git a/src/main/java/com/powsybl/nad/model/AbstractNode.java b/src/main/java/com/powsybl/nad/model/AbstractNode.java index e4c0d55b..833aa5fa 100644 --- a/src/main/java/com/powsybl/nad/model/AbstractNode.java +++ b/src/main/java/com/powsybl/nad/model/AbstractNode.java @@ -45,11 +45,21 @@ public Optional getName() { return Optional.ofNullable(name); } + @Override + public void setPosition(Point position) { + this.position = position; + } + @Override public void setPosition(double x, double y) { position = new Point(x, y); } + @Override + public Point getPosition() { + return position; + } + @Override public double getX() { return position.getX(); diff --git a/src/main/java/com/powsybl/nad/model/Node.java b/src/main/java/com/powsybl/nad/model/Node.java index 4b32fad6..8fced32e 100644 --- a/src/main/java/com/powsybl/nad/model/Node.java +++ b/src/main/java/com/powsybl/nad/model/Node.java @@ -17,6 +17,10 @@ public interface Node { String getEquipmentId(); + void setPosition(Point point); + + Point getPosition(); + void setPosition(double x, double y); double getX(); diff --git a/src/main/java/com/powsybl/nad/svg/SvgWriter.java b/src/main/java/com/powsybl/nad/svg/SvgWriter.java index b24ed188..ff32b204 100644 --- a/src/main/java/com/powsybl/nad/svg/SvgWriter.java +++ b/src/main/java/com/powsybl/nad/svg/SvgWriter.java @@ -113,9 +113,7 @@ private void computeBranchEdgesCoordinates(Graph graph) { } private void computeTextEdgeLayoutCoordinates(Node node1, Node node2, TextEdge edge) { - Point point1 = new Point(node1.getX(), node1.getY()); - Point point2 = new Point(node2.getX(), node2.getY()); - edge.setPoints(point1, point2); + edge.setPoints(node1.getPosition(), node2.getPosition()); } private void computeSingleBranchEdgeCoordinates(Graph graph, BranchEdge edge) { @@ -135,8 +133,7 @@ private void computeSingleBranchEdgeCoordinates(Graph graph, BranchEdge edge) { private Point getDirection(Node directionBusGraphNode, Supplier vlNodeSupplier) { if (directionBusGraphNode == BusNode.UNKNOWN) { - Node vlNode = vlNodeSupplier.get(); - return new Point(vlNode.getX(), vlNode.getY()); + return vlNodeSupplier.get().getPosition(); } return new Point(directionBusGraphNode.getX(), directionBusGraphNode.getY()); } @@ -144,9 +141,8 @@ private Point getDirection(Node directionBusGraphNode, Supplier vlNodeSupp private Point computeEdgeStart(Node node, Point direction, Supplier vlNodeSupplier) { // If edge not connected to a bus node on that side, we use corresponding voltage level with specific extra radius if (node == BusNode.UNKNOWN) { - Node vlNode = vlNodeSupplier.get(); double unknownBusRadius = svgParameters.getVoltageLevelCircleRadius() + svgParameters.getUnknownBusNodeExtraRadius(); - return new Point(vlNode.getX(), vlNode.getY()).atDistance(unknownBusRadius, direction); + return vlNodeSupplier.get().getPosition().atDistance(unknownBusRadius, direction); } Point edgeStart = new Point(node.getX(), node.getY()); @@ -163,8 +159,8 @@ private void computeMultiBranchEdgesCoordinates(Graph graph, List ed Edge firstEdge = edges.iterator().next(); Node nodeA = graph.getNode1(firstEdge); Node nodeB = graph.getNode2(firstEdge); - Point pointA = new Point(nodeA.getX(), nodeA.getY()); - Point pointB = new Point(nodeB.getX(), nodeB.getY()); + Point pointA = nodeA.getPosition(); + Point pointB = nodeB.getPosition(); double dx = pointB.getX() - pointA.getX(); double dy = pointB.getY() - pointA.getY(); @@ -452,7 +448,7 @@ private void drawTextNodes(Graph graph, XMLStreamWriter writer) throws XMLStream } private String getTranslateString(Node node) { - return getTranslateString(node.getX(), node.getY()); + return getTranslateString(node.getPosition()); } private String getTranslateString(Point point) {