public static <T> SpatialRegistry<T> create() { return new MapSpatialRegistry<>(); }
@Override public Point getPosition(T object) { synchronized (objLocs) { checkArgument(containsObject(object), "RoadUser does not exist: %s.", object); return objLocs.get(object); } }
@Override public ImmutableSet<T> findObjectsInRect(Point min, Point max) { checkArgument(min.x < max.x && min.y < max.y, "Invalid rectangle, expected 'min' < 'max', found %s and %s.", min, max); final ImmutableSet.Builder<T> builder = ImmutableSet.builder(); synchronized (objLocs) { for (final Entry<T, Point> entry : objLocs.entrySet()) { if (isInRect(min, max, entry.getValue())) { builder.add(entry.getKey()); } } } return builder.build(); }
@Before public void setUp() { reg = MapSpatialRegistry.create(); }
@Override public ImmutableSet<T> findNearestObjects(Point position, int n) { checkArgument(n > 0, "n should be strictly positive, found %s.", n); synchronized (objLocs) { if (objLocs.isEmpty()) { return ImmutableSet.of(); } else if (objLocs.size() <= n) { return getObjects(); } final Queue<ObjDist<T>> queue = new PriorityQueue<>(n); for (final Entry<T, Point> entry : objLocs.entrySet()) { final double dist = Point.distance(position, entry.getValue()); if (queue.size() < n) { queue.add(ObjDist.create(entry.getKey(), dist)); } else if (queue.peek().dist() > dist) { queue.remove(); queue.add(ObjDist.create(entry.getKey(), dist)); } } final ImmutableSet.Builder<T> objs = ImmutableSet.builder(); for (final ObjDist<T> od : queue) { objs.add(od.obj()); } return objs.build(); } }
/** * Constructs a new instance. * @param b The builder with configuration options. * @param c A reference to the clock. */ protected CollisionPlaneRoadModel(CollisionPlaneRMB b, Clock c) { super(b); objRadius = b.getObjectRadius(); deltaMax = maxSpeed * unitConversion.toInTime(c.getTickLength(), c.getTimeUnit()); checkArgument(deltaMax <= DMAX_RAD_RATIO * objRadius, "Incompatible parameters. The following condition should hold: deltaMax " + "(%s) <= %s * objRadius (%s), where deltaMax = maxSpeed (%s %s) * " + "tickLength (%s %s).", deltaMax, DMAX_RAD_RATIO, objRadius, maxSpeed, getSpeedUnit(), c.getTickLength(), c.getTimeUnit()); blockingRegistry = MapSpatialRegistry.create(); }
/** * Creates a new instance using the specified {@link Graph} as road structure. * The default units are used as defined by {@link AbstractRoadModel}. * @param g The graph which will be used as road structure. * @param b The builder that contains the properties. */ protected GraphRoadModelImpl(Graph<? extends ConnectionData> g, RoadModelBuilders.AbstractGraphRMB<?, ?, ?> b) { super(b.getDistanceUnit(), b.getSpeedUnit()); graph = g; snapshot = GraphRoadModelSnapshot.create( ImmutableGraph.copyOf(graph), b.getDistanceUnit()); registry = GraphSpatialRegistry.create(MapSpatialRegistry.<RoadUser>create()); }
PlaneRoadModel(RoadModelBuilders.AbstractPlaneRMB<?, ?> b) { super(b.getDistanceUnit(), b.getSpeedUnit()); min = b.getMin(); max = b.getMax(); width = max.x - min.x; height = max.y - min.y; maxSpeed = unitConversion.toInSpeed(b.getMaxSpeed()); snapshot = PlaneRoadModelSnapshot.create(this); planeGraph = new PlaneGraph<>(); registry = MapSpatialRegistry.create(); }