diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/DijkstraPathFinder.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/DijkstraPathFinder.java index 934df590b..db448d4f0 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/DijkstraPathFinder.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/DijkstraPathFinder.java @@ -33,7 +33,10 @@ public List findShortestPath(Grid grid, int startX, int startY, int endX, Grid.Node currentParent = current.getParent(); // Path can be rebuilt only when goal point is reached if (currentPoint.equals(goal)) { - return rebuildPath(current); + List path = rebuildPath(current); + // Make path not available + grid.setAvailability(path, false); + return path; } // Store node already visited visited.add(currentPoint); @@ -60,7 +63,8 @@ public List findShortestPath(Grid grid, int startX, int startY, int endX, return new ArrayList<>(); } - private List rebuildPath(Grid.Node current) { + private List rebuildPath(Grid.Node goal) { + Grid.Node current = goal; // Reconstruct path List path = new ArrayList<>(); while (current != null) { diff --git a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Grid.java b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Grid.java index 22128b89d..4cc0e03b0 100644 --- a/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Grid.java +++ b/single-line-diagram/single-line-diagram-core/src/main/java/com/powsybl/sld/layout/pathfinding/Grid.java @@ -133,6 +133,14 @@ public void setAvailability(int x, int y, boolean available) { getNode(x, y).cost = available ? WALKABLE : NOT_WALKABLE; } + public void setAvailability(Point point, boolean available) { + setAvailability(point.x(), point.y(), available); + } + + public void setAvailability(List path, boolean available) { + path.forEach(p -> setAvailability(p, available)); + } + public boolean isAvailable(Node n) { return isAvailable(n.point); } @@ -144,21 +152,21 @@ public boolean isAvailable(Point point) { public List getNeighbors(Point point) { // Considering only adjacent points List neighbors = new ArrayList<>(); - Node n = getNode(point.x() + 1, point.y()); - if (isAvailable(n)) { - neighbors.add(n); + Node right = getNode(point.x() + 1, point.y()); + Node left = getNode(point.x() - 1, point.y()); + Node up = getNode(point.x(), point.y() + 1); + Node down = getNode(point.x(), point.y() - 1); + if (isAvailable(right)) { + neighbors.add(right); } - n = getNode(point.x() - 1, point.y()); - if (isAvailable(n)) { - neighbors.add(n); + if (isAvailable(left)) { + neighbors.add(left); } - n = getNode(point.x(), point.y() + 1); - if (isAvailable(n)) { - neighbors.add(n); + if (isAvailable(up)) { + neighbors.add(up); } - n = getNode(point.x(), point.y() - 1); - if (isAvailable(n)) { - neighbors.add(n); + if (isAvailable(down)) { + neighbors.add(down); } return neighbors; } diff --git a/single-line-diagram/single-line-diagram-core/src/test/resources/TestCase13ZoneGraphMatrix1x5.svg b/single-line-diagram/single-line-diagram-core/src/test/resources/TestCase13ZoneGraphMatrix1x5.svg index ff66e248e..5544f3720 100644 --- a/single-line-diagram/single-line-diagram-core/src/test/resources/TestCase13ZoneGraphMatrix1x5.svg +++ b/single-line-diagram/single-line-diagram-core/src/test/resources/TestCase13ZoneGraphMatrix1x5.svg @@ -1075,22 +1075,22 @@ - + - + - + - + - + - + diff --git a/single-line-diagram/single-line-diagram-core/src/test/resources/TestCase13ZoneGraphMatrix2x3.svg b/single-line-diagram/single-line-diagram-core/src/test/resources/TestCase13ZoneGraphMatrix2x3.svg index 6e5af48bd..1f0e21b4a 100644 --- a/single-line-diagram/single-line-diagram-core/src/test/resources/TestCase13ZoneGraphMatrix2x3.svg +++ b/single-line-diagram/single-line-diagram-core/src/test/resources/TestCase13ZoneGraphMatrix2x3.svg @@ -1075,22 +1075,22 @@ - + - + - + - + - + - +