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
This commit is contained in:
Daisy 2021-05-07 05:01:20 -07:00 committed by Facebook GitHub Bot
parent 34163326b2
commit 5241b7dd4e
2 changed files with 16 additions and 6 deletions

View File

@ -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)

View File

@ -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])