# VTKm: Reading DataSet from Legacy .vtk-File

**URL:** https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285
**Category:** Support
**Created:** [May 12, 2020, 2:28pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285 "2020-05-12T14:28:44Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [May 12, 2020, 2:28pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/1 "2020-05-12T14:28:44Z")

</div>

Hello!

In context of my final Bachelors work I’m trying to read a Legacy .VTK-File using the vtkm::io::reader::VTKDataSetReader and then run a Countour Filter on it. The DataSet successfully is read, although it throws two warnings:

2020-05-12 16:07:12.392 ( 6.164s) [main thread] VTKDataSetReaderBase.h:515 WARN| Reader may not correctly read \>v4.2 files. Reading version 5.0.  
2020-05-12 16:07:12.395 ( 6.167s) [main thread] VTKDataSetReaderBase.h:602 WARN| Support for COLOR\_SCALARS is not implemented. Skipping.

Im not sure how to create 4.2 .VTK Files. The ones I use right now are created using ParaView from a .RAW File where I manually put in dimensions, word length and endian type.

When I further try to run a Contour-Filter on the DataSet I get a Exception thrown by the DataSet and my program crashes.  
Code:

```
vtkm::io::reader::VTKDataSetReader reader(path.toStdString());
vtkmDataSet = reader.ReadDataSet();

vtkm::filter::Contour contour;
contour.SetActiveField("pointvar");

vtkm::cont::DataSet outData = contour.Execute(vtkmDataSet); // exception thrown on this line

vtkm::rendering::Actor actor(
    outData.GetCellSet(),
    outData.GetCoordinateSystem(),
    outData.GetField("pointvar")
);

```

Exception:  
ErrorBadValue(No field with requested name: pointvar)

The filter seems to work when I use Mock-DataSets created at runtime by VTKm.

Running on Windows 10  
VTKm 1.5.1  
Compiled with VS 2017 Community  
CMake 3.17.2

If you need further Information please tell me what exactly I can further provide. Im happy for all help I can receive.

Sending best regards,  
David

---

<div class="post-metadata">

### Author: ![TJ\_Corona](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tj_corona/32/1013_2.png) [@TJ\_Corona](https://discourse.vtk.org/u/TJ_Corona)
#### Post date: [May 12, 2020, 4:53pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/2 "2020-05-12T16:53:15Z")

</div>

Hello David,

If you are exporting your datasets using ParaView, I would recommend using its xml readers/writers over its legacy readers/writers. You will likely have more success reading in the generated dataset.

Sincerely,  
T.J.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [May 12, 2020, 5:58pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/3 "2020-05-12T17:58:14Z")

</div>

Hello T.J.,

Is there a special XML-ReaderClass, or does the VTKDataSetReader also the class I would use for that?

Thank you!  
David

---

<div class="post-metadata">

### Author: ![TJ\_Corona](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tj_corona/32/1013_2.png) [@TJ\_Corona](https://discourse.vtk.org/u/TJ_Corona)
#### Post date: [May 12, 2020, 6:09pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/4 "2020-05-12T18:09:00Z")

</div>

You would want to use `vtkXMLReader`.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [May 12, 2020, 6:09pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/5 "2020-05-12T18:09:27Z")

</div>

Thank you, will try in a Minute.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [May 12, 2020, 6:29pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/6 "2020-05-12T18:29:57Z")

</div>

This now makes me curious. In a completed older project I used vtkMetaImageReader to obtain Data from MHD-Files. How compatible are VTK and VTKm? Will I maybe be able to use the exat same code to import data into the VTKm Projekt? Is the vtkmDataSet compatible with the vtkDataSet?

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [May 18, 2020, 3:30pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/7 "2020-05-18T15:30:12Z")

</div>

> [@TJ\_Corona](#):
>
> Hello David,
> 
> If you are exporting your datasets using ParaView, I would recommend using its xml readers/writers over its legacy readers/writers. You will likely have more success reading in the generated dataset.
> 
> Sincerely,  
> T.J.

Hi T.J.

So the import seems to be working just fine. But im still experiencing the issue with the ErrorBadValue-Exception from the Original question. Any idea on how I can further debug that?

Currently im using a XML Reader an then convert the data to a vtkmDataSet via the “DataSetConverters”-helpers provided in VTK:

```
vtkNew<vtkXMLRectilinearGridReader> reader;
reader->SetFileName(path.toLocal8Bit().data());
reader->Update();

vtkmDataSet = tovtkm::Convert(reader->GetOutputAsDataSet());
vtkmDataAvailable = true;

```

I also tried the same with an vtkMetaImageReader:

```
vtkNew<vtkMetaImageReader> reader;
reader->SetFileName(path.toLocal8Bit().data());
reader->Update();

vtkmDataSet = tovtkm::Convert(reader->GetOutput());
vtkmDataAvailable = true;

```

Both solutions still trigger the Exception.

Happy for any help and best regards,  
David

---

<div class="post-metadata">

### Author: ![TJ\_Corona](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tj_corona/32/1013_2.png) [@TJ\_Corona](https://discourse.vtk.org/u/TJ_Corona)
#### Post date: [May 18, 2020, 3:36pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/8 "2020-05-18T15:36:34Z")

</div>

Can you confirm that the dataset from `reader->GetOutputAsDataSet()` contains a field “pointvar”? My guess is either the vtkm converter is not passing this array through correctly, or there is no array in the first place.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [May 19, 2020, 11:41am UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/9 "2020-05-19T11:41:45Z")

</div>

Actually, sadly i can’t. To be honest im not sure how to successfully render one of my Datasets using vtkm. The same Datasets were successfully used in VTK but I can’t manage to get it working in VTK-m.

I already successfully setup an OpenGL pane in my GUI and I am able to render to it.

In my old project I was using the MarchingCubes algorithm for example. I can’t even seem to include that in VTK-m, do you know if I have to enable a option in CMake while compiling VTK-m to eanble that filter?

---

<div class="post-metadata">

### Author: ![RobertMaynard](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robertmaynard/32/24_2.png) [@RobertMaynard](https://discourse.vtk.org/u/RobertMaynard)
#### Post date: [May 19, 2020, 1:31pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/10 "2020-05-19T13:31:14Z")

</div>

> [@McDave17](#):
>
> vtkmDataSet = tovtkm::Convert(reader-\>GetOutputAsDataSet());

The `tovtkm::Convert` function has two signatures. The single parameter version converts only point coordinates and topology, all fields are not converted. To convert fields you need to use the two  
parameter version like this:

```auto
vtkmDataSet = tovtkm::Convert(reader->GetOutputAsDataSet(), tovtkm::FieldsFlag::PointsAndCells);

```

> [@McDave17](#):
>
> Actually, sadly i can’t. To be honest im not sure how to successfully render one of my Datasets using vtkm. The same Datasets were successfully used in VTK but I can’t manage to get it working in VTK-m.

An easy way to debug/see what is part of a `vtkm::cont::DataSet` is to use the `PrintSummary` method:

```auto
vtkmDataSet.PrintSummary(std::cout);

```

> [@McDave17](#):
>
> In my old project I was using the MarchingCubes algorithm for example. I can’t even seem to include that in VTK-m, do you know if I have to enable a option in CMake while compiling VTK-m to eanble that filter?

The VTK-m entry points for iso-contouring are `vtkm::filter::Contour` and `vtkm::worklet::Contour`. These classes will internally select an appropriate implementation such as MarchingCells or FlyingEdges based on the input data. It is not possible to explicitly call `MarchingCubes`

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [May 19, 2020, 1:44pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/11 "2020-05-19T13:44:20Z")

</div>

> [@RobertMaynard](#):
>
> vtkmDataSet.PrintSummary(std::cout);

Thank you for your Help!

So this is the output of my program when I print the Dataset after I added the flags you suggested. How would I be able to determain how to create a contour over this?

```
MainWindow::onOpenMHDFile path=D:/Studium/Abschlussarbeit/raw/Lobster_u8_301_324_56_LE.mhd
DataSet:
  CoordSystems[1]
   Coordinate System coords assoc= Points valueType=class vtkm::Vec<float,3> storageType=struct vtkm::cont::StorageTagVirtual numValues=5461344 bytes=65536128 [(0,0,0) (1,0,0) (2,0,0) ... (298,323,55) (299,323,55) (300,323,55)]
  CellSet
  StructuredCellSet:
   UniformConnectivity<3> pointDim[301 324 56]
  Fields[1]
   MetaImage assoc= Points valueType=unsigned char storageType=struct vtkm::cont::StorageTagVirtual numValues=5461344 bytes=5461344 [0 0 0 ... 0 0 10]

```

---

<div class="post-metadata">

### Author: ![RobertMaynard](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robertmaynard/32/24_2.png) [@RobertMaynard](https://discourse.vtk.org/u/RobertMaynard)
#### Post date: [May 19, 2020, 1:50pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/12 "2020-05-19T13:50:14Z")

</div>

Your field name in VTK-m is `MetaImage` and not `pointvar`. Your original example should work with the correction for the field name.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [May 19, 2020, 2:05pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/13 "2020-05-19T14:05:12Z")

</div>

Thank you, finally making progress, sadly running into the next issue rather quickly. When I execute the contour I get some (to me) rather cryptic error messages and then a crash:

```
16:03:06.986 ( 17.858s) [main thread] TryExecute.cxx:55 ERR| TryExecute encountered an error: ErrorBadValue (Input array to worklet invocation the wrong size.)
16:03:06.989 ( 17.861s) [main thread] TryExecute.cxx:55 ERR| Failing functor: struct vtkm::worklet::internal::detail::DispatcherBaseTryExecuteFunctor
16:03:06.991 ( 17.863s) [main thread] TryExecute.cxx:55 ERR| Failing device: Serial
```

---

<div class="post-metadata">

### Author: ![RobertMaynard](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robertmaynard/32/24_2.png) [@RobertMaynard](https://discourse.vtk.org/u/RobertMaynard)
#### Post date: [May 20, 2020, 3:41pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/14 "2020-05-20T15:41:48Z")

</div>

> [@McDave17](#):
>
> Thank you, finally making progress, sadly running into the next issue rather quickly. When I execute the contour I get some (to me) rather cryptic error messages and then a crash:

The “Input array to worklet invocation the wrong size.” error message means that the `MetaImage` field length doesn’t match the number of points in the input data.

It sounds like the input data set being to VTK-m isn’t the exact same one as you read from disk, and therefore have a different number of points.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [June 1, 2020, 6:22pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/15 "2020-06-01T18:22:36Z")

</div>

Thank you, but how can I control that? Why would the transformation create a DataSet not equal to the one loaded? Or did I specify a wrong dimension size? Last isn’t likely, since the MetaImage is read fine in plain VTK.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [June 2, 2020, 11:53am UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/16 "2020-06-02T11:53:42Z")

</div>

Should the number of cells and the number of points be the same?

```
vtkmDataSet.GetNumberOfPoints()

```

returns 5 461 344 (=301 x 324 x 56, matches dimensions)

```
vtkmDataSet.GetNumberOfCells()

```

returns 5 329 500 (=300 x 323 x 55, shoudlnt this be smaller? More like dimenX - 2 instead dimenX - 1?)

This confuses me, I’d expect the number of cells be smaller than the number of points since the points on the edges are part of less cells.

---

<div class="post-metadata">

### Author: ![RobertMaynard](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robertmaynard/32/24_2.png) [@RobertMaynard](https://discourse.vtk.org/u/RobertMaynard)
#### Post date: [June 2, 2020, 12:20pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/17 "2020-06-02T12:20:44Z")

</div>

> More like dimenX - 2 instead dimenX - 1?

The number of cells is always Dim-1 compared to the points. The way I think about it is to imagine two connected cells. You have 3 points in the connected direction, and two cells.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [June 2, 2020, 12:33pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/18 "2020-06-02T12:33:15Z")

</div>

Okay. Then these values seem fine.

> [@RobertMaynard](#):
>
> It sounds like the input data set being to VTK-m isn’t the exact same one as you read from disk, and therefore have a different number of points.

Do you mean that the vtkMetaImageReader reads the data incorrectly? I.e. that the .mhd file ist not setup correclty? An therefore the Conversion is done correctly with an non functioning data set.

---

<div class="post-metadata">

### Author: ![McDave17](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mcdave17/32/1778_2.png) [@McDave17](https://discourse.vtk.org/u/McDave17)
#### Post date: [June 2, 2020, 1:29pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/19 "2020-06-02T13:29:29Z")

</div>

This also makes me curios, why is the Coordinate System so massive? Is it supposed to be 65MB for 5.4 million 8 Byte values? Is the coordinatesystem to big for the actual MetaImage-Field?

```
DataSet:
  CoordSystems[1]
    Coordinate System coords assoc= Points valueType=class vtkm::Vec<float,3> storageType=struct vtkm::cont::StorageTagVirtual numValues=5461344 bytes=65536128 [(0,0,0) (1,0,0) (2,0,0) ... (298,323,55) (299,323,55) (300,323,55)]
  CellSet
  StructuredCellSet:
   UniformConnectivity<3> pointDim[301 324 56]
  Fields[1]
   MetaImage assoc= Points valueType=unsigned char storageType=struct vtkm::cont::StorageTagVirtual numValues=5461344 bytes=5461344 [0 0 0 ... 0 0 10]
MainWindow::onLeftRender
  num cells=5329500, num fields=1, num points=5461344
```

---

<div class="post-metadata">

### Author: ![RobertMaynard](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robertmaynard/32/24_2.png) [@RobertMaynard](https://discourse.vtk.org/u/RobertMaynard)
#### Post date: [June 3, 2020, 12:29pm UTC](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285/20 "2020-06-03T12:29:51Z")

</div>

The coordinate system has 5461344 entries which is `= 301 x 324 x 56`. The 65MB datasize is correct if the coordinate system is being treated as a fully explicit ( each point is stored as an x,y,z coordinate ) instead of using an optimized layout.

The reason for the explicit coordinates is that the `tovtkm::Convert` function doesn’t currently have optimized paths for RectilinearGrids, and instead converts them into fully ExplictGrids.

[Next page](https://discourse.vtk.org/t/vtkm-reading-dataset-from-legacy-vtk-file/3285.md?page=2)
