From 5241b7dd4e03ee3d7fdec4b1258da05c299eb98c Mon Sep 17 00:00:00 2001 From: Daisy Date: Fri, 7 May 2021 05:01:20 -0700 Subject: [PATCH] Handle header has no newline or space after OFF. (#665) Summary: Allow a line like `OFF10 10 10` as the start of an OFF file. Such things apparently occur in ModelNet40. This resolves https://github.com/facebookresearch/pytorch3d/issues/663. Pull Request resolved: https://github.com/facebookresearch/pytorch3d/pull/665 Test Plan: New test Reviewed By: nikhilaravi Differential Revision: D28180006 Pulled By: bottler fbshipit-source-id: 7f474c6a262e32da012217e09f76e8672a7f0278 --- pytorch3d/io/off_io.py | 17 +++++++++++------ tests/test_io_off.py | 5 +++++ 2 files changed, 16 insertions(+), 6 deletions(-) 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])