Skip to content

Meshes.update_padded_ #983

@timlod

Description

@timlod

🚀 Feature

Currently, update_padded only exists as an out-of-place operation which shallow copies attributes onto a new object.
It would be nice to have an in-place version that just modifies the vertices of the existing Meshes object.

Motivation

I'm updating a mesh, but I'm not generating the new vertices through pytorch3d, or some method such that I could use offset_verts_, but an auxiliary model.
I would like to reuse the existing Meshes object for better efficiency and to allow the use of side effects when convenient.

Pitch

I may be missing some things (like updating normals if they'd been computed), but this could be enough:

    def update_padded_(self, new_verts_padded):
        """
        In-place version of update_padded.

        Args:
            new_verts_padded: FloatTensor of shape (N, V, 3)

        Returns:
            Meshes with updated padded representations
        """

        def check_shapes(x, size):
            if x.shape[0] != size[0]:
                raise ValueError("new values must have the same batch dimension.")
            if x.shape[1] != size[1]:
                raise ValueError("new values must have the same number of points.")
            if x.shape[2] != size[2]:
                raise ValueError("new values must have the same dimension.")

        self._verts_padded = new_verts_padded

        # update verts/faces packed if they are computed in self
        if self._verts_packed is not None:
            # update verts_packed
            pad_to_packed = self.verts_padded_to_packed_idx()
            new_verts_packed = new_verts_padded.reshape(-1, 3)[pad_to_packed, :]
            self._verts_packed = new_verts_packed
            self._verts_padded_to_packed_idx = pad_to_packed

        return self

There's an obvious huge speed-up involved when not creating a new object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions