ImageDataToPointSet

VTKExamples/Cxx/ImageData/ImageDataToPointSet

Code

ImageDataToPointSet.cxx

#include <vtkImageData.h>
#include <vtkPoints.h>
#include <vtkImageDataToPointSet.h>
#include <vtkSmartPointer.h>
#include <vtkXMLStructuredGridWriter.h>
#include <vtkStructuredGrid.h>

static void CreateImage(vtkImageData* image);

int main(int, char *[])
{
  vtkSmartPointer<vtkImageData> image =
    vtkSmartPointer<vtkImageData>::New();
  CreateImage(image);

  vtkSmartPointer<vtkImageDataToPointSet> imageDataToPointSet =
    vtkSmartPointer<vtkImageDataToPointSet>::New();
  imageDataToPointSet->SetInputData(image);
  imageDataToPointSet->Update();

  vtkSmartPointer<vtkXMLStructuredGridWriter> writer =
    vtkSmartPointer<vtkXMLStructuredGridWriter>::New();
  writer->SetInputConnection(imageDataToPointSet->GetOutputPort());
  writer->SetFileName("Test.vts");
  writer->Write();

  return EXIT_SUCCESS;
}

void CreateImage(vtkImageData* image)
{
  // Create an image
  image->SetDimensions(3,3,1);
  image->AllocateScalars(VTK_UNSIGNED_CHAR,1);

  int dims[3];
  image->GetDimensions(dims);
  for(int i = 0; i < dims[0]; i++)
  {
    for(int j = 0; j < dims[1]; j++)
    {
      unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(i,j,0));
      pixel[0] = 1;
    }
  }
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ImageDataToPointSet)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersGeneral
  vtkIOXML QUIET)
if (NOT VTK_FOUND)
  message("Skipping ImageDataToPointSet: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(ImageDataToPointSet MACOSX_BUNDLE ImageDataToPointSet.cxx )
  target_link_libraries(ImageDataToPointSet PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ImageDataToPointSet MACOSX_BUNDLE ImageDataToPointSet.cxx )
  target_link_libraries(ImageDataToPointSet PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ImageDataToPointSet
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build ImageDataToPointSet

Click here to download ImageDataToPointSet and its CMakeLists.txt file. Once the tarball ImageDataToPointSet.tar has been downloaded and extracted,

cd ImageDataToPointSet/build 

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./ImageDataToPointSet

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.