sim.field.grid
Class AbstractGrid2D

java.lang.Object
  extended by sim.field.grid.AbstractGrid2D
All Implemented Interfaces:
java.io.Serializable, Grid2D
Direct Known Subclasses:
DenseGrid2D, DoubleGrid2D, IntGrid2D, ObjectGrid2D

public abstract class AbstractGrid2D
extends java.lang.Object
implements Grid2D

A concrete implementation of the Grid2D methods; used by several subclasses. Note that you should avoid calling these methods from an object of type Grid2D; instead try to call them from something more concrete (AbstractGrid2D or SparseGrid2D). Otherwise they will not get inlined. For example,


   Grid2D foo = ... ;
   foo.tx(4);  // will not get inlined

   AbstractGrid2D bar = ...;
   bar.tx(4);  // WILL get inlined

   ObjectGrid2D baz = ...;  // (assuming we're an ObjectGrid2D)
   baz.tx(4);   // WILL get inlined
   

See Also:
Serialized Form

Field Summary
protected  int height
           
protected  int width
           
 
Fields inherited from interface sim.field.grid.Grid2D
ALL, ANY, ANY_SIZE, BOUNDED, CENTER, TOROIDAL, UNBOUNDED
 
Constructor Summary
AbstractGrid2D()
           
 
Method Summary
 java.util.Map buildMap(int size)
          Creates a map of the provided size (or any size it likes if ANY_SIZE is passed in).
 java.util.Map buildMap(java.util.Map other)
          Creates a Map which is a copy of another.
 int dlx(int x, int y)
          Hex downleft x.
 int dly(int x, int y)
          Hex downleft y.
 int downx(int x, int y)
          Hex down x.
 int downy(int x, int y)
          Hex down y.
 int drx(int x, int y)
          Hex downright x.
 int dry(int x, int y)
          Hex downright y.
 int getHeight()
          Returns the width of the field.
 void getHexagonalLocations(int x, int y, int dist, int mode, boolean includeOrigin, IntBag xPos, IntBag yPos)
          Gets all neighbors located within the hexagon centered at (X,Y) and 2*dist+1 cells from point to opposite point inclusive.
 void getMooreLocations(int x, int y, int dist, int mode, boolean includeOrigin, IntBag xPos, IntBag yPos)
          Gets all neighbors of a location that satisfy max( abs(x-X) , abs(y-Y) ) <= dist.
 void getNeighborsHamiltonianDistance(int x, int y, int dist, boolean toroidal, IntBag xPos, IntBag yPos)
          Deprecated.  
 void getNeighborsHexagonalDistance(int x, int y, int dist, boolean toroidal, IntBag xPos, IntBag yPos)
          Deprecated.  
 void getNeighborsMaxDistance(int x, int y, int dist, boolean toroidal, IntBag xPos, IntBag yPos)
          Deprecated.  
 void getRadialLocations(int x, int y, double dist, int mode, boolean includeOrigin, IntBag xPos, IntBag yPos)
          Gets all neighbors overlapping with a circular region centered at (X,Y) and with a radius of dist.
 void getRadialLocations(int x, int y, double dist, int mode, boolean includeOrigin, int measurementRule, boolean closed, IntBag xPos, IntBag yPos)
          Gets all neighbors overlapping with a circular region centered at (X,Y) and with a radius of dist.
 void getVonNeumannLocations(int x, int y, int dist, int mode, boolean includeOrigin, IntBag xPos, IntBag yPos)
          Gets all neighbors of a location that satisfy abs(x-X) + abs(y-Y) <= dist.
 int getWidth()
          Returns the width of the field.
protected  void removeOrigin(int x, int y, IntBag xPos, IntBag yPos)
           
