Skip to content

connected_components

connected_components(F, return_face_indices=False)

Computes the connected components of a triangle mesh. This follows the convention of gptoolbox (https://github.com/alecjacobson/gptoolbox/blob/master/mesh/connected_components.m)

Parameters:

Name Type Description Default
F (m,3) numpy int array

face index list of a triangle mesh

required
return_face_indices bool, optional (default False)

whether to return component IDs for faces for faces

False

Returns:

Name Type Description
C (n,) numpy int array

connected component ID for each vertex

CF if requested, (m,) numpy int array

connected component ID for each face

Examples:

V,F = gpy.read_mesh("mesh.obj")
C,CF = gpy.connected_components(F)
Source code in src/gpytoolbox/connected_components.py
 6
 7
 8
 9
10
11
12
13
14
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
def connected_components(F,
    return_face_indices=False):
    """Computes the connected components of a triangle mesh.
    This follows the convention of gptoolbox (https://github.com/alecjacobson/gptoolbox/blob/master/mesh/connected_components.m)

    Parameters
    ----------
    F : (m,3) numpy int array
        face index list of a triangle mesh
    return_face_indices : bool, optional (default False)
        whether to return component IDs for faces for faces

    Returns
    -------
    C : (n,) numpy int array
        connected component ID for each vertex
    CF : if requested, (m,) numpy int array
        connected component ID for each face

    Examples
    --------
    ```python
    V,F = gpy.read_mesh("mesh.obj")
    C,CF = gpy.connected_components(F)
    ```
    """

    if F.size==0:
        C = np.array([], dtype=int)
        CF = np.array([], dtype=int)
    else:
        assert F.shape[1]==3, "This function only works for triangle meshes."

        A = adjacency_matrix(F)
        _,C = sp.sparse.csgraph.connected_components(A)
        CF = C[F[:,0]]

    if return_face_indices:
        return C,CF
    else:
        return C