Skip to content

Commit 01e691c

Browse files
committed
ply polygon test + fixes
1 parent 2806717 commit 01e691c

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

meshio/ply/_ply.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,17 @@
4343
numpy_to_ply_dtype = {np.dtype(v): k for k, v in ply_to_numpy_dtype.items()}
4444

4545

46-
_cell_type_to_count = {"vertex": 1, "line": 2, "triangle": 3, "quad": 4}
47-
48-
49-
def cell_type_to_count(cell_type):
50-
if cell_type in _cell_type_to_count:
51-
return _cell_type_to_count[cell_type]
52-
53-
match = re.fullmatch(r"polygon(\d+)", cell_type)
54-
if match:
55-
return int(match.group(1))
56-
57-
58-
_cell_type_from_count = {val: key for (key, val) in _cell_type_to_count.items()}
59-
60-
6146
def cell_type_from_count(count):
62-
"""Reverse of ``cell_type_to_count``, defaults to ``"polygon" + str(counr)``
63-
if unknown."""
64-
return _cell_type_from_count.get(count) or "polygon" + str(count)
47+
if count == 1:
48+
return "vertex"
49+
elif count == 2:
50+
return "line"
51+
elif count == 3:
52+
return "triangle"
53+
elif count == 4:
54+
return "quad"
55+
56+
return "polygon"
6557

6658

6759
def read(filename):
@@ -145,7 +137,6 @@ def read_buffer(f):
145137
f"got `{line}`"
146138
)
147139

148-
# read header
149140
if is_binary:
150141
mesh = _read_binary(
151142
f,
@@ -210,7 +201,7 @@ def _read_ascii(
210201
}
211202
cell_data = {}
212203

213-
# the faces must be read line-by-line
204+
# polygons must be read line-by-line
214205
polygons = collections.defaultdict(list)
215206
for k in range(num_cells):
216207
line = f.readline().decode("utf-8").strip()
@@ -445,9 +436,11 @@ def write(filename, mesh, binary=True): # noqa: C901
445436
pd.append(value)
446437

447438
num_cells = 0
439+
legal_cell_types = ["vertex", "line", "triangle", "quad", "polygon"]
448440
for cell_type, c in mesh.cells:
449-
if cell_type_to_count(cell_type):
441+
if cell_type in legal_cell_types:
450442
num_cells += c.data.shape[0]
443+
451444
if num_cells > 0:
452445
fh.write(f"element face {num_cells:d}\n".encode("utf-8"))
453446

@@ -474,9 +467,7 @@ def write(filename, mesh, binary=True): # noqa: C901
474467
if cell_dtype is not None:
475468
ply_type = numpy_to_ply_dtype[cell_dtype]
476469
fh.write(
477-
"property list {} {} vertex_indices\n".format(
478-
"uint8", ply_type
479-
).encode("utf-8")
470+
f"property list uint8 {ply_type} vertex_indices\n".encode("utf-8")
480471
)
481472

482473
# TODO other cell data
@@ -489,7 +480,7 @@ def write(filename, mesh, binary=True): # noqa: C901
489480

490481
# cells
491482
for cell_type, data in mesh.cells:
492-
if cell_type_to_count(cell_type) is None:
483+
if cell_type not in legal_cell_types:
493484
warnings.warn(
494485
'cell_type "{}" is not supported by ply format - skipping'
495486
)

test/test_ply.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
helpers.add_point_data(helpers.tri_mesh, 1, dtype=int),
1818
helpers.add_point_data(helpers.tri_mesh, 1, dtype=float),
1919
helpers.line_mesh,
20+
helpers.polygon_mesh,
2021
# helpers.add_cell_data(helpers.tri_mesh, [("a", (), np.float64)]),
2122
# helpers.add_cell_data(helpers.tri_mesh, [("a", (2,), np.float64)]),
2223
# helpers.add_cell_data(helpers.tri_mesh, [("a", (3,), np.float64)]),

0 commit comments

Comments
 (0)