protected  void removeOriginToroidal(int x, int y, IntBag xPos, IntBag yPos)
           
 int stx(int x)
          Simple [and fast] toroidal x.
 int sty(int y)
          Simple [and fast] toroidal y.
 boolean trb(int x, int y)
          Horizontal edge is on the bottom for triangle.
 boolean trt(int x, int y)
          Horizontal edge is on the top for triangle.
 int tx(int x)
          Toroidal x.
 int ty(int y)
          Toroidal y.
 int ulx(int x, int y)
          Hex upleft x.
 int uly(int x, int y)
          Hex upleft y.
 int upx(int x, int y)
          Hex up x.
 int upy(int x, int y)
          Hex up y.
 int urx(int x, int y)
          Hex upright x.
 int ury(int x, int y)
          Hex upright y.
 
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
Constructor Detail

AbstractGrid2D

public AbstractGrid2D()
Method Detail

getWidth

public final int getWidth()
Description copied from interface: Grid2D
Returns the width of the field.

Specified by:
getWidth in interface Grid2D

getHeight

public final int getHeight()
Description copied from interface: Grid2D
Returns the width of the field.

Specified by:
getHeight in interface Grid2D

buildMap

public java.util.Map buildMap(java.util.Map other)
Description copied from interface: Grid2D
Creates a Map which is a copy of another. By default, HashMap is used.

Specified by:
buildMap in interface Grid2D

buildMap

public java.util.Map buildMap(int size)
Description copied from interface: Grid2D
Creates a map of the provided size (or any size it likes if ANY_SIZE is passed in). By default, HashMap is used.

Specified by:
buildMap in interface Grid2D

tx

public final int tx(int x)
Description copied from interface: Grid2D
Toroidal x. The following definition:

final int length = this.length;
if (z >= 0) return (z % length);
final int length2 = (z % length) + length;
if (length2 < length) return length2;
return 0;

... produces the correct code and is 27 bytes, so it's likely to be inlined in Hotspot for 1.4.1.

Specified by:
tx in interface Grid2D

ty

public final int ty(int y)
Description copied from interface: Grid2D
Toroidal y. The following definition:

final int length = this.length;
if (z >= 0) return (z % length);
final int length2 = (z % length) + length;
if (length2 < length) return length2;
return 0;

... produces the correct code and is 27 bytes, so it's likely to be inlined in Hotspot for 1.4.1.

Specified by:
ty in interface Grid2D

stx

public final int stx(int x)
Description copied from interface: Grid2D
Simple [and fast] toroidal x. Use this if the values you'd pass in never stray beyond (-width ... width * 2) not inclusive. It's a bit faster than the full toroidal computation as it uses if statements rather than two modulos. The following definition:
{ int width = this.width; if (x >= 0) { if (x < width) return x; return x - width; } return x + width; } ...produces the shortest code (24 bytes) and is inlined in Hotspot for 1.4.1. However in most cases removing the int width = this.width; is likely to be a little faster if most objects are usually within the toroidal region.

Specified by:
stx in interface Grid2D

sty

public final int sty(int y)
Description copied from interface: Grid2D
Simple [and fast] toroidal y. Use this if the values you'd pass in never stray beyond (-height ... height * 2) not inclusive. It's a bit faster than the full toroidal computation as it uses if statements rather than two modulos. The following definition:
{ int height = this.height; if (y >= 0) { if (y < height) return y ; return y - height; } return y + height; } ...produces the shortest code (24 bytes) and is inlined in Hotspot for 1.4.1. However in most cases removing the int height = this.height; is likely to be a little faster if most objects are usually within the toroidal region.

Specified by:
sty in interface Grid2D

ulx

public final int ulx(int x,
                     int y)
Description copied from interface: Grid2D
Hex upleft x.

Specified by:
ulx in interface Grid2D

uly

public final int uly(int x,
                     int y)
Description copied from interface: Grid2D
Hex upleft y.

Specified by:
uly in interface Grid2D

urx

public final int urx(int x,
                     int y)
Description copied from interface: Grid2D
Hex upright x.

Specified by:
urx in interface Grid2D

ury

public final int ury(int x,
                     int y)
