diff --git a/pytorch3d/io/off_io.py b/pytorch3d/io/off_io.py index 30244370..822946ae 100644 --- a/pytorch3d/io/off_io.py +++ b/pytorch3d/io/off_io.py @@ -6,6 +6,9 @@ """ This module implements utility functions for loading and saving meshes as .off files. + +This format is introduced, for example, at +http://www.geomview.org/docs/html/OFF.html . """ import warnings from pathlib import Path @@ -226,15 +229,17 @@ def _load_off_stream(file) -> dict: faces_colors: FloatTensor of shape (F, C), where C is 3 or 4. """ header = file.readline() - if header.lower() in (b"off\n", b"off\r\n", "off\n"): - header = file.readline() while _is_line_empty(header): header = file.readline() - items = header.split(b" ") - if len(items) and items[0].lower() in ("off", b"off"): - items = items[1:] + if header[:3].lower() == b"off": + header = header[3:] + + while _is_line_empty(header): + header = file.readline() + + items = header.split() if len(items) < 3: raise ValueError("Invalid counts line: %s" % header) @@ -247,7 +252,7 @@ def _load_off_stream(file) -> dict: except ValueError: raise ValueError("Invalid counts line: %s" % header) - if (len(items) > 3 and not items[3].startswith("#")) or n_verts < 0 or n_faces < 0: + if (len(items) > 3 and not items[3].startswith(b"#")) or n_verts < 0 or n_faces < 0: raise ValueError("Invalid counts line: %s" % header) verts, verts_colors = _read_verts(file, n_verts) diff --git a/tests/test_io_off.py b/tests/test_io_off.py index 16cf766d..c28fe7fd 100644 --- a/tests/test_io_off.py +++ b/tests/test_io_off.py @@ -254,6 +254,11 @@ class TestMeshOffIO(TestCaseMixin, unittest.TestCase): 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])