Browse Source

Merge new-length.

Holt59 6 years ago
parent
commit
440dc48c6a

+ 1
- 1
src/main/org/insa/algo/AbstractInputData.java View File

@@ -20,7 +20,7 @@ public abstract class AbstractInputData {
20 20
     }
21 21
 
22 22
     /**
23
-     * Filtering inteface for arcs - This class can be used to indicate to an
23
+     * Filtering interface for arcs - This class can be used to indicate to an
24 24
      * algorithm which arc can be used.
25 25
      *
26 26
      */

+ 1
- 1
src/main/org/insa/graph/Arc.java View File

@@ -31,7 +31,7 @@ public interface Arc {
31 31
     /**
32 32
      * @return Length of this arc, in meters.
33 33
      */
34
-    public int getLength();
34
+    public float getLength();
35 35
 
36 36
     /**
37 37
      * @return Minimum time required to travel this arc, in seconds.

+ 1
- 1
src/main/org/insa/graph/ArcBackward.java View File

@@ -37,7 +37,7 @@ class ArcBackward implements Arc {
37 37
     }
38 38
 
39 39
     @Override
40
-    public int getLength() {
40
+    public float getLength() {
41 41
         return this.originalArc.getLength();
42 42
     }
43 43
 

+ 3
- 3
src/main/org/insa/graph/ArcForward.java View File

@@ -15,7 +15,7 @@ class ArcForward implements Arc {
15 15
     private final Node origin, destination;
16 16
 
17 17
     // Length of the road (in meters).
18
-    private final int length;
18
+    private final float length;
19 19
 
20 20
     // Road information.
21 21
     private final RoadInformation info;
@@ -32,7 +32,7 @@ class ArcForward implements Arc {
32 32
      * @param roadInformation Road information for this arc.
33 33
      * @param points Points representing this arc.
34 34
      */
35
-    protected ArcForward(Node origin, Node dest, int length, RoadInformation roadInformation,
35
+    protected ArcForward(Node origin, Node dest, float length, RoadInformation roadInformation,
36 36
             ArrayList<Point> points) {
37 37
         this.origin = origin;
38 38
         this.destination = dest;
@@ -52,7 +52,7 @@ class ArcForward implements Arc {
52 52
     }
53 53
 
54 54
     @Override
55
-    public int getLength() {
55
+    public float getLength() {
56 56
         return length;
57 57
     }
58 58
 

+ 3
- 3
src/main/org/insa/graph/GraphStatistics.java View File

@@ -14,7 +14,7 @@ public class GraphStatistics {
14 14
     private final int maximumSpeed;
15 15
 
16 16
     // Maximum length of any arc on this graph.
17
-    private final int maximumLength;
17
+    private final float maximumLength;
18 18
 
19 19
     /**
20 20
      * Create a new GraphStatistics instance with the given value.
@@ -23,7 +23,7 @@ public class GraphStatistics {
23 23
      * be used to indicate that this graph has no maximum limitation.
24 24
      * @param maximumLength Maximum length of any arc of the graph.
25 25
      */
26
-    public GraphStatistics(int maximumSpeed, int maximumLength) {
26
+    public GraphStatistics(int maximumSpeed, float maximumLength) {
27 27
         this.maximumLength = maximumLength;
28 28
         this.maximumSpeed = maximumSpeed;
29 29
     }
@@ -39,7 +39,7 @@ public class GraphStatistics {
39 39
     /**
40 40
      * @return Maximum length of any arc in the graph.
41 41
      */
42
-    public int getMaximumLength() {
42
+    public float getMaximumLength() {
43 43
         return this.maximumLength;
44 44
     }
45 45
 

+ 4
- 1
src/main/org/insa/graph/Node.java View File

@@ -31,7 +31,7 @@ public class Node implements Comparable<Node> {
31 31
      * 
32 32
      * @return The newly created forward arc (origin to destination).
33 33
      */
34
-    public static Arc linkNodes(Node origin, Node destination, int length,
34
+    public static Arc linkNodes(Node origin, Node destination, float length,
35 35
             RoadInformation roadInformation, ArrayList<Point> points) {
36 36
         ArcForward arc = new ArcForward(origin, destination, length, roadInformation, points);
37 37
         origin.addSuccessor(arc);
@@ -95,6 +95,9 @@ public class Node implements Comparable<Node> {
95 95
 
96 96
     @Override
97 97
     public boolean equals(Object other) {
98
+        if (other == null) {
99
+            return false;
100
+        }
98 101
         if (other instanceof Node) {
99 102
             return getId() == ((Node) other).getId();
100 103
         }

+ 8
- 2
src/main/org/insa/graph/io/BinaryGraphReader.java View File

@@ -212,7 +212,7 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
212 212
         checkByteOrThrow(254);
213 213
 
214 214
         // Read successors and convert to arcs.
215
-        int maxLength = 0;
215
+        float maxLength = 0;
216 216
         final int copyNbTotalSuccesors = nbTotalSuccessors; // Stupid Java...
217 217
         observers.forEach((observer) -> observer.notifyStartReadingArcs(copyNbTotalSuccesors));
218 218
         for (int node = 0; node < nbNodes; ++node) {
@@ -225,7 +225,13 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
225 225
                 int descrNum = this.read24bits();
226 226
 
227 227
                 // Length of the arc.
228
-                int length = dis.readUnsignedShort();
228
+                float length;
229
+                if (getCurrentVersion() < 8) {
230
+                    length = dis.readUnsignedShort();
231
+                }
232
+                else {
233
+                    length = dis.readInt() / 1000.0f;
234
+                }
229 235
                 maxLength = Math.max(length, maxLength);
230 236
 
231 237
                 // Number of segments.

+ 2
- 2
src/main/org/insa/graphics/PathsPanel.java View File

@@ -126,9 +126,9 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
126 126
             String info = "";
127 127
 
128 128
             // Display length
129
-            int length = path.getLength();
129
+            float length = path.getLength();
130 130
             if (length < 2000) {
131
-                info += String.format("Length = %d meters", length);
131
+                info += String.format("Length = %.1f meters", length);
132 132
             }
133 133
             else {
134 134
                 info += String.format("Length = %.3f kilometers", length / 1000.);

+ 27
- 27
src/test/org/insa/graph/PathTest.java View File

@@ -22,7 +22,7 @@ public class PathTest {
22 22
 
23 23
     // List of arcs in the graph, a2b is the arc from node A (0) to B (1).
24 24
     @SuppressWarnings("unused")
25
-    private static ArcForward a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d;
25
+    private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d;
26 26
 
27 27
     // Some paths...
28 28
     private static Path emptyPath, shortPath, longPath, loopPath, longLoopPath, invalidPath;
@@ -41,26 +41,26 @@ public class PathTest {
41 41
         }
42 42
 
43 43
         // Add arcs...
44
-        a2b = new ArcForward(nodes[0], nodes[1], 10, speed10, null);
45
-        a2c = new ArcForward(nodes[0], nodes[2], 15, speed10, null);
46
-        a2e = new ArcForward(nodes[0], nodes[4], 15, speed20, null);
47
-        b2c = new ArcForward(nodes[1], nodes[2], 10, speed10, null);
48
-        c2d_1 = new ArcForward(nodes[2], nodes[3], 20, speed10, null);
49
-        c2d_2 = new ArcForward(nodes[2], nodes[3], 10, speed10, null);
50
-        c2d_3 = new ArcForward(nodes[2], nodes[3], 15, speed20, null);
51
-        d2a = new ArcForward(nodes[3], nodes[0], 15, speed10, null);
52
-        d2e = new ArcForward(nodes[3], nodes[4], 20, speed20, null);
53
-        e2d = new ArcForward(nodes[4], nodes[0], 10, speed10, null);
44
+        a2b = Node.linkNodes(nodes[0], nodes[1], 10, speed10, null);
45
+        a2c = Node.linkNodes(nodes[0], nodes[2], 15, speed10, null);
46
+        a2e = Node.linkNodes(nodes[0], nodes[4], 15, speed20, null);
47
+        b2c = Node.linkNodes(nodes[1], nodes[2], 10, speed10, null);
48
+        c2d_1 = Node.linkNodes(nodes[2], nodes[3], 20, speed10, null);
49
+        c2d_2 = Node.linkNodes(nodes[2], nodes[3], 10, speed10, null);
50
+        c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null);
51
+        d2a = Node.linkNodes(nodes[3], nodes[0], 15, speed10, null);
52
+        d2e = Node.linkNodes(nodes[3], nodes[4], 20, speed20, null);
53
+        e2d = Node.linkNodes(nodes[4], nodes[0], 10, speed10, null);
54 54
 
55 55
         graph = new Graph("ID", "", Arrays.asList(nodes), null);
56 56
 
57 57
         emptyPath = new Path(graph, new ArrayList<Arc>());
58
-        shortPath = new Path(graph, Arrays.asList(new ArcForward[]{ a2b, b2c, c2d_1 }));
59
-        longPath = new Path(graph, Arrays.asList(new ArcForward[]{ a2b, b2c, c2d_1, d2e }));
60
-        loopPath = new Path(graph, Arrays.asList(new ArcForward[]{ a2b, b2c, c2d_1, d2a }));
58
+        shortPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1 }));
59
+        longPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2e }));
60
+        loopPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a }));
61 61
         longLoopPath = new Path(graph,
62
-                Arrays.asList(new ArcForward[]{ a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c }));
63
-        invalidPath = new Path(graph, Arrays.asList(new ArcForward[]{ a2b, c2d_1, d2e }));
62
+                Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c }));
63
+        invalidPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, c2d_1, d2e }));
64 64
 
