Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to import various VTK and VTU file types #21

Open
2pt0 opened this issue Apr 11, 2022 · 10 comments
Open

Unable to import various VTK and VTU file types #21

2pt0 opened this issue Apr 11, 2022 · 10 comments

Comments

@2pt0
Copy link

2pt0 commented Apr 11, 2022

vtkio is unable to parse VTK and VTU files generated by pygmsh which internally uses meshio for generation of VTK and VTU files.

I have tested the following file types:

  • VTK ASCII (Legacy)
  • VTK XML Binary
  • VTU ASCII
  • VTU XML Binary - No compression
  • VTU XML Binary - LZMA compression
  • VTU XML Binary - zlib compression

Paraview is able to visualize each file without problem. The spreadsheet view for each file shows the appropriate data.

Here is the script used to generate the VTK and VTU files:

import pygmsh

with pygmsh.geo.Geometry() as geom:
    p = geom.add_polygon(
        [
            [0.0, 0.0],
            [1.0, -0.2],
            [1.1, 1.2],
            [0.1, 0.7],
        ],
        mesh_size=0.4,
    )
    geom.add_physical(p.lines[0], label="bottom")
    geom.add_physical(p.lines[1], label="right")
    geom.add_physical(p.lines[2], label="top")
    geom.add_physical(p.lines[3], label="left")

    mesh = geom.generate_mesh()

mesh.write("no-compression.vtu", compression=None)
mesh.write("lzma.vtu", compression="lzma")
mesh.write("zlib.vtu", compression="zlib")
mesh.write("ascii.vtu", binary=False)
mesh.write("binary.vtk")
mesh.write("ascii.vtk", binary=False)

Here is the file I am using to test the import of these files:

use std::path::Path;
use vtkio::model::Vtk;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let inputs = &[
        "ascii.vtk",
        "binary.vtk",
        "ascii.vtu",
        "no-compression.vtu",
        "lzma.vtu",
        "zlib.vtu",
    ];

    let vtks = inputs
        .iter()
        .map(|input| Vtk::import(input));

    for (input, vtk) in inputs.iter().zip(vtks) {
        print!("{}.. ", input);

        match vtk {
            Ok(_) => println!("parsed!"),
            Err(e) => println!("{}", e),
        };
    }

    Ok(())
}

And the output

ascii.vtk.. Parse error: Alt
binary.vtk.. Parse error: Alt
ascii.vtu.. XML error: Validation error: InvalidDataFormat
no-compression.vtu.. XML error: Validation error: DataArraySizeMismatch { name: "types", expected: 38, actual: 37 }
lzma.vtu.. XML error: Validation error: Base64Decode(InvalidByte(22, 61))
zlib.vtu.. XML error: Validation error: Base64Decode(InvalidByte(22, 61))

I'm not sure if the problem is with vtkio or mehsio. I have opened an issue here since Kitware's Paraview (which also develops VTK) reports no problems with these files.

Thanks for your help!

@elrnv
Copy link
Owner

elrnv commented Apr 14, 2022

Thank you for the issue! It is definitely a goal to support files accepted by VTK or ParaView as much as possible.

@2pt0
Copy link
Author

2pt0 commented Apr 18, 2022

Thanks for the library! Is there a path forward to fix this or any obvious reasons why the parsing is failing?

@elrnv
Copy link
Owner

elrnv commented Apr 20, 2022

I took a quick look at the ascii.vtk file, and noticed there are two additional fields titled "CONNECTIVITY" and "OFFSETS", which aren't documented (https://kitware.github.io/vtk-examples/site/VTKFileFormats/) unfortunately.
I think these use the same format as the xml files use, which means vtkio can already represent this data in a straightforward way, we just need to add some parsing functions for those extra fields.

I haven't yet looked at the other files. It's possible they are failing for different reasons.

I have been adding these undocumented fields/extensions to the vtk formats previously. I think these are inevitable, but I simply don't know what they are until someone finds files which don't work with vtkio, so thank you for bringing it up. I think these can serve as great test examples that we can add to assets and add more tests for these files in particular.

It might be a while until I have time to dig into these myself, but I'm willing to help out if you have time trying to fix some of these failures :)

@brioglade

This comment was marked as off-topic.

elrnv added a commit that referenced this issue Oct 1, 2022
Added a parser for OFFSETS and CONNECTIVITY tags within legacy vtk files
that mimic the way the xml file work.

There is a new version of legacy vtk files (seemingly v5 and up) that
specify cells via OFFSETS and CONNECTIVITY tags. This parser adds
support for that.

See issue #21 for exmaples.

This commit addresses the parsing of the legacy files in that issue.
elrnv added a commit that referenced this issue Oct 1, 2022
Added the ability to write cells in new offsets + connectivity format
for vtk versions 5+.

