sim.field.grid
Class SparseGrid3D

java.lang.Object
  extended bysim.field.SparseField
      extended bysim.field.grid.SparseGrid3D
All Implemented Interfaces:
java.io.Serializable

public class SparseGrid3D
extends SparseField

A storage facility for sparse objects in discrete 3D space, using HashMaps. SparseGrid3D differs from ObjectGrid3D in several respects:

Generally speaking, if you have a grid of objects, one per location, you should use an ObjectGrid3D. If you have a large grid occupied by a few objects, or those objects can pile up on the same grid location, you should use a SparseGrid3D.

In either case, you might consider storing the location of an object IN THE OBJECT ITSELF if you need to query for the object location often -- it's faster than the hashtable lookup in SparseGrid3D, and certainly faster than searching the entire array of an ObjectGrid3D.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class sim.field.SparseField
SparseField.LocationAndIndex
 
Field Summary
protected  int height
           
protected  int length
           
protected  int width
           
 
Fields inherited from class sim.field.SparseField
allObjects, LARGE_BAG_RATIO, locationAndIndexHash, MIN_BAG_SIZE, objectHash, removeEmptyBags, replaceLargeBags
 
Constructor Summary
SparseGrid3D(int width, int height, int length)
           
 
Method Summary
 int getHeight()
           
 int getLength()
           
 void getNeighborsHamiltonianDistance(int x, int y, int z, int dist, boolean toroidal, Bag result, IntBag xPos, IntBag yPos, IntBag zPos)
           
 void getNeighborsHamiltonianDistance(int x, int y, int z, int dist, boolean toroidal, IntBag xPos, IntBag yPos, IntBag zPos)
           
 void getNeighborsMaxDistance(int x, int y, int z, int dist, boolean toroidal, Bag result, IntBag xPos, IntBag yPos, IntBag zPos)
           
 void getNeighborsMaxDistance(int x, int y, int z, int dist, boolean toroidal, IntBag xPos, IntBag yPos, IntBag zPos)
           
 Int3D getObjectLocation(java.lang.Object obj)
           
 Double3D getObjectLocationAsDouble3D(java.lang.Object obj)
           
 Bag getObjectsAtLocation(int x, int y, int z)
          Returns a bag containing all the objects at a given location, or possibly null (either that or an empty Bag, who can tell?) when there are no objects at the location.
 Bag getObjectsAtLocations(IntBag xPos, IntBag yPos, IntBag zPos, Bag result)
          For each location, puts all such objects into the result bag.
 int getWidth()
           
 int numObjectsAtLocation(int x, int y, int z)
           
 Bag removeObjectsAtLocation(Int3D location)
           
 Bag removeObjectsAtLocation(int x, int y, int z)
           
 boolean setObjectLocation(java.lang.Object obj, Int3D location)
           
 boolean setObjectLocation(java.lang.Object obj, int x, int y, int z)
           
 int stx(int x)
           
 int sty(int y)
           
 int stz(int z)
           
 int tx(int x)
           
 int ty(int y)
           
 int tz(int z)
           
 
Methods inherited from class sim.field.SparseField
clear, exists, getAllObjects, getObjectIndex, getObjectsAtLocation, getObjectsAtLocations, getRawObjectLocation, iterator, locationBagIterator, numObjectsAtLocation, remove, removeObjectsAtLocation, setObjectLocation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

width

protected int width

height

protected int height

length

protected int length
Constructor Detail

SparseGrid3D

public SparseGrid3D(int width,
                    int height,
                    int length)
Method Detail

getWidth

public int getWidth()

getHeight

public int getHeight()

getLength

public int getLength()

tx

public int tx(int x)

ty

public int ty(int y)

tz

public int tz(int z)

stx

public int stx(int x)

sty

public int sty(int y)

stz

public int stz(int z)

numObjectsAtLocation

public int numObjectsAtLocation(int x,
                                int y,
                                int z)

getObjectsAtLocation

public Bag getObjectsAtLocation(int x,
                                int y,
                                int z)
Returns a bag containing all the objects at a given location, or possibly null (either that or an empty Bag, who can tell?) when there are no objects at the location. You should NOT MODIFY THIS BAG. This is the actual container bag, and modifying it will almost certainly break the Sparse Field object. If you want to modify the bag, make a copy and modify the copy instead, using something along the lines of new Bag(foo.getObjectsAtLocation(location)) . Furthermore, changing values in the Sparse Field may result in a different bag being used -- so you should not rely on this bag staying valid.


getObjectLocationAsDouble3D

public Double3D getObjectLocationAsDouble3D(java.lang.Object obj)

getObjectLocation

public Int3D getObjectLocation(java.lang.Object obj)

removeObjectsAtLocation

public Bag removeObjectsAtLocation(int x,
                                   int y,
                                   int z)

removeObjectsAtLocation

public Bag removeObjectsAtLocation(Int3D location)

setObjectLocation

public boolean setObjectLocation(java.lang.Object obj,
                                 int x,
                                 int y,
                                 int z)

setObjectLocation

public boolean setObjectLocation(java.lang.Object obj,
                                 Int3D location)

getNeighborsMaxDistance

public void getNeighborsMaxDistance(int x,
                                    int y,
                                    int z,
                                    int dist,
                                    boolean toroidal,
                                    IntBag xPos,
                                    IntBag yPos,
                                    IntBag zPos)

getNeighborsHamiltonianDistance

public void getNeighborsHamiltonianDistance(int x,
                                            int y,
                                            int z,
                                            int dist,
                                            boolean toroidal,
                                            IntBag xPos,
                                            IntBag yPos,
                                            IntBag zPos)

getNeighborsMaxDistance

public void getNeighborsMaxDistance(int x,
                                    int y,
                                    int z,
                                    int dist,
                                    boolean toroidal,
                                    Bag result,
                                    IntBag xPos,
                                    IntBag yPos,
                                    IntBag zPos)

getNeighborsHamiltonianDistance

public void getNeighborsHamiltonianDistance(int x,
                                            int y,
                                            int z,
                                            int dist,
                                            boolean toroidal,
                                            Bag result,
                                            IntBag xPos,
                                            IntBag yPos,
                                            IntBag zPos)

getObjectsAtLocations

public Bag getObjectsAtLocations(IntBag xPos,
                                 IntBag yPos,
                                 IntBag zPos,
                                 Bag result)
For each location, puts all such objects into the result bag. Returns the result bag. If the provided result bag is null, one will be created and returned.