Description copied from interface: Grid2D
Hex upright y.

Specified by:
ury in interface Grid2D

dlx

public final int dlx(int x,
                     int y)
Description copied from interface: Grid2D
Hex downleft x.

Specified by:
dlx in interface Grid2D

dly

public final int dly(int x,
                     int y)
Description copied from interface: Grid2D
Hex downleft y.

Specified by:
dly in interface Grid2D

drx

public final int drx(int x,
                     int y)
Description copied from interface: Grid2D
Hex downright x.

Specified by:
drx in interface Grid2D

dry

public final int dry(int x,
                     int y)
Description copied from interface: Grid2D
Hex downright y.

Specified by:
dry in interface Grid2D

upx

public final int upx(int x,
                     int y)
Description copied from interface: Grid2D
Hex up x.

Specified by:
upx in interface Grid2D

upy

public final int upy(int x,
                     int y)
Description copied from interface: Grid2D
Hex up y.

Specified by:
upy in interface Grid2D

downx

public final int downx(int x,
                       int y)
Description copied from interface: Grid2D
Hex down x.

Specified by:
downx in interface Grid2D

downy

public final int downy(int x,
                       int y)
Description copied from interface: Grid2D
Hex down y.

Specified by:
downy in interface Grid2D

trb

public boolean trb(int x,
                   int y)
Description copied from interface: Grid2D
Horizontal edge is on the bottom for triangle. True if x + y is odd. One definition of this is return ((x + y) & 1) == 1;

Specified by:
trb in interface Grid2D

trt

public boolean trt(int x,
                   int y)
Description copied from interface: Grid2D
Horizontal edge is on the top for triangle. True if x + y is even. One definition of this is return ((x + y) & 1) == 0;

Specified by:
trt in interface Grid2D

removeOrigin

protected void removeOrigin(int x,
                            int y,
                            IntBag xPos,
                            IntBag yPos)

removeOriginToroidal

protected void removeOriginToroidal(int x,
                                    int y,
                                    IntBag xPos,
                                    IntBag yPos)

getNeighborsMaxDistance

public void getNeighborsMaxDistance(int x,
                                    int y,
                                    int dist,
                                    boolean toroidal,
                                    IntBag xPos,
                                    IntBag yPos)
Deprecated. 

Description copied from interface: Grid2D
Gets all neighbors of a location that satisfy max( abs(x-X) , abs(y-Y) ) <= dist. This region forms a square 2*dist+1 cells across, centered at (X,Y). If dist==1, this is equivalent to the so-called "Moore Neighborhood" (the eight neighbors surrounding (X,Y)), plus (X,Y) itself. Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first.

This function may only run in two modes: toroidal or bounded. Unbounded lookup is not permitted, and so this function is deprecated: instead you should use the other version of this function which has more functionality. If "bounded", then the neighbors are restricted to be only those which lie within the box ranging from (0,0) to (width, height), that is, the width and height of the grid. if "toroidal", then the environment is assumed to be toroidal, that is, wrap-around, and neighbors are computed in this fashion. Toroidal locations will not appear multiple times: specifically, if the neighborhood distance is so large that it wraps completely around the width or height of the box, neighbors will not be counted multiple times. Note that to ensure this, subclasses may need to resort to expensive duplicate removal, so it's not suggested you use so unreasonably large distances.

The origin -- that is, the (x,y) point at the center of the neighborhood -- is always included in the results.

This function is equivalent to: getNeighborsMaxDistance(x,y,dist,toroidal ? Grid2D.TOROIDAL : Grid2D.BOUNDED, true, xPos, yPos);

Specified by:
getNeighborsMaxDistance in interface Grid2D

getMooreLocations

public void getMooreLocations(int x,
                              int y,
                              int dist,
                              int mode,
                              boolean includeOrigin,
                              IntBag xPos,
                              IntBag yPos)
