VTK/Examples/Cxx/PolyData/CopyAllArrays

From KitwarePublic
Jump to navigationJump to search

CopyAllArrays.cxx

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>
#include <vtkIntArray.h>

int main(int argc, char *argv[])
{
  vtkSmartPointer<vtkSphereSource> sphereSource = 
      vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();
  
  vtkPolyData* polydata = sphereSource->GetOutput();

  vtkSmartPointer<vtkDoubleArray> doubles = 
      vtkSmartPointer<vtkDoubleArray>::New();
  doubles->SetName("Doubles");
  doubles->SetNumberOfTuples(polydata->GetNumberOfPoints());
  
  polydata->GetPointData()->AddArray(doubles);
  
  vtkSmartPointer<vtkIntArray> ints = 
      vtkSmartPointer<vtkIntArray>::New();
  ints->SetName("Ints");
  ints->SetNumberOfTuples(polydata->GetNumberOfPoints());
  
  polydata->GetPointData()->AddArray(ints);
  
  unsigned int numberOfArrays = polydata->GetPointData()->GetNumberOfArrays();
  cout << "There are " << numberOfArrays << " arrays." << endl;
  
  vtkSmartPointer<vtkPolyData> newPolyData = 
      vtkSmartPointer<vtkPolyData>::New();
  
  for(unsigned int i = 0; i < numberOfArrays; i++)
    {
    cout << "array " << i << ":" << endl;
    cout << "name: " << polydata->GetPointData()->GetArrayName(i) << endl;
    cout << "type: " << polydata->GetPointData()->GetArray(i)->GetDataType() << endl;
    cout << endl;
    
    newPolyData->GetPointData()->AddArray(polydata->GetPointData()->GetArray(i));
    }
    
  cout << "new polydata: " << endl;
  
  for(unsigned int i = 0; i < numberOfArrays; i++)
    {
    cout << "array " << i << ":" << endl;
    cout << "name: " << newPolyData->GetPointData()->GetArrayName(i) << endl;
    cout << "type: " << newPolyData->GetPointData()->GetArray(i)->GetDataType() << endl;
    cout << endl;
    }

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT(CopyAllArrays)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(CopyAllArrays CopyAllArrays.cxx)
TARGET_LINK_LIBRARIES(CopyAllArrays vtkHybrid)