From 2e7a1c564aea96ff6010db9a16880a1275dbada6 Mon Sep 17 00:00:00 2001 From: Mikael Capelle Date: Fri, 2 Mar 2018 17:49:30 +0100 Subject: [PATCH] Add my AStar for computing path. Change ID and map names in file. --- FORMAT | 61 ------------------- FORMAT_PATH | 21 ------- src/main/org/insa/graph/Graph.java | 17 +++++- .../graph/io/BinaryGraphReaderInsa2016.java | 2 +- .../graph/io/BinaryGraphReaderInsa2018.java | 20 +++++- .../org/insa/graph/io/BinaryPathReader.java | 7 ++- .../org/insa/graph/io/BinaryPathWriter.java | 7 ++- .../insa/graph/io/GraphReaderObserver.java | 2 +- .../insa/graph/io/MapMismatchException.java | 8 +-- .../insa/graphics/GraphReaderProgressBar.java | 4 +- src/main/org/insa/graphics/MainWindow.java | 9 ++- src/test/org/insa/graph/PathTest.java | 2 +- 12 files changed, 57 insertions(+), 103 deletions(-) delete mode 100644 FORMAT delete mode 100644 FORMAT_PATH diff --git a/FORMAT b/FORMAT deleted file mode 100644 index 3cd3022..0000000 --- a/FORMAT +++ /dev/null @@ -1,61 +0,0 @@ -=== Format des fichiers .map === - - - Version du document (= version du format) : 4 - - - Sauf mention contraire, les entiers sont codés en big endian (compatible DataOutputStream). - - [No d'octets] = signification - - [0-3] = Magic number 0xbacaff (doit se trouver au début du fichier) - [4-7] = Version du format - [8-11] = Identifiant de carte - [12-15] = Numéro de zone - [16-19] = Nombre de descripteurs dans ce fichier - [20-23] = Nombre de noeuds dans ce fichier - - [24-..] = - * Tous les noeuds, les uns après les autres, en commençant par le numéro 0. Voir le format d'un noeud. - * Puis un octet à 255. - - * Puis, tous les descripteurs, les uns après les autres, en commençant par le numéro 0. - Voir le format des descripteurs. - * Puis un octet à 254. - - * Puis, toutes les routes sortantes (routes sortantes du premier noeud, puis celles du deuxième noeud, etc. ) - * Puis un octet à 253. - - (fin du fichier) - - -=== Format des noeuds === - - [0-3] = longitude sur 32 bits (à diviser par 1E6) - [4-7] = latitude sur 32 bits (à diviser par 1E6) - [8] = Nombre de routes sortantes sur 8 bits - - -=== Format des routes sortantes (taille variable car dépend du nombre de segments) === - - [0] = Numéro de zone du noeud destination (8 bits) - [1-3] = Numéro du noeud destination, dans la zone donnée (24 bits, big endian) - [4-6] = Numéro de descripteur (24 bits) - [7-8] = Longueur de l'arête (16 bits), en mètres, prenant en compte tous les segments. - [9-10] = Nombre de segments (16 bits), éventuellement 0. - [11-...] = Segments - - -=== Format des segments === - - [0-1] = Delta de longitude, sur 16 bits signés (à diviser par 2.0E5) - [2-3] = Delta de latitude, sur 16 bits signés (à diviser par 2.0E5) - -=== Format des descripteurs (la taille est variable, car elle dépend du nom du chemin) === - - [0] = Un caractère indiquant le type de chemin (voir dans Descripteur.java) - [1] - .bit 7 = sens unique - .bits 0-6 = vitesse max en km/h à multiplier par 5. - - [2-] = Nom du chemin, de type String-UTF8 (les deux premiers octets donnent la longueur de la chaîne) - - diff --git a/FORMAT_PATH b/FORMAT_PATH deleted file mode 100644 index dbc0615..0000000 --- a/FORMAT_PATH +++ /dev/null @@ -1,21 +0,0 @@ -=== Format des fichiers .path === - - - Version du document (= version du format) : 1 - - - Sauf mention contraire, les entiers sont codés en big endian (compatible DataOutputStream). - - [No d'octets] = signification - - [0-3] = Magic number 0xdecafe (doit se trouver au début du fichier) - [4-7] = Version du format - [8-11] = Identifiant de carte - [12-15] = Nombre de noeuds dans le chemin - [16-19] = Identifiant du premier noeud (8 bits zone + 24 bits numéro noeud) - [20-23] = Identifiant du dernier noeud (8 bits zone + 24 bits numéro noeud) - - [24-27] = Identifiant du premier noeud (encore) - [28-31] = Identifiant du deuxième noeud - [32-35] = Identifiant du troisième noeud - etc. - [derniers octets] = Identifiant du dernier noeud - diff --git a/src/main/org/insa/graph/Graph.java b/src/main/org/insa/graph/Graph.java index 97f7d8c..a5c3db0 100644 --- a/src/main/org/insa/graph/Graph.java +++ b/src/main/org/insa/graph/Graph.java @@ -6,7 +6,10 @@ import java.util.List; public class Graph { // Map identifier. - private final int mapId; + private final String mapId; + + // Map name + private final String mapName; // Nodes of the graph. private final List nodes; @@ -15,8 +18,9 @@ public class Graph { * @param mapId ID of this graph. * @param list List of nodes for this graph. */ - public Graph(int mapId, List list) { + public Graph(String mapId, String mapName, List list) { this.mapId = mapId; + this.mapName = mapName; this.nodes = list; } @@ -50,10 +54,17 @@ public class Graph { /** * @return Map ID of this graph. */ - public int getMapId() { + public String getMapId() { return mapId; } + /** + * @return Name of the map associated with this graph. + */ + public String getMapName() { + return mapName; + } + /** * @return Return the transpose graph of this graph. */ diff --git a/src/main/org/insa/graph/io/BinaryGraphReaderInsa2016.java b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2016.java index f22632b..5711006 100644 --- a/src/main/org/insa/graph/io/BinaryGraphReaderInsa2016.java +++ b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2016.java @@ -179,7 +179,7 @@ public class BinaryGraphReaderInsa2016 extends BinaryReader implements GraphRead // Check format. checkByteOrThrow(253); - return new Graph(mapId, nodes); + return new Graph(Integer.toHexString(mapId), "", nodes); } /** diff --git a/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java index 038ea5e..e6262ba 100644 --- a/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java +++ b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java @@ -12,8 +12,8 @@ import org.insa.graph.Graph; import org.insa.graph.Node; import org.insa.graph.Point; import org.insa.graph.RoadInformation; -import org.insa.graph.RoadInformation.AccessRestriction; import org.insa.graph.RoadInformation.AccessMode; +import org.insa.graph.RoadInformation.AccessRestriction; import org.insa.graph.RoadInformation.RoadType; public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphReader { @@ -22,6 +22,9 @@ public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphRead private static final int VERSION = 5; private static final int MAGIC_NUMBER = 0x208BC3B3; + // Length of the map id field (in bytes) + protected static final int MAP_ID_FIELD_LENGTH = 32; + // Some masks... private static final int MASK_UNKNOWN = 0x01; private static final int MASK_PRIVATE = 0x02; @@ -128,7 +131,18 @@ public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphRead checkVersionOrThrow(dis.readInt()); // Read map id. - int mapId = dis.readInt(); + String mapId; + String mapName = ""; + + if (getCurrentVersion() < 6) { + mapId = "0x" + Integer.toHexString(dis.readInt()); + } + else { + byte[] byteId = new byte[MAP_ID_FIELD_LENGTH]; + dis.read(byteId); + mapId = new String(byteId, "UTF-8"); + mapName = dis.readUTF(); + } observers.forEach((observer) -> observer.notifyStartReading(mapId)); @@ -229,7 +243,7 @@ public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphRead observers.forEach((observer) -> observer.notifyEndReading()); - return new Graph(mapId, nodes); + return new Graph(mapId, mapName, nodes); } /** diff --git a/src/main/org/insa/graph/io/BinaryPathReader.java b/src/main/org/insa/graph/io/BinaryPathReader.java index 8b5787c..1f62fbc 100644 --- a/src/main/org/insa/graph/io/BinaryPathReader.java +++ b/src/main/org/insa/graph/io/BinaryPathReader.java @@ -26,9 +26,12 @@ public class BinaryPathReader extends BinaryReader implements PathReader { checkVersionOrThrow(dis.readInt()); // Read map ID and check against graph. - int mapId = dis.readInt(); + byte[] mapIdBytes = new byte[BinaryGraphReaderInsa2018.MAP_ID_FIELD_LENGTH]; + dis.read(mapIdBytes); - if (mapId != graph.getMapId()) { + String mapId = new String(mapIdBytes, "UTF-8"); + + if (!mapId.equals(graph.getMapId())) { throw new MapMismatchException(mapId, graph.getMapId()); } diff --git a/src/main/org/insa/graph/io/BinaryPathWriter.java b/src/main/org/insa/graph/io/BinaryPathWriter.java index ac7cbea..1c729df 100644 --- a/src/main/org/insa/graph/io/BinaryPathWriter.java +++ b/src/main/org/insa/graph/io/BinaryPathWriter.java @@ -2,6 +2,7 @@ package org.insa.graph.io; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Arrays; import org.insa.graph.Arc; import org.insa.graph.Path; @@ -23,9 +24,11 @@ public class BinaryPathWriter extends BinaryWriter implements PathWriter { dos.writeInt(BinaryPathReader.VERSION); // Write map id. - dos.writeInt(path.getGraph().getMapId()); + byte[] bytes = Arrays.copyOf(path.getGraph().getMapId().getBytes("UTF-8"), + BinaryGraphReaderInsa2018.MAP_ID_FIELD_LENGTH); + dos.write(bytes); - // Write number of racs + // Write number of arcs dos.writeInt(path.getArcs().size() + 1); // Write origin / destination. diff --git a/src/main/org/insa/graph/io/GraphReaderObserver.java b/src/main/org/insa/graph/io/GraphReaderObserver.java index 44d5753..ed614f7 100644 --- a/src/main/org/insa/graph/io/GraphReaderObserver.java +++ b/src/main/org/insa/graph/io/GraphReaderObserver.java @@ -12,7 +12,7 @@ public interface GraphReaderObserver { * * @param mapId ID of the graph. */ - public void notifyStartReading(int mapId); + public void notifyStartReading(String mapId); /** * Notify that the graph has been fully read. diff --git a/src/main/org/insa/graph/io/MapMismatchException.java b/src/main/org/insa/graph/io/MapMismatchException.java index 79be472..095d461 100644 --- a/src/main/org/insa/graph/io/MapMismatchException.java +++ b/src/main/org/insa/graph/io/MapMismatchException.java @@ -9,14 +9,14 @@ public class MapMismatchException extends IOException { */ private static final long serialVersionUID = 3076730078387819138L; // Actual and expected magic numbers. - private int actualMapId, expectedMapId; + private String actualMapId, expectedMapId; /** * * @param actualVersion * @param expectedVersion */ - public MapMismatchException(int actualMapId, int expectedMapId) { + public MapMismatchException(String actualMapId, String expectedMapId) { super(); this.actualMapId = actualMapId; this.expectedMapId = expectedMapId; @@ -25,14 +25,14 @@ public class MapMismatchException extends IOException { /** * @return */ - public int getActualMapId() { + public String getActualMapId() { return actualMapId; } /** * @return */ - public int getExpectedMapId() { + public String getExpectedMapId() { return expectedMapId; } } diff --git a/src/main/org/insa/graphics/GraphReaderProgressBar.java b/src/main/org/insa/graphics/GraphReaderProgressBar.java index eaf8eef..8b0c1f4 100644 --- a/src/main/org/insa/graphics/GraphReaderProgressBar.java +++ b/src/main/org/insa/graphics/GraphReaderProgressBar.java @@ -73,8 +73,8 @@ public class GraphReaderProgressBar extends JDialog implements GraphReaderObserv } @Override - public void notifyStartReading(int mapId) { - setTitle(String.format("Reading graph %#x... ", mapId)); + public void notifyStartReading(String mapId) { + setTitle("Reading graph " + mapId + "... "); setVisible(true); } diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index 0534b5d..ac77fcb 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -278,6 +278,7 @@ public class MainWindow extends JFrame { threadTimer.stop(); threadPanel.setVisible(false); currentThread.setThread(null); + spPanel.setEnabled(true); } private void displayShortestPathSolution(ShortestPathSolution solution) { @@ -428,8 +429,12 @@ public class MainWindow extends JFrame { } notifyNewGraphLoaded(); - graphInfoPanel.setText(String.format("Map ID: %#x, %d nodes", graph.getMapId(), - graph.getNodes().size())); + String info = graph.getMapId(); + if (graph.getMapName() != null && !graph.getMapName().isEmpty()) { + info += " - " + graph.getMapName(); + } + info += ", " + graph.getNodes().size() + " nodes"; + graphInfoPanel.setText(info); drawGraph(); for (JMenuItem item: graphLockItems) { diff --git a/src/test/org/insa/graph/PathTest.java b/src/test/org/insa/graph/PathTest.java index ffc6dfd..d8c3b60 100644 --- a/src/test/org/insa/graph/PathTest.java +++ b/src/test/org/insa/graph/PathTest.java @@ -52,7 +52,7 @@ public class PathTest { d2e = new Arc(nodes[3], nodes[4], 20, speed20, null); e2d = new Arc(nodes[4], nodes[0], 10, speed10, null); - graph = new Graph(0, Arrays.asList(nodes)); + graph = new Graph("ID", "", Arrays.asList(nodes)); emptyPath = new Path(graph, new ArrayList()); shortPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1 }));