Description copied from interface: Grid2D
Gets all neighbors of a location that satisfy max( abs(x-X) , abs(y-Y) ) <= dist. This region forms a square 2*dist+1 cells across, centered at (X,Y). If dist==1, this is equivalent to the so-called "Moore Neighborhood" (the eight neighbors surrounding (X,Y)), plus (X,Y) itself. Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first.

This function may be run in one of three modes: Grid2D.BOUNDED, Grid2D.UNBOUNDED, and Grid2D.TOROIDAL. If "bounded", then the neighbors are restricted to be only those which lie within the box ranging from (0,0) to (width, height), that is, the width and height of the grid. If "unbounded", then the neighbors are not so restricted. Note that unbounded neighborhood lookup only makes sense if your grid allows locations to actually be outside this box. For example, SparseGrid2D permits this but ObjectGrid2D and DoubleGrid2D and IntGrid2D and DenseGrid2D do not. Finally if "toroidal", then the environment is assumed to be toroidal, that is, wrap-around, and neighbors are computed in this fashion. Toroidal locations will not appear multiple times: specifically, if the neighborhood distance is so large that it wraps completely around the width or height of the box, neighbors will not be counted multiple times. Note that to ensure this, subclasses may need to resort to expensive duplicate removal, so it's not suggested you use so unreasonably large distances.

You can also opt to include the origin -- that is, the (x,y) point at the center of the neighborhood -- in the neighborhood results.

Specified by:
getMooreLocations in interface Grid2D

getNeighborsHamiltonianDistance

public void getNeighborsHamiltonianDistance(int x,
                                            int y,
                                            int dist,
                                            boolean toroidal,
                                            IntBag xPos,
                                            IntBag yPos)
Deprecated. 

Description copied from interface: Grid2D
Gets all neighbors of a location that satisfy abs(x-X) + abs(y-Y) <= dist. This region forms a diamond 2*dist+1 cells from point to opposite point inclusive, centered at (X,Y). If dist==1 this is equivalent to the so-called "Von-Neumann Neighborhood" (the four neighbors above, below, left, and right of (X,Y)), plus (X,Y) itself. Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first.

This function may only run in two modes: toroidal or bounded. Unbounded lookup is not permitted, and so this function is deprecated: instead you should use the other version of this function which has more functionality. If "bounded", then the neighbors are restricted to be only those which lie within the box ranging from (0,0) to (width, height), that is, the width and height of the grid. if "toroidal", then the environment is assumed to be toroidal, that is, wrap-around, and neighbors are computed in this fashion. Toroidal locations will not appear multiple times: specifically, if the neighborhood distance is so large that it wraps completely around the width or height of the box, neighbors will not be counted multiple times. Note that to ensure this, subclasses may need to resort to expensive duplicate removal, so it's not suggested you use so unreasonably large distances.

The origin -- that is, the (x,y) point at the center of the neighborhood -- is always included in the results.

This function is equivalent to: getNeighborsHamiltonianDistance(x,y,dist,toroidal ? Grid2D.TOROIDAL : Grid2D.BOUNDED, true, xPos, yPos);

Specified by:
getNeighborsHamiltonianDistance in interface Grid2D

getVonNeumannLocations

public void getVonNeumannLocations(int x,
                                   int y,
                                   int dist,
                                   int mode,
                                   boolean includeOrigin,
                                   IntBag xPos,
                                   IntBag yPos)
Description copied from interface: Grid2D
Gets all neighbors of a location that satisfy abs(x-X) + abs(y-Y) <= dist. This region forms a diamond 2*dist+1 cells from point to opposite point inclusive, centered at (X,Y). If dist==1 this is equivalent to the so-called "Von-Neumann Neighborhood" (the four neighbors above, below, left, and right of (X,Y)), plus (X,Y) itself. Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first.