65 65
     }
66 66
 
@@ -103,11 +103,11 @@ public class PathTest {
103 103
 
104 104
     @Test
105 105
     public void testGetLength() {
106
-        assertEquals(emptyPath.getLength(), 0);
107
-        assertEquals(shortPath.getLength(), 40);
108
-        assertEquals(longPath.getLength(), 60);
109
-        assertEquals(loopPath.getLength(), 55);
110
-        assertEquals(longLoopPath.getLength(), 120);
106
+        assertEquals(emptyPath.getLength(), 0, 1e-6);
107
+        assertEquals(shortPath.getLength(), 40, 1e-6);
108
+        assertEquals(longPath.getLength(), 60, 1e-6);
109
+        assertEquals(loopPath.getLength(), 55, 1e-6);
110
+        assertEquals(longLoopPath.getLength(), 120, 1e-6);
111 111
     }
112 112
 
113 113
     @Test
@@ -122,12 +122,12 @@ public class PathTest {
122 122
     @Test
123 123
     public void testCreateFastestPathFromNodes() {
124 124
         Path path;
125
-        ArcForward[] expected;
125
+        Arc[] expected;
126 126
 
127 127
         // Simple construction
128 128
         path = Path.createFastestPathFromNodes(graph,
129 129
                 Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] }));
