/** * Create a path of bi-directional connections on the specified {@link Graph} * using the specified {@link Point}s. If the points <code>A, B, C</code> are * specified, the four connections: <code>A -> B</code>, * <code>B -> A</code>, <code>B -> C</code> and <code>C -> B</code> * will be added to the graph. * @param graph The graph to which the connections will be added. * @param path Points that will be treated as a path. * @param <E> The type of connection data. */ public static <E extends ConnectionData> void addBiPath(Graph<E> graph, Iterable<Point> path) { addPath(graph, path); addPath(graph, reverse(newArrayList(path))); }
/** * Create a path of bi-directional connections on the specified {@link Graph} * using the specified {@link Point}s. If the points <code>A, B, C</code> are * specified, the four connections: <code>A -> B</code>, * <code>B -> A</code>, <code>B -> C</code> and <code>C -> B</code> * will be added to the graph. * @param graph The graph to which the connections will be added. * @param path Points that will be treated as a path. * @param <E> The type of connection data. */ public static <E extends ConnectionData> void addBiPath(Graph<E> graph, Iterable<Point> path) { addPath(graph, path); addPath(graph, reverse(newArrayList(path))); }
/** * Create a path of bi-directional connections on the specified {@link Graph} * using the specified {@link Point}s. If the points <code>A, B, C</code> are * specified, the four connections: <code>A -> B</code>, * <code>B -> A</code>, <code>B -> C</code> and <code>C -> B</code> * will be added to the graph. * @param graph The graph to which the connections will be added. * @param path Points that will be treated as a path. * @param <E> The type of connection data. */ public static <E extends ConnectionData> void addBiPath(Graph<E> graph, Point... path) { addPath(graph, path); final List<Point> list = Arrays.asList(path); Collections.reverse(list); addPath(graph, list.toArray(new Point[path.length])); }
/** * Create a path of bi-directional connections on the specified {@link Graph} * using the specified {@link Point}s. If the points <code>A, B, C</code> are * specified, the four connections: <code>A -> B</code>, * <code>B -> A</code>, <code>B -> C</code> and <code>C -> B</code> * will be added to the graph. * @param graph The graph to which the connections will be added. * @param path Points that will be treated as a path. * @param <E> The type of connection data. */ public static <E extends ConnectionData> void addBiPath(Graph<E> graph, Point... path) { addPath(graph, path); final List<Point> list = Arrays.asList(path); Collections.reverse(list); addPath(graph, list.toArray(new Point[path.length])); }
static ListenableGraph<LengthData> createSimpleGraph() { final Graph<LengthData> g = new TableGraph<>(); final Table<Integer, Integer, Point> matrix = createMatrix(8, 6, new Point(0, 0)); for (int i = 0; i < matrix.columnMap().size(); i++) { final Iterable<Point> path; if (i % 2 == 0) { path = Lists.reverse(newArrayList(matrix.column(i).values())); } else { path = matrix.column(i).values(); } Graphs.addPath(g, path); } Graphs.addPath(g, matrix.row(0).values()); Graphs.addPath(g, Lists.reverse(newArrayList(matrix.row( matrix.rowKeySet().size() - 1).values()))); return new ListenableGraph<>(g); }
static ListenableGraph<LengthData> createSimpleGraph() { final Graph<LengthData> g = new TableGraph<>(); final Table<Integer, Integer, Point> matrix = createMatrix(8, 6, new Point(0, 0)); for (int i = 0; i < matrix.columnMap().size(); i++) { final Iterable<Point> path; if (i % 2 == 0) { path = Lists.reverse(newArrayList(matrix.column(i).values())); } else { path = matrix.column(i).values(); } Graphs.addPath(g, path); } Graphs.addPath(g, matrix.row(0).values()); Graphs.addPath(g, Lists.reverse(newArrayList(matrix.row( matrix.rowKeySet().size() - 1).values()))); return new ListenableGraph<>(g); }
static void addPath(Graph<?> graph, Point... points) { final List<Point> newPoints = newArrayList(); for (int i = 0; i < points.length - 1; i++) { final double dist = Point.distance(points[i], points[i + 1]); final Point unit = Point.divide(Point.diff(points[i + 1], points[i]), dist); final int numPoints = DoubleMath.roundToInt(dist / POINT_DISTANCE, RoundingMode.FLOOR); for (int j = 0; j < numPoints; j++) { final double factor = j * POINT_DISTANCE; newPoints.add(new Point(points[i].x + factor * unit.x, points[i].y + factor * unit.y)); } } newPoints.add(points[points.length - 1]); Graphs.addPath(graph, newPoints.toArray(new Point[newPoints.size()])); }
static void addPath(Graph<?> graph, Point... points) { final List<Point> newPoints = newArrayList(); for (int i = 0; i < points.length - 1; i++) { final double dist = Point.distance(points[i], points[i + 1]); final Point unit = Point.divide(Point.diff(points[i + 1], points[i]), dist); final int numPoints = DoubleMath.roundToInt(dist / POINT_DISTANCE, RoundingMode.FLOOR); for (int j = 0; j < numPoints; j++) { final double factor = j * POINT_DISTANCE; newPoints.add(new Point(points[i].x + factor * unit.x, points[i].y + factor * unit.y)); } } newPoints.add(points[points.length - 1]); Graphs.addPath(graph, newPoints.toArray(new Point[newPoints.size()])); }
static ListenableGraph<LengthData> createGraph() { final Graph<LengthData> g = new TableGraph<>(); final Table<Integer, Integer, Point> leftMatrix = createMatrix(5, 10, new Point(0, 0)); for (final Map<Integer, Point> column : leftMatrix.columnMap().values()) { Graphs.addBiPath(g, column.values()); } Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_U_ROW).values()); Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_L_ROW).values()); final Table<Integer, Integer, Point> rightMatrix = createMatrix(10, 7, new Point(30, 6)); for (final Map<Integer, Point> row : rightMatrix.rowMap().values()) { Graphs.addBiPath(g, row.values()); } Graphs.addBiPath(g, rightMatrix.column(0).values()); Graphs.addBiPath(g, rightMatrix.column(rightMatrix.columnKeySet().size() - 1).values()); Graphs.addPath(g, rightMatrix.get(RIGHT_CENTER_U_ROW, RIGHT_COL), leftMatrix.get(LEFT_CENTER_U_ROW, LEFT_COL)); Graphs.addPath(g, leftMatrix.get(LEFT_CENTER_L_ROW, LEFT_COL), rightMatrix.get(RIGHT_CENTER_L_ROW, RIGHT_COL)); return new ListenableGraph<>(g); } }
static ListenableGraph<LengthData> createGraph() { final Graph<LengthData> g = new TableGraph<>(); final Table<Integer, Integer, Point> leftMatrix = createMatrix(5, 10, new Point(0, 0)); for (final Map<Integer, Point> column : leftMatrix.columnMap().values()) { Graphs.addBiPath(g, column.values()); } Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_U_ROW).values()); Graphs.addBiPath(g, leftMatrix.row(LEFT_CENTER_L_ROW).values()); final Table<Integer, Integer, Point> rightMatrix = createMatrix(10, 7, new Point(30, 6)); for (final Map<Integer, Point> row : rightMatrix.rowMap().values()) { Graphs.addBiPath(g, row.values()); } Graphs.addBiPath(g, rightMatrix.column(0).values()); Graphs.addBiPath(g, rightMatrix.column(rightMatrix.columnKeySet().size() - 1).values()); Graphs.addPath(g, rightMatrix.get(RIGHT_CENTER_U_ROW, RIGHT_COL), leftMatrix.get(LEFT_CENTER_U_ROW, LEFT_COL)); Graphs.addPath(g, leftMatrix.get(LEFT_CENTER_L_ROW, LEFT_COL), rightMatrix.get(RIGHT_CENTER_L_ROW, RIGHT_COL)); return new ListenableGraph<>(g); } }
@Test public void connectionOrder() { Point N, NE, E, SE, S, SW, W, NW; N = new Point(0, 5); NE = new Point(5, 5); E = new Point(5, 0); SE = new Point(5, -5); S = new Point(0, -5); SW = new Point(-5, -5); W = new Point(-5, 0); NW = new Point(-5, 5); Graphs.addPath(graph, N, NE, E, SE, S, SW, W, NW); final List<Point> points = Arrays.asList(N, NE, E, SE, S, SW, W, NW); final List<Connection<LengthData>> connections = newArrayList(graph .getConnections()); for (int i = 1; i < points.size(); i++) { assertSame(connections.get(i - 1).from(), points.get(i - 1)); assertSame(connections.get(i - 1).to(), points.get(i)); } }
@Test public void followLongPath() { final Point A = new Point(10, 20); final Point B = new Point(10, 22); final Point C = new Point(0, 15); Graphs.addPath(graph, NE, A, B, C); final TestRoadUser ab = new TestRoadUser(); model.addObjectAt(ab, A); model.followPath(ab, asPath(B), hour()); assertEquals(A, model.getConnection(ab).get().from()); assertEquals(B, model.getConnection(ab).get().to()); final TestRoadUser bc = new TestRoadUser(); model.addObjectAt(bc, B); model.followPath(bc, asPath(C), hour()); assertEquals(B, model.getConnection(bc).get().from()); assertEquals(C, model.getConnection(bc).get().to()); final Queue<Point> path = asPath(SW, SE, NE, A, model.getPosition(ab), B, model.getPosition(bc), C); assertEquals(8, path.size()); final TestRoadUser driver1 = new TestRoadUser(); model.addObjectAt(driver1, SW); final MoveProgress pp = model.followPath(driver1, path, hour(44)); assertEquals(hour(44).getTickLength(), pp.time().getValue().longValue()); assertEquals(44, pp.distance().getValue(), EPSILON); assertEquals(asList(SW, SE, NE, A, B), pp.travelledNodes()); }
@Override public void tick(TimeLapse timeLapse) { if (timeLapse.getTime() == 10 * timeMul) { Graphs.addPath(graph, new Point(0, 10), new Point(0, 20), new Point(20, 20), new Point(20, 0), new Point(10, 0)); } else if (timeLapse.getTime() == 16 * timeMul) { Graphs.addBiPath(graph, new Point(20, 0), new Point(40, 0), new Point(40, 20), new Point(20, 20)); } else if (timeLapse.getTime() == 20 * timeMul) { Graphs.addBiPath(graph, new Point(20, 20), new Point(40, 35), new Point(60, 20), new Point(60, 10), new Point(40, 20)); } else if (timeLapse.getTime() == 25 * timeMul) { Graphs.addPath(graph, new Point(60, 10), new Point(60, 6), new Point(56, 6), new Point(60, 2), new Point(40, 0)); } else if (timeLapse.getTime() == 30 * timeMul) { Graphs.addBiPath(graph, new Point(20, 20), new Point(20, 30), new Point(20, 40)); } else if (timeLapse.getTime() == 35 * timeMul) { Graphs.addBiPath(graph, new Point(0, 0), new Point(-10, 0), new Point(-10, -10), new Point(0, -10), new Point(0, 0)); } else if (timeLapse.getTime() == 40 * timeMul) { graph.removeNode(new Point(0, 0)); } else if (timeLapse.getTime() == 45 * timeMul) { graph.addConnection(new Point(20, 40), new Point(20, 400)); } else if (timeLapse.getTime() == 50 * 60000L) { graph.removeConnection(new Point(20, 40), new Point(20, 400)); } else if (timeLapse.getTime() >= 55 * timeMul) { sim.stop(); } }
new TableGraph<LengthData>()); Graphs.addPath(graph, new Point(0, 0), new Point(10, 0), new Point(10, 10), new Point(0, 10), new Point(0, 0)); Graphs.addPath(graph, new Point(0, 0), new Point(0, 20), new Point(20, 20), new Point(20, 0), new Point(0, 0)); Graphs.addBiPath(graph, new Point(20, 0), new Point(40, 0), 20), new Point(60, 10), new Point(40, 20)); Graphs.addPath(graph, new Point(60, 10), new Point(60, 6), new Point(56, 6), new Point(60, 2), new Point(40, 0));
new TableGraph<LengthData>()); Graphs.addPath(graph, new Point(0, 0), new Point(10, 0), new Point(10, 10), new Point(0, 10), new Point(0, 0));