📄 sparsegrid3d.java
字号:
package sim.field.grid;import sim.field.*;import sim.util.*;/** A storage facility for sparse objects in discrete 3D space, using HashMaps. SparseGrid3D differs from ObjectGrid3D in several respects: <ul> <li>SparseGrid3D can store more than one object at a location. ObjectGrid3D cannot. <li>ObjectGrid3D can store an object at more than one location (though it's bad form!). <li>SparseGrid3D can efficiently (O(1)) tell you the location of an object. <li>SparseGrid3D can efficiently (O(#objs)) scan through all objects. The best you can do with ObjectGrid3D is search its array (which might have many empty slots). <li>Storing an object, finding its location, or changing its location, in a SparseGrid3D is O(1) but requires several HashMap lookups and/or removes, which has a significant constant overhead. <li>SparseGrid3D can associate objects with <i>any</i> 3D integer location. ObjectGrid3D's locations are restricted to be within its array. </ul> <p>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. <p>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.*/public class SparseGrid3D extends SparseField { protected int width; protected int height; protected int length; public SparseGrid3D(int width, int height, int length) { this.width = width; this.height = height; this.length = length; } /** Returns the width of the grid */ public int getWidth() { return width; } /** Returns the height of the grid */ public int getHeight() { return height; } /** Returns the length of the grid */ public int getLength() { return length; } public final int tx(final int x) { final int width = this.width; if (x >= 0) return (x % width); final int width2 = (x % width) + width; if (width2 < width) return width2; return 0; } public final int ty(final int y) { final int height = this.height; if (y >= 0) return (y % height); final int height2 = (y % height) + height; if (height2 < height) return height2; return 0; } public final int tz(final int z) { final int length = this.length; if (z >= 0) return (z % length); final int length2 = (z % length) + length; if (length2 < length) return length2; return 0; } public int stx(final int x) { if (x >= 0) { if (x < width) return x; return x - width; } return x + width; } public int sty(final int y) { if (y >= 0) { if (y < height) return y ; return y - height; } return y + height; } public int stz(final int z) { if (z >= 0) { if (z < length) return z ; return z - length; } return z + length; } MutableInt3D speedyMutableInt3D = new MutableInt3D(); /** Returns the number of objects stored in the grid at the given location. */ public int numObjectsAtLocation(final int x, final int y, final int z) { MutableInt3D speedyMutableInt3D = this.speedyMutableInt3D; // a little faster (local) speedyMutableInt3D.x = x; speedyMutableInt3D.y = y; speedyMutableInt3D.z = z; return numObjectsAtLocation(speedyMutableInt3D); } /** Returns a bag containing all the objects at a given location -- which MIGHT be empty or MIGHT be null (which should also be interpreted as "empty") 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 <b> new Bag(<i>foo</i>.getObjectsAtLocation(<i>location</i>)) </b>. 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. */ public Bag getObjectsAtLocation(final int x, final int y, final int z) { MutableInt3D speedyMutableInt3D = this.speedyMutableInt3D; // a little faster (local) speedyMutableInt3D.x = x; speedyMutableInt3D.y = y; speedyMutableInt3D.z = z; return getObjectsAtLocation(speedyMutableInt3D); } /** Returns the object location as a Double3D, or as null if there is no such object. */ public Double3D getObjectLocationAsDouble3D(Object obj) { Int3D loc = (Int3D) super.getRawObjectLocation(obj); if (loc == null) return null; return new Double3D(loc); } /** Returns the object location, or null if there is no such object. */ public Int3D getObjectLocation(Object obj) { return (Int3D) super.getRawObjectLocation(obj); } /** Removes all the objects stored at the given location and returns them as a Bag (which you are free to modify). */ public Bag removeObjectsAtLocation(final int x, final int y, final int z) { MutableInt3D speedyMutableInt3D = this.speedyMutableInt3D; // a little faster (local) speedyMutableInt3D.x = x; speedyMutableInt3D.y = y; speedyMutableInt3D.z = z; return removeObjectsAtLocation(speedyMutableInt3D); } /** Changes the location of an object, or adds if it doesn't exist yet. Returns false if the object is null (null objects cannot be put into the grid). */ public boolean setObjectLocation(final Object obj, final int x, final int y, final int z) { return super.setObjectLocation(obj,new Int3D(x,y,z)); } /** Changes the location of an object, or adds if it doesn't exist yet. Returns false if the object is null (null objects cannot be put into the grid) or if the location is null. */ public boolean setObjectLocation(Object obj, final Int3D location) { return super.setObjectLocation(obj, location); } public void getNeighborsMaxDistance( final int x, final int y, final int z, final int dist, final boolean toroidal, IntBag xPos, IntBag yPos, IntBag zPos ) { // won't work for negative distances if( dist < 0 ) { throw new RuntimeException( "Runtime exception in method getNeighborsMaxDistance: Distance must be positive" ); } if( xPos == null || yPos == null || zPos == null ) { throw new RuntimeException( "Runtime exception in method getNeighborsMaxDistance: xPos and yPos should not be null" ); } xPos.clear(); yPos.clear(); zPos.clear(); // for toroidal environments the code will be different because of wrapping arround if( toroidal ) { // compute xmin and xmax for the neighborhood final int xmin = x - dist; final int xmax = x + dist; // compute ymin and ymax for the neighborhood final int ymin = y - dist; final int ymax = y + dist; final int zmin = z - dist; final int zmax = z + dist; for( int x0 = xmin; x0 <= xmax ; x0++ ) { final int x_0 = stx(x0); for( int y0 = ymin ; y0 <= ymax ; y0++ ) { final int y_0 = sty(y0); for( int z0 = zmin ; z0 <= zmax ; z0++ ) { final int z_0 = stz(z0); if( x_0 != x || y_0 != y || z_0 != z ) { xPos.add( x_0 ); yPos.add( y_0 ); zPos.add( z_0 ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -