CopyAllArrays
Repository source: CopyAllArrays
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CopyAllArrays.cxx
#include <vtkDoubleArray.h>
#include <vtkFloatArray.h>
#include <vtkIntArray.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
namespace {
void ManualMethod(vtkPolyData* input);
void AutomaticMethod(vtkPolyData* input);
} // namespace
int main(int, char*[])
{
  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->Update();
  vtkPolyData* polydata = sphereSource->GetOutput();
  vtkNew<vtkDoubleArray> doubles;
  doubles->SetName("Doubles");
  doubles->SetNumberOfTuples(polydata->GetNumberOfPoints());
  polydata->GetPointData()->AddArray(doubles);
  vtkNew<vtkIntArray> ints;
  ints->SetName("Ints");
  ints->SetNumberOfTuples(polydata->GetNumberOfPoints());
  polydata->GetPointData()->AddArray(ints);
  ManualMethod(polydata);
  AutomaticMethod(polydata);
  return EXIT_SUCCESS;
}
namespace {
void ManualMethod(vtkPolyData* input)
{
  unsigned int numberOfArrays = input->GetPointData()->GetNumberOfArrays();
  std::cout << "There are " << numberOfArrays << " arrays." << std::endl;
  vtkNew<vtkPolyData> newPolyData;
  for (unsigned int i = 0; i < numberOfArrays; i++)
  {
    std::cout << "array " << i << ":" << std::endl;
    std::cout << "name: " << input->GetPointData()->GetArrayName(i)
              << std::endl;
    std::cout << "type: " << input->GetPointData()->GetArray(i)->GetDataType()
              << std::endl;
    std::cout << std::endl;
    newPolyData->GetPointData()->AddArray(input->GetPointData()->GetArray(i));
  }
  std::cout << "new polydata: " << std::endl;
  for (unsigned int i = 0; i < numberOfArrays; i++)
  {
    std::cout << "array " << i << ":" << std::endl;
    std::cout << "name: " << newPolyData->GetPointData()->GetArrayName(i)
              << std::endl;
    std::cout << "type: "
              << newPolyData->GetPointData()->GetArray(i)->GetDataType()
              << std::endl;
    std::cout << std::endl;
  }
}
void AutomaticMethod(vtkPolyData* input)
{
  unsigned int numberOfArrays = input->GetPointData()->GetNumberOfArrays();
  std::cout << "There are " << numberOfArrays << " arrays." << std::endl;
  vtkNew<vtkPolyData> newPolyData;
  newPolyData->GetPointData()->PassData(input->GetPointData());
  for (unsigned int i = 0; i < numberOfArrays; i++)
  {
    std::cout << "array " << i << ":" << std::endl;
    std::cout << "name: " << input->GetPointData()->GetArrayName(i)
              << std::endl;
    std::cout << "type: " << input->GetPointData()->GetArray(i)->GetDataType()
              << std::endl;
    std::cout << std::endl;
  }
  std::cout << "new polydata: " << std::endl;
  for (unsigned int i = 0; i < numberOfArrays; i++)
  {
    std::cout << "array " << i << ":" << std::endl;
    std::cout << "name: " << newPolyData->GetPointData()->GetArrayName(i)
              << std::endl;
    std::cout << "type: "
              << newPolyData->GetPointData()->GetArray(i)->GetDataType()
              << std::endl;
    std::cout << std::endl;
  }
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(CopyAllArrays)
find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  FiltersSources
)
if (NOT VTK_FOUND)
  message(FATAL_ERROR "CopyAllArrays: 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(CopyAllArrays MACOSX_BUNDLE CopyAllArrays.cxx )
  target_link_libraries(CopyAllArrays PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS CopyAllArrays
  MODULES ${VTK_LIBRARIES}
)
Download and Build CopyAllArrays¶
Click here to download CopyAllArrays and its CMakeLists.txt file. Once the tarball CopyAllArrays.tar has been downloaded and extracted,
cd CopyAllArrays/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:
./CopyAllArrays
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.