This function may be run in one of three modes: Grid2D.BOUNDED, Grid2D.UNBOUNDED, and Grid2D.TOROIDAL. If "bounded", then the neighbors are restricted to be only those which lie within the box ranging from (0,0) to (width, height), that is, the width and height of the grid. If "unbounded", then the neighbors are not so restricted. Note that unbounded neighborhood lookup only makes sense if your grid allows locations to actually be outside this box. For example, SparseGrid2D permits this but ObjectGrid2D and DoubleGrid2D and IntGrid2D and DenseGrid2D do not. Finally if "toroidal", then the environment is assumed to be toroidal, that is, wrap-around, and neighbors are computed in this fashion. Toroidal locations will not appear multiple times: specifically, if the neighborhood distance is so large that it wraps completely around the width or height of the box, neighbors will not be counted multiple times. Note that to ensure this, subclasses may need to resort to expensive duplicate removal, so it's not suggested you use so unreasonably large distances.

You can also opt to include the origin -- that is, the (x,y) point at the center of the neighborhood -- in the neighborhood results.

Specified by:
getVonNeumannLocations in interface Grid2D

getNeighborsHexagonalDistance

public void getNeighborsHexagonalDistance(int x,
                                          int y,
                                          int dist,
                                          boolean toroidal,
                                          IntBag xPos,
                                          IntBag yPos)
Deprecated. 

Description copied from interface: Grid2D
Gets all neighbors located within the hexagon centered at (X,Y) and 2*dist+1 cells from point to opposite point inclusive. If dist==1, this is equivalent to the six neighbors immediately surrounding (X,Y), plus (X,Y) itself. Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first.

This function may only run in two modes: toroidal or bounded. Unbounded lookup is not permitted, and so this function is deprecated: instead you should use the other version of this function which has more functionality. If "bounded", then the neighbors are restricted to be only those which lie within the box ranging from (0,0) to (width, height), that is, the width and height of the grid. if "toroidal", then the environment is assumed to be toroidal, that is, wrap-around, and neighbors are computed in this fashion. Toroidal locations will not appear multiple times: specifically, if the neighborhood distance is so large that it wraps completely around the width or height of the box, neighbors will not be counted multiple times. Note that to ensure this, subclasses may need to resort to expensive duplicate removal, so it's not suggested you use so unreasonably large distances.

The origin -- that is, the (x,y) point at the center of the neighborhood -- is always included in the results.

This function is equivalent to: getNeighborsHexagonalDistance(x,y,dist,toroidal ? Grid2D.TOROIDAL : Grid2D.BOUNDED, true, xPos, yPos);

Specified by:
getNeighborsHexagonalDistance in interface Grid2D

getHexagonalLocations

public void getHexagonalLocations(int x,
                                  int y,
                                  int dist,
                                  int mode,
                                  boolean includeOrigin,
                                  IntBag xPos,
                                  IntBag yPos)
Description copied from interface: Grid2D
Gets all neighbors located within the hexagon centered at (X,Y) and 2*dist+1 cells from point to opposite point inclusive. If dist==1, this is equivalent to the six neighbors immediately surrounding (X,Y), plus (X,Y) itself. Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first.

This function may be run in one of three modes: Grid2D.BOUNDED, Grid2D.UNBOUNDED, and Grid2D.TOROIDAL. If "bounded", then the neighbors are restricted to be only those which lie within the box ranging from (0,0) to (width, height), that is, the width and height of the grid. If "unbounded", then the neighbors are not so restricted. Note that unbounded neighborhood lookup only makes sense if your grid allows locations to actually be outside this box. For example, SparseGrid2D permits this but ObjectGrid2D and DoubleGrid2D and IntGrid2D and DenseGrid2D do not. Finally if "toroidal", then the environment is assumed to be toroidal, that is, wrap-around, and neighbors are computed in this fashion. Toroidal locations will not appear multiple times: specifically, if the neighborhood distance is so large that it wraps completely around the width or height of the box, neighbors will not be counted multiple times. Note that to ensure this, subclasses may need to resort to expensive duplicate removal, so it's not suggested you use so unreasonably large distances.

