Skip to content

Commit

Permalink
Add binary new-lines to VTK export. (#795)
Browse files Browse the repository at this point in the history
Fix VTK output: add newlines after binary data blocks to ensure compatibility with meshio.
  • Loading branch information
gertjanvanzwieten committed May 19, 2023
2 parents 8d1e969 + 0170613 commit c471750
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions nutils/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,18 @@ def vtkarray(a): # convert to big endian data and zero-pad all axes to length 3
vtk.write(b'# vtk DataFile Version 3.0\nvtk output\nBINARY\nDATASET UNSTRUCTURED_GRID\n')
vtk.write('POINTS {} {}\n'.format(npoints, vtkdtype[points.dtype]).encode('ascii'))
points.tofile(vtk)
vtk.write(b"\n")
vtk.write('CELLS {} {}\n'.format(ncells, t_cells.size).encode('ascii'))
t_cells.tofile(vtk)
vtk.write(b"\n")
vtk.write('CELL_TYPES {}\n'.format(ncells).encode('ascii'))
vtkcelltype[nverts].repeat(ncells).tofile(vtk)
vtk.write(b"\n")
for n, items in gathered:
vtk.write('{}_DATA {}\n'.format('POINT' if n == npoints else 'CELL', n).encode('ascii'))
for dname, array in items:
vtk.write(vtkndim[array.ndim].format(dname, vtkdtype[array.dtype]).encode('ascii'))
array.tofile(vtk)
vtk.write(b"\n")

# vim:sw=4:sts=4:et
10 changes: 7 additions & 3 deletions tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,25 @@ def data(self):
yield bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 128, 0, 0, 0, 0, 0, 0, 63, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 128, 0, 0])
else:
raise Exception('not supported: xtype={!r}, ndims={}'.format(self.xtype, self.ndims))
yield b'\n'
if self.ndims == 1:
yield b'CELLS 3 9\n'
yield bytes([0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 3])
yield b'CELL_TYPES 3\n'
yield b'\nCELL_TYPES 3\n'
yield bytes([0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3])
elif self.ndims == 2:
yield b'CELLS 2 8\n'
yield bytes([0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3])
yield b'CELL_TYPES 2\n'
yield b'\nCELL_TYPES 2\n'
yield bytes([0, 0, 0, 5, 0, 0, 0, 5])
elif self.ndims == 3:
yield b'CELLS 1 5\n'
yield bytes([0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3])
yield b'CELL_TYPES 1'
yield b'\nCELL_TYPES 1'
yield bytes([10, 0, 0, 0, 10])
else:
raise Exception('invalid ndims {}'.format(self.ndims))
yield b'\n'
if self.p is not None:
yield b'POINT_DATA 4\n'
if self.ptype == 'f4' and self.pshape == ():
Expand Down Expand Up @@ -128,13 +130,15 @@ def data(self):
yield bytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35])
else:
raise Exception('not supported: ptype={}, udims={}'.format(self.ptype, self.udims))
yield b'\n'
if self.c is not None:
yield b'CELL_DATA 1\n'
if self.ndims == 3 and self.ctype == 'i1' and self.cshape == ():
yield b'SCALARS c char 1\nLOOKUP_TABLE default\n'
yield bytes([0])
else:
raise Exception('not supported: ndims={}, ctype={}, cdims={}'.format(self.ndims, self.ctype, self.cdims))
yield b'\n'

def test_data(self):
with tempfile.TemporaryDirectory() as outdir, treelog.set(treelog.DataLog(outdir)):
Expand Down

0 comments on commit c471750

Please sign in to comment.