Skip to content

Commit

Permalink
ZoneGraph : reduce duplication
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <tadam@silicom.fr>
  • Loading branch information
tadam50 committed May 5, 2023
1 parent a958dc1 commit d00aca9
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ private void setWindingOrder(Middle3WTNode node,
protected abstract List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParam, Node node1, Node node2,
boolean increment);

protected Direction getNodeDirection(Node node, int nb) {
public static Direction getNodeDirection(BaseGraph graph, Node node, int nb) {
if (node.getType() != Node.NodeType.FEEDER) {
throw new PowsyblException("Node " + nb + " is not a feeder node");
}
Direction dNode = getGraph().getCell(node).map(Cell::getDirection).orElse(Direction.TOP);
Direction dNode = graph.getCell(node).map(Cell::getDirection).orElse(Direction.TOP);
if (dNode != Direction.TOP && dNode != Direction.BOTTOM) {
throw new PowsyblException("Node " + nb + " cell direction not TOP or BOTTOM");
}
Expand All @@ -143,31 +143,33 @@ protected Direction getNodeDirection(Node node, int nb) {
* Calculate polyline points of a snakeLine
* This is a default implementation of 'calculatePolylineSnakeLine' for a horizontal layout
*/
protected List<Point> calculatePolylineSnakeLineForHorizontalLayout(LayoutParameters layoutParam, Node node1, Node node2,
boolean increment, InfosNbSnakeLinesHorizontal infosNbSnakeLines,
double yMin, double yMax) {
public static List<Point> calculatePolylineSnakeLineForHorizontalLayout(BaseGraph graph,
LayoutParameters layoutParam, Node node1, Node node2,
boolean increment, InfosNbSnakeLinesHorizontal infosNbSnakeLines,
double yMin, double yMax) {
List<Point> pol = new ArrayList<>();
pol.add(getGraph().getShiftedPoint(node1));
addMiddlePoints(layoutParam, new Pair<>(node1, node2), infosNbSnakeLines, increment, pol, new Pair<>(yMin, yMax));
pol.add(getGraph().getShiftedPoint(node2));
pol.add(graph.getShiftedPoint(node1));
addMiddlePoints(graph, layoutParam, new Pair<>(node1, node2), infosNbSnakeLines, increment, pol, new Pair<>(yMin, yMax));
pol.add(graph.getShiftedPoint(node2));
return pol;
}

private void addMiddlePoints(LayoutParameters layoutParam,
Pair<Node, Node> nodes,
InfosNbSnakeLinesHorizontal infosNbSnakeLines, boolean increment,
List<Point> pol,
Pair<Double, Double> yMinMax) {
private static void addMiddlePoints(BaseGraph graph,
LayoutParameters layoutParam,
Pair<Node, Node> nodes,
InfosNbSnakeLinesHorizontal infosNbSnakeLines, boolean increment,
List<Point> pol,
Pair<Double, Double> yMinMax) {
Node node1 = nodes.getFirst();
Node node2 = nodes.getSecond();
double yMin = yMinMax.getFirst();
double yMax = yMinMax.getSecond();

Direction dNode1 = getNodeDirection(node1, 1);
Direction dNode2 = getNodeDirection(node2, 2);
Direction dNode1 = getNodeDirection(graph, node1, 1);
Direction dNode2 = getNodeDirection(graph, node2, 2);

VoltageLevelGraph vlGraph1 = getGraph().getVoltageLevelGraph(node1);
VoltageLevelGraph vlGraph2 = getGraph().getVoltageLevelGraph(node2);
VoltageLevelGraph vlGraph1 = graph.getVoltageLevelGraph(node1);
VoltageLevelGraph vlGraph2 = graph.getVoltageLevelGraph(node2);

Map<Direction, Integer> nbSnakeLinesTopBottom = infosNbSnakeLines.getNbSnakeLinesTopBottom();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public VoltageLevelGraph getGraph() {
protected List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParam, Node node1, Node node2, boolean increment) {
double yMin = getGraph().getY();
double yMax = getGraph().getY() + getGraph().getInnerHeight(layoutParam.getVerticalSpaceBus());
return calculatePolylineSnakeLineForHorizontalLayout(layoutParam, node1, node2, increment, infosNbSnakeLines, yMin, yMax);
return calculatePolylineSnakeLineForHorizontalLayout(getGraph(), layoutParam, node1, node2, increment, infosNbSnakeLines, yMin, yMax);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParam, N
boolean increment) {
double yMin = getGraph().getVoltageLevels().stream().mapToDouble(VoltageLevelGraph::getY).min().orElse(0.0);
double yMax = getGraph().getVoltageLevels().stream().mapToDouble(g -> g.getY() + g.getInnerHeight(layoutParam.getVerticalSpaceBus())).max().orElse(0.0);
return calculatePolylineSnakeLineForHorizontalLayout(layoutParam, node1, node2, increment, infosNbSnakeLines, yMin, yMax);
return calculatePolylineSnakeLineForHorizontalLayout(getGraph(), layoutParam, node1, node2, increment, infosNbSnakeLines, yMin, yMax);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParam, N
boolean increment) {
double yMin = getGraph().getVoltageLevels().stream().mapToDouble(VoltageLevelGraph::getY).min().orElse(0.0);
double yMax = getGraph().getVoltageLevels().stream().mapToDouble(g -> g.getY() + g.getInnerHeight(layoutParam.getVerticalSpaceBus())).max().orElse(0.0);
return calculatePolylineSnakeLineForHorizontalLayout(layoutParam, node1, node2, increment, infosNbSnakeLines, yMin, yMax);
return calculatePolylineSnakeLineForHorizontalLayout(getGraph(), layoutParam, node1, node2, increment, infosNbSnakeLines, yMin, yMax);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.sld.layout;

import com.powsybl.sld.model.coordinate.Direction;
import com.powsybl.sld.model.coordinate.Side;
import com.powsybl.sld.model.graphs.AbstractBaseGraph;
import com.powsybl.sld.model.graphs.BaseGraph;
import com.powsybl.sld.model.graphs.VoltageLevelGraph;
import com.powsybl.sld.model.nodes.Node;

import java.util.List;

import static com.powsybl.sld.model.coordinate.Direction.BOTTOM;
import static com.powsybl.sld.model.coordinate.Direction.TOP;

/**
* @author Thomas Adam <tadam at silicom.fr>
*/
public final class VerticalLayout {

private VerticalLayout() {
}

public static void adaptPaddingToSnakeLines(AbstractBaseGraph graph, LayoutParameters layoutParameters, InfosNbSnakeLinesVertical infosNbSnakeLines) {
double widthSnakeLinesLeft = Math.max(infosNbSnakeLines.getNbSnakeLinesLeftRight().get(Side.LEFT) - 1, 0) * layoutParameters.getHorizontalSnakeLinePadding();

LayoutParameters.Padding diagramPadding = layoutParameters.getDiagramPadding();
LayoutParameters.Padding voltageLevelPadding = layoutParameters.getVoltageLevelPadding();

double xVoltageLevels = widthSnakeLinesLeft + diagramPadding.getLeft() + voltageLevelPadding.getLeft();
double y = diagramPadding.getTop()
+ graph.getVoltageLevelStream().findFirst().map(vlg -> getHeightHorizontalSnakeLines(vlg.getId(), TOP, layoutParameters, infosNbSnakeLines)).orElse(0.);

for (VoltageLevelGraph vlGraph : graph.getVoltageLevels()) {
vlGraph.setCoord(xVoltageLevels, y + voltageLevelPadding.getTop());
y += vlGraph.getHeight() + getHeightHorizontalSnakeLines(vlGraph.getId(), BOTTOM, layoutParameters, infosNbSnakeLines);
}

double widthSnakeLinesRight = Math.max(infosNbSnakeLines.getNbSnakeLinesLeftRight().get(Side.RIGHT) - 1, 0) * layoutParameters.getHorizontalSnakeLinePadding();
double substationWidth = graph.getWidth() + widthSnakeLinesLeft + widthSnakeLinesRight;
double substationHeight = y - diagramPadding.getTop();
graph.setSize(substationWidth, substationHeight);

infosNbSnakeLines.reset();
}

private static double getHeightHorizontalSnakeLines(String vlGraphId, Direction direction, LayoutParameters layoutParameters, InfosNbSnakeLinesVertical infosNbSnakeLines) {
return Math.max(infosNbSnakeLines.getNbSnakeLinesHorizontalBetween(vlGraphId, direction) - 1, 0) * layoutParameters.getHorizontalSnakeLinePadding();
}

public static double getVerticalShift(LayoutParameters layoutParam, Direction dNode1, int nbSnakeLines1) {
return (nbSnakeLines1 - 1) * layoutParam.getVerticalSnakeLinePadding()
+ (dNode1 == Direction.TOP ? layoutParam.getVoltageLevelPadding().getTop() : layoutParam.getVoltageLevelPadding().getBottom());
}

/**
* Dispatching the snake lines to the right and to the left
*/
public static Side getSide(boolean increment) {
return increment ? Side.LEFT : Side.RIGHT;
}

public static double getXSnakeLine(BaseGraph graph, Node node, Side side, LayoutParameters layoutParam, InfosNbSnakeLinesVertical infosNbSnakeLines, double maxVoltageLevelWidth) {
double shiftLeftRight = Math.max(infosNbSnakeLines.getNbSnakeLinesLeftRight().compute(side, (k, v) -> v + 1) - 1, 0) * layoutParam.getHorizontalSnakeLinePadding();
return graph.getVoltageLevelGraph(node).getX() - layoutParam.getVoltageLevelPadding().getLeft()
+ (side == Side.LEFT ? -shiftLeftRight : shiftLeftRight + maxVoltageLevelWidth);
}

public static double getYSnakeLine(BaseGraph graph, Node node, Direction dNode1, double decalV, LayoutParameters layoutParam) {
double y = graph.getShiftedPoint(node).getY();
if (dNode1 == BOTTOM) {
return y + decalV;
} else {
List<VoltageLevelGraph> vls = graph.getVoltageLevels();
int iVl = vls.indexOf(graph.getVoltageLevelGraph(node));
if (iVl == 0) {
return y - decalV;
} else {
VoltageLevelGraph vlAbove = vls.get(iVl - 1);
return vlAbove.getY()
+ vlAbove.getHeight() - layoutParam.getVoltageLevelPadding().getTop() - layoutParam.getVoltageLevelPadding().getBottom()
+ decalV;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParam, N
double yMax = vlGraph.getY() + vlGraph.getInnerHeight(layoutParam.getVerticalSpaceBus());

// Calculate the snakeline as an horizontal layout
polyline = calculatePolylineSnakeLineForHorizontalLayout(layoutParam, node1, node2, increment, infosNbSnakeLinesH, yMin, yMax);
polyline = calculatePolylineSnakeLineForHorizontalLayout(getGraph(), layoutParam, node1, node2, increment, infosNbSnakeLinesH, yMin, yMax);

// Update the vertical layout maps
Integer updatedNbLinesBottom = infosNbSnakeLinesH.getNbSnakeLinesTopBottom().get(BOTTOM);
Expand All @@ -148,15 +148,15 @@ protected List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParam, N
}

protected void addMiddlePoints(LayoutParameters layoutParam, Node node1, Node node2, boolean increment, List<Point> polyline) {
Direction dNode1 = getNodeDirection(node1, 1);
Direction dNode2 = getNodeDirection(node2, 2);
Direction dNode1 = getNodeDirection(getGraph(), node1, 1);
Direction dNode2 = getNodeDirection(getGraph(), node2, 2);

// increment not needed for 3WT for the common node
String vl1 = getGraph().getVoltageLevelInfos(node1).getId();
int nbSnakeLines1 = increment
? infosNbSnakeLines.incrementAndGetNbSnakeLinesTopBottom(vl1, dNode1)
: infosNbSnakeLines.getNbSnakeLinesHorizontalBetween(vl1, dNode1);
double decal1V = getVerticalShift(layoutParam, dNode1, nbSnakeLines1);
double decal1V = VerticalLayout.getVerticalShift(layoutParam, dNode1, nbSnakeLines1);

Point p1 = getGraph().getShiftedPoint(node1);
Point p2 = getGraph().getShiftedPoint(node2);
Expand All @@ -173,59 +173,23 @@ protected void addMiddlePoints(LayoutParameters layoutParam, Node node1, Node no
} else {
String vl2 = getGraph().getVoltageLevelInfos(node2).getId();
int nbSnakeLines2 = infosNbSnakeLines.incrementAndGetNbSnakeLinesTopBottom(vl2, dNode2);
double decal2V = getVerticalShift(layoutParam, dNode2, nbSnakeLines2);
double decal2V = VerticalLayout.getVerticalShift(layoutParam, dNode2, nbSnakeLines2);

double ySnakeLine1 = getYSnakeLine(node1, dNode1, decal1V, layoutParam);
double ySnakeLine2 = getYSnakeLine(node2, dNode2, decal2V, layoutParam);
double ySnakeLine1 = VerticalLayout.getYSnakeLine(getGraph(), node1, dNode1, decal1V, layoutParam);
double ySnakeLine2 = VerticalLayout.getYSnakeLine(getGraph(), node2, dNode2, decal2V, layoutParam);

Side side = getSide(increment);
double xSnakeLine = getXSnakeLine(node1, side, layoutParam);
Side side = VerticalLayout.getSide(increment);
double xSnakeLine = VerticalLayout.getXSnakeLine(getGraph(), node1, side, layoutParam, infosNbSnakeLines, maxVoltageLevelWidth);
polyline.addAll(Point.createPointsList(p1.getX(), ySnakeLine1,
xSnakeLine, ySnakeLine1,
xSnakeLine, ySnakeLine2,
p2.getX(), ySnakeLine2));
}
}

private double getVerticalShift(LayoutParameters layoutParam, Direction dNode1, int nbSnakeLines1) {
return (nbSnakeLines1 - 1) * layoutParam.getVerticalSnakeLinePadding()
+ (dNode1 == Direction.TOP ? layoutParam.getVoltageLevelPadding().getTop() : layoutParam.getVoltageLevelPadding().getBottom());
}

/**
* Dispatching the snake lines to the right and to the left
*/
private Side getSide(boolean increment) {
return increment ? Side.LEFT : Side.RIGHT;
}

private double getXSnakeLine(Node node, Side side, LayoutParameters layoutParam) {
double shiftLeftRight = Math.max(infosNbSnakeLines.getNbSnakeLinesLeftRight().compute(side, (k, v) -> v + 1) - 1, 0) * layoutParam.getHorizontalSnakeLinePadding();
return getGraph().getVoltageLevelGraph(node).getX() - layoutParam.getVoltageLevelPadding().getLeft()
+ (side == Side.LEFT ? -shiftLeftRight : shiftLeftRight + maxVoltageLevelWidth);
}

private double getYSnakeLine(Node node, Direction dNode1, double decalV, LayoutParameters layoutParam) {
double y = getGraph().getShiftedPoint(node).getY();
if (dNode1 == BOTTOM) {
return y + decalV;
} else {
List<VoltageLevelGraph> vls = getGraph().getVoltageLevels();
int iVl = vls.indexOf(getGraph().getVoltageLevelGraph(node));
if (iVl == 0) {
return y - decalV;
} else {
VoltageLevelGraph vlAbove = vls.get(iVl - 1);
return vlAbove.getY()
+ vlAbove.getHeight() - layoutParam.getVoltageLevelPadding().getTop() - layoutParam.getVoltageLevelPadding().getBottom()
+ decalV;
}
}
}

private boolean facingNodes(Node node1, Node node2) {
Direction dNode1 = getNodeDirection(node1, 1);
Direction dNode2 = getNodeDirection(node2, 2);
Direction dNode1 = getNodeDirection(getGraph(), node1, 1);
Direction dNode2 = getNodeDirection(getGraph(), node2, 2);
VoltageLevelGraph vlGraph1 = getGraph().getVoltageLevelGraph(node1);
VoltageLevelGraph vlGraph2 = getGraph().getVoltageLevelGraph(node2);
return (dNode1 == BOTTOM && dNode2 == TOP && getGraph().graphAdjacents(vlGraph1, vlGraph2))
Expand Down
Loading

0 comments on commit d00aca9

Please sign in to comment.