Skip to content


# ExtractArrayComponent

VTKEx/Cxx/Utilities/ExtractArrayComponent

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

ExtractArrayComponent.cxx

    #include <vtkArrayCalculator.h>
    #include <vtkDoubleArray.h>
    #include <vtkPointData.h>
    #include <vtkPolyData.h>
    #include <vtkSmartPointer.h>

    int main(int, char *[])
    {
      vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
      points->InsertNextPoint(1.0, 0.0, 0.0);
      points->InsertNextPoint(2.0, 0.0, 0.0);
      points->InsertNextPoint(3.0, 0.0, 0.0);

      vtkSmartPointer<vtkDoubleArray> array =
          vtkSmartPointer<vtkDoubleArray>::New();
      array->SetName("InputArray");
      array->SetNumberOfComponents(3);
      array->InsertNextTuple3(1, 10, 100);
      array->InsertNextTuple3(2, 20, 200);
      array->InsertNextTuple3(3, 30, 300);

      vtkSmartPointer<vtkPolyData> polydata =
          vtkSmartPointer<vtkPolyData>::New();
      polydata->SetPoints(points);
      polydata->GetPointData()->AddArray(array);

      vtkSmartPointer<vtkArrayCalculator> arrayCalculator =
          vtkSmartPointer<vtkArrayCalculator>::New();
      arrayCalculator->SetInputData(polydata);
      arrayCalculator->AddVectorArrayName("InputArray");
      arrayCalculator->SetFunction("InputArray . jHat"); // Extract component '1' from the InputArray by taking the dot product of each tuple with the vector (0,1,0)
      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.3 FATAL_ERROR)

    project(ExtractArrayComponent)

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

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