Skip to content

utils

graph_edge_lengths(coords, edge_index)

Compute the lengths of edges in a graph.

Parameters:

Name Type Description Default
coords Tensor

Tensor of shape [N, 3] representing the coordinates of N nodes.

required
edge_index Tensor

Tensor of shape [2, E] representing the edges in the graph, where each column defines an edge from edge_index[0, i] to edge_index[1, i].

required

Returns:

Type Description
Tensor

Tensor of shape [E, 1] representing the lengths of the edges.

Source code in src/torch_structure/geometry/utils.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def graph_edge_lengths(coords, edge_index):
    r""" Compute the lengths of edges in a graph.

    Args:
        coords (torch.Tensor): Tensor of shape [N, 3] representing the coordinates of N nodes.
        edge_index (torch.Tensor): Tensor of shape [2, E] representing the edges in the graph,
            where each column defines an edge from edge_index[0, i] to edge_index[1, i].

    Returns:
        (torch.Tensor): Tensor of shape [E, 1] representing the lengths of the edges.
    """
    start_coords = coords[edge_index[0]]
    end_coords = coords[edge_index[1]]
    lengths = line_length(start_coords, end_coords)
    return lengths

line_direction(start_coords, end_coords)

Compute the direction vectors of lines defined by start and end coordinates.

Parameters:

Name Type Description Default
start_coords Tensor

Tensor of shape [N, 3] representing the start coordinates of N lines.

required
end_coords Tensor

Tensor of shape [N, 3] representing the end coordinates of N lines.

required

Returns:

Type Description
Tensor

Tensor of shape [N, 3] representing the direction vectors of the lines.

Source code in src/torch_structure/geometry/utils.py
32
33
34
35
36
37
38
39
40
41
42
43
def line_direction(start_coords, end_coords):
    r""" Compute the direction vectors of lines defined by start and end coordinates.

    Args:
        start_coords (torch.Tensor): Tensor of shape [N, 3] representing the start coordinates of N lines.
        end_coords (torch.Tensor): Tensor of shape [N, 3] representing the end coordinates of N lines.

    Returns:
        (torch.Tensor): Tensor of shape [N, 3] representing the direction vectors of the lines.
    """
    direction = end_coords - start_coords
    return direction / line_length(start_coords, end_coords)

line_length(start_coords, end_coords)

Compute the length of lines defined by start and end coordinates.

Parameters:

Name Type Description Default
start_coords Tensor

Tensor of shape [N, 3] representing the start coordinates of N lines.

required
end_coords Tensor

Tensor of shape [N, 3] representing the end coordinates of N lines.

required

Returns:

Type Description
Tensor

Tensor of shape [N, 1] representing the lengths of the lines.

Source code in src/torch_structure/geometry/utils.py
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def line_length(start_coords, end_coords):
    r""" Compute the length of lines defined by start and end coordinates.

    Args:
        start_coords (torch.Tensor): Tensor of shape [N, 3] representing the start coordinates of N lines.
        end_coords (torch.Tensor): Tensor of shape [N, 3] representing the end coordinates of N lines.

    Returns:
        (torch.Tensor): Tensor of shape [N, 1] representing the lengths of the lines.
    """
    length = torch.norm(end_coords - start_coords, dim=1, keepdim=True)
    return length