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 matrix

Inertia matrix to use for transformation

required
rotation matrix

Rotation matrix to apply to the inertia matrix

required

Returns:

Type Description
matrix

Transformed inertia matrix

Source code in onshape_robotics_toolkit\mesh.py
57
58
59
60
61
62
63
64
65
66
67
68
69
def transform_inertia_matrix(inertia_matrix: np.matrix, rotation: np.matrix) -> np.matrix:
    """
    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
    """

    return rotation @ inertia_matrix @ rotation.T

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
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
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

Array of vectors to use for transformation

required
rotation ndarray

Rotation matrix to apply to the vectors

required
translation ndarray

Translation matrix to apply to the vectors

required

Returns:

Type Description
ndarray

Array of transformed vectors

Source code in onshape_robotics_toolkit\mesh.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def transform_vectors(vectors: np.ndarray, rotation: np.ndarray, translation: np.ndarray) -> np.ndarray:
    """
    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
    """

    return np.dot(vectors, rotation.T) + translation * len(vectors)