Field module

Field module handles field discretization and validation.

Author:

Yaolin Ge yaolin.ge@ntnu.no

Objective:
  1. Generate grid discretization.

  2. Check legal conditions of given locations.

  3. Check collision with obstacles.

Examples

>>> from Field import Field
>>> f = Field()
>>> f.get_grid()
np.array([[ 0.  0.]
    [ 0.  1.]
    ...
    [ 9.  8.]])
>>> f.get_neighbour_indices(0)
1 2 3 4
>>> f.border_contains(np.array([10, 10]))
True
>>> f.obstacle_contains(np.array([10, 10]))
False
>>> f.is_border_in_the_way(np.array([10, 10]), np.array([20, 20]))
False
>>> f.is_obstacle_in_the_way(np.array([10, 10]), np.array([20, 20]))
True
>>> f.set_neighbour_distance(100)
>>> f.get_neighbour_distance()
100.0
>>> f.get_border_limits()
(0.0, 0.0, 9.0, 8.0)
>>> f.get_border_polygon()
np.array([[ 0.,  0.],
    [ 0.,  8.],
    [ 9.,  8.],
    [ 9.,  0.],
    [ 0.,  0.]])
>>> f.get_obstacle_polygon()
np.array([[ 2.,  2.],
    [ 2.,  6.],
    [ 7.,  6.],
    [ 7.,  2.],
    [ 2.,  2.]])
>>> f.get_border_line()
LineString([(0.0, 0.0), (0.0, 8.0), (9.0, 8.0), (9.0, 0.0), (0.0, 0.0)])
>>> f.get_obstacle_line()
LineString([(2.0, 2.0), (2.0, 6.0), (7.0, 6.0), (7.0, 2.0), (2.0, 2.0)])
>>> f.get_ind_from_location(np.array([10, 10]))
0
>>> f.get_location_from_ind(10)
np.array([ 1.,  0.])

Notes

1. The coordinate system is x-y with x pointing up, y pointing to the right which cooresponds to NED system (North-East-Down). 2. The origin is at the bottom left corner of the field, i.e. the bottom left corner has coordinates (0, 0).

References

TODO: Add references. TODO: Add figures to demonstrate the capaibility.

class Field.Field(neighbour_distance: float = 120)

Bases: object

Class to handle everything related to the field element, including border, obstacles, grid, and neighbors.

__config

Configuration object.

Type

Config

__neighbour_distance

Distance between neighboring locations.

Type

float

__polygon_border

Array of polygon border vertices.

Type

np.ndarray

__polygon_border_shapely

Shapely polygon object for border.

Type

shapely.geometry.Polygon

__line_border_shapely

Shapely line string object for border.

Type

shapely.geometry.LineString

__polygon_obstacle

Array of polygon obstacle vertices.

Type

np.ndarray

__polygon_obstacle_shapely

Shapely polygon object for obstacle.

Type

shapely.geometry.Polygon

__line_obstacle_shapely

Shapely line string object for obstacle.

Type

shapely.geometry.LineString

__xmin

Minimum x value of the border.

Type

float

__ymin

Minimum y value of the border.

Type

float

__xmax

Maximum x value of the border.

Type

float

__ymax

Maximum y value of the border.

Type

float

__xlim

Array of [xmin, xmax].

Type

np.ndarray

__ylim

Array of [ymin, ymax].

Type

np.ndarray

__ygap

Gap between rows.

Type

float

__xgap

Gap between columns.

Type

float

__grid

Array of field grid points.

Type

np.ndarray

__neighbour_hash_table

Hash table for containing neighboring indices around each waypoint.

Type

dict

__construct_grid() None

Construct the field grid using a regular meshgrid.

Steps:
  1. Create a meshgrid with x and y coordinates.

  2. Shift the y coordinates by half the y gap.

  3. Filter out the points that are not within the border polygon or within obstacle polygon.

  4. Append the points to the grid.

Returns

None

__construct_hash_neighbours() None

Construct the hash table for containing neighbour indices around each waypoint.

Methods: Directly use the neighbouring radius to determine the neighbouring indices.

Returns

None

__line_obstacle_shapely

Get the xy limits and gaps for the bigger box

border_contains(loc: numpy.ndarray) bool

Test if point is within the border polygon.

Parameters

loc – Location (x, y) to test.

Returns

True if point is within the border polygon, False otherwise.

get_border_limits()

Return the border limits.

Returns

The border limits in tuple format.

Examples

xlim = (xmin, xmax) ylim = (ymin, ymax)

get_grid() numpy.ndarray

Return the grid.

Parameters

None

Returns

The grid in np.ndarray format.

Examples

grid = np.array([[x1, y1], [x2, y2], …, [xn, yn]])

get_ind_from_location(location: numpy.ndarray) Optional[numpy.ndarray]

Return waypoint index using location.

Parameters

location – Waypoint location.

Returns

Waypoint index in np.ndarray format.

Examples

ind = 1 location = np.array([x1, y1])

get_location_from_ind(ind: Union[int, list, numpy.ndarray]) numpy.ndarray

Return waypoint locations using ind.

Parameters

ind – Index of the waypoint.

Returns

Waypoint locations in np.ndarray format.

Examples

ind = 1 location = np.array([x1, y1])

get_neighbour_distance() float

Return neighbour distance.

Parameters

None

Returns

Neighbour distance as a float number.

get_neighbour_indices(ind_now: Union[int, numpy.ndarray]) numpy.ndarray

Return neighbouring indices according to given current index.

Parameters

ind_now – Current index.

Returns

An array of neighbouring indices.

is_border_in_the_way(loc_start: numpy.ndarray, loc_end: numpy.ndarray) bool

Check if border is in the way between loc_start and loc_end.

Parameters
  • loc_start – Start location.

  • loc_end – End location.

Returns

True if border is in the way, False otherwise.

is_obstacle_in_the_way(loc_start: numpy.ndarray, loc_end: numpy.ndarray) bool

Check if obstacle is in the way between loc_start and loc_end.

Parameters
  • loc_start – Start location.

  • loc_end – End location.

Returns

True if obstacle is in the way, False otherwise.

obstacle_contains(loc: numpy.ndarray) bool

Test if obstacle contains the point.

Parameters

loc – Location (x, y) to test.

Returns

True if obstacle contains the point, False otherwise.

set_neighbour_distance(value: float) None

Set the neighbour distance.

Parameters

value – New neighbour distance in meters.

Returns

None