Skip to content

Commit

Permalink
Put snakeline margin into LayoutParameters
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas ADAM <[email protected]>
  • Loading branch information
tadam50 committed Jan 17, 2024
1 parent 799c043 commit b20d43e
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class LayoutParameters {
private double cgmesScaleFactor = 1;
private String cgmesDiagramName = null;
private boolean cgmesUseNames = true;
private double zoneLayoutSnakeLinePadding = 90;

@JsonIgnore
private Map<String, ComponentSize> componentsSize;
Expand Down Expand Up @@ -77,7 +78,8 @@ public LayoutParameters(@JsonProperty("verticalSpaceBus") double verticalSpaceBu
@JsonProperty("removeFictitiousSwitchNodes") boolean removeFictitiousSwitchNodes,
@JsonProperty("cgmesScaleFactor") double cgmesScaleFactor,
@JsonProperty("cgmesDiagramName") String cgmesDiagramName,
@JsonProperty("cgmesUseNames") boolean cgmesUseNames) {
@JsonProperty("cgmesUseNames") boolean cgmesUseNames,
@JsonProperty("zoneLayoutSnakeLinePadding") int zoneLayoutSnakeLinePadding) {

this.verticalSpaceBus = verticalSpaceBus;
this.horizontalBusPadding = horizontalBusPadding;
Expand All @@ -100,6 +102,7 @@ public LayoutParameters(@JsonProperty("verticalSpaceBus") double verticalSpaceBu
this.cgmesDiagramName = cgmesDiagramName;
this.cgmesScaleFactor = cgmesScaleFactor;
this.cgmesUseNames = cgmesUseNames;
this.zoneLayoutSnakeLinePadding = zoneLayoutSnakeLinePadding;
}

public LayoutParameters(LayoutParameters other) {
Expand All @@ -126,6 +129,7 @@ public LayoutParameters(LayoutParameters other) {
cgmesScaleFactor = other.cgmesScaleFactor;
cgmesDiagramName = other.cgmesDiagramName;
cgmesUseNames = other.cgmesUseNames;
zoneLayoutSnakeLinePadding = other.zoneLayoutSnakeLinePadding;
}

public double getVerticalSpaceBus() {
Expand Down Expand Up @@ -330,8 +334,17 @@ public LayoutParameters setCgmesUseNames(boolean cgmesUseNames) {
return this;
}

public double getZoneLayoutSnakeLinePadding() {
return zoneLayoutSnakeLinePadding;
}

public LayoutParameters setZoneLayoutSnakeLinePadding(double zoneLayoutSnakeLinePadding) {
this.zoneLayoutSnakeLinePadding = zoneLayoutSnakeLinePadding;
return this;
}

public enum Alignment {
FIRST, LAST, MIDDLE, NONE;
FIRST, LAST, MIDDLE, NONE
}

public static class Padding {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class MatrixZoneLayout extends AbstractZoneLayout {

protected MatrixZoneLayout(ZoneGraph graph, String[][] matrix, SubstationLayoutFactory sLayoutFactory, VoltageLevelLayoutFactory vLayoutFactory) {
super(graph, sLayoutFactory, vLayoutFactory);
// FIXME: add hallway size into LayoutParameters.get
this.model = new MatrixZoneLayoutModel(90);
this.model = new MatrixZoneLayoutModel();
this.matrix = matrix;
this.pathFinder = new PathFinderFactory().createDijkstra();
}
Expand Down Expand Up @@ -58,7 +57,7 @@ protected void calculateCoordSubstations(LayoutParameters layoutParameters) {
// Width by col
int maxWidthCol = model.getMatrixCellWidth();
// Snakeline hallway (horizontal & vertical)
int hallway = model.getSnakelineMargin();
int snakelineMargin = (int) layoutParameters.getZoneLayoutSnakeLinePadding();
// Zone size
int nbRows = matrix.length;
int nbCols = matrix[0].length;
Expand All @@ -68,19 +67,19 @@ protected void calculateCoordSubstations(LayoutParameters layoutParameters) {
String id = matrix[row][col];
SubstationGraph graph = getGraph().getSubstationGraph(id);
if (graph != null) {
double dx = col * maxWidthCol + (col + 1.0) * hallway;
double dy = row * maxHeightRow + (row + 1.0) * hallway;
double dx = col * maxWidthCol + (col + 1.0) * snakelineMargin;
double dy = row * maxHeightRow + (row + 1.0) * snakelineMargin;
move(graph, dx, dy);
}
}
}
double zoneWidth = nbCols * maxWidthCol + (nbCols + 1.0) * hallway;
double zoneHeight = nbRows * maxHeightRow + (nbRows + 1.0) * hallway;
double zoneWidth = nbCols * maxWidthCol + (nbCols + 1.0) * snakelineMargin;
double zoneHeight = nbRows * maxHeightRow + (nbRows + 1.0) * snakelineMargin;
getGraph().setSize(zoneWidth, zoneHeight);
}

@Override
protected List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParam, Pair<Node, Node> nodes,
protected List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParameters, Pair<Node, Node> nodes,
boolean increment) {
List<Point> polyline = new ArrayList<>();
Node node1 = nodes.getFirst();
Expand All @@ -99,7 +98,7 @@ protected List<Point> calculatePolylineSnakeLine(LayoutParameters layoutParam, P
// Add starting point
polyline.add(p1);
// Find snakeline path
polyline.addAll(model.buildSnakeline(pathFinder, p1, dNode1, p2, dNode2));
polyline.addAll(model.buildSnakeline(pathFinder, p1, dNode1, p2, dNode2, layoutParameters.getZoneLayoutSnakeLinePadding()));
// Add ending point
polyline.add(p2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ public class MatrixZoneLayoutModel {

private int matrixCellWidth = -1;

private final int snakelineMargin;

private Grid pathFinderGrid;

public MatrixZoneLayoutModel(int hallway) {
this.snakelineMargin = hallway;
}

public void addSubstationSubgraph(SubstationGraph graph, int col, int row) {
if (graph != null) {
cellsById.put(graph.getId(), new MatrixCell(graph, col, row));
Expand All @@ -51,10 +45,6 @@ public void addSubstationSubgraph(SubstationGraph graph, int col, int row) {
matrixNbCol = Math.max(matrixNbCol, col + 1);
}

public int getSnakelineMargin() {
return snakelineMargin;
}

public int getMatrixCellWidth() {
return matrixCellWidth;
}
Expand All @@ -63,30 +53,31 @@ public int getMatrixCellHeight() {
return matrixCellHeight;
}

private int getX(int col) {
return ((col + 1) * snakelineMargin) + (col * matrixCellWidth);
private int getX(int col, double snakelineMargin) {
return ((col + 1) * (int) snakelineMargin) + (col * matrixCellWidth);
}

private int getY(int row) {
return (row + 1) * snakelineMargin + row * matrixCellHeight;
private int getY(int row, double snakelineMargin) {
return (row + 1) * (int) snakelineMargin + row * matrixCellHeight;
}

public List<Point> buildSnakeline(PathFinder pathfinder,
Point p1, Direction d1,
Point p2, Direction d2) {
insertFreePathInSubstation(p1, d1);
insertFreePathInSubstation(p2, d2);
Point p2, Direction d2,
double snakelineMargin) {
insertFreePathInSubstation(p1, d1, snakelineMargin);
insertFreePathInSubstation(p2, d2, snakelineMargin);

// Use path finding algo
return pathfinder.findShortestPath(pathFinderGrid, p1, p2);
}

private void insertFreePathInSubstation(Point p, Direction d) {
private void insertFreePathInSubstation(Point p, Direction d, double snakelineMargin) {
int dy = 1;

int x1 = (int) p.getX();
int y1 = (int) p.getY();
int ss1Y = y1 + snakelineMargin * (d == Direction.TOP ? -1 : 1);
int ss1Y = y1 + (int) snakelineMargin * (d == Direction.TOP ? -1 : 1);
int min1Y = Math.max(Math.min(y1, ss1Y), 0);
int max1Y = Math.max(y1, ss1Y) + dy;
for (int y = min1Y; y < max1Y; y++) {
Expand Down Expand Up @@ -134,12 +125,13 @@ private void computeSubstationsAvailability(LayoutParameters layoutParameters) {
}

private void computeMatrixCellsAvailability(LayoutParameters layoutParameters) {
int snakelineMargin = (int) layoutParameters.getZoneLayoutSnakeLinePadding();
List<MatrixCell> allCells = new ArrayList<>(cellsById.values().stream().toList());
// Make empty cells available for snakeline computation
allCells.addAll(emptyCells);
allCells.forEach(cell -> {
int ssX = getX(cell.col());
int ssY = getY(cell.row());
int ssX = getX(cell.col(), snakelineMargin);
int ssY = getY(cell.row(), snakelineMargin);
for (int x = ssX - snakelineMargin; x < ssX - snakelineMargin + matrixCellWidth + snakelineMargin; x++) {
for (int y = ssY - snakelineMargin; y < ssY - snakelineMargin + matrixCellHeight + snakelineMargin; y += layoutParameters.getHorizontalSnakeLinePadding()) {
pathFinderGrid.setAvailability(x, y, true);
Expand All @@ -154,6 +146,7 @@ private void computeMatrixCellsAvailability(LayoutParameters layoutParameters) {
}

private void computeHorizontalHallwaysAvailability(int width, int height, LayoutParameters layoutParameters) {
int snakelineMargin = (int) layoutParameters.getZoneLayoutSnakeLinePadding();
for (int r = 0; r < matrixNbRow; r++) {
for (int x = 0; x < width; x++) {
for (int hy = 0; hy < height; hy += snakelineMargin + matrixCellHeight) {
Expand All @@ -166,6 +159,7 @@ private void computeHorizontalHallwaysAvailability(int width, int height, Layout
}

private void computeVerticalHallwaysAvailability(int width, int height, LayoutParameters layoutParameters) {
int snakelineMargin = (int) layoutParameters.getZoneLayoutSnakeLinePadding();
for (int c = 0; c < matrixNbCol; c++) {
for (int y = 0; y < height; y++) {
for (int hx = 0; hx < width; hx += snakelineMargin + matrixCellWidth) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ void test() {
.setRemoveFictitiousSwitchNodes(true)
.setCgmesScaleFactor(2)
.setCgmesDiagramName("diag")
.setCgmesUseNames(true);
.setCgmesUseNames(true)
.setZoneLayoutSnakeLinePadding(120);

layoutParameters.setComponentsSize(null);

Expand All @@ -62,6 +63,24 @@ void test() {
assertEquals(layoutParameters.getMaxComponentHeight(), layoutParameters2.getMaxComponentHeight(), 0);
assertEquals(layoutParameters.getMinSpaceBetweenComponents(), layoutParameters2.getMinSpaceBetweenComponents(), 0);
assertEquals(layoutParameters.getMinExternCellHeight(), layoutParameters2.getMinExternCellHeight(), 0);
assertEquals(layoutParameters.getBusbarsAlignment(), layoutParameters2.getBusbarsAlignment());
assertEquals(layoutParameters.getComponentsOnBusbars(), layoutParameters2.getComponentsOnBusbars());
assertEquals(layoutParameters.isRemoveFictitiousSwitchNodes(), layoutParameters2.isRemoveFictitiousSwitchNodes());
assertEquals(layoutParameters.getComponentsSize(), layoutParameters2.getComponentsSize());
assertEquals(layoutParameters.getCgmesScaleFactor(), layoutParameters2.getCgmesScaleFactor(), 0);
assertEquals(layoutParameters.getCgmesDiagramName(), layoutParameters2.getCgmesDiagramName());
assertEquals(layoutParameters.isCgmesUseNames(), layoutParameters2.isCgmesUseNames());
assertEquals(layoutParameters.getZoneLayoutSnakeLinePadding(), layoutParameters2.getZoneLayoutSnakeLinePadding());
}

@Test
void testPadding() {
LayoutParameters layoutParameters = new LayoutParameters()
.setVoltageLevelPadding(15, 35, 25, 45)
.setDiagrammPadding(20, 40, 30, 50);

LayoutParameters layoutParameters2 = new LayoutParameters(layoutParameters);

assertEquals(layoutParameters.getVoltageLevelPadding().getLeft(), layoutParameters2.getVoltageLevelPadding().getLeft(), 0);
assertEquals(layoutParameters.getVoltageLevelPadding().getTop(), layoutParameters2.getVoltageLevelPadding().getTop(), 0);
assertEquals(layoutParameters.getVoltageLevelPadding().getRight(), layoutParameters2.getVoltageLevelPadding().getRight(), 0);
Expand All @@ -70,12 +89,5 @@ void test() {
assertEquals(layoutParameters.getDiagramPadding().getTop(), layoutParameters2.getDiagramPadding().getTop(), 0);
assertEquals(layoutParameters.getDiagramPadding().getRight(), layoutParameters2.getDiagramPadding().getRight(), 0);
assertEquals(layoutParameters.getDiagramPadding().getBottom(), layoutParameters2.getDiagramPadding().getBottom(), 0);
assertEquals(layoutParameters.getBusbarsAlignment(), layoutParameters2.getBusbarsAlignment());
assertEquals(layoutParameters.getComponentsOnBusbars(), layoutParameters2.getComponentsOnBusbars());
assertEquals(layoutParameters.isRemoveFictitiousSwitchNodes(), layoutParameters2.isRemoveFictitiousSwitchNodes());
assertEquals(layoutParameters.getComponentsSize(), layoutParameters2.getComponentsSize());
assertEquals(layoutParameters.getCgmesScaleFactor(), layoutParameters2.getCgmesScaleFactor(), 0);
assertEquals(layoutParameters.getCgmesDiagramName(), layoutParameters2.getCgmesDiagramName());
assertEquals(layoutParameters.isCgmesUseNames(), layoutParameters2.isCgmesUseNames());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,8 @@
"removeFictitiousSwitchNodes" : false,
"cgmesScaleFactor" : 1.0,
"cgmesDiagramName" : null,
"cgmesUseNames" : true
"cgmesUseNames" : true,
"zoneLayoutSnakeLinePadding" : 90.0
},
"svgParams" : {
"prefixId" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@
"removeFictitiousSwitchNodes" : false,
"cgmesScaleFactor" : 1.0,
"cgmesDiagramName" : null,
"cgmesUseNames" : true
"cgmesUseNames" : true,
"zoneLayoutSnakeLinePadding" : 90.0
},
"svgParams" : {
"prefixId" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2948,7 +2948,8 @@
"removeFictitiousSwitchNodes" : false,
"cgmesScaleFactor" : 1.0,
"cgmesDiagramName" : null,
"cgmesUseNames" : true
"cgmesUseNames" : true,
"zoneLayoutSnakeLinePadding" : 90.0
},
"svgParams" : {
"prefixId" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@
"removeFictitiousSwitchNodes" : false,
"cgmesScaleFactor" : 1.0,
"cgmesDiagramName" : null,
"cgmesUseNames" : true
"cgmesUseNames" : true,
"zoneLayoutSnakeLinePadding" : 90.0
},
"svgParams" : {
"prefixId" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,8 @@
"removeFictitiousSwitchNodes" : false,
"cgmesScaleFactor" : 1.0,
"cgmesDiagramName" : null,
"cgmesUseNames" : true
"cgmesUseNames" : true,
"zoneLayoutSnakeLinePadding" : 90.0
},
"svgParams" : {
"prefixId" : "",
Expand Down

0 comments on commit b20d43e

Please sign in to comment.