I examined the .vtk and compared it to the torus.vtk from the examples that worked.
1. The cube has type POLYDATA while my torus has type UNSTRUCTURED_GRID
2. The cube1.vtk has version 5.1 while the torus has vtk data file version 2.0
3. The cube1.vtk is not a tetrahedral volumetric mesh. I triangulized it, but I did not tetrahedalize it.
I will try to sort out these issues as they are all possible causes for the error and will post updates.
The problematic 'cube1.vtk' can be found here:
https://gofile.io/d/tkJ4ue
UPDATE:
I tetrahedralized my original .obj using
https://github.com/Yixin-Hu/TetWild resulting in a .msh file.
Then I used
https://pypi.org/project/meshio/ to convert the .msh to .vtk. The resulting .vtk was of data file version 5.1 which is indeed an issue.
I could not find any tool that would convert between different versions of the vtk data file format.
Therefore I wrote a script myself that converts from 5.1 to 2.0. Use with cation as some things are hard coded to my files.
Code: Select all
import numpy as np
import os
import sys
# load 5.1
def pad_last(list_of_arrays):
forlast = list_of_arrays[-2]
last = np.zeros_like(forlast)
last[:len(list_of_arrays[-1])] = list_of_arrays[-1]
list_of_arrays[-1] = last
return list_of_arrays
filename = sys.argv[1]
outname = "converted".join(os.path.splitext(filename))
with open(filename, 'r') as f:
points = []
offsets = []
connectivity = []
meta = []
reading_section = "meta"
for l in f.readlines():
if "POINTS" in l:
reading_section = "points"
n_points = int(l.split(" ")[1])
continue
elif "OFFSET" in l:
reading_section = "offsets"
continue
elif "CONNECTIVITY" in l:
reading_section = "connec"
continue
elif "META" in l or l == "\n":
reading_section = "meta"
if reading_section == "meta":
meta.append(l)
if reading_section == "points":
points.append(np.array([float(x) for x in l.split(" ") if x not in ["\n", ""]]))
if reading_section == "offsets":
offsets.append(np.array([int(x) for x in l.split(" ") if x not in ["\n", ""]]))
if reading_section == "connec":
connectivity.append(np.array([int(x) for x in l.split(" ") if x not in ["\n", ""]], dtype=np.int64))
points = pad_last(points)
offsets = pad_last(offsets)
connectivity = pad_last(connectivity)
points = np.stack(points)
offsets = np.stack(offsets)
connectivity = np.stack(connectivity)
print(points.shape)
print(offsets.shape)
print(connectivity.shape)
# convert to 2.0 format
points = points.reshape(-1, 3)[:n_points]
offsets = offsets.reshape(-1)
offset = offsets[1] - offsets[0]
cells = connectivity.reshape(-1, offset)
cells_with_offset = np.zeros((cells.shape[0], offset+1))
print(cells_with_offset.shape)
cells_with_offset[:, 1:] = cells
cells_with_offset[:, 0] = offset
# write 2.0
with open(outname, 'w') as f:
f.write("# vtk DataFile Version 2.0\ncustom vtk output\nASCII\nDATASET UNSTRUCTURED_GRID\n")
# write points
f.write("POINTS {} double\n".format(n_points))
for p in points:
f.write(" ".join([str(x) for x in p])+"\n")
f.write("\n")
f.write("CELLS {} {}\n".format(cells_with_offset.shape[0], cells_with_offset.shape[0] * cells_with_offset.shape[1]))
for c in cells_with_offset:
f.write(" ".join([str(int(x)) for x in c]) + "\n")
f.write("\n")
f.write("CELL_TYPES {}\n".format(cells_with_offset.shape[0]))
f.writelines(["10\n"]*cells_with_offset.shape[0])