Exemple #1
0
    def _overlay_image(self):
        for node in self.scene.get_nodes():
            if node.name == 'body_mesh':
                self.scene.remove_node(node)
                break
        rectified_orient = self.model.global_orient.detach().clone()
        rectified_orient[0][0] += np.pi
        model_obj = self.model(global_orient=rectified_orient)
        out_mesh = Trimesh(model_obj.vertices.detach().numpy().squeeze(),
                           self.model.faces.squeeze())
        rot = trimesh.transformations.rotation_matrix(np.radians(180),
                                                      [1, 0, 0])
        out_mesh.apply_transform(rot)
        mesh = Mesh.from_trimesh(out_mesh, material=self.material)

        self.scene.add(mesh, name='body_mesh')
        rendered, _ = self.renderer.render(self.scene)

        if self.base_img is not None:
            img = self.base_img.copy()
        else:
            img = np.uint8(rendered)
        img_mask = (rendered != 255.0).sum(axis=2) == 3

        img[img_mask] = rendered[img_mask].astype(np.uint8)
        img = np.concatenate(
            (img, img_mask[:, :, np.newaxis].astype(np.uint8)), axis=2)
        return img
Exemple #2
0
def _to_unit_cube(mesh: trimesh.Trimesh):

    bounds = mesh.extents
    if bounds.min() == 0.0:
        return

    # translate to origin
    translation = (mesh.bounds[0] + mesh.bounds[1]) * 0.5
    translation = trimesh.transformations.translation_matrix(direction=-translation)
    mesh.apply_transform(translation)

    # scale to unit cube
    scale = 1.0/bounds.max()
    scale_trafo = trimesh.transformations.scale_matrix(factor=scale)
    mesh.apply_transform(scale_trafo)

    return mesh