elrnv / vtkio

Visualization ToolKit (VTK) file parser and writer
Apache License 2.0
55 stars 12 forks source link

How to write a new file? #2

Closed dineshadepu closed 5 years ago

dineshadepu commented 5 years ago

Hello,

I am trying to use this library to write a vtk file of unstructured grid, used in SPH simulation. Is there any example where we can write the data to a file?

A sample file, I want to write is

# vtk DataFile Version 3.0
Time some
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 16 float
-0.8999 -0.0003 0.0
-0.6000 -0.0003 0.0
-0.3000 -0.0003 0.0
0.0000 -0.0003 0.0
-0.8999 0.2997 0.0
-0.6000 0.2997 0.0
-0.3000 0.2997 0.0
0.0000 0.2997 0.0
-0.8999 0.5997 0.0
-0.6000 0.5997 0.0
-0.3000 0.5997 0.0
0.0000 0.5997 0.0
-0.8999 0.8997 0.0
-0.6000 0.8997 0.0
-0.3000 0.8997 0.0
0.0000 0.8997 0.0
POINT_DATA 16
SCALARS Diameter float 1
LOOKUP_TABLE default
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
0.1500
VECTORS Force float
1464120.5000 437985.7188 0.0000
406.5037 -1765.8165 0.0000
0.0000 -1765.8000 0.0000
0.0000 -1765.8000 0.0000
1464120.5000 437985.6562 0.0000
406.5037 -1765.8165 0.0000
0.0000 -1765.8000 0.0000
0.0000 -1765.8000 0.0000
1464120.5000 437985.5938 0.0000
406.5037 -1765.8165 0.0000
0.0000 -1765.8000 0.0000
0.0000 -1765.8000 0.0000
1464120.2500 437985.9688 0.0000
406.5037 -1765.8165 0.0000
0.0000 -1765.8000 0.0000
0.0000 -1765.8000 0.0000
elrnv commented 5 years ago

Sorry for missing this post, somehow it has slipped through my notifications. There are a few examples in tests/vtkio.rs that may be of use to you. The way to do it is to construct a Vtk struct and populate all the relevant fields as is done in tests/vtkio.rs. Perhaps I should include some more examples for this.

Currently the parser expects there to be a CELLS and CELL_TYPES fields for unstructured grid. So adding

CELLS 0 0
CELL_TYPES 0

will work for your example.

For the specific example you provided, something like the following will work:

    let vtk_model = Vtk {
        version: Version::new((3, 0)),
        title: String::from("Time some"),
        data: DataSet::UnstructuredGrid {
            points: vec![
                    -0.8999f32, -0.0003, 0.0,
                    -0.6000, -0.0003, 0.0,
                    -0.3000, -0.0003, 0.0,
                     0.0000, -0.0003, 0.0,
                    -0.8999, 0.2997, 0.0,
                    -0.6000, 0.2997, 0.0,
                    -0.3000, 0.2997, 0.0,
                     0.0000, 0.2997, 0.0,
                    -0.8999, 0.5997, 0.0,
                    -0.6000, 0.5997, 0.0,
                    -0.3000, 0.5997, 0.0,
                     0.0000, 0.5997, 0.0,
                    -0.8999, 0.8997, 0.0,
                    -0.6000, 0.8997, 0.0,
                    -0.3000, 0.8997, 0.0,
                     0.0000, 0.8997, 0.0,
            ].into(),
            cells: Cells {
                num_cells: 0,
                vertices: vec![],
            },
            cell_types: vec![],
            data: Attributes {
                point: vec![
                    (
                        String::from("Diameter"),
                        Attribute::Scalars {
                            num_comp: 1,
                            lookup_table: None,
                            data: vec![
                                0.1500f32; 16
                            ].into(),
                        },
                    ),
                    (
                        String::from("Force"),
                        Attribute::Vectors {
                            data: vec![
                                1464120.5000f32, 437985.7188, 0.0000,
                                406.5037, -1765.8165, 0.0000,
                                0.0000, -1765.8000, 0.0000,
                                0.0000, -1765.8000, 0.0000,
                                1464120.5000, 437985.6562, 0.0000,
                                406.5037, -1765.8165, 0.0000,
                                0.0000, -1765.8000, 0.0000,
                                0.0000, -1765.8000, 0.0000,
                                1464120.5000, 437985.5938, 0.0000,
                                406.5037, -1765.8165, 0.0000,
                                0.0000, -1765.8000, 0.0000,
                                0.0000, -1765.8000, 0.0000,
                                1464120.2500, 437985.9688, 0.0000,
                                406.5037, -1765.8165, 0.0000,
                                0.0000, -1765.8000, 0.0000,
                                0.0000, -1765.8000, 0.0000,
                            ].into(),
                        },
                    ),
                ],
                cell: vec![],
            },
        },
    };
    vtkio::export_ascii(vtk_model, &std::path::PathBuf::from("test.vtk"))
        .expect("Failed to write to vtk file.");

This will write your example file into "test.vtk" in your crate's root directory.

Thank you for this issue!

dineshadepu commented 5 years ago

Thanks. I figured this out earlier.