Class SpatialIndex<A extends Agent>

java.lang.Object
com.eco.bio7.simulation.SpatialIndex<A>
Type Parameters:
A - the class of the agents

public final class SpatialIndex<A extends Agent> extends Object
A fast search of the agents near a position: the agents are sorted into the cells of a grid (the positions when the index is built). A search looks only into the cells near the position, so the neighbors of all agents cost about n instead of n * n distances (e.g., a flock of thousands of boids). The results are in the order of the list of the agents (the same results as AgentModel.neighbors(Agent, double), in Java and in the HTML page).
SpatialIndex<Boid> index = index(radius); // in step(): the positions at the start of the step
for (Boid b : agents) {
        for (Boid o : index.neighbors(b, radius)) {
                ...
        }
}
A good cell size is the radius of the searches. Build the index again when the agents have moved (e.g., once in each step).
  • Constructor Summary

    Constructors
    Constructor
    Description
    SpatialIndex(List<A> agents, double cellSize)
    An index of the agents (their positions now) with the size of the cells (e.g., the radius of the searches).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    forEachPair(double radius, BiConsumer<A,A> action)
    Calls the action for each pair of agents within the radius (once for a pair: a before b in the list).
    near(double px, double py, double pz, double radius)
    The agents within the radius of the point (in the order of the list).
    nearest(A agent, double radius)
    The nearest other agent within the radius (null if none; the first in the list for equal distances).
    neighbors(A agent, double radius)
    The other agents within the radius of the agent (its position when the index was built; in the order of the list).
    int
    The number of the agents in the index.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SpatialIndex

      public SpatialIndex(List<A> agents, double cellSize)
      An index of the agents (their positions now) with the size of the cells (e.g., the radius of the searches).
  • Method Details

    • near

      public ArrayList<A> near(double px, double py, double pz, double radius)
      The agents within the radius of the point (in the order of the list).
    • neighbors

      public ArrayList<A> neighbors(A agent, double radius)
      The other agents within the radius of the agent (its position when the index was built; in the order of the list).
    • nearest

      public A nearest(A agent, double radius)
      The nearest other agent within the radius (null if none; the first in the list for equal distances).
    • forEachPair

      public void forEachPair(double radius, BiConsumer<A,A> action)
      Calls the action for each pair of agents within the radius (once for a pair: a before b in the list).
    • size

      public int size()
      The number of the agents in the index.