meshio icon indicating copy to clipboard operation
meshio copied to clipboard

[BUG] Converting from .obj to .vtk results in type error when reading converted file with VTK/Paraview

Open user27182 opened this issue 2 years ago • 1 comments

Describe the bug OBJ files which are converted to VTK format with meshio generate a type error when read back with VTK or Paraview. The expected behaviour is that converting between formats would not result in a read error.

Based on an initial assessment this read error does not seem to impact the underlying data and/or ability to further process the mesh or view it with Paraview. It does, however, cause the user to think that the mesh is bad or improper in some way (and may lead them down a debugging rabbit hole to find the problem).

The issue can be resolved by first converting to an intermediate format, e.g. OBJ -> INP -> VTK, though this is a workaround.

To Reproduce The following code writes an OBJ file, converts it to VTK, then reads it back with VTK (which generates a type error). Note that other file saves and conversions are also performed to assess whether the problem applies only to OBJ to VTK conversion.

import meshio
import subprocess
from vtk import vtkUnstructuredGridReader

# two triangles and one quad
points = [
    [0.0, 0.0, 0.0],
    [1.0, 0.0, 0.0],
    [0.0, 1.0, 0.0],
    [1.0, 1.0, 0.0],
    [2.0, 0.0, 0.0],
    [2.0, 1.0, 0.0],
]
cells = [
    ("triangle", [[0, 1, 2], [1, 3, 2]]),
    ("quad", [[1, 4, 5, 3]]),
]

mesh = meshio.Mesh(
    points,
    cells,
    # Optionally provide extra data on points, cells, etc.
    point_data={"T": [0.3, -1.2, 0.5, 0.7, 0.0, -3.0]},
    # Each item in cell data must match the cells array
    cell_data={"a": [[0.1, 0.2], [0.4]]},
)
mesh.write("foo.vtk")
mesh.write("foo.obj")

# # convert with CLI
subprocess.run(["meshio", "convert", "foo.vtk", "vtk_to_vtk.vtk"])  # test VTK to VTK
subprocess.run(["meshio", "convert", "foo.obj", "obj_to_vtk.vtk"])  # test OBJ to VTK

# test OBJ to VTK with intermediate conversion through another format (e.g. INP)
subprocess.run(["meshio", "convert", "foo.obj", "obj_to_inp.inp"])
subprocess.run(["meshio", "convert", "obj_to_inp.inp", "obj_to_inp_to_vtk.vtk"])

def read_vtk(path):
    reader = vtkUnstructuredGridReader()
    reader.SetFileName(path)
    reader.Update()
    return reader.GetOutput()

# read results
read_vtk("foo.vtk")  # no error
read_vtk("vtk_to_vtk.vtk")  # no error
read_vtk("obj_to_vtk.vtk")  # Unsupported data type: vtktypeint32
read_vtk("obj_to_inp_to_vtk.vtk")  # no error

Output: vtkDataReader.cxx:1978 ERR| vtkUnstructuredGridReader (000001C031212400): Unsupported data type: vtktypeint32

user27182 avatar Apr 27 '23 00:04 user27182

I also had problems with the VTK 5.1 format and forced the writing to vtk42 - which seems to work more reliably in paraview.

reox avatar Jul 25 '23 13:07 reox