Skip to content

Commit

Permalink
Reduce dupplication with static methods
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <tadam@silicom.fr>
  • Loading branch information
tadam50 committed Jun 13, 2023
1 parent db75ac7 commit 424e53b
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static Network createNetworkWithLine() {
.setId("Bus1")
.add();
Substation substation2 = network.newSubstation()
.setId("Substation2")
.setId(SUBSTATION_2_ID)
.setCountry(Country.FR)
.add();
VoltageLevel voltageLevel2 = substation2.newVoltageLevel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
/**
* @author Thomas Adam <tadam at silicom.fr>
*/
public final class HorizontalLayout {
public final class HorizontalLayoutUtil {

private HorizontalLayout() {
private HorizontalLayoutUtil() {
}

public static double computeCoordY(AbstractBaseGraph graph, LayoutParameters layoutParameters, double topPadding, VoltageLevelGraph vlGraph) {
Expand Down Expand Up @@ -67,7 +67,7 @@ public static void adaptPaddingToSnakeLines(AbstractBaseGraph graph, LayoutParam

for (VoltageLevelGraph vlGraph : graph.getVoltageLevels()) {
x += AbstractLayout.getWidthVerticalSnakeLines(vlGraph.getId(), layoutParameters, infosNbSnakeLines);
vlGraph.setCoord(x + voltageLevelPadding.getLeft(), HorizontalLayout.computeCoordY(graph, layoutParameters, topPadding, vlGraph));
vlGraph.setCoord(x + voltageLevelPadding.getLeft(), HorizontalLayoutUtil.computeCoordY(graph, layoutParameters, topPadding, vlGraph));
x += vlGraph.getWidth();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected void calculateCoordVoltageLevels(LayoutParameters layoutParameters) {
vLayout.run(layoutParameters);

x += voltageLevelPadding.getLeft();
vlGraph.setCoord(x, HorizontalLayout.computeCoordY(getGraph(), layoutParameters, topPadding, vlGraph));
vlGraph.setCoord(x, HorizontalLayoutUtil.computeCoordY(getGraph(), layoutParameters, topPadding, vlGraph));

x += vlGraph.getWidth() + voltageLevelPadding.getRight();
substationHeight = Math.max(substationHeight, vlGraph.getHeight());
Expand All @@ -72,7 +72,7 @@ public void manageSnakeLines(LayoutParameters layoutParameters) {
}

private void adaptPaddingToSnakeLines(LayoutParameters layoutParameters) {
HorizontalLayout.adaptPaddingToSnakeLines(getGraph(), layoutParameters, infosNbSnakeLines);
HorizontalLayoutUtil.adaptPaddingToSnakeLines(getGraph(), layoutParameters, infosNbSnakeLines);

getGraph().getVoltageLevels().forEach(g -> manageSnakeLines(g, layoutParameters));
manageSnakeLines(getGraph(), layoutParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void manageSnakeLines(LayoutParameters layoutParameters) {
}

private void adaptPaddingToSnakeLines(LayoutParameters layoutParameters) {
HorizontalLayout.adaptPaddingToSnakeLines(getGraph(), layoutParameters, infosNbSnakeLines);
HorizontalLayoutUtil.adaptPaddingToSnakeLines(getGraph(), layoutParameters, infosNbSnakeLines);

manageAllSnakeLines(layoutParameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,17 @@
*/
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.coordinate.Point;
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 interface VerticalLayout {

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);
}
void addMiddlePoints(LayoutParameters layoutParam, Node node1, Node node2, boolean increment, List<Point> polyline);

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;
}
}
}
boolean facingNodes(Node node1, Node node2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* 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.Point;
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.ArrayList;
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 VerticalLayoutUtil {

private VerticalLayoutUtil() {
}

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;
}
}
}

public static List<Point> calculatePolylineSnakeLine(VerticalLayout layout,
BaseGraph graph, InfosNbSnakeLinesVertical infosNbSnakeLines,
LayoutParameters layoutParam, Node node1, Node node2,
boolean increment) {
List<Point> polyline;
if (graph.getVoltageLevelGraph(node1) == graph.getVoltageLevelGraph(node2)) { // in the same VL (so far always horizontal layout)
VoltageLevelGraph vlGraph = graph.getVoltageLevelGraph(node1);
String graphId = vlGraph.getId();

InfosNbSnakeLinesHorizontal infosNbSnakeLinesH = InfosNbSnakeLinesHorizontal.create(vlGraph);

// Reset the horizontal layout numbers to current graph numbers
int currentNbBottom = infosNbSnakeLines.getNbSnakeLinesHorizontalBetween(graphId, BOTTOM);
int currentNbTop = infosNbSnakeLines.getNbSnakeLinesHorizontalBetween(graphId, TOP);
int currentNbLeft = infosNbSnakeLines.getNbSnakeLinesLeftRight().get(Side.LEFT);
infosNbSnakeLinesH.getNbSnakeLinesTopBottom().put(BOTTOM, currentNbBottom);
infosNbSnakeLinesH.getNbSnakeLinesTopBottom().put(TOP, currentNbTop);
infosNbSnakeLinesH.getNbSnakeLinesVerticalBetween().put(graphId, currentNbLeft);

double yMin = vlGraph.getY();
double yMax = vlGraph.getY() + vlGraph.getInnerHeight(layoutParam.getVerticalSpaceBus());

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

// Update the vertical layout maps
Integer updatedNbLinesBottom = infosNbSnakeLinesH.getNbSnakeLinesTopBottom().get(BOTTOM);
Integer updatedNbLinesTop = infosNbSnakeLinesH.getNbSnakeLinesTopBottom().get(TOP);
Integer updatedNbLinesLeft = infosNbSnakeLinesH.getNbSnakeLinesVerticalBetween().get(graphId);
infosNbSnakeLines.setNbSnakeLinesTopBottom(graphId, BOTTOM, updatedNbLinesBottom);
infosNbSnakeLines.setNbSnakeLinesTopBottom(graphId, TOP, updatedNbLinesTop);
infosNbSnakeLines.getNbSnakeLinesLeftRight().put(Side.LEFT, updatedNbLinesLeft);
} else {
polyline = new ArrayList<>();
polyline.add(graph.getShiftedPoint(node1));
layout.addMiddlePoints(layoutParam, node1, node2, increment, polyline);
polyline.add(graph.getShiftedPoint(node2));
}
return polyline;
}

public static void addMiddlePoints(VerticalLayout layout,
BaseGraph graph,
InfosNbSnakeLinesVertical infosNbSnakeLines, double maxVoltageLevelWidth,
LayoutParameters layoutParam, Node node1, Node node2,
boolean increment,
List<Point> polyline) {
Direction dNode1 = AbstractLayout.getNodeDirection(graph, node1, 1);
Direction dNode2 = AbstractLayout.getNodeDirection(graph, node2, 2);

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

Point p1 = graph.getShiftedPoint(node1);
Point p2 = graph.getShiftedPoint(node2);

if (layout.facingNodes(node1, node2)) {
// if the two nodes are facing each other, no need to add more than 2 points (and one point is enough if same abscissa)
double ySnakeLine = Math.min(p1.getY(), p2.getY()) + decal1V;
if (p1.getX() != p2.getX()) {
polyline.add(new Point(p1.getX(), ySnakeLine));
polyline.add(new Point(p2.getX(), ySnakeLine));
} else {
polyline.add(new Point(p1.getX(), ySnakeLine));
}
} else {
String vl2 = graph.getVoltageLevelInfos(node2).getId();
int nbSnakeLines2 = infosNbSnakeLines.incrementAndGetNbSnakeLinesTopBottom(vl2, dNode2);
double decal2V = VerticalLayoutUtil.getVerticalShift(layoutParam, dNode2, nbSnakeLines2);

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

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

0 comments on commit 424e53b

Please sign in to comment.