Skip to content

Geometry

Module for geometric calculations in 2D and 3D spaces.

This module provides utility functions for geometric calculations. It include the computation of distances between points and line segments in 2D and 3D spaces. It is used to remove the crack from the pacman region in SIF estimation.

Functions:

Name Description
distance_point_to_segment

Computes the distance between points and a line segment.

distance_point_to_segment(P, A, B)

Computes the distance between points and a line segment in 2D or 3D space.

This function calculates the shortest distance from each point in array P to the line segment defined by endpoints A and B.

Parameters:

Name Type Description Default
P ndarray

Array of shape (n, m) where n is the number of points and m is the number of dimensions (2 or 3). Each row represents a point.

required
A ndarray

1D array of shape (m,) representing one endpoint of the segment.

required
B ndarray

1D array of shape (m,) representing the other endpoint of the segment.

required

Returns:

Name Type Description
distance ndarray

Array of shape (n,) where each element is the distance from the corresponding point in P to the segment AB.

Source code in src/gcrack/utils/geometry.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def distance_point_to_segment(
    P: np.ndarray, A: np.ndarray, B: np.ndarray
) -> np.ndarray:
    """Computes the distance between points and a line segment in 2D or 3D space.

    This function calculates the shortest distance from each point in array P to the line segment defined by endpoints A and B.

    Args:
        P (np.ndarray):
            Array of shape (n, m) where n is the number of points and m is the number of dimensions (2 or 3).
            Each row represents a point.
        A (np.ndarray):
            1D array of shape (m,) representing one endpoint of the segment.
        B (np.ndarray):
            1D array of shape (m,) representing the other endpoint of the segment.

    Returns:
        distance (np.ndarray):
            Array of shape (n,) where each element is the distance from the corresponding point in P to the segment AB.
    """
    # Ensure A and B are 2D for broadcasting with P
    A = A[:, np.newaxis] if A.ndim == 1 else A
    B = B[:, np.newaxis] if B.ndim == 1 else B

    # Vector AB
    AB = B - A

    # Vector AP for each point P
    AP = P - A

    # Project vector AP onto AB to find the projection point on the line (may lie outside the segment)
    AB_squared = np.sum(AB**2, axis=0)
    if np.all(AB_squared == 0):  # A and B are the same point
        return np.linalg.norm(AP, axis=0)

    t = np.clip(np.sum(AP * AB, axis=0) / AB_squared, 0, 1)

    # The projection point on the segment
    projection = A + t * AB

    # Distance from point P to the projection point
    distance = np.linalg.norm(P - projection, axis=0)

    return distance