You can also opt to include the origin -- that is, the (x,y) point at the center of the neighborhood -- in the neighborhood results.

Specified by:
getHexagonalLocations in interface Grid2D

getRadialLocations

public void getRadialLocations(int x,
                               int y,
                               double dist,
                               int mode,
                               boolean includeOrigin,
                               IntBag xPos,
                               IntBag yPos)
Description copied from interface: Grid2D
Gets all neighbors overlapping with a circular region centered at (X,Y) and with a radius of dist. The measurement rule is Grid2D.ANY, meaning those cells which overlap at all with the region. The region is closed, meaning that that points which touch on the outer surface of the circle will be considered members of the region.

Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first.

This function may be run in one of three modes: Grid2D.BOUNDED, Grid2D.UNBOUNDED, and Grid2D.TOROIDAL. If "bounded", then the neighbors are restricted to be only those which lie within the box ranging from (0,0) to (width, height), that is, the width and height of the grid. If "unbounded", then the neighbors are not so restricted. Note that unbounded neighborhood lookup only makes sense if your grid allows locations to actually be outside this box. For example, SparseGrid2D permits this but ObjectGrid2D and DoubleGrid2D and IntGrid2D and DenseGrid2D do not. Finally if "toroidal", then the environment is assumed to be toroidal, that is, wrap-around, and neighbors are computed in this fashion. Toroidal locations will not appear multiple times: specifically, if the neighborhood distance is so large that it wraps completely around the width or height of the box, neighbors will not be counted multiple times. Note that to ensure this, subclasses may need to resort to expensive duplicate removal, so it's not suggested you use so unreasonably large distances.

You can also opt to include the origin -- that is, the (x,y) point at the center of the neighborhood -- in the neighborhood results.

Specified by:
getRadialLocations in interface Grid2D

getRadialLocations

public void getRadialLocations(int x,
                               int y,
                               double dist,
                               int mode,
                               boolean includeOrigin,
                               int measurementRule,
                               boolean closed,
                               IntBag xPos,
                               IntBag yPos)
Description copied from interface: Grid2D
Gets all neighbors overlapping with a circular region centered at (X,Y) and with a radius of dist. If measurementRule is Grid2D.CENTER, then the measurement rule will be those cells whose centers overlap with the region. If measurementRule is Grid2D.ALL, then the measurement rule will be those cells which entirely overlap with the region. If measurementrule is Grid2D.ANY, then the measurement rule will be those cells which overlap at all with the region. If closed is true, then the region will be considered "closed", that is, that points which touch on the outer surface of the circle will be considered members of the region. If closed is open, then the region will be considered "open", that is, that points which touch on the outer surface of the circle will NOT be considered members of the region.

Places each x and y value of these locations in the provided IntBags xPos and yPos, clearing the bags first.

This function may be run in one of three modes: Grid2D.BOUNDED, Grid2D.UNBOUNDED, and Grid2D.TOROIDAL. If "bounded", then the neighbors are restricted to be only those which lie within the box ranging from (0,0) to (width, height), that is, the width and height of the grid. If "unbounded", then the neighbors are not so restricted. Note that unbounded neighborhood lookup only makes sense if your grid allows locations to actually be outside this box. For example, SparseGrid2D permits this but ObjectGrid2D and DoubleGrid2D and IntGrid2D and DenseGrid2D do not. Finally if "toroidal", then the environment is assumed to be toroidal, that is, wrap-around, and neighbors are computed in this fashion. Toroidal locations will not appear multiple times: specifically, if the neighborhood distance is so large that it wraps completely around the width or height of the box, neighbors will not be counted multiple times. Note that to ensure this, subclasses may need to resort to expensive duplicate removal, so it's not suggested you use so unreasonably large distances.

You can also opt to include the origin -- that is, the (x,y) point at the center of the neighborhood -- in the neighborhood results.

Specified by:
getRadialLocations in interface Grid2D