Skip to content

Mesh

This module contains functions for transforming meshes and inertia matrices.

transform_inertia_matrix(inertia_matrix, rotation)

Transform an inertia matrix

Parameters:

Name Type Description Default
inertia_matrix NDArray[floating]

Inertia matrix to use for transformation

required
rotation NDArray[floating]

Rotation matrix to apply to the inertia matrix

required

Returns:

Type Description
NDArray[floating]

Transformed inertia matrix

Source code in onshape_robotics_toolkit\mesh.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def transform_inertia_matrix(
    inertia_matrix: NDArray[np.floating], rotation: NDArray[np.floating]
) -> NDArray[np.floating]:
    """
    Transform an inertia matrix

    Args:
        inertia_matrix: Inertia matrix to use for transformation
        rotation: Rotation matrix to apply to the inertia matrix

    Returns:
        Transformed inertia matrix
    """

    result: NDArray[np.floating] = rotation @ inertia_matrix @ rotation.T
    return result

transform_mesh(mesh, transform)

Apply a transformation matrix to an STL mesh.

Parameters:

Name Type Description Default
mesh Mesh

STL mesh to use for transformation

required
transform ndarray

Transformation matrix to apply to the mesh

required

Returns:

Type Description
Mesh

Transformed STL mesh

Examples:

>>> mesh = Mesh.from_file("mesh.stl")
>>> transform = np.eye(4)
>>> transform_mesh(mesh, transform)
Source code in onshape_robotics_toolkit\mesh.py
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 transform_mesh(mesh: Mesh, transform: np.ndarray) -> Mesh:
    """
    Apply a transformation matrix to an STL mesh.

    Args:
        mesh: STL mesh to use for transformation
        transform: Transformation matrix to apply to the mesh

    Returns:
        Transformed STL mesh

    Examples:
        >>> mesh = Mesh.from_file("mesh.stl")
        >>> transform = np.eye(4)
        >>> transform_mesh(mesh, transform)
    """

    _transform_vectors = partial(
        transform_vectors, rotation=transform[:3, :3], translation=transform[0:3, 3:4].T.tolist()
    )

    mesh.v0 = _transform_vectors(mesh.v0)
    mesh.v1 = _transform_vectors(mesh.v1)
    mesh.v2 = _transform_vectors(mesh.v2)
    mesh.normals = _transform_vectors(mesh.normals)

    return mesh

transform_vectors(vectors, rotation, translation)

Apply a transformation matrix to a set of vectors.

Parameters:

Name Type Description Default
vectors NDArray[floating]

Array of vectors to use for transformation

required
rotation NDArray[floating]

Rotation matrix to apply to the vectors

required
translation NDArray[floating]

Translation matrix to apply to the vectors

required

Returns:

Type Description
NDArray[floating]

Array of transformed vectors

Source code in onshape_robotics_toolkit\mesh.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def transform_vectors(
    vectors: NDArray[np.floating], rotation: NDArray[np.floating], translation: NDArray[np.floating]
) -> NDArray[np.floating]:
    """
    Apply a transformation matrix to a set of vectors.

    Args:
        vectors: Array of vectors to use for transformation
        rotation: Rotation matrix to apply to the vectors
        translation: Translation matrix to apply to the vectors

    Returns:
        Array of transformed vectors
    """

    result: NDArray[np.floating] = np.dot(vectors, rotation.T) + translation * len(vectors)
    return result