# Using \`vtkStructuredGrid\` in Slicer

**URL:** <https://discourse.slicer.org/t/using-vtkstructuredgrid-in-slicer/20858>\
**Category:** Development\
**Created:** [December 1, 2021, 3:00am UTC](https://discourse.slicer.org/t/using-vtkstructuredgrid-in-slicer/20858 "2021-12-01T03:00:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [December 1, 2021, 3:00am UTC](https://discourse.slicer.org/t/using-vtkstructuredgrid-in-slicer/20858/1 "2021-12-01T03:00:41Z")

</div>

Hi,

Just wondering does Slicer support `vtkStructuredGrid`? I can’t find any information on that

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 1, 2021, 3:04pm UTC](https://discourse.slicer.org/t/using-vtkstructuredgrid-in-slicer/20858/2 "2021-12-01T15:04:09Z")

</div>

Working directly with structured grids could be hard because I think most VTK filters and mappers do not support it.

Would you like to manage images with varying spacing? Slicer supports that via storing an image with uniform spacing and applying a non-linear transform that moves each slice to the correct physical location.

You can also convert your structured grid to an unstructured grid (volumetric mesh) with scalar value associated with each point. Cross-sections can be displayed in slice views and outer surface (some internal surfaces as well, if clipping planes are used) can be displayed in 3D views.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [December 1, 2021, 3:55pm UTC](https://discourse.slicer.org/t/using-vtkstructuredgrid-in-slicer/20858/3 "2021-12-01T15:55:42Z")

</div>

Thank you for response,

> [@lassoan](#):
>
> Would you like to manage images with varying spacing?

I’m still struggling with volumetric [data that is arbitrary spaced in the XY plane but has uniform Z spacings](https://discourse.vtk.org/t/writing-custom-class-based-on-vtkdataset/6950)  
Even `vtkStructuredGrid` is not a good choice for me.

> [@lassoan](#):
>
> Slicer supports that via storing an image with uniform spacing and applying a non-linear transform that moves each slice to the correct physical location.

Didn’t think about that, probably I need to investigate nonlinear transformations.

> [@lassoan](#):
>
> You can also convert your structured grid to an unstructured grid (volumetric mesh) with scalar value associated with each point.

Unstructured grid is a possible solution but has a drawback that it consumes RAM. I think I will implement that for visualizing relatively small datasets (I also like `vtkExplicitStructuredGrid` but that is not a nearest task).

But for general case when the data is pretty big I created custom dataset [vtkRaisedGrid](https://github.com/tierra-colada/Colada/tree/master/Applications/Libs/vtkEigen) that stores only XY points (triangulated with `vtkDelaunay2D`) and Z spacings vector. I guess it is partly implicit grid. To display the data in Slicer I interpolate point scalars on `vtkImageData` via `vtkProbeFilter`. It takes some time to do that but I hope when working in release mode with TBB enabled the perfomance will be few times higher.

The problem has arised when I started testing it on real data and I’ve got some bugs when probing my custom dataset on `vtkImageData`:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/d/9dbb4f383213e91cdd720b35b0bc39c7c52200de.jpeg)

I’ve looked to the source code of `vtkProbeFilter` and it seems that when probing on `vtkImageData` it works very efficiently: for each cell of source data it probes all cells (or points I don’t remember) of image data that are within that source cell. Most likely the problem is in my implementation of my dataset or the way `vtkDelaunay2D` works.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 1, 2021, 7:08pm UTC](https://discourse.slicer.org/t/using-vtkstructuredgrid-in-slicer/20858/4 "2021-12-01T19:08:12Z")

</div>

If the topology of your data set is the same as an image volume (you can index points using IJK coordinates) and just geometry is different (the physical coordinate of each point is arbitrary) then using an image data with a warping transform should work well. You can specify physical location of each point using a vtkOrientedGridTransform.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [December 2, 2021, 5:42pm UTC](https://discourse.slicer.org/t/using-vtkstructuredgrid-in-slicer/20858/5 "2021-12-02T17:42:53Z")

</div>

Thank you for the help,

Unfortunately in general case I can’t use IJK indexing. I will keep in mind `vtkOrientedGridTransform`

But I have found bug in my custom vtk dataset: the method `MyDataset::GetCell()` that is supposed to be thread safe was not that. So I fixed that and now vtk probing from my dataset on `vtkImageData` works pretty fast with TBB enabled.

Also I implemented loading my data using `vtkUnstructuredGrid`.  
If the actual size of my data (scalar values) is 20 Mb, then Clipping Planes (from Models module) is very slow (probably 30-40 sec to clip) in DEBUG mode. Though that is not the primary task for now, I mostly rely on `vtkImageData`

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/b/1b052b164f02081dd00284488faa017e42283b15.jpeg)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [December 2, 2021, 6:31pm UTC](https://discourse.slicer.org/t/using-vtkstructuredgrid-in-slicer/20858/6 "2021-12-02T18:31:11Z")

</div>

> [@keri](#):
>
> Also I implemented loading my data using `vtkUnstructuredGrid` .  
> If the actual size of my data (scalar values) is 20 Mb, then Clipping Planes (from Models module) is very slow (probably 30-40 sec to clip) in DEBUG mode. Though that is not the primary task for now, I mostly rely on `vtkImageData`

Performance in debug mode can be 10-100x slower, so don’t make any judgement based on debug-mode tests. Nevertheless, if you have dense data then resampling on a regular grid (resulting in a vtkImageData) makes a lot of sense, as it can make processing and visualization magnitudes faster.
