How to create vtkUnstructuredGrid from an itk::Image?

Hi,

I have binary label files in itk::Image. I have gone through a few examples in ITK and VTK. What I understood, we need to create the cell type object for generating a specific mesh in both ITK and Vtk. For example, I need to generate a tetrahedron mesh, and I need to choose typedef MeshType::CellType CellType; in which CellType can be various type of cells.

In VTK, there is an example that they create the mesh cells. However, I do not how to connect the data types between itk and vtk.

How can I create the mesh from itk::Image and from that generate vtkUnstructuredGrid?
Is there any example available to show some tips and tricks?

Thanks

The answer mostly depends on what you are trying to accomplish. Generally, some classes from ITKVtkGlue could help.

The ITKVtkGlue external module in particular has a class with this functionality:

https://github.com/InsightSoftwareConsortium/ITKVtkGlue

i.e. itk::MeshToVTKUnstructuredGridFilter.

1 Like

Is it like that, I need to follow the following transformation:

itk::Image ==> create the point sets (link) ==> generate the tetrahedron mesh from the point sets and save as an vtkunstructuredGrid ( MakeTetrahedron() function in this link)

Is this procedure correct?

You might need BinaryMask3DMeshSource as the first step.

1 Like

Hi again,

I have enabled ITKVtkGlue Module and recompiled and rebuilt ITK. Then, I added, ITKVtkGlue in the cmakeList find package list, however, once I am trying to include #include "itkMeshToVTKUnstructuredGridFilter.h" in the code , what could be the reason? Should I add other modules to the cmakeList?

Thanks

Hi Sara,

The ITKVtkGlue repository is ahead of the version distributed in ITK and "itkMeshToVTKUnstructuredGridFilter.h" is not yet in ITK. One option is to build the module externally; that is, clone the repository:

https://github.com/InsightSoftwareConsortium/ITKVtkGlue

Then create a build directory, and build against the ITK build tree like it is an application being built against ITK. Then, build your application.

Or, since they are templated headers, you could just copy the *.h and *.hxx files.

2 Likes

@matt.mccormick Thank you, Sir, I am just checking with you if the installation procedure is correct?

I tried to follow the link that you had guided a user. I set the ITK_DIR and VTK_DIR to the build directory of each application. Then, I set the following flags:

BUILD_SHARED_LIBS ON
BUILD_TESTING OFF
ITK_USE_KWSTYLE OFF

ITK_DIR /home/esara/software/vtk-itk/ITK-build
VTK_DIR /home/esara/software/vtk-itk/VTK-build

After running cmake successfully, I could build the ITKVtkGlue

~/software/vtk-itk/ITKVtkGlue-build$ sudo make -j4
Scanning dependencies of target VtkGlue-all
Scanning dependencies of target ITKData
Built target VtkGlue-all
Built target ITKData

Thank you

Perfect, Sara – great work!

1 Like

Thanks for replying the message.

Do you mean these files?
Where should I copy the files?

Still the #include "itkMeshToVTKUnstructuredGridFilter.h" is shown with red underline indicating has not been included yet. Or should I include any module in the CmakeListfile of my code?

Thanks

Include ITKVtkGlue in the find_package(ITK [...] COMPONENTS list. For example, in your CMakeLists.txt file:

find_package(ITK REQUIRED
  COMPONENTS
    ITKVtkGlue
    ITKImageIO
    # Any other ITK modules your application requires. These can be generated with https://github.com/InsightSoftwareConsortium/ITK/blob/master/Utilities/Maintenance/WhatModulesITK.py
  )
# This line adds the include directories required to use the modules listed above to any executables that follow
include(${ITK_USE_FILE})

add_executable(MyExecutable MyExecutable.cxx)
target_link_libraries(MyExecutable ${ITK_LIBRARIES})

@matt.mccormick Hi again and sorry for inconveniences. I had already included ITKVtkGlue in the ITK package. That is why I asked the reason here. Could not find any reason!