ard.utils.geometry#
Functions
|
Find the distance between two line segments in 2d or 3d. |
Calculate the distance from each point to the nearest point on a polygon or set of polygons using the ray-casting (Jordan curve theorem) algorithm. |
|
|
Find the distance from a point to a line segment in N-Dimensions. |
|
Determines the signed distance from a point to a polygon using the Jordan curve theorem (ray-casting) approach as discussed in [1] and [2]. |
|
Get the closest point on a line segment to the point of interest in N-Dimensions using vector projection. |
|
Determines the nearest polygon for each point using the ray-casting algorithm. |
Calculate unit vectors perpendicular to each edge of each polygon in a set of polygons. |
|
|
Determine if a point lies on a line segment. |
|
Calculate unit vectors perpendicular to each edge of each polygon in a set of polygons. |
Calculate unit vectors perpendicular to each edge of a polygon pointing into the polygon. |
- ard.utils.geometry.get_nearest_polygons(boundary_vertices, points_x, points_y, s=700, tol=1e-06)[source]#
Determines the nearest polygon for each point using the ray-casting algorithm. This function may be used to assign turbines to regions in a wind farm layout, but is not intended for use in a gradient-based optimization context. The function is not differentiable. This implementation is based on FLOWFarm.jl (byuflowlab/FLOWFarm.jl)
- Parameters:
boundary_vertices (np.ndarray or list of np.ndarray) -- Vertices of the boundary in counterclockwise order. If discrete is True, this should be a list of arrays.
points_x (np.ndarray) -- points x coordinates.
points_y (np.ndarray) -- points y coordinates.
s (float, optional) -- Smoothing factor for smooth max. Defaults to 700.
tol (float, optional) -- Tolerance for determining proximity. Defaults to 1e-6.
- Returns:
Region assignments for each turbine.
- Return type:
np.ndarray
- ard.utils.geometry.distance_multi_point_to_multi_polygon_ray_casting(points_x, points_y, boundary_vertices, regions, s=700, tol=1e-06)[source]#
Calculate the distance from each point to the nearest point on a polygon or set of polygons using the ray-casting (Jordan curve theorem) algorithm. Negative means the turbine is inside at least one polygon. This implementation is based on FLOWFarm.jl (byuflowlab/FLOWFarm.jl)
- Return type:
ndarray- Parameters:
points_x (np.ndarray[list]) -- points x coordinates.
points_y (np.ndarray[list]) -- points y coordinates.
boundary_vertices (list[list[np.ndarray]]) -- Vertices of the each boundary in counterclockwise order. Boundaries should be simple polygons but do not need to have the same number of vertices.
regions (np.array[int]) -- Predefined region assignments for each point. Defaults to None.
s (float, optional) -- Smoothing factor for smooth max. Defaults to 700.
tol (float, optional) -- Tolerance for determining proximity of point to polygon to be considered inside the polygon. Defaults to 1e-6.
c (np.ndarray, optional) -- Preallocated array for constraint values. Defaults to None.
- Returns:
Constraint values for each turbine. np.ndarray (optional): Region assignments for each turbine (if return_region is True).
- Return type:
np.ndarray
- ard.utils.geometry.distance_point_to_polygon_ray_casting(point, vertices, s=700, shift=1e-10, return_distance=True)[source]#
Determines the signed distance from a point to a polygon using the Jordan curve theorem (ray-casting) approach as discussed in [1] and [2]. The polygon is assumed to be simple and defined in counterclockwise order. Complex polygons (where edges cross one another) are not supported. The function is differentiable with respect to the point coordinates.
[1] Numerical Recipes: The Art of Scientific Computing by Press, et al. 3rd edition, sec. 21.4.3 (p. 1124) [2] https://en.wikipedia.org/wiki/Point_in_polygon
- Parameters:
point (jnp.ndarray) -- Point of interest (2D vector).
vertices (jnp.ndarray) -- Vertices of the polygon (Nx2 array) in counterclockwise order.
s (float, optional) -- Smoothing factor for the smoothmin function. Defaults to 700.
shift (float, optional) -- Small shift to handle edge cases. Defaults to 1e-10.
return_distance (bool, optional) -- Whether to return the signed distance or just inside/outside status. Defaults to True. When False, the function is not differentiable.
- Returns:
Signed distance or inside/outside status. Negative if inside, positive if outside.
- Return type:
float
- ard.utils.geometry.polygon_normals_calculator(boundary_vertices, n_polygons=1)[source]#
Calculate unit vectors perpendicular to each edge of each polygon in a set of polygons. This implementation is based on FLOWFarm.jl (byuflowlab/FLOWFarm.jl). This function is not intended to be differentiable wrt n_polygons.
- Return type:
list[ndarray]- Parameters:
boundary_vertices (list of np.ndarray) -- List of m-by-2 arrays, where each array contains the boundary vertices of a polygon in counter-clockwise order. The number of vertices (m) in each polygon does not need to be the same.
nboundaries (int, optional) -- The number of boundaries in the set. Defaults to 1.
n_polygons (int)
- Returns:
- List of m-by-2 arrays of unit vectors perpendicular to each edge
of each polygon.
- Return type:
list of np.ndarray
- ard.utils.geometry.multi_polygon_normals_calculator(boundary_vertices)[source]#
Calculate unit vectors perpendicular to each edge of each polygon in a set of polygons. This implementation is based on FLOWFarm.jl (byuflowlab/FLOWFarm.jl).
- Return type:
Array- Parameters:
boundary_vertices (list of np.ndarray) -- List of m-by-2 arrays, where each array contains the boundary vertices of a polygon in counterclockwise order.
nboundaries (int, optional) -- The number of boundaries in the set. Defaults to 1.
- Returns:
- List of m-by-2 arrays of unit vectors perpendicular to each edge
of each polygon.
- Return type:
list of np.ndarray
- ard.utils.geometry.single_polygon_normals_calculator(boundary_vertices)[source]#
Calculate unit vectors perpendicular to each edge of a polygon pointing into the polygon. This implementation is based on FLOWFarm.jl (byuflowlab/FLOWFarm.jl).
- Return type:
Array- Parameters:
boundary_vertices (np.ndarray) -- m-by-2 array containing all the boundary vertices in counterclockwise order.
- Returns:
m-by-2 array of unit vectors perpendicular to each edge of the polygon pointing into the polygon.
- Return type:
np.ndarray
- ard.utils.geometry.point_on_line(p, v1, v2, tol=1e-06)[source]#
Determine if a point lies on a line segment.
Given a line determined by two points (v1 and v2), determine if the point (p) lies on the line between those points within a given tolerance.
- Parameters:
p (np.ndarray) -- Point of interest (2D vector).
v1 (np.ndarray) -- First vertex of the line (2D vector).
v2 (np.ndarray) -- Second vertex of the line (2D vector).
tol (float) -- Tolerance for determining co-linearity.
- Returns:
True if the point lies on the line, False otherwise.
- Return type:
bool
- ard.utils.geometry.distance_lineseg_to_lineseg_nd(line_a_start, line_a_end, line_b_start, line_b_end, tol=1e-12)[source]#
Find the distance between two line segments in 2d or 3d. This method is primarily based on reference [1], using a parametric approach based on the determinant and cross product to find the closest points on the two line segments. However, to handle the special case of line segments that are coplanar, we use the smooth minimum of the distance between the endpoints of the two line segments and the other line segment. In the coplanar case, the returned distance between the two line segments may have a noticeable error due to possibly having multiple points with the same distance, which leads to error in the smooth minimum function.
[1] Numerical Recipes: The Art of Scientific Computing by Press, et al. 3rd edition, sec. 21.4.2 (p. 1121)
- Return type:
float- Parameters:
line_a_start (np.ndarray) -- The start point of line segment "a" as either [x,y,z] or [x,y]
line_a_end (np.ndarray) -- The end point of line segment "a" as either [x,y,z] or [x,y]
line_b_start (np.ndarray) -- The start point of line segment "b" as either [x,y,z] or [x,y]
line_b_end (np.ndarray) -- The end point of line segment "b" as either [x,y,z] or [x,y]
tol (float, optional) -- If denominator in key equation is less than or equal tol, then an alternative method is used. Defaults to 0.0.
- Returns:
Distance between the two line segments
- Return type:
float
- ard.utils.geometry.distance_point_to_lineseg_nd(point, segment_start, segment_end)[source]#
Find the distance from a point to a line segment in N-Dimensions. This implementation can handle any number of dimensions as well as the reduced case of point to point distance. If the same point is passed for the start and end of the line segment, then the distance is simply the distance from the point of interest to the single start/end point.
- Return type:
float- Parameters:
point (np.ndarray) -- point of interest [x,y,...]
segment_start (np.ndarray) -- start point of line segment [x,y,...]
segment_end (np.ndarray) -- end point of line segment [x,y,...]
- Returns:
shortest distance between the point and finite line segment
- Return type:
distance (float)
- ard.utils.geometry.get_closest_point_on_line_seg(point, segment_start, segment_end, segment_vector)[source]#
Get the closest point on a line segment to the point of interest in N-Dimensions using vector projection.
- Return type:
ndarray- Parameters:
point (np.ndarray) -- point of interest [x,y,...]
segment_start (np.ndarray) -- start point of line segment [x,y,...]
segment_end (np.ndarray) -- end point of line segment [x,y,...]
segment_vector (np.ndarray) -- segment_end - segment_start
- Returns:
closest point on the line segment to the point of interest
- Return type:
np.ndarray