diff --git a/src/meshio/vtp/_vtp.py b/src/meshio/vtp/_vtp.py index 9287aed51..54eae6d12 100644 --- a/src/meshio/vtp/_vtp.py +++ b/src/meshio/vtp/_vtp.py @@ -4,11 +4,11 @@ """ -import warnings import base64 import logging import re import sys +import warnings import zlib import numpy as np @@ -288,9 +288,10 @@ def _parse_raw_binary(filename): } numpy_to_vtp_type = {v: k for k, v in vtp_to_numpy_type.items()} + def _decompose_strips(piece_strips): offset_start = 0 - cells = {"connectivity" : [], "offsets" : []} + cells = {"connectivity": [], "offsets": []} offset_count = 0 num_triangles = 0 tris_per_strip = [] @@ -298,33 +299,34 @@ def _decompose_strips(piece_strips): for offset in np.nditer(piece_strips["offsets"]): npts = offset - offset_start p1 = piece_strips["connectivity"][offset_start] - p2 = piece_strips["connectivity"][offset_start+1] - tris_per_strip.append(npts-2) + p2 = piece_strips["connectivity"][offset_start + 1] + tris_per_strip.append(npts - 2) num_triangles += tris_per_strip[-1] - for i in range(npts-2): - p3 = piece_strips["connectivity"][offset_start+i+2] - if (i%2): + for i in range(npts - 2): + p3 = piece_strips["connectivity"][offset_start + i + 2] + if i % 2: cells["connectivity"].extend([p2, p1, p3]) else: cells["connectivity"].extend([p1, p2, p3]) - p1=p2 - p2=p3 + p1 = p2 + p2 = p3 offset_count += 3 cells["offsets"].append(offset_count) offset_start = offset - cells["types"] = np.full(num_triangles, - meshio_to_vtk_type["triangle"], - dtype=type_dtype) - cells["connectivity"] = np.array(cells["connectivity"], - dtype=piece_strips["connectivity"].dtype ) - cells["offsets"] = np.array(cells["offsets"], - dtype=piece_strips["offsets"].dtype ) + cells["types"] = np.full( + num_triangles, meshio_to_vtk_type["triangle"], dtype=type_dtype + ) + cells["connectivity"] = np.array( + cells["connectivity"], dtype=piece_strips["connectivity"].dtype + ) + cells["offsets"] = np.array(cells["offsets"], dtype=piece_strips["offsets"].dtype) cells["tris_per_strip"] = np.array(tris_per_strip, dtype=type_dtype) return cells + def _copy_cell_data_piece(cells, cell_data_raw, num_strips): tris_per_strip = cells.pop("tris_per_strip") append_sz = tris_per_strip.sum() @@ -337,15 +339,16 @@ def _copy_cell_data_piece(cells, cell_data_raw, num_strips): # so expect them at end of cellData for i in range(num_strips): _stop = _start + tris_per_strip[i] - append_ar[_start:_stop] = values[-num_strips+i] + append_ar[_start:_stop] = values[-num_strips + i] _start = _stop - + cell_data_raw[name] = np.append(values[0:-num_strips], append_ar) - if (cells["offsets"].shape[0] != cell_data_raw[name].shape[0]): + if cells["offsets"].shape[0] != cell_data_raw[name].shape[0]: # should be a conversion error raise ReadError() return + class VtpReader: """Helper class for reading VTP files. Some properties are global to the file (e.g., byte_order), and instead of passing around these parameters, make them properties of @@ -495,7 +498,9 @@ def __init__(self, filename): # noqa: C901 piece_cells[key] = values elif child.tag == "Strips": - warnings.warn(f"Meshio cannot handle (type {child.tag}) but we explicitly convert them to triangles here.") + warnings.warn( + f"Meshio cannot handle (type {child.tag}) but we explicitly convert them to triangles here." + ) for data_array in child: if data_array.tag != "DataArray": @@ -558,16 +563,14 @@ def __init__(self, filename): # noqa: C901 if cell_data_raw: # have to copy cell data to converted triangles # will invalidate CellId numbering - if (num_strips > 0): - _copy_cell_data_piece(piece_cells, - piece_cell_data_raw, - num_strips) - + if num_strips > 0: + _copy_cell_data_piece(piece_cells, piece_cell_data_raw, num_strips) + # only append if not empty if piece_cells: cells.append(piece_cells) if not cell_data_raw: - cell_data_raw = [{}] * len(cells) + cell_data_raw = [{}] * len(cells) if len(cell_data_raw) != len(cells): raise ReadError()