# AoiSampler#

class AoiSampler[source]#

Bases: `object`

Given a set of polygons representing the AOI, allows efficiently sampling points inside the AOI uniformly at random.

To achieve this, each polygon 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: ) None[source]#
Parameters

polygons (Sequence[Polygon]) –

Return type

None

Methods

 `__init__`(polygons) `polygon_to_graph`(polygon) Given the exterior of 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) Extract vertices and edges from the polygon (and its holes, if any) and pass them to the Triangle library for triangulation.
__init__(polygons: ) None[source]#
Parameters

polygons (Sequence[Polygon]) –

Return type

None

polygon_to_graph(polygon: LinearRing) [source]#

Given the exterior of a polygon, return its graph representation.

Parameters

polygon (LinearRing) – The exterior of a polygon.

Returns

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

Return type

Tuple[np.ndarray, np.ndarray]

sample(n: int = 1) [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, optional) – 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) [source]#

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

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

• simplices (np.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) [source]#

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

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

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

Returns

3 arrays of shape

(N, 2), organized into tuples like so: (point A, (vector AB, vector AC)).

Return type

Tuple[np.ndarray, Tuple[np.ndarray, np.ndarray]]

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

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

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

• simplices (np.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]#

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

Parameters

polygon (Polygon) –

Return type

dict