/** * Convenience method for {@link Graphs#findClosestObject}. * @param pos The {@link Point} which is used as reference. * @param rm The {@link RoadModel} which is searched. * @return The closest object in <code>rm</code> to <code>pos</code>. * @see Graphs#findClosestObject */ @Nullable public static RoadUser findClosestObject(Point pos, RoadModel rm) { return findClosestObject(pos, rm, RoadUser.class); }
static int computeRoundedTravelTime(Measure<Double, Velocity> speed, Measure<Double, Length> dist, Unit<Duration> outputTimeUnit) { return DoubleMath.roundToInt( RoadModels.computeTravelTime(speed, dist, outputTimeUnit), RoundingMode.CEILING); }
/** * Returns a list of objects from {@link RoadModel} <code>rm</code> ordered by * its distance to position <code>pos</code>. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance in which the closest objects are * searched. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static List<RoadUser> findClosestObjects(Point pos, RoadModel rm) { return RoadModels.findClosestObjects(pos, rm, RoadUser.class, Integer.MAX_VALUE); }
/** * Returns all {@link RoadUser}s in <code>model</code> that are * <strong>within</strong> a bird-flight distance of <code>radius</code> to * <code>position</code>. * @param position The position which is used to measure distance. * @param model The {@link RoadModel} which contains the objects. * @param radius Objects with a distance smaller than <code>radius</code> to * <code>position</code> are included. * @return A collection of {@link RoadUser}s. */ public static Collection<RoadUser> findObjectsWithinRadius( final Point position, final RoadModel model, final double radius) { return RoadModels.findObjectsWithinRadius(position, model, radius, model.getObjects()); }
/** * Returns all {@link RoadUser}s of type <code> type</code> in * <code>model</code> that are <strong>within</strong> a bird-flight distance * of <code>radius</code> to <code>position</code>. * @param position The position which is used to measure distance. * @param model The {@link RoadModel} which contains the objects. * @param radius Objects with a distance smaller than <code>radius</code> to * <code>position</code> are included. * @param type The {@link Class} of the required type. * @param <T> The type of the objects in the returned collection. * @return A collection of type <code>type</code>. */ public static <T extends RoadUser> Collection<T> findObjectsWithinRadius( final Point position, final RoadModel model, final double radius, final Class<T> type) { return RoadModels.findObjectsWithinRadius(position, model, radius, model.getObjectsOfType(type)); }
/** * Convenience method for {@link Graphs#findClosestObject}. * @param pos The {@link Point} which is used as reference. * @param rm The {@link RoadModel} which is searched. * @param type The type of object that is searched. * @param <T> The type of the returned object. * @return The closest object in <code>rm</code> to <code>pos</code> of type * <code>type</code>. * @see Graphs#findClosestObject */ @Nullable public static <T extends RoadUser> T findClosestObject(Point pos, RoadModel rm, final Class<T> type) { return findClosestObject(pos, rm, rm.getObjectsOfType(type)); }
/** * Searches the closest <code>n</code> objects to position <code>pos</code> in * {@link RoadModel} <code>rm</code>. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance in which the closest objects are * searched. * @param n The maximum number of objects to return where n must be ≥ 0. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static List<RoadUser> findClosestObjects(Point pos, RoadModel rm, int n) { return RoadModels.findClosestObjects(pos, rm, RoadUser.class, n); }
@Override public long getShortestTravelTime(Point from, Point to) { final Iterator<Point> path = roadModel.getShortestPathTo(from, to) .iterator(); long travelTime = 0L; Point prev = path.next(); while (path.hasNext()) { final Point cur = path.next(); final Measure<Double, Length> distance = Measure.valueOf( Point.distance(prev, cur), roadModel.getDistanceUnit()); travelTime += RoadModels.computeTravelTime(vehicleSpeed, distance, timeUnit); prev = cur; } return travelTime; }
@Test public void findObjectsWithinRadiusTest() { final Point ref = new Point(10, 10); assertArrayEquals(asList(o1, o2, o3, o4, o6).toArray(), RoadModels .findObjectsWithinRadius(ref, rm, 15).toArray()); assertArrayEquals(asList(o3).toArray(), RoadModels.findObjectsWithinRadius(ref, rm, 10).toArray()); assertArrayEquals(asList(o2, o3).toArray(), RoadModels .findObjectsWithinRadius(ref, rm, 10.000000001).toArray()); assertArrayEquals(asList().toArray(), RoadModels.findObjectsWithinRadius(ref, rm, 5).toArray()); assertArrayEquals(asList(o1, o2, o3).toArray(), RoadModels .findObjectsWithinRadius(ref, rm, 15, StringRoadUser.class).toArray()); assertArrayEquals(asList(o3).toArray(), RoadModels.findObjectsWithinRadius(ref, rm, 10, StringRoadUser.class) .toArray()); assertArrayEquals(allObjects.toArray(), RoadModels.findObjectsWithinRadius(ref, rm, 30, RoadUser.class) .toArray()); assertArrayEquals(asList().toArray(), RoadModels.findObjectsWithinRadius(ref, rm, 30, EmptyRoadUser.class) .toArray()); }
/** * Convenience method for {@link Graphs#findClosestObject}. * @param pos The {@link Point} which is used as reference. * @param rm The {@link RoadModel} which is searched. * @param predicate A {@link Predicate} indicating which objects are included * in the search. * @return The closest object in <code>rm</code> to <code>pos</code> which * satisfies the <code>predicate</code>. * @see Graphs#findClosestObject */ @Nullable public static RoadUser findClosestObject(Point pos, RoadModel rm, Predicate<RoadUser> predicate) { final Collection<RoadUser> filtered = Collections2.filter(rm.getObjects(), predicate); return findClosestObject(pos, rm, filtered); }
/** * Searches the closest <code>n</code> objects to position <code>pos</code> in * {@link RoadModel} <code>rm</code>. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance in which the closest objects are * searched. * @param type The type of objects which are included in the search. * @param n The maximum number of objects to return where n must be ≥ 0. * @param <T> The type of the objects in the returned collection. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static <T extends RoadUser> List<T> findClosestObjects(Point pos, RoadModel rm, Class<T> type, int n) { return RoadModels.findClosestObjects(pos, rm, rm.getObjectsOfType(type), n); }
Point.distance(points.get(i), points.get(j)), distUnit); final double duration = RoadModels.computeTravelTime(speed, dist, outputTimeUnit);
curr = Optional.fromNullable(RoadModels.findClosestObject( rm.getPosition(this), rm, Parcel.class));
/** * Searches the closest <code>n</code> objects to position <code>pos</code> in * {@link RoadModel} <code>rm</code>. Only the objects that satisfy * <code>predicate</code> are included in the search. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance in which the closest objects are * searched. * @param predicate Only objects that satisfy this predicate will be returned. * @param n The maximum number of objects to return where n must be ≥ 0. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static List<RoadUser> findClosestObjects(Point pos, RoadModel rm, Predicate<RoadUser> predicate, int n) { final Collection<RoadUser> filtered = Collections2.filter(rm.getObjects(), predicate); return RoadModels.findClosestObjects(pos, rm, filtered, n); }
try { travelTime += Math.min(RoadModels.computeTravelTime(vehicleSpeed, distance, timeUnit), RoadModels.computeTravelTime( Measure.valueOf( ((MultiAttributeData) conn.data().get()).getMaxSpeed().get(), } catch (final Exception e) { travelTime += RoadModels.computeTravelTime(vehicleSpeed, distance, timeUnit);
curr = Optional.fromNullable(RoadModels.findClosestObject( rm.getPosition(this), rm, Parcel.class));
/** * Test non positive n value. */ @Test(expected = IllegalArgumentException.class) public void findClosestObjectsTestFail() { RoadModels.findClosestObjects(new Point(5, 5), rm, 0); }
@Test public void findClosestObjectTest() { // actually, o1 and o2 have the same distance to ref (but o1 is added // earlier) assertEquals(o1, RoadModels.findClosestObject(new Point(5, 5), rm)); assertEquals(o2, RoadModels.findClosestObject(new Point(5.000001, 5), rm)); assertEquals(o6, RoadModels.findClosestObject(new Point(5, 5), rm, LongRoadUser.class)); assertEquals(null, RoadModels.findClosestObject(new Point(5, 5), rm, new Predicate<RoadUser>() { @Override public boolean apply(RoadUser input) { return false; } })); }
@Test public void findClosestObjectsTest() { assertEquals(Arrays.asList(o1, o2, o3, o6, o5, o4), RoadModels.findClosestObjects(new Point(5, 5), rm)); assertEquals(Arrays.asList(o1, o2, o3), RoadModels.findClosestObjects(new Point(5, 5), rm, 3)); assertEquals(Arrays.asList(o6, o5, o4), RoadModels.findClosestObjects( new Point(5, 5), rm, LongRoadUser.class, 300)); assertEquals(Arrays.asList(), RoadModels.findClosestObjects( new Point(5, 5), rm, EmptyRoadUser.class, 1)); assertEquals(Arrays.asList(o3, o6, o4, o5), RoadModels.findClosestObjects( new Point(8, 8), rm, new Predicate<RoadUser>() { @Override public boolean apply(RoadUser input) { return input instanceof LongRoadUser || rm.getPosition(input).equals(new Point(15, 15)); } }, 99)); }
@Override protected void tickImpl(TimeLapse time) { final RoadModel rm = getRoadModel(); final PDPModel pm = getPDPModel(); // we always go to the closest available parcel final Parcel closest = (Parcel) RoadModels .findClosestObject(rm.getPosition(this), rm, new Predicate<RoadUser>() { @Override public boolean apply(@Nullable RoadUser input) { return input instanceof Parcel && pm.getParcelState((Parcel) input) == ParcelState.AVAILABLE; } }); if (closest != null) { rm.moveTo(this, closest, time); if (rm.equalPosition(closest, this) && pm .getTimeWindowPolicy() .canPickup(closest.getPickupTimeWindow(), time.getTime(), closest.getPickupDuration())) { pm.pickup(this, closest, time); } } } }