AoiSampler#

class AoiSampler[source]#

Bases: object

Allows efficiently sampling points inside an AOI uniformly at random.

To achieve this, each polygon in the AOI is first partitioned into triangles (triangulation). Then, to sample a single point, we first sample a triangle at random with probability proportional to its area and then sample a point within that triangle uniformly at random.

__init__(polygons: Sequence[Polygon]) None[source]#
Parameters:

polygons (Sequence[Polygon]) –

Return type:

None

Methods

__init__(polygons)

polygon_to_graph(polygon)

Given a polygon, return its graph representation.

sample([n])

Sample a random point within the AOI, using the following algorithm:

triangle_area(vertices, simplices)

Calculate area of each triangle specified by the simplices array using Heron's formula.

triangle_origin_and_basis(vertices, simplices)

For each triangle ABC, return point A, vector AB, and vector AC.

triangle_side_lengths(vertices, simplices)

Calculate lengths of all 3 sides of each triangle specified by the simplices array.

triangulate(polygons)

triangulate_polygon(polygon)

Triangulate polygon.

__init__(polygons: Sequence[Polygon]) None[source]#
Parameters:

polygons (Sequence[Polygon]) –

Return type:

None

polygon_to_graph(polygon: shapely.geometry.polygon.Polygon | shapely.geometry.polygon.LinearRing) tuple[numpy.ndarray, numpy.ndarray][source]#

Given a polygon, return its graph representation.

Parameters:

polygon (shapely.geometry.polygon.Polygon | shapely.geometry.polygon.LinearRing) – A polygon or polygon-exterior.

Returns:

An (N, 2) array of vertices and an (N, 2) array of indices to vertices representing edges.

Return type:

tuple[numpy.ndarray, numpy.ndarray]

sample(n: int = 1) ndarray[source]#
Sample a random point within the AOI, using the following algorithm:
  • Randomly sample one triangle (ABC) with probability proportional

to its area. - Starting at A, travel a random distance along vectors AB and AC. - Return the final position.

Parameters:

n (int) – Number of points to sample. Defaults to 1.

Returns:

(n, 2) 2D coordinates of the sampled points.

Return type:

np.ndarray

triangle_area(vertices: ndarray, simplices: ndarray) ndarray[source]#

Calculate area of each triangle specified by the simplices array using Heron’s formula.

Parameters:
  • vertices (ndarray) – (N, 2) array of vertex coords in 2D.

  • simplices (ndarray) – (N, 3) array of indexes to entries in the vertices array. Each row represents one triangle.

Returns:

(N,) array of areas

Return type:

np.ndarray

triangle_origin_and_basis(vertices: ndarray, simplices: ndarray) tuple[numpy.ndarray, tuple[numpy.ndarray, numpy.ndarray]][source]#

For each triangle ABC, return point A, vector AB, and vector AC.

Parameters:
  • vertices (ndarray) – (N, 2) array of vertex coords in 2D.

  • simplices (ndarray) – (N, 3) array of indexes to entries in the vertices array. Each row represents one triangle.

Returns:

(point A, (vector AB, vector AC)).

Return type:

3 arrays of shape (N, 2), organized into tuples like so

triangle_side_lengths(vertices: ndarray, simplices: ndarray) tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray][source]#

Calculate lengths of all 3 sides of each triangle specified by the simplices array.

Parameters:
  • vertices (ndarray) – (N, 2) array of vertex coords in 2D.

  • simplices (ndarray) – (N, 3) array of indexes to entries in the vertices array. Each row represents one triangle.

Returns:

||AB||, ||BC||, ||AC||

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray]

triangulate(polygons) dict[source]#
Return type:

dict

triangulate_polygon(polygon: Polygon) dict[source]#

Triangulate polygon.

Extracts vertices and edges from the polygon (and its holes, if any) and passes them to the Triangle library for triangulation.

Parameters:

polygon (Polygon) –

Return type:

dict