|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectsim.field.SparseField
sim.field.continuous.Continuous2D
public class Continuous2D
A storage facility for objects located in a continuous 2D environment. This facility relates objects with 2D double tuples (in the form of Double2D). The facility extends SparseField, and like other objects which extend SparseField (such as SparseGrid2D), the facility can only relate any given object with a single location at a time -- that is, an object cannot hold two locations at once in the Continuous2D field.
Because hashtable lookups are more expensive than just storing the object, we suggest that you ALSO store the location of an object in the object itself, so you can read from the object rather than having to call getObjectLocation(object).
The Continuous2D has been arranged to make neighborhood lookup information reasonably efficient. It discretizes the space into grid buckets. The discretization size of the buckets is provided in the constructor and cannot be changed thereafter. If the discretization was 0.7, for example, then one bucket would be (0,0) to (under 0.7, under 0.7), another bucket would be (0,0,0.7) to (under 0.7, under 1.4), etc.
You can use Continuous2D to look up objects in a given region by asking for objects within the enclosing buckets, then rummaging through the buckets to find the individuals actually in the desired region. The trick here is to come up with a good bucket size. If the bucket size is much larger than the typical size of a neighborhood lookup, then a typical lookup will include large numbers of objects you don't care about; in the worst case, this is an O(n) lookup for something that could have been much smaller. On the other hand, if the bucket size is much smaller than the typical size of a neighborhood lookup, then you have to do lots of bucket lookups to cover your range; many if not most of these buckets could be empty. This can also be highly inefficient.
Stored objects are best thought of as one of two types: point objects and non-point objects. A point object is represented in space by a single point. It has no area or volume. A non-point object has area or volume. You specify whether or not your objects are point or non-point objects when calling getObjectsWithinDistance(). The distinction matters when you care about this function returning either all the objects whose point location is within the distance range, or returning all the (non-point) the objects which could possibly overlap with the range.
This distinction also is important when determining the discretization size of your grid. If your objects are point objects, you have no minimum bound on the discretization size. But if the object are non-point location objects (that is, they have dimensions of width, height, etc.), and you care about this overlap when you do distance lookups, then you have a minimum bound on your discretization. In this case, you want to make certain that your discretization is at LEAST larger than the LARGEST dimension of any object you plan on putting in the Continuous2D. The idea here is that if an any part of an object fell within the bounding box for your distance lookup task (see getObjectsWithinDistance(...)), you're guaranteed that the stored location of the object must be within a bounding box 1 discretization larger in each direction.
Okay, so that gives you the minimum discretization you should use. What about the maximum discretization? It depends largely on the number of objects expected to occupy a given discretized bucket region, and on what kind of lookups you need to do for objects within a given distance. Searching through one bucket is a hash table lookup. A smaller discretization returns a more accurate sample of objects within the requested bounding box, but requires more hash table lookups. If you have point location objects, and your field is very dense (LOTS of objects in a bucket on average), then we recommend a discretization equal to the maximum range distance you are likely to look up; but if your field is very sparse, then we recommend a discretization equal to twice the maximum range distance. You have to tune it. If you have non-point-location objects, then you have two choices. One approach is to assume a discretization equal to the maximum range distance, but when doing lookups with getObjectsWithinDistance(...), you need to state that you're using non-point-location objects. If you're fairly sparse and your objects aren't big, you can set the discretization to twice the maximum range distance, and you should be safe calling getObjectsWithinDistance() pretending that your objects are point-location; this saves you a lot of hash table lookups.
At any rate, do NOT go below the minimum discretization rules.
But wait, you say, I have objects of widely varying sizes. Or I have many different neighborhood lookup range needs. Never fear. Just use multiple Continuous2Ds of different discretizations. Depending on your needs, you can put all the objects in all of the Continuous2Ds (making different range lookups efficient) or various-sized classes of objects in their own Continuous2Ds perhaps. You have to think this through based on your needs. If all the objects were in all of the Continuous2Ds, you'd think that'd be inefficient in moving objects around. Not really: if the discretizations doubled (or more) each time, you're looking at typically an O(ln n) number of Continuous2Ds, and a corresponding number of lookups.
Continuous2D objects have a width and a height, but this is used for two functions: first, to determine the bounds for toroidal functions. Second, to determine the bounds for drawing on the screen in a portrayal. Otherwise, width and height are not used. If your space is bounded, you should set the width and height to those bounds. If it's unbounded, then you should set the width and height to the bounds you would like displayed on-screen.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class sim.field.SparseField |
---|
SparseField.LocationAndIndex |
Field Summary | |
---|---|
double |
discretization
|
java.util.HashMap |
doubleLocationHash
Where we store the Double2D values hashed by object |
double |
height
|
double |
width
|
Fields inherited from class sim.field.SparseField |
---|
allObjects, INITIAL_BAG_SIZE, LARGE_BAG_RATIO, locationAndIndexHash, MIN_BAG_SIZE, objectHash, removeEmptyBags, replaceLargeBags, REPLACEMENT_BAG_RATIO |
Constructor Summary | |
---|---|
Continuous2D(Continuous2D other)
|
|
Continuous2D(double discretization,
double width,
double height)
Provide expected bounds on the SparseContinuous2D |
Method Summary | |
---|---|
Bag |
clear()
Deletes everything, returning all the objects as a Bag (which you can freely use and modify). |
Int2D |
discretize(Double2D location)
|
Double2D |
getDimensions()
Returns the width and height of the sparse field as a Double2D |
double |
getHeight()
Get the height |
Bag |
getNearestNeighbors(Double2D position,
int atLeastThisMany,
boolean toroidal,
boolean nonPointObjects,
boolean radial,
Bag result)
Finds and returns at LEAST the 'atleastThisMany' items closest to a given 'position', plus potentially other items. |
Double2D |
getObjectLocation(java.lang.Object obj)
|
Double2D |
getObjectLocationAsDouble2D(java.lang.Object obj)
Synonymous with getObjectLocation, which you should generally use instead. |
Bag |
getObjectsAtDiscretizedLocation(Int2D location)
Returns a bag containing all the objects at a given discretized location, or null when there are no objects at the location. |
Bag |
getObjectsAtLocation(Double2D location)
Returns a bag containing all the objects at a given location, or null if there are no such objects or if location is null. |
Bag |
getObjectsAtLocationOfObject(java.lang.Object obj)
Returns a bag containing all the objects at the exact same location as a given object, including the object itself, or null if the object is not in the Field. |
Bag |
getObjectsExactlyWithinDistance(Double2D position,
double distance)
Returns a Bag containing EXACTLY those objects within a certain distance of a given position, or equal to that distance, measuring using a circle of radius 'distance' around the given position. |
Bag |
getObjectsExactlyWithinDistance(Double2D position,
double distance,
boolean toroidal)
Returns a Bag containing EXACTLY those objects within a certain distance of a given position, or equal to that distance, measuring using a circle of radius 'distance' around the given position. |
Bag |
getObjectsExactlyWithinDistance(Double2D position,
double distance,
boolean toroidal,
boolean radial,
boolean inclusive,
Bag result)
Returns a Bag containing EXACTLY those objects within a certain distance of a given position. |
Bag |
getObjectsWithinDistance(Double2D position,
double distance)
Returns a bag containing AT LEAST those objects within the bounding box surrounding the specified distance of the specified position. |
Bag |
getObjectsWithinDistance(Double2D position,
double distance,
boolean toroidal)
Returns a bag containing AT LEAST those objects within the bounding box surrounding the specified distance of the specified position. |
Bag |
getObjectsWithinDistance(Double2D position,
double distance,
boolean toroidal,
boolean nonPointObjects)
Returns a bag containing AT LEAST those objects within the bounding box surrounding the specified distance of the specified position. |
Bag |
getObjectsWithinDistance(Double2D position,
double distance,
boolean toroidal,
boolean nonPointObjects,
Bag result)
Puts into the result Bag (and returns it) AT LEAST those objects within the bounding box surrounding the specified distance of the specified position. |
double |
getWidth()
Get the width |
int |
numObjectsAtLocation(Double2D location)
Returns the number of the objects at a given location, or 0 if there are no such objects or if location is null. |
int |
numObjectsAtLocationOfObject(java.lang.Object obj)
Returns the number of objects at the exact same location as a given object, including the object itself, or 0 if the object is not in the Field. |
java.lang.Object |
remove(java.lang.Object obj)
Removes an object if it exists. |
Bag |
removeObjectsAtLocation(Double2D location)
Removes objects at exactly the given location, and returns a bag of them, or null of no objects are at that location. |
boolean |
setObjectLocation(java.lang.Object obj,
Double2D location)
|
double |
stx(double x)
Simple [and fast] toroidal x. |
double |
sty(double y)
Simple [and fast] toroidal y. |
double |
tds(Double2D d1,
Double2D d2)
Minimum Toroidal Distance Squared between two points. |
double |
tdx(double x1,
double x2)
Minimum toroidal distance between two values in the X dimension. |
double |
tdy(double y1,
double y2)
Minimum toroidal distance between two values in the Y dimension. |
Double2D |
tv(Double2D d1,
Double2D d2)
Minimum Toroidal difference vector between two points. |
double |
tx(double x)
Toroidal x |
double |
ty(double y)
Toroidal y |
Methods inherited from class sim.field.SparseField |
---|
exists, getAllObjects, getObjectIndex, getObjectsAtLocation, getObjectsAtLocations, getRawObjectLocation, getRawObjectsAtLocation, iterator, locationBagIterator, numObjectsAtLocation, removeObjectsAtLocation, setObjectLocation, size |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public java.util.HashMap doubleLocationHash
public double width
public double height
public final double discretization
Constructor Detail |
---|
public Continuous2D(double discretization, double width, double height)
public Continuous2D(Continuous2D other)
Method Detail |
---|
public final Double2D getObjectLocation(java.lang.Object obj)
public final Double2D getObjectLocationAsDouble2D(java.lang.Object obj)
getObjectLocationAsDouble2D
in interface SparseField2D
public final Double2D getDimensions()
SparseField2D
getDimensions
in interface SparseField2D
public final Int2D discretize(Double2D location)
public final boolean setObjectLocation(java.lang.Object obj, Double2D location)
public final Bag clear()
SparseField
clear
in class SparseField
public final java.lang.Object remove(java.lang.Object obj)
SparseField
remove
in class SparseField
public double getWidth()
public double getHeight()
public final double tx(double x)
public final double ty(double y)
public double stx(double x)
public double sty(double y)
public double tdx(double x1, double x2)
public double tdy(double y1, double y2)
public double tds(Double2D d1, Double2D d2)
public Double2D tv(Double2D d1, Double2D d2)
public Bag getNearestNeighbors(Double2D position, int atLeastThisMany, boolean toroidal, boolean nonPointObjects, boolean radial, Bag result)
public Bag getObjectsExactlyWithinDistance(Double2D position, double distance)
public Bag getObjectsExactlyWithinDistance(Double2D position, double distance, boolean toroidal)
public Bag getObjectsExactlyWithinDistance(Double2D position, double distance, boolean toroidal, boolean radial, boolean inclusive, Bag result)
public Bag getObjectsWithinDistance(Double2D position, double distance)
public Bag getObjectsWithinDistance(Double2D position, double distance, boolean toroidal)
public Bag getObjectsWithinDistance(Double2D position, double distance, boolean toroidal, boolean nonPointObjects)
public Bag getObjectsWithinDistance(Double2D position, double distance, boolean toroidal, boolean nonPointObjects, Bag result)
The bag could include other objects than this. If toroidal, then wrap-around possibilities are also considered. If nonPointObjects, then it is presumed that the object isn't just a point in space, but in fact fills an area in space where the x/y point location could be at the extreme corner of a bounding box of the object. In this case we include the object if any part of the bounding box could overlap into the desired region. To do this, if nonPointObjects is true, we extend the search space by one extra discretization in all directions. For small distances within a single bucket, this returns nine bucket's worth rather than 1, so if you know you only care about the actual x/y points stored, rather than possible object overlap into the distance sphere you specified, you'd want to set nonPointObjects to FALSE.
public Bag getObjectsAtDiscretizedLocation(Int2D location)
public Bag getObjectsAtLocation(Double2D location)
public int numObjectsAtLocation(Double2D location)
public Bag getObjectsAtLocationOfObject(java.lang.Object obj)
getObjectsAtLocationOfObject
in class SparseField
public int numObjectsAtLocationOfObject(java.lang.Object obj)
numObjectsAtLocationOfObject
in class SparseField
public Bag removeObjectsAtLocation(Double2D location)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |