Class SpatialHashEnvironment<TEntity>
Represents the spatial hash environment for a grid-based
index with exploration, movement and lookup.
This class provides all base functionality with provided
properties and the initialization of the index.
Inheritance
System.Object
SpatialHashEnvironment<TEntity>
Assembly: Mars.Components.dll
Syntax
public sealed class SpatialHashEnvironment<TEntity> : AbstractEnvironment, IEnvironment<TEntity>, IEnvironment, IModelObject, IDisposable where TEntity : IPositionable, IEntity
Type Parameters
Name |
Description |
TEntity |
The type to store within the index. |
Constructors
SpatialHashEnvironment(Double, Double, Boolean, Int32)
Creates a spatial hash grid environment without boundaries and the centre specified by the
and
Declaration
public SpatialHashEnvironment(double dimensionX, double dimensionY, bool withBoundaries = false, int cellSize = 1)
Parameters
Type |
Name |
Description |
System.Double |
dimensionX |
The X axes of this grid environment |
System.Double |
dimensionY |
The Y axes of this grid environment |
System.Boolean |
withBoundaries |
Indicates whether this environment has boundaries
specified by the dimensions
|
System.Int32 |
cellSize |
The size of each grid cell, Default is 1 |
Properties
CellSize
The size of each grid cell covering the passed radius for exploration.
Declaration
public int CellSize { get; }
Property Value
Type |
Description |
System.Int32 |
|
Centre
The centre point of this environment
Declaration
public Position Centre { get; }
Property Value
CheckBoundaries
Gets or sets the flag to check for boundaries of this environment
and repositioning of entities if they try to move outside
the desired
DimensionX and
DimensionY.
Declaration
public bool CheckBoundaries { get; set; }
Property Value
Type |
Description |
System.Boolean |
|
DimensionX
The extension and maximum x-position of this environment.
Declaration
public int DimensionX { get; }
Property Value
Type |
Description |
System.Int32 |
|
DimensionY
The extension and maximum y-position of this environment.
Declaration
public int DimensionY { get; }
Property Value
Type |
Description |
System.Int32 |
|
Entities
All managed entities of type within this environment.
Declaration
public IEnumerable<TEntity> Entities { get; }
Property Value
Type |
Description |
IEnumerable<TEntity> |
|
GridHeight
Gets the amount of grid cell in y-direction
Declaration
public int GridHeight { get; }
Property Value
Type |
Description |
System.Int32 |
|
GridWidth
Gets the amount of grid cell in x-direction
Declaration
public int GridWidth { get; }
Property Value
Type |
Description |
System.Int32 |
|
IsDiscretizePosition
Gets or sets the flag indicating whether the environment
should discretize the (x,y) position coordinate or should keep it continuous.
Declaration
public bool IsDiscretizePosition { get; set; }
Property Value
Type |
Description |
System.Boolean |
|
Methods
Dispose()
Performs application-defined tasks associated with freeing,
releasing, or resetting unmanaged resources.
Declaration
Explore(Position, Double, Int32, Func<TEntity, Boolean>)
Declaration
public IEnumerable<TEntity> Explore(Position position = null, double radius = null, int count = null, Func<TEntity, bool> predicate = null)
Parameters
Type |
Name |
Description |
Position |
position |
|
System.Double |
radius |
|
System.Int32 |
count |
|
Func<TEntity, System.Boolean> |
predicate |
|
Returns
Type |
Description |
IEnumerable<TEntity> |
|
Explore(Double, Double, Double, Int32, Func<TEntity, Boolean>)
Queries the environment for entities of type from a specified position in form
of a 2D coordinate (x,y). The query can be restricted by optional passed query limit and the radius to lookup.
Also the lookup in up-/downwards or in right/leftwards can be configured.
Declaration
public IEnumerable<TEntity> Explore(double x, double y, double radius = null, int count = null, Func<TEntity, bool> predicate = null)
Parameters
Type |
Name |
Description |
System.Double |
x |
The outgoing x coordinate. |
System.Double |
y |
The outgoing y coordinate. |
System.Double |
radius |
Optional radius interpreted as euclidean coordinate distance. |
System.Int32 |
count |
Optional limit of entities to query. |
Func<TEntity, System.Boolean> |
predicate |
Optional predicate which will be applied during query execution
to filter out entities matching a specific condition.
|
Returns
Type |
Description |
IEnumerable<TEntity> |
Returns a collection of distance-based ordered entities of type . |
Explore(Double[], Double, Int32, Func<TEntity, Boolean>)
Declaration
public IEnumerable<TEntity> Explore(double[] position, double radius = null, int count = null, Func<TEntity, bool> predicate = null)
Parameters
Type |
Name |
Description |
System.Double[] |
position |
|
System.Double |
radius |
|
System.Int32 |
count |
|
Func<TEntity, System.Boolean> |
predicate |
|
Returns
Type |
Description |
IEnumerable<TEntity> |
|
Insert(TEntity)
Inserts an positionable entity into the environment defined by their
Declaration
public bool Insert(TEntity entity)
Parameters
Type |
Name |
Description |
TEntity |
entity |
The entity to passionate. |
Returns
Type |
Description |
System.Boolean |
Returns True if the entity was successfully inserted |
InsertExplicit(TEntity)
Inserts an entity
explicitly into environment so that when it is not boundary conform.
The nearest edge position will be calculated and the agent will be set directly there.
Declaration
public bool InsertExplicit(TEntity entity)
Parameters
Type |
Name |
Description |
TEntity |
entity |
The entity to insert |
Returns
Type |
Description |
System.Boolean |
Returns true when the entity was successfully inserted. |
IntersectionPoints(Double, Double, Double, Double)
Find the coordinates where the line y=mx+b intersects the edges of the environment.
Declaration
public ISet<double[]> IntersectionPoints(double sourceX, double sourceY, double targetX, double targetY)
Parameters
Type |
Name |
Description |
System.Double |
sourceX |
The x coordinate of the source for the intersection ray. |
System.Double |
sourceY |
The y coordinate of the source for the intersection ray. |
System.Double |
targetX |
The x coordinate of the target for the intersection ray. |
System.Double |
targetY |
The y coordinate of the target for the intersection ray. |
Returns
Type |
Description |
ISet<System.Double[]> |
Returns a set of intersection points between the source and target |
IntersectionPoints(Double[], Double[])
Find the coordinates where the line y=mx+b intersects the edges of the environment.
Declaration
public ISet<double[]> IntersectionPoints(double[] source, double[] target)
Parameters
Type |
Name |
Description |
System.Double[] |
source |
Source point of the ray |
System.Double[] |
target |
Target point of the ray |
Returns
Type |
Description |
ISet<System.Double[]> |
the points of intersection |
MoveTo(TEntity, Position, Double, Func<Double[], Boolean>)
Moves an
entity
to the destination of type
Position along an optimal ray path with
minimal costs. Note that his method does not guarantee to reach the destination.
Declaration
public Position MoveTo(TEntity entity, Position position, double travellingDistance = null, Func<double[], bool> predicate = null)
Parameters
Type |
Name |
Description |
TEntity |
entity |
The entity to move. |
Position |
position |
The target destination coordinate. |
System.Double |
travellingDistance |
The distance to move |
Func<System.Double[], System.Boolean> |
predicate |
The predicate along the movement which need to be valid or null |
Returns
Type |
Description |
Position |
Returns the new position of the entity > or null if no positioning happened. |
MoveTo(TEntity, Double, Double, Double, Func<Double[], Boolean>)
Moves an entity
to the destination along an optimal ray path with
minimal costs. Note that his method does not guarantee to reach the destination.
Declaration
public Position MoveTo(TEntity entity, double x, double y, double travellingDistance = null, Func<double[], bool> predicate = null)
Parameters
Type |
Name |
Description |
TEntity |
entity |
The entity to move |
System.Double |
x |
The x coordinate of the destination. |
System.Double |
y |
The y coordinate of the destination. |
System.Double |
travellingDistance |
The distance to move |
Func<System.Double[], System.Boolean> |
predicate |
The predicate along the movement which need to be valid or null |
Returns
Type |
Description |
Position |
Returns the new position of the entity > or null if no positioning happened. |
MoveTo(TEntity, Double[], Double, Func<Double[], Boolean>)
Moves an entity
to destination along an optimal path with
minimal costs. Note that his method does not guarantee to reach the destination.
Declaration
public Position MoveTo(TEntity entity, double[] position = null, double travellingDistance = null, Func<double[], bool> predicate = null)
Parameters
Type |
Name |
Description |
TEntity |
entity |
|
System.Double[] |
position |
The discrete position target |
System.Double |
travellingDistance |
The distance to move |
Func<System.Double[], System.Boolean> |
predicate |
The predicate along the movement which need to be valid or null |
Returns
Type |
Description |
Position |
Returns the new position of the entity > or null if no positioning happened. |
MoveTowards(TEntity, DirectionType, Double)
Moves an entity
the travelling distance distanceToPass
towards the direction
with a fixed bearing.
Declaration
public Position MoveTowards(TEntity entity, DirectionType direction, double distanceToPass = null)
Parameters
Type |
Name |
Description |
TEntity |
entity |
The entity to move |
DirectionType |
direction |
One of the eight direction on a 2D plane, including diagonal |
System.Double |
distanceToPass |
The distance units to pass. |
Returns
Type |
Description |
Position |
Returns the new position of the
entity
or null if no positioning happened.
|
MoveTowards(TEntity, Double, Double)
Moves an entity
the travelling distance distanceToPass
towards the bearing
direction.
Declaration
public Position MoveTowards(TEntity entity, double bearing, double distanceToPass = null)
Parameters
Type |
Name |
Description |
TEntity |
entity |
The entity to move |
System.Double |
bearing |
The bearing in degree in value range 0-360 |
System.Double |
distanceToPass |
The distance units to pass. |
Returns
Type |
Description |
Position |
Returns the new position of the entity > or null if no positioning happened. |
PosAt(TEntity, Double, Double)
Moves an entity
to the desired destination.
Declaration
public Position PosAt(TEntity entity, double x, double y)
Parameters
Type |
Name |
Description |
TEntity |
entity |
the object to move |
System.Double |
x |
The x coordinate of the destination. |
System.Double |
y |
The y coordinate of the destination. |
Returns
Type |
Description |
Position |
Returns the new position of the entity > or null if no positioning happened. |
PosAt(TEntity, Double[])
Set the entity
exactly at the desired position without any movement actions.
Declaration
public Position PosAt(TEntity entity, double[] position)
Parameters
Type |
Name |
Description |
TEntity |
entity |
the object to move |
System.Double[] |
position |
The coordinates of the position to position to |
Returns
Type |
Description |
Position |
Returns the new position of the entity > or null if no positioning happened. |
Remove(TEntity)
Removes the entity item from the environment at the defined
Position
over the
IPositionable implementation of
entity
.
Declaration
public bool Remove(TEntity entity)
Parameters
Type |
Name |
Description |
TEntity |
entity |
The entity and position to remove |
Returns
Type |
Description |
System.Boolean |
Returns True if the entity was successfully removed |
Reset()
Clear the whole environment and each environment related information.
Declaration
Implements
IDisposable