Package repast.simphony.space.continuous
Class AbstractContinuousSpace<T,U>
- java.lang.Object
-
- repast.simphony.space.projection.DefaultProjection<T>
-
- repast.simphony.space.continuous.AbstractContinuousSpace<T,U>
-
- All Implemented Interfaces:
ContinuousSpace<T>
,Projection<T>
- Direct Known Subclasses:
DefaultContinuousSpace
public abstract class AbstractContinuousSpace<T,U> extends DefaultProjection<T> implements ContinuousSpace<T>
Default implementation of an n-dimensional continuous space.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbstractContinuousSpace.PointHolder
A little class used for getting rid of some hash table lookups.
-
Field Summary
Fields Modifier and Type Field Description protected CoordinateAccessor<T,U>
accessor
protected ContinuousAdder<T>
adder
protected Map<T,AbstractContinuousSpace.PointHolder>
agentLocationMap
protected Dimensions
dimensions
protected U
locationStorage
protected int
size
protected PointTranslator
translator
-
Fields inherited from class repast.simphony.space.projection.DefaultProjection
listeners, name
-
-
Constructor Summary
Constructors Constructor Description AbstractContinuousSpace(String name, ContinuousAdder<T> adder, PointTranslator translator, CoordinateAccessor<T,U> accessor, double... size)
Constructs this space with the specified name, adder, translator, accessor and size.AbstractContinuousSpace(String name, ContinuousAdder<T> adder, PointTranslator translator, CoordinateAccessor<T,U> accessor, double[] size, double[] origin)
Constructs this space with the specified name, adder, translator, accessor and size.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract U
createLocationStorage()
protected boolean
doMove(T object, double[] displacement, double[] newLocation)
boolean
evaluate(ProjectionPredicate predicate)
Evaluate this Projection against the specified Predicate.ContinuousAdder<T>
getAdder()
Gets the adder used by this space to add new objects.Dimensions
getDimensions()
Gets the dimensions of the grid.double[]
getDisplacement(NdPoint point1, NdPoint point2)
Returns the displacement between the NdPoints point1 and point2.double
getDistance(NdPoint point1, NdPoint point2)
Calculates the euclidian distance between the NdPoints point1 and point2.double
getDistanceSq(NdPoint point1, NdPoint point2)
Calculates the square of the euclidian distance between the NdPoints point1 and point2.protected double[]
getLocation(double... location)
NdPoint
getLocation(Object obj)
Gets the location of the specified object.T
getObjectAt(double... location)
Gets the object at the specified location.Iterable<T>
getObjects()
Gets all the object currently in the space.Iterable<T>
getObjectsAt(double... location)
Gets all the objects at the specified location.PointTranslator
getPointTranslator()
Retrieves the rule being used for controlling what happens at or beyond the borders of the space.T
getRandomObjectAt(double... location)
Gets a random object from among those at the specified location.boolean
isPeriodic()
True if this space is periodic (in the sense that moving off one border makes you appear on the other one), otherwise false.NdPoint
moveByDisplacement(T object, double... displacement)
Moves the specified object from its current location by the specified amount.NdPoint
moveByVector(T object, double distance, double... anglesInRadians)
Moves the specifed object the specified distance from its current position along the specified angle.boolean
moveTo(T object, double... newLocation)
Moves the specified object from its current location into the new location.protected void
remove(T t)
protected void
removeAll()
void
setAdder(ContinuousAdder<T> adder)
Sets the adder used by this space to add new objects.void
setPointTranslator(PointTranslator rule)
Sets the rule to use for controlling what happens at or beyond the borders of the space.int
size()
Gets the number of objects currently in the space.-
Methods inherited from class repast.simphony.space.projection.DefaultProjection
addProjectionListener, fireProjectionEvent, getName, getProjectionListeners, removeProjectionListener
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface repast.simphony.space.projection.Projection
addProjectionListener, getName, getProjectionListeners, removeProjectionListener
-
-
-
-
Field Detail
-
agentLocationMap
protected Map<T,AbstractContinuousSpace.PointHolder> agentLocationMap
-
locationStorage
protected U locationStorage
-
dimensions
protected Dimensions dimensions
-
adder
protected ContinuousAdder<T> adder
-
translator
protected PointTranslator translator
-
accessor
protected CoordinateAccessor<T,U> accessor
-
size
protected int size
-
-
Constructor Detail
-
AbstractContinuousSpace
public AbstractContinuousSpace(String name, ContinuousAdder<T> adder, PointTranslator translator, CoordinateAccessor<T,U> accessor, double... size)
Constructs this space with the specified name, adder, translator, accessor and size. The size is the size of the space meaning [3, 3] is a 3x3 space.- Parameters:
name
- the name of the spacesize
- the dimensions of the space
-
AbstractContinuousSpace
public AbstractContinuousSpace(String name, ContinuousAdder<T> adder, PointTranslator translator, CoordinateAccessor<T,U> accessor, double[] size, double[] origin)
Constructs this space with the specified name, adder, translator, accessor and size. The size is the size of the space meaning [3, 3] is a 3x3 space.- Parameters:
name
- the name of the spacesize
- the dimensions of the spaceorigin
- the origin of the space
-
-
Method Detail
-
createLocationStorage
protected abstract U createLocationStorage()
-
moveTo
public boolean moveTo(T object, double... newLocation)
Moves the specified object from its current location into the new location. The object must previously have been introduced into the space. Objects are introduced into the space by adding them to the context of which this space is a projection.- Specified by:
moveTo
in interfaceContinuousSpace<T>
- Parameters:
object
-newLocation
-- Returns:
- true if the move was successful, otherwise false.
- Throws:
SpatialException
- if the object is not already in the space or if the number of dimensions in the location does not agree with the number in the space.
-
doMove
protected boolean doMove(T object, double[] displacement, double[] newLocation)
-
getLocation
public NdPoint getLocation(Object obj)
Gets the location of the specified object.- Specified by:
getLocation
in interfaceContinuousSpace<T>
- Parameters:
obj
-- Returns:
- the location of the specified object or null if the object is not in the space.
-
size
public int size()
Gets the number of objects currently in the space. This does NOT include any objects that may have been added, but have NOT been moved to a space location.- Specified by:
size
in interfaceContinuousSpace<T>
- Returns:
- the number of objects currently in the space. This does NOT include any objects that may have been added, but have NOT been moved to a space location.
-
getPointTranslator
public PointTranslator getPointTranslator()
Retrieves the rule being used for controlling what happens at or beyond the borders of the space.- Specified by:
getPointTranslator
in interfaceContinuousSpace<T>
- Returns:
- the rule for handling out of bounds coordinates
-
setPointTranslator
public void setPointTranslator(PointTranslator rule)
Sets the rule to use for controlling what happens at or beyond the borders of the space.- Specified by:
setPointTranslator
in interfaceContinuousSpace<T>
- Parameters:
rule
- the rule for handling out of bounds coordinates
-
getObjects
public Iterable<T> getObjects()
Gets all the object currently in the space. This does NOT include any objects that may have been added, but have NOT been moved to a space location.- Specified by:
getObjects
in interfaceContinuousSpace<T>
- Returns:
- an iteratable over all the object currently in the space. This does NOT include any objects that may have been added, but have NOT been moved to a space location.
-
getObjectAt
public T getObjectAt(double... location)
Gets the object at the specified location.- Specified by:
getObjectAt
in interfaceContinuousSpace<T>
- Parameters:
location
-- Returns:
- the object at the specified location.
-
getObjectsAt
public Iterable<T> getObjectsAt(double... location)
Gets all the objects at the specified location. For a multi occupancy space this will be all the objects at that location. For a single occupancy space this will be the single object at that location.- Specified by:
getObjectsAt
in interfaceContinuousSpace<T>
- Parameters:
location
-- Returns:
- the object at the specified location.
-
getLocation
protected double[] getLocation(double... location)
-
getRandomObjectAt
public T getRandomObjectAt(double... location)
Gets a random object from among those at the specified location. If this is a single occupancy space this will return the single object at that location, if any.- Specified by:
getRandomObjectAt
in interfaceContinuousSpace<T>
- Parameters:
location
-- Returns:
- the object at the specified location.
-
moveByDisplacement
public NdPoint moveByDisplacement(T object, double... displacement)
Moves the specified object from its current location by the specified amount. For examplemoveByDisplacement(object, 3, -2, 1)
will move the object by 3 along the x-axis, -2 along the y and 1 along the z. The displacement argument can be less than the number of dimensions in the space.- Specified by:
moveByDisplacement
in interfaceContinuousSpace<T>
- Parameters:
object
- the object to movedisplacement
- the amount to move the object- Returns:
- the new location if the move was successful, otherwise null
- Throws:
SpatialException
- if the object is not already in the space or if the number of dimensions in the displacement greater than the number of grid dimensions.
-
moveByVector
public NdPoint moveByVector(T object, double distance, double... anglesInRadians)
Moves the specifed object the specified distance from its current position along the specified angle.- Specified by:
moveByVector
in interfaceContinuousSpace<T>
- Parameters:
object
- the object to movedistance
- the distance to moveanglesInRadians
- the angle to move along- Returns:
- the new location or null if the move is not successful
- Throws:
SpatialException
- if the object is not already in the space or if the number of angles is greater than the number of dimensions- See Also:
Direction.EAST
,Direction.NORTH
,Direction.SOUTH
,Direction.WEST
-
getDimensions
public Dimensions getDimensions()
Gets the dimensions of the grid.- Specified by:
getDimensions
in interfaceContinuousSpace<T>
- Returns:
- the dimensions of the grid.
-
setAdder
public void setAdder(ContinuousAdder<T> adder)
Sets the adder used by this space to add new objects.- Specified by:
setAdder
in interfaceContinuousSpace<T>
- Parameters:
adder
- the adder
-
getAdder
public ContinuousAdder<T> getAdder()
Gets the adder used by this space to add new objects.- Specified by:
getAdder
in interfaceContinuousSpace<T>
- Returns:
- the adder used by this space to add new objects.
-
removeAll
protected void removeAll()
-
remove
protected void remove(T t)
-
isPeriodic
public boolean isPeriodic()
Description copied from interface:ContinuousSpace
True if this space is periodic (in the sense that moving off one border makes you appear on the other one), otherwise false.- Specified by:
isPeriodic
in interfaceContinuousSpace<T>
- Returns:
- true if this space is periodic, otherwise false.
-
evaluate
public boolean evaluate(ProjectionPredicate predicate)
Evaluate this Projection against the specified Predicate. This typically involves a double dispatch where the Projection calls back to the predicate, passing itself.- Specified by:
evaluate
in interfaceProjection<T>
- Overrides:
evaluate
in classDefaultProjection<T>
- Parameters:
predicate
-- Returns:
- true if the predicate evaluates to true, otherwise false. False can also mean that the predicate is not applicable to this Projection. For example, a linked type predicate evaluated against a grid projection.
-
getDistance
public double getDistance(NdPoint point1, NdPoint point2)
Calculates the euclidian distance between the NdPoints point1 and point2. If the points do not have the same dimension then this returns Double.NaN- Specified by:
getDistance
in interfaceContinuousSpace<T>
- Parameters:
point1
- the first pointpoint2
- the second point- Returns:
- the euclidian distance between the NdPoints point1 and point2. If the points do not have the same dimension then this returns Double.NaN
-
getDistanceSq
public double getDistanceSq(NdPoint point1, NdPoint point2)
Calculates the square of the euclidian distance between the NdPoints point1 and point2. If the points do not have the same dimension then this returns Double.NaN- Specified by:
getDistanceSq
in interfaceContinuousSpace<T>
- Parameters:
point1
- the first pointpoint2
- the second point- Returns:
- the square of the euclidian distance between the NdPoints point1 and point2. If the points do not have the same dimension then this returns Double.NaN
-
getDisplacement
public double[] getDisplacement(NdPoint point1, NdPoint point2)
Returns the displacement between the NdPoints point1 and point2. If the points do not have the same dimension then this returns null- Specified by:
getDisplacement
in interfaceContinuousSpace<T>
- Parameters:
point1
- the first pointpoint2
- the second point- Returns:
- the displacement between the NdPoints point1 and point2. If the points do not have the same dimension then this returns null
-
-