ExtractArrayComponent
Repository source: ExtractArrayComponent
Other languages
See (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ExtractArrayComponent.cxx
#include <vtkArrayCalculator.h>
#include <vtkDoubleArray.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <iostream>
#include <string>
int main(int, char*[])
{
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(2.0, 0.0, 0.0);
  points->InsertNextPoint(3.0, 0.0, 0.0);
  vtkNew<vtkDoubleArray> array;
  array->SetName("InputArray");
  array->SetNumberOfComponents(3);
  array->InsertNextTuple3(1, 10, 100);
  array->InsertNextTuple3(2, 20, 200);
  array->InsertNextTuple3(3, 30, 300);
  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);
  polydata->GetPointData()->AddArray(array);
  vtkNew<vtkArrayCalculator> arrayCalculator;
  arrayCalculator->SetInputData(polydata);
  arrayCalculator->AddVectorArrayName("InputArray");
  // Extract component '1' from the InputArray by
  // taking the dot product of each tuple with the
  // vector (0,1,0)
  arrayCalculator->SetFunction("dot(InputArray,jHat)");
  arrayCalculator->SetResultArrayName("OutputArray");
  arrayCalculator->Update();
  vtkSmartPointer<vtkDoubleArray> outputArray = dynamic_cast<vtkDoubleArray*>(
      arrayCalculator->GetPolyDataOutput()->GetPointData()->GetArray(
          "OutputArray"));
  for (vtkIdType i = 0; i < outputArray->GetNumberOfTuples(); i++)
  {
    double val = outputArray->GetValue(i);
    std::cout << "output value " << i << ": " << val << std::endl;
  }
  return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ExtractArrayComponent)
find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
  FiltersCore
)
if (NOT VTK_FOUND)
  message(FATAL_ERROR "ExtractArrayComponent: 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(ExtractArrayComponent MACOSX_BUNDLE ExtractArrayComponent.cxx )
  target_link_libraries(ExtractArrayComponent PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ExtractArrayComponent
  MODULES ${VTK_LIBRARIES}
)
Download and Build ExtractArrayComponent¶
Click here to download ExtractArrayComponent and its CMakeLists.txt file. Once the tarball ExtractArrayComponent.tar has been downloaded and extracted,
cd ExtractArrayComponent/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:
./ExtractArrayComponent
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.