Skip to content

Commit b1753bd

Browse files
authored
Merge pull request #1086 from nschloe/polygons
Polygons
2 parents 2e46a6c + 0525557 commit b1753bd

File tree

8 files changed

+62
-65
lines changed

8 files changed

+62
-65
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ This document only describes _breaking_ changes in meshio. If you are interested
44
fixes, enhancements etc., best follow [the meshio project on
55
GitHub](https://github.com/nschloe/meshio).
66

7+
## v4.4.0 (Apr 29, 2021)
8+
9+
- Polygons are now stored as `"polygon"` cell blocks, not `"polygonN"` (where `N` is the
10+
number of nodes per polygon). One can simply retrieve the number of points via
11+
`cellblock.data.shape[1]`.
12+
13+
714
## v4.0.0 (Feb 18, 2020)
815

916
- `mesh.cells` used to be a dictionary of the form

meshio/_mesh.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,24 @@ def __init__(
8282

8383
def __repr__(self):
8484
lines = ["<meshio mesh object>", f" Number of points: {len(self.points)}"]
85+
special_cells = [
86+
"polygon",
87+
"polyhedron",
88+
"VTK_LAGRANGE_CURVE",
89+
"VTK_LAGRANGE_TRIANGLE",
90+
"VTK_LAGRANGE_QUADRILATERAL",
91+
"VTK_LAGRANGE_TETRAHEDRON",
92+
"VTK_LAGRANGE_HEXAHEDRON",
93+
"VTK_LAGRANGE_WEDGE",
94+
"VTK_LAGRANGE_PYRAMID",
95+
]
8596
if len(self.cells) > 0:
8697
lines.append(" Number of cells:")
87-
for tpe, elems in self.cells:
88-
lines.append(f" {tpe}: {len(elems)}")
98+
for cell_type, elems in self.cells:
99+
string = cell_type
100+
if cell_type in special_cells:
101+
string += f"({elems.shape[1]})"
102+
lines.append(f" {string}: {len(elems)}")
89103
else:
90104
lines.append(" No cells.")
91105

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
)

meshio/vtk/_vtk.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,12 +586,17 @@ def translate_cells(data, types, cell_data_raw):
586586
cell = data[cell_idx]
587587

588588
cell_type = vtk_to_meshio_type[vtk_cell_type]
589-
if cell_type == "polygon":
590-
cell_type += str(data[offsets[idx]])
591589

592-
if len(cells) > 0 and cells[-1].type == cell_type:
590+
if (
591+
len(cells) > 0
592+
and cells[-1].type == cell_type
593+
# the following check if needed for polygons; key can be equal, but
594+
# still needs to go into a new block
595+
and len(cell) == len(cells[-1].data[-1])
596+
):
593597
cells[-1].data.append(cell)
594598
else:
599+
# open up a new cell block
595600
cells.append(CellBlock(cell_type, [cell]))
596601

597602
# convert data to numpy arrays
@@ -752,15 +757,14 @@ def _write_cells(f, cells, binary):
752757
f.write(f"CELL_TYPES {total_num_cells}\n".encode("utf-8"))
753758
if binary:
754759
for c in cells:
755-
key_ = c.type[:7] if c.type[:7] == "polygon" else c.type
756-
vtk_type = meshio_to_vtk_type[key_]
760+
vtk_type = meshio_to_vtk_type[c.type]
757761
np.full(len(c.data), vtk_type, dtype=np.dtype(">i4")).tofile(f, sep="")
758762
f.write(b"\n")
759763
else:
760764
# ascii
761765
for c in cells:
762-
key_ = c.type[:7] if c.type[:7] == "polygon" else c.type
763-
np.full(len(c.data), meshio_to_vtk_type[key_]).tofile(f, sep="\n")
766+
vtk_type = meshio_to_vtk_type[c.type]
767+
np.full(len(c.data), vtk_type).tofile(f, sep="\n")
764768
f.write(b"\n")
765769

766770

meshio/vtu/_vtu.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def _cells_from_data(connectivity, offsets, types, cell_data_raw):
9797
start_cn[items + 1],
9898
_vtk_to_meshio_order(types[start], sz, dtype=offsets.dtype) - sz,
9999
)
100-
cells.append(CellBlock(meshio_type + str(sz), connectivity[indices]))
100+
cells.append(CellBlock(meshio_type, connectivity[indices]))
101101

102102
# Store cell data for this set of cells
103103
for name, d in cell_data_raw.items():
@@ -668,9 +668,10 @@ def write(filename, mesh, binary=True, compression="zlib", header_type=None):
668668
# in certain places.
669669
is_polyhedron_grid = False
670670
for c in mesh.cells:
671-
if c.type[:10] == "polyhedron":
671+
if c.type.startswith("polyhedron"):
672672
is_polyhedron_grid = True
673673
break
674+
674675
# The current implementation cannot mix polyhedral cells with other cell types.
675676
# To write such meshes, represent all cells as polyhedra.
676677
if is_polyhedron_grid:
@@ -908,31 +909,9 @@ def _polyhedron_face_cells(face_cells):
908909

909910
# types
910911
types_array = []
911-
for k, v in mesh.cells:
912-
# For polygon and polyhedron grids, the number of nodes is part of the cell
913-
# type key. This part must be stripped away.
914-
special_cells = [
915-
"polygon",
916-
"polyhedron",
917-
"VTK_LAGRANGE_CURVE",
918-
"VTK_LAGRANGE_TRIANGLE",
919-
"VTK_LAGRANGE_QUADRILATERAL",
920-
"VTK_LAGRANGE_TETRAHEDRON",
921-
"VTK_LAGRANGE_HEXAHEDRON",
922-
"VTK_LAGRANGE_WEDGE",
923-
"VTK_LAGRANGE_PYRAMID",
924-
]
925-
key_ = None
926-
for string in special_cells:
927-
if k.startswith(string):
928-
key_ = string
929-
930-
if key_ is None:
931-
# No special treatment
932-
key_ = k
933-
934-
# further adaptions for polyhedron
935-
if k.startswith("polyhedron"):
912+
for key, v in mesh.cells:
913+
# some adaptions for polyhedron
914+
if key.startswith("polyhedron"):
936915
# Get face-cell relation on the vtu format. See comments in helper
937916
# function for more information of how to specify this.
938917
faces_loc, faceoffsets_loc = _polyhedron_face_cells(v)
@@ -944,8 +923,9 @@ def _polyhedron_face_cells(face_cells):
944923
assert faces is not None
945924
faces += faces_loc
946925
faceoffsets += faceoffsets_loc
926+
key = "polyhedron"
947927

948-
types_array.append(np.full(len(v), meshio_to_vtk_type[key_]))
928+
types_array.append(np.full(len(v), meshio_to_vtk_type[key]))
949929

950930
types = np.concatenate(
951931
types_array

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = meshio
3-
version = 4.3.13
3+
version = 4.4.0
44
author = Nico Schlömer et al.
55
author_email = [email protected]
66
description = I/O for many mesh formats

test/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@
254254
[
255255
("triangle", [[0, 1, 2], [4, 5, 6]]),
256256
("quad", [[0, 1, 2, 3]]),
257-
("polygon5", [[1, 4, 5, 6, 2]]),
258-
("polygon6", [[0, 3, 7, 8, 9, 10], [1, 3, 7, 8, 9, 10]]),
257+
("polygon", [[1, 4, 5, 6, 2]]),
258+
("polygon", [[0, 3, 7, 8, 9, 10], [1, 3, 7, 8, 9, 10]]),
259259
],
260260
)
261261

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)