Skip to content


# StructuredPointsToUnstructuredGrid

VTKEx/Cxx/StructuredPoints/StructuredPointsToUnstructuredGrid

Other languages

See (Java)

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

StructuredPointsToUnstructuredGrid.cxx

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

    int main(int, char *[])
    {
      // Create a structuredpoints
      vtkSmartPointer<vtkStructuredPoints> structuredPoints =
        vtkSmartPointer<vtkStructuredPoints>::New();

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

      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
      vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();

      //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);
      }

      vtkSmartPointer<vtkUnstructuredGrid> ug =
        vtkSmartPointer<vtkUnstructuredGrid>::New();
      ug->SetPoints(points);

      vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
        vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
      writer->SetFileName("output.vtu");
      writer->SetInputData(ug);
      writer->Write();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(StructuredPointsToUnstructuredGrid)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonDataModel
      vtkIOXML QUIET)
    if (NOT VTK_FOUND)
      message("Skipping StructuredPointsToUnstructuredGrid: ${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(StructuredPointsToUnstructuredGrid MACOSX_BUNDLE StructuredPointsToUnstructuredGrid.cxx )
      target_link_libraries(StructuredPointsToUnstructuredGrid PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      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}
        )
    endif ()

### 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.