See issue #21 for details.
elrnv added a commit that referenced this issue Oct 1, 2022
This is taken from issue #21.

All credit for these test files and gen.py script goes to @2pt0
elrnv added a commit that referenced this issue Oct 1, 2022
Since some files can give non uint8 type cell types, we explicitly cast
everything to the right type instead of expecting it to be given
correctly. This helps parse files generated by meshio (See issue #21)
elrnv added a commit that referenced this issue Oct 2, 2022
Each inline (non-appended) binary DataArray now supports compression in the
same way appended data did before.

This commit concludes fixes for all failing meshes from issue #21.

The remaining pygmsh tests are now enabled.
elrnv added a commit that referenced this issue Oct 2, 2022
Each inline (non-appended) binary DataArray now supports compression in the
same way appended data did before.

This commit concludes fixes for all failing meshes from issue #21.

The remaining pygmsh tests are now enabled.
@elrnv elrnv mentioned this issue Feb 17, 2023
2 tasks
elrnv added a commit that referenced this issue Feb 17, 2023
Each inline (non-appended) binary DataArray now supports compression in the
same way appended data did before.

This commit concludes fixes for all failing meshes from issue #21.

The remaining pygmsh tests are now enabled.
@w1th0utnam3
Copy link
Contributor

w1th0utnam3 commented Apr 18, 2023

@elrnv I'm not sure if I should open a new issue for this but it seems to be related. A user of my surface reconstruction tool had problems loading a file that they converted from VTU to VTK using Paraview.
I attached two sample files which both result in a nom error "Parse error: Alt" with the latest commit in your release-0.7 branch.
For me it's a bit unclear how to debug with the macro based interface of nom, so I wasn't able to find out where the error actually occurs.

Here is the content of the simpler file that causes the error:

# vtk DataFile Version 5.1
vtk output
ASCII
DATASET UNSTRUCTURED_GRID

POINTS 8 double
0.5 0.4995095 0.5 0.5 0.4995095 1 0.5 0.9995095 0.5 
0.5 0.9995095 1 1 0.4995095 0.5 1 0.4995095 1 
1 0.9995095 0.5 1 0.9995095 1 

CELLS 9 8

OFFSETS vtktypeint64
0 1 2 3 4 5 6 7 8 

CONNECTIVITY vtktypeint64
0 1 2 3 4 5 6 7 

CELL_TYPES 8
1
1
1
1
1
1
1
1

Files to reproduce: legacy.zip

@elrnv
Copy link
Owner

elrnv commented Apr 19, 2023

Thank you @w1th0utnam3 for the flagging this! As far as I can tell the issue is the with DOS line endings. In the short term, feel free to swap to using Unix line endings, but please feel free to open another issue requesting support for dos line endings. It should be a simple fix in the parser.

@w1th0utnam3
Copy link
Contributor

Thanks for looking into this. I formatted the files to use LF instead of CRLF for the line endings, but for me it did not seem to fix the issue, I still get Parse error: Alt. Did it work for you?

@elrnv
Copy link
Owner

elrnv commented Apr 19, 2023

I only tested the simplified vtk file in the zip. Now looking at legacy_full.vtk, it makes sense that it doesn't work, there are whole bunch of new fields like INFORMATION, METADATA, NAME, and DATA. This is a different issue than supporting CRLF line endings. To unblock you, you should be able to just remove those fields, but it would be good to add automatic support for them in the parser.

@w1th0utnam3
Copy link
Contributor

Ah, sorry, I think when I tried to load the file with LF line endings I forgot to use the version 0.7 branch. In this branch changing from CRLF to LF actually works as you said. I'll open a separate issue regarding the line endings.
Even the legacy_full.vtk works, to me it looks like it silently ignores these METADATA lines.

However it also doesn't load the attributes in the file. I played a bit around and it seems like the usage of the datatype vtktypeint64 (which is not documented for the VTK legacy format) of the index attribute causes it to fail to parse all the attributes. When I change this datatype to int, the entire file is loaded as I would expect (minus the missing metadata, where I anyway don't know what that is). Maybe we could consider supporting this datatype as this is apparently supported by Paraview and also used for OFFSETS and CONNECTIVITY?

@elrnv
Copy link
Owner

elrnv commented Apr 27, 2023

Yes absolutely I agree we should add support for vtktypeinit64 and whatever else new versions of VTK are spitting out even if undocumented. I think we want vtkio to be compatible with other tooling people use in the wild.

elrnv added a commit that referenced this issue Jun 19, 2023
Each inline (non-appended) binary DataArray now supports compression in the
same way appended data did before.

This commit concludes fixes for all failing meshes from issue #21.

The remaining pygmsh tests are now enabled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants