Skip to content

StructuredPointsToUnstructuredGrid

Repository source: StructuredPointsToUnstructuredGrid

Other languages

See (Java)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

StructuredPointsToUnstructuredGrid.cxx

#include <vtkNew.h>
#include <vtkStructuredPoints.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridWriter.h>

int main(int, char*[])
{
  // Create a structuredpoints
  vtkNew<vtkStructuredPoints> structuredPoints;

  // Specify the size of the image data
  structuredPoints->SetDimensions(2, 3, 1);
  structuredPoints->AllocateScalars(VTK_DOUBLE, 1);

  const int* dims = structuredPoints->GetDimensions();

  std::cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1]
            << " z: " << dims[2] << std::endl;

  std::cout << "Number of points: " << structuredPoints->GetNumberOfPoints()
            << std::endl;
  std::cout << "Number of cells: " << structuredPoints->GetNumberOfCells()
            << std::endl;

  // fill every entry of the image data with "2.0"
  for (int z = 0; z < dims[2]; z++)
  {
    for (int y = 0; y < dims[1]; y++)
    {
      for (int x = 0; x < dims[0]; x++)
      {
        double* pixel =
            static_cast<double*>(structuredPoints->GetScalarPointer(x, y, z));
        pixel[0] = 2.0;
      }
    }
  }

  // Copy the points from the StructuredPoints to the UnstructuredGrid
  vtkNew<vtkPoints> points;

  // retrieve the entries from the grid and print them to the screen
  for (vtkIdType i = 0; i < structuredPoints->GetNumberOfPoints(); i++)
  {
    double p[3];
    structuredPoints->GetPoint(i, p);

    points->InsertNextPoint(p);
  }

  vtkNew<vtkUnstructuredGrid> ug;
  ug->SetPoints(points);

  vtkNew<vtkXMLUnstructuredGridWriter> writer;
  writer->SetFileName("output.vtu");
  writer->SetInputData(ug);
  writer->Write();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(StructuredPointsToUnstructuredGrid)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  IOXML
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "StructuredPointsToUnstructuredGrid: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(StructuredPointsToUnstructuredGrid MACOSX_BUNDLE StructuredPointsToUnstructuredGrid.cxx )
  target_link_libraries(StructuredPointsToUnstructuredGrid PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS StructuredPointsToUnstructuredGrid
  MODULES ${VTK_LIBRARIES}
)

Download and Build StructuredPointsToUnstructuredGrid

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

cd StructuredPointsToUnstructuredGrid/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:

./StructuredPointsToUnstructuredGrid

WINDOWS USERS

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