node.obstacle module

Module obstacle contains a obstacle class to encapsulate a list of polygons describing static obstacle on the simulation area or map. It also provide useful methods to test whether a line crosses any obstacle, and number of obstacles crossed if required.

Note

  • Although the coordinates of obstacles can be float or integer type, it is strongly recommended to use integer for speed consideration.

  • Implementing this class in Python isn’t efficient, suggest using shapely package or use Cython to compile _LSI functions into C language.

Note

  • Created by: CH Foh (2022)

class node.obstacle.Obstacle

Bases: object

This class provides storage to keep polygons as obstacles and useful methods if check if a line has crossed any obstacle, and how many.

Methods

add_poly(polygon)

This method adds a polygon as an obstacle.

add_rect(x, y, w, h)

This method adds a rectangle as an obstacle.

get_list()

This method returns a list containing all obstacle polygons.

is_crossed(point1, point2)

This method tests whether the given line crosses any obstacle.

num_crossed(point1, point2)

This method counts the number of obstacles that the given line has crossed.

remove(polygon)

This method removes an obstacle from the existing list.

__init__()

Initialize self. See help(type(self)) for accurate signature.

get_list()

This method returns a list containing all obstacle polygons.

Returns
List

The list of all obstacle polygons.

add_poly(polygon)

This method adds a polygon as an obstacle.

Parameters
polygonlist of a pair of float or int (be consistent) as (x,y)

The vertices of the polygon describing the obstacle.

add_rect(x, y, w, h)

This method adds a rectangle as an obstacle.

Parameters
xint

The left coordinate of the obstacle.

yint

The top coordinate of the obstacle.

wint

The width of the obstacle.

hint

The heightof the obstacle.

remove(polygon)

This method removes an obstacle from the existing list.

Parameters
polygonlist of float or int (be consistent)

The polygon to be removed. For this method to be effective, the exact same polygon object must have been added into the list earlier.

is_crossed(point1, point2) → bool

This method tests whether the given line crosses any obstacle. It does not tell how many obstacles the line crosses.

Note

This isn’t an efficient implementation, suggest using shapely package or compile this class into C language using Cython.

Parameters
point1float or int

One end of the line.

point2float or int

Another end of the line.

Returns
bool

It returns whether the given line specified by point1 and point2 has crossed any obstacle.

num_crossed(point1, point2) → int

This method counts the number of obstacles that the given line has crossed. To test whether a line has crossed any obstacle, use is_crossed() method instead for efficiency.

Parameters
point1float or int

One end of the line.

point2float or int

Another end of the line.

Returns
int

The number of obstacles that the given line has crossed.