def test_save_load_meshes(self): verts = torch.tensor([[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=torch.float32) faces = torch.tensor([[0, 1, 2], [0, 2, 3]]) normals = torch.tensor([[0, 1, 0], [1, 0, 0], [1, 4, 1], [1, 0, 0]], dtype=torch.float32) vert_colors = torch.rand_like(verts) texture = TexturesVertex(verts_features=[vert_colors]) for do_textures, do_normals in itertools.product([True, False], [True, False]): mesh = Meshes( verts=[verts], faces=[faces], textures=texture if do_textures else None, verts_normals=[normals] if do_normals else None, ) device = torch.device("cuda:0") io = IO() with NamedTemporaryFile(mode="w", suffix=".ply") as f: io.save_mesh(mesh.cuda(), f.name) f.flush() mesh2 = io.load_mesh(f.name, device=device) self.assertEqual(mesh2.device, device) mesh2 = mesh2.cpu() self.assertClose(mesh2.verts_padded(), mesh.verts_padded()) self.assertClose(mesh2.faces_padded(), mesh.faces_padded()) if do_normals: self.assertTrue(mesh.has_verts_normals()) self.assertTrue(mesh2.has_verts_normals()) self.assertClose(mesh2.verts_normals_padded(), mesh.verts_normals_padded()) else: self.assertFalse(mesh.has_verts_normals()) self.assertFalse(mesh2.has_verts_normals()) self.assertFalse( torch.allclose(mesh2.verts_normals_padded(), normals)) if do_textures: self.assertIsInstance(mesh2.textures, TexturesVertex) self.assertClose(mesh2.textures.verts_features_list()[0], vert_colors) else: self.assertIsNone(mesh2.textures)
def test_save_too_many_colors(self): verts = torch.tensor([[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=torch.float32) faces = torch.tensor([[0, 1, 2], [0, 2, 3]]) vert_colors = torch.rand((4, 7)) texture_with_seven_colors = TexturesVertex( verts_features=[vert_colors]) mesh = Meshes( verts=[verts], faces=[faces], textures=texture_with_seven_colors, ) io = IO() msg = "Texture will not be saved as it has 7 colors, not 3." with NamedTemporaryFile(mode="w", suffix=".ply") as f: with self.assertWarnsRegex(UserWarning, msg): io.save_mesh(mesh.cuda(), f.name)