@Override public void addObjectAt(RoadUser obj, Point pos) { if (obj instanceof MovingRoadUser) { checkArgument(!isOccupied(pos), "Cannot add an object on an occupied position: %s.", pos); blockingRegistry.addAt((MovingRoadUser) obj, pos); } super.addObjectAt(obj, pos); }
@Override public Measure<Double, Length> getDistanceOfPath(Iterable<Point> path) throws IllegalArgumentException { final List<Point> pathAsList = Lists.newArrayList(path); checkArgument(pathAsList.size() > 1, "Cannot evaluate the distance of a path with less than two points."); return Measure.valueOf(Graphs.pathLength(pathAsList), getDistanceUnit()); }
@Override public RoadPath getPathTo(MovingRoadUser object, Point destination, Unit<Duration> timeUnit, Measure<Double, Velocity> speed, GeomHeuristic heuristic) { return getPathTo(getPosition(object), destination, timeUnit, speed, heuristic); }
TimeLapse time) { final long startTimeConsumed = time.getTimeConsumed(); Point loc = registry().getPosition(object); getDistanceUnit()); final Measure<Long, Duration> dur = Measure.valueOf(0L, time.getTimeUnit()); checkArgument(isPointInBoundary(path.peek()), "points in the path must be within the predefined boundary of the " + "plane"); final double travelableDistance = computeTravelableDistance(loc, path.peek(), speed, time.getTimeLeft(), time.getTimeUnit()); checkState( registry().addAt(object, loc);
model.addObjectAt(agent, SW); assertEquals(new Point(0, 0), model.getPosition(agent)); model.moveTo(agent, NW, hour(9)); assertTrue( Point.distance(model.getPosition(agent), new Point(0, 9)) < EPSILON); model.followPath(agent, newLinkedList(asList(SW, SE)), hour(10)); assertTrue( Point.distance(model.getPosition(agent), new Point(1, 0)) < EPSILON); model.moveTo(agent, SE, hour(4)); assertTrue( Point.distance(model.getPosition(agent), new Point(5, 0)) < EPSILON); model.moveTo(agent, NW, hour(Math.sqrt(5))); assertTrue( Point.distance(model.getPosition(agent), new Point(4, 2)) < EPSILON); model.moveTo(agent, new Point(6, 2), hour(2)); assertTrue( Point.distance(model.getPosition(agent), new Point(6, 2)) < EPSILON); model.followPath(agent, newLinkedList(asList(NE)), hour(Math.sqrt(5))); assertTrue( Point.distance(model.getPosition(agent), new Point(7, 4)) < EPSILON); model.moveTo(agent, NW, hour(13)); assertTrue(
@Test public void followPath() { final MovingRoadUser mru = new TestRoadUser(); model.addObjectAt(mru, new Point(0, 0)); final Queue<Point> path = asPath(new Point(0, 0), new Point(5, 0), new Point( 5, 5)); final MoveProgress pp = model.followPath(mru, path, hour()); assertEquals(asPath(new Point(5, 0), new Point(5, 5)), path); assertEquals(1, pp.distance().getValue(), EPSILON); assertEquals(hour().getTickLength(), pp.time().getValue(), EPSILON); assertEquals(asList(new Point(0, 0)), pp.travelledNodes()); assertTrue( Point.distance(new Point(1, 0), model.getPosition(mru)) < EPSILON); final MoveProgress pp2 = model.followPath(mru, path, hour(5)); assertEquals(asPath(new Point(5, 5)), path); assertEquals(5, pp2.distance().getValue(), EPSILON); assertEquals(hour(5).getTickLength(), pp2.time().getValue(), EPSILON); assertEquals(asList(new Point(5, 0)), pp2.travelledNodes()); assertTrue( Point.distance(new Point(5, 1), model.getPosition(mru)) < EPSILON); final MoveProgress pp3 = model.followPath(mru, path, hour(50)); assertTrue(path.isEmpty()); assertEquals(4, pp3.distance().getValue(), EPSILON); assertEquals(hour(4).getTickLength(), pp3.time().getValue(), EPSILON); assertEquals(asList(new Point(5, 5)), pp3.travelledNodes()); assertTrue( Point.distance(new Point(5, 5), model.getPosition(mru)) < EPSILON); }
/** * Tests that units are correctly set. */ @Test public void testAbstractRMBUnits() { final PlaneRoadModel prm = RoadModelBuilders.plane() .withDistanceUnit(NonSI.LIGHT_YEAR) .withSpeedUnit(NonSI.C) .build(mock(DependencyProvider.class)); assertThat(prm.getDistanceUnit()).isEqualTo(NonSI.LIGHT_YEAR); assertThat(prm.getSpeedUnit()).isEqualTo(NonSI.C); }
@Test public void checkPointIsInBoundary() { assertTrue(model.isPointInBoundary(new Point(0, 0))); assertTrue(model.isPointInBoundary(new Point(10, 10))); assertTrue(model.isPointInBoundary(new Point(0, 10))); assertTrue(model.isPointInBoundary(new Point(10, 0))); assertTrue(model.isPointInBoundary(new Point(5, 5))); assertTrue(model.isPointInBoundary(new Point(0, 3))); assertFalse(model.isPointInBoundary(new Point(-1, 5))); assertFalse(model.isPointInBoundary(new Point(11, 5))); assertFalse(model.isPointInBoundary(new Point(5, -234))); assertFalse(model.isPointInBoundary(new Point(5, 10.00001))); assertFalse(model.isPointInBoundary(new Point(-5, -0.0001))); assertFalse(model.isPointInBoundary(new Point(14, -0.0009))); assertFalse(model.isPointInBoundary(new Point(100, 100))); assertFalse(model.isPointInBoundary(new Point(-100, 100))); final RandomGenerator rnd = new MersenneTwister(123); for (int i = 0; i < 100; i++) { final Point p = model.getRandomPosition(rnd); assertTrue(model.isPointInBoundary(p)); } }
@Test(expected = IllegalArgumentException.class) public void followPathFail() { final Queue<Point> path = asPath(new Point(0, 0), new Point(5, 0), new Point( 5, 5), new Point(100, 0)); final MovingRoadUser mru = new TestRoadUser(); model.addObjectAt(mru, new Point(0, 0)); model.followPath(mru, path, hour(100)); }
@Override public List<Point> getShortestPathTo(Point from, Point to) { checkArgument( isPointInBoundary(from), "from must be within the predefined boundary of the plane, from is %s, " + "boundary: min %s, max %s.", from, min, max); checkArgument( isPointInBoundary(to), "to must be within the predefined boundary of the plane, to is %s," + " boundary: min %s, max %s.", to, min, max); return asList(from, to); }
@Override public PlaneRoadModel build(DependencyProvider dependencyProvider) { checkArgument( getMin().x < getMax().x && getMin().y < getMax().y, "Min should have coordinates smaller than max, found min %s and max" + " %s.", getMin(), getMax()); return new PlaneRoadModel(this); }
@Override public Measure<Double, Length> getDistanceOfPath(Iterable<Point> path) throws IllegalArgumentException { return getModel().getDistanceOfPath(path); }
@Override public RoadPath getPathTo(Point from, Point to, Unit<Duration> timeUnit, Measure<Double, Velocity> speed, GeomHeuristic heuristic) { return getModel().getPathTo(from, to, timeUnit, speed, heuristic); }
@Override protected MoveProgress doFollowPath(MovingRoadUser object, Queue<Point> path, TimeLapse time) { blockingRegistry.removeObject(object); final MoveProgress mp = super.doFollowPath(object, path, time); blockingRegistry.addAt(object, getPosition(object)); return mp; }
super.computeTravelableDistance(from, to, speed, tLeft, tUnit);
@Override public void addObjectAt(RoadUser obj, Point pos) { checkArgument( isPointInBoundary(pos), "objects can only be added within the boundaries of the plane, %s is " + "not in the boundary.", pos); super.addObjectAt(obj, pos); }
/** * Creates a snapshot of a {@link PlaneRoadModel}. * @param minimum The minimum bound of the plane. * @param maximum The maximum bound of the plane. * @param planeDistanceUnit The distance unit of the model. * @return A snapshot with the desired properties. */ public static RoadModelSnapshot createPlaneRoadModelSnapshot( Point minimum, Point maximum, Unit<Length> planeDistanceUnit) { return new AutoValue_PlaneRoadModelSnapshot( new PlaneRoadModel(RoadModelBuilders.plane().withMinPoint(minimum) .withMaxPoint(maximum).withDistanceUnit(planeDistanceUnit))); }
@Override public RoadPath getPathTo(Point from, Point to, Unit<Duration> timeUnit, Measure<Double, Velocity> speed, GeomHeuristic heuristic) { return RoadPath.create( asList(from, to), heuristic.calculateCost(planeGraph, from, to), heuristic.calculateTravelTime(planeGraph, from, to, getDistanceUnit(), speed, timeUnit)); }