VTK/Examples/Cxx/StructuredPoints/StructuredPointsToUnstructuredGrid

From KitwarePublic

Jump to: navigation, search

StructuredPointsToUnstructuredGrid.cxx

#include <vtkSmartPointer.h>
#include <vtkStructuredPoints.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkUnstructuredGrid.h>
 
int main(int argc, char *argv[])
{
  // Create a structuredpoints
  vtkSmartPointer<vtkStructuredPoints> structuredPoints = 
    vtkSmartPointer<vtkStructuredPoints>::New();
 
  // Specify the size of the image data
  structuredPoints->SetDimensions(2,3,1);
  structuredPoints->SetNumberOfScalarComponents(1);
  structuredPoints->SetScalarTypeToDouble();
 
  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
  unsigned int counter = 0;
  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->SetInput(ug);
  writer->Write();
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
PROJECT(StructuredPointsToUnstructuredGrid)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(StructuredPointsToUnstructuredGrid StructuredPointsToUnstructuredGrid.cxx)
TARGET_LINK_LIBRARIES(StructuredPointsToUnstructuredGrid vtkHybrid)
Personal tools