Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 20, 2021
1 parent 5065b6f commit 810d822
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions src/meshio/vtp/_vtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<https://vtk.org/wp-content/uploads/2015/04/file-formats.pdf>
"""

import warnings
import base64
import logging
import re
import sys
import warnings
import zlib

import numpy as np
Expand Down Expand Up @@ -288,43 +288,45 @@ 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 = []
type_dtype = vtp_to_numpy_type["Int64"]
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()
Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 810d822

Please sign in to comment.