[vtkusers] Making custom data for a regular 3d voxel grid

Elvis Stansvik elvis.stansvik at orexplore.com
Tue Aug 29 13:57:18 EDT 2017

Hi Daniel,

2017-08-29 19:20 GMT+02:00 Daniel Minsky <minsky at tandar.cnea.gov.ar>:
> Dear all,
> I am very new to VTK, I could follow the C++ examples to read an image from
> disk, however I would like to generate the image inside my program with some
> voxel data coming from other object in my code.
> I would like to generate a vtkImageData with a regular 3d voxel grid values.
> I do have all the unsigned_char 100x200x40 voxels in a unsigned char array.
> All voxels are cubic with 1cm side.
> How do I feed the vtkImageData object with my data array?

You can get a pointer to the data underlying the vtkImageData with
GetScalarPointer(). Using that you can simply copy your values there.
Make sure that you call SetExtent(...) followed by
AllocateScalars(...) so that the underlying data has been allocated
first, and that you write your data in the correct order (I think VTK
uses Fortran order). You can use SetSpacing(...) to control the
spacing of the resulting image, which will determine its bounds in the
VTK world.

> How do I use the vtkImageData? The vtkImageData does not have the
> GetOutputPort() method to feed a Mapper and I am very confused about the
> correct pipeline.

vtkImageData is not a VTK "algorithm". It is instead normally the
output of an algorithm (for example a file reading algorithm, or some
image filtering algorithm). That's why it doesn't have the
GetOutputPort(..)/SetInputConnection(..), since it doesn't take part
as a filter in VTK pipelines.

To render a ready-made vtkImageData as a volume, you set up a
vtkVolumeMapper (for example vtkGPUVolumeRayCastMapper or a
vtkSmartVolumeMapper), and set the vtkImageData as its input data
using SetInputData(...). You then set up a vtkVolumeProperty, with an
associated scalar opacity transfer function (a vtkPiecewiseFunction)
and color transfer function (a vtkColorTransferFunction). You then
create a vtkVolume and set the vtkVolumeProperty and the volume mapper
on it. Finally, you add the volume to your vtkRenderer.

I know it's a lot to process at first, but I think the VTK volume
rendering examples should be more helpful than my explanation above,
once you've got your vtkImageData ready.

Hope that helps.


> My final goal is to make a 3d image of the data within an upper and lower
> thresholds and an arbitrary plane slice.
> Kind regards,
> Daniel
> --
> Dr. Daniel Minsky
> Investigador CONICET - CNEA
> Departamento de Física
> Centro Atómico Constituyentes - Comisión Nacional de Energía Atómica
> Av. Gral. Paz 1499 (B1650KNA) San Martín - Pcia. de Buenos Aires - Argentina
> Tel: +54 11 6772 7913
> Fax: +54 11 6772 7121
> e-mail: minsky at tandar.cnea.gov.ar
> _______________________________________________
> Powered by www.kitware.com
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
> Search the list archives at: http://markmail.org/search/?q=vtkusers
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers

More information about the vtkusers mailing list