Spaces:
Running
Running
File size: 11,776 Bytes
7088d16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import unittest
from tempfile import NamedTemporaryFile
import torch
from pytorch3d.io import IO
from pytorch3d.renderer import TexturesAtlas, TexturesVertex
from pytorch3d.utils import ico_sphere
from .common_testing import TestCaseMixin
CUBE_FACES = [
[0, 1, 2],
[7, 4, 0],
[4, 5, 1],
[5, 6, 2],
[3, 2, 6],
[6, 5, 4],
[0, 2, 3],
[7, 0, 3],
[4, 1, 0],
[5, 2, 1],
[3, 6, 7],
[6, 4, 7],
]
class TestMeshOffIO(TestCaseMixin, unittest.TestCase):
def test_load_face_colors(self):
# Example from wikipedia
off_file_lines = [
"OFF",
"# cube.off",
"# A cube",
" ",
"8 6 12",
" 1.0 0.0 1.4142",
" 0.0 1.0 1.4142",
"-1.0 0.0 1.4142",
" 0.0 -1.0 1.4142",
" 1.0 0.0 0.0",
" 0.0 1.0 0.0",
"-1.0 0.0 0.0",
" 0.0 -1.0 0.0",
"4 0 1 2 3 255 0 0 #red",
"4 7 4 0 3 0 255 0 #green",
"4 4 5 1 0 0 0 255 #blue",
"4 5 6 2 1 0 255 0 ",
"4 3 2 6 7 0 0 255",
"4 6 5 4 7 255 0 0",
]
off_file = "\n".join(off_file_lines)
io = IO()
with NamedTemporaryFile(mode="w", suffix=".off") as f:
f.write(off_file)
f.flush()
mesh = io.load_mesh(f.name)
self.assertEqual(mesh.verts_padded().shape, (1, 8, 3))
verts_str = " ".join(off_file_lines[5:13])
verts_data = torch.tensor([float(i) for i in verts_str.split()])
self.assertClose(mesh.verts_padded().flatten(), verts_data)
self.assertClose(mesh.faces_padded(), torch.tensor(CUBE_FACES)[None])
faces_colors_full = mesh.textures.atlas_padded()
self.assertEqual(faces_colors_full.shape, (1, 12, 1, 1, 3))
faces_colors = faces_colors_full[0, :, 0, 0]
max_color = faces_colors.max()
self.assertEqual(max_color, 1)
# Every face has one color 1, the rest 0.
total_color = faces_colors.sum(dim=1)
self.assertEqual(total_color.max(), max_color)
self.assertEqual(total_color.min(), max_color)
def test_load_vertex_colors(self):
# Example with no faces and with integer vertex colors
off_file_lines = [
"8 1 12",
" 1.0 0.0 1.4142 0 1 0",
" 0.0 1.0 1.4142 0 1 0",
"-1.0 0.0 1.4142 0 1 0",
" 0.0 -1.0 1.4142 0 1 0",
" 1.0 0.0 0.0 0 1 0",
" 0.0 1.0 0.0 0 1 0",
"-1.0 0.0 0.0 0 1 0",
" 0.0 -1.0 0.0 0 1 0",
"3 0 1 2",
]
off_file = "\n".join(off_file_lines)
io = IO()
with NamedTemporaryFile(mode="w", suffix=".off") as f:
f.write(off_file)
f.flush()
mesh = io.load_mesh(f.name)
self.assertEqual(mesh.verts_padded().shape, (1, 8, 3))
verts_lines = (line.split()[:3] for line in off_file_lines[1:9])
verts_data = [[[float(x) for x in line] for line in verts_lines]]
self.assertClose(mesh.verts_padded(), torch.tensor(verts_data))
self.assertClose(mesh.faces_padded(), torch.tensor([[[0, 1, 2]]]))
self.assertIsInstance(mesh.textures, TexturesVertex)
colors = mesh.textures.verts_features_padded()
self.assertEqual(colors.shape, (1, 8, 3))
self.assertClose(colors[0, :, [0, 2]], torch.zeros(8, 2))
self.assertClose(colors[0, :, 1], torch.full((8,), 1.0 / 255))
def test_load_lumpy(self):
# Example off file whose faces have different numbers of vertices.
off_file_lines = [
"8 3 12",
" 1.0 0.0 1.4142",
" 0.0 1.0 1.4142",
"-1.0 0.0 1.4142",
" 0.0 -1.0 1.4142",
" 1.0 0.0 0.0",
" 0.0 1.0 0.0",
"-1.0 0.0 0.0",
" 0.0 -1.0 0.0",
"3 0 1 2 255 0 0 #red",
"4 7 4 0 3 0 255 0 #green",
"4 4 5 1 0 0 0 255 #blue",
]
off_file = "\n".join(off_file_lines)
io = IO()
with NamedTemporaryFile(mode="w", suffix=".off") as f:
f.write(off_file)
f.flush()
mesh = io.load_mesh(f.name)
self.assertEqual(mesh.verts_padded().shape, (1, 8, 3))
verts_str = " ".join(off_file_lines[1:9])
verts_data = torch.tensor([float(i) for i in verts_str.split()])
self.assertClose(mesh.verts_padded().flatten(), verts_data)
self.assertEqual(mesh.faces_padded().shape, (1, 5, 3))
faces_expected = [[0, 1, 2], [7, 4, 0], [7, 0, 3], [4, 5, 1], [4, 1, 0]]
self.assertClose(mesh.faces_padded()[0], torch.tensor(faces_expected))
def test_save_load_icosphere(self):
# Test that saving a mesh as an off file and loading it results in the
# same data on the correct device, for all permitted types of textures.
# Standard test is for random colors, but also check totally white,
# because there's a different in OFF semantics between "1.0" color (=full)
# and "1" (= 1/255 color)
sphere = ico_sphere(0)
io = IO()
device = torch.device("cuda:0")
atlas_padded = torch.rand(1, sphere.faces_list()[0].shape[0], 1, 1, 3)
atlas = TexturesAtlas(atlas_padded)
atlas_padded_white = torch.ones(1, sphere.faces_list()[0].shape[0], 1, 1, 3)
atlas_white = TexturesAtlas(atlas_padded_white)
verts_colors_padded = torch.rand(1, sphere.verts_list()[0].shape[0], 3)
vertex_texture = TexturesVertex(verts_colors_padded)
verts_colors_padded_white = torch.ones(1, sphere.verts_list()[0].shape[0], 3)
vertex_texture_white = TexturesVertex(verts_colors_padded_white)
# No colors case
with NamedTemporaryFile(mode="w", suffix=".off") as f:
io.save_mesh(sphere, f.name)
f.flush()
mesh1 = io.load_mesh(f.name, device=device)
self.assertEqual(mesh1.device, device)
mesh1 = mesh1.cpu()
self.assertClose(mesh1.verts_padded(), sphere.verts_padded())
self.assertClose(mesh1.faces_padded(), sphere.faces_padded())
self.assertIsNone(mesh1.textures)
# Atlas case
sphere.textures = atlas
with NamedTemporaryFile(mode="w", suffix=".off") as f:
io.save_mesh(sphere, 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(), sphere.verts_padded())
self.assertClose(mesh2.faces_padded(), sphere.faces_padded())
self.assertClose(mesh2.textures.atlas_padded(), atlas_padded, atol=1e-4)
# White atlas case
sphere.textures = atlas_white
with NamedTemporaryFile(mode="w", suffix=".off") as f:
io.save_mesh(sphere, f.name)
f.flush()
mesh3 = io.load_mesh(f.name)
self.assertClose(mesh3.textures.atlas_padded(), atlas_padded_white, atol=1e-4)
# TexturesVertex case
sphere.textures = vertex_texture
with NamedTemporaryFile(mode="w", suffix=".off") as f:
io.save_mesh(sphere, f.name)
f.flush()
mesh4 = io.load_mesh(f.name, device=device)
self.assertEqual(mesh4.device, device)
mesh4 = mesh4.cpu()
self.assertClose(mesh4.verts_padded(), sphere.verts_padded())
self.assertClose(mesh4.faces_padded(), sphere.faces_padded())
self.assertClose(
mesh4.textures.verts_features_padded(), verts_colors_padded, atol=1e-4
)
# white TexturesVertex case
sphere.textures = vertex_texture_white
with NamedTemporaryFile(mode="w", suffix=".off") as f:
io.save_mesh(sphere, f.name)
f.flush()
mesh5 = io.load_mesh(f.name)
self.assertClose(
mesh5.textures.verts_features_padded(), verts_colors_padded_white, atol=1e-4
)
def test_bad(self):
# Test errors from various invalid OFF files.
io = IO()
def load(lines):
off_file = "\n".join(lines)
with NamedTemporaryFile(mode="w", suffix=".off") as f:
f.write(off_file)
f.flush()
io.load_mesh(f.name)
# First a good example
lines = [
"4 2 12",
" 1.0 0.0 1.4142",
" 0.0 1.0 1.4142",
" 1.0 0.0 0.4142",
" 0.0 1.0 0.4142",
"3 0 1 2 ",
"3 1 3 0 ",
]
# This example passes.
load(lines)
# OFF can occur on the first line separately
load(["OFF"] + lines)
# OFF line can be merged in to the first line
lines2 = lines.copy()
lines2[0] = "OFF " + lines[0]
load(lines2)
# OFF line can be merged in to the first line with no space
lines2 = lines.copy()
lines2[0] = "OFF" + lines[0]
load(lines2)
with self.assertRaisesRegex(ValueError, "Not enough face data."):
load(lines[:-1])
lines2 = lines.copy()
lines2[0] = "4 1 12"
with self.assertRaisesRegex(ValueError, "Extra data at end of file:"):
load(lines2)
lines2 = lines.copy()
lines2[-1] = "2 1 3"
with self.assertRaisesRegex(ValueError, "Faces must have at least 3 vertices."):
load(lines2)
lines2 = lines.copy()
lines2[-1] = "4 1 3 0"
with self.assertRaisesRegex(
ValueError, "A line of face data did not have the specified length."
):
load(lines2)
lines2 = lines.copy()
lines2[0] = "6 2 0"
with self.assertRaisesRegex(ValueError, "Wrong number of columns at line 5"):
load(lines2)
lines2[0] = "5 1 0"
with self.assertRaisesRegex(ValueError, "Wrong number of columns at line 5"):
load(lines2)
lines2[0] = "16 2 0"
with self.assertRaisesRegex(ValueError, "Wrong number of columns at line 5"):
load(lines2)
lines2[0] = "3 3 0"
# This is a bit of a special case because the last vertex could be a face
with self.assertRaisesRegex(ValueError, "Faces must have at least 3 vertices."):
load(lines2)
lines2[4] = "7.3 4.2 8.3"
with self.assertRaisesRegex(
ValueError, "A line of face data did not have the specified length."
):
load(lines2)
# Now try bad number of colors
lines2 = lines.copy()
lines2[2] = "7.3 4.2 8.3 932"
with self.assertRaisesRegex(ValueError, "Wrong number of columns at line 2"):
load(lines2)
lines2[1] = "7.3 4.2 8.3 932"
lines2[3] = "7.3 4.2 8.3 932"
lines2[4] = "7.3 4.2 8.3 932"
with self.assertRaisesRegex(ValueError, "Bad vertex data."):
load(lines2)
lines2 = lines.copy()
lines2[5] = "3 0 1 2 0.9"
lines2[6] = "3 0 3 0 0.9"
with self.assertRaisesRegex(ValueError, "Unexpected number of colors."):
load(lines2)
lines2 = lines.copy()
for i in range(1, 7):
lines2[i] = lines2[i] + " 4 4 4 4"
msg = "Faces colors ignored because vertex colors provided too."
with self.assertWarnsRegex(UserWarning, msg):
load(lines2)
|