130
-        expected = new ArcForward[]{ a2b, b2c };
130
+        expected = new Arc[]{ a2b, b2c };
131 131
         assertEquals(expected.length, path.getArcs().size());
132 132
         for (int i = 0; i < expected.length; ++i) {
133 133
             assertEquals(expected[i], path.getArcs().get(i));
@@ -136,7 +136,7 @@ public class PathTest {
136 136
         // Not so simple construction
137 137
         path = Path.createFastestPathFromNodes(graph,
138 138
                 Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] }));
139
-        expected = new ArcForward[]{ a2b, b2c, c2d_3 };
139
+        expected = new Arc[]{ a2b, b2c, c2d_3 };
140 140
         assertEquals(expected.length, path.getArcs().size());
141 141
         for (int i = 0; i < expected.length; ++i) {
142 142
             assertEquals(expected[i], path.getArcs().get(i));
@@ -146,12 +146,12 @@ public class PathTest {
146 146
     @Test
147 147
     public void testCreateShortestPathFromNodes() {
148 148
         Path path;
149
-        ArcForward[] expected;
149
+        Arc[] expected;
150 150
 
151 151
         // Simple construction
152 152
         path = Path.createShortestPathFromNodes(graph,
153 153
                 Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] }));
154
-        expected = new ArcForward[]{ a2b, b2c };
154
+        expected = new Arc[]{ a2b, b2c };
155 155
         assertEquals(expected.length, path.getArcs().size());
156 156
         for (int i = 0; i < expected.length; ++i) {
157 157
             assertEquals(expected[i], path.getArcs().get(i));
@@ -160,7 +160,7 @@ public class PathTest {
160 160
         // Not so simple construction
161 161
         path = Path.createShortestPathFromNodes(graph,
162 162
                 Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] }));
163
-        expected = new ArcForward[]{ a2b, b2c, c2d_2 };
163
+        expected = new Arc[]{ a2b, b2c, c2d_2 };
164 164
         assertEquals(expected.length, path.getArcs().size());
165 165
         for (int i = 0; i < expected.length; ++i) {
166 166
             assertEquals(expected[i], path.getArcs().get(i));

Loading…
Cancel
Save