Skip to content


# VectorField

VTKEx/Cxx/Visualization/VectorField

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

VectorField.cxx

    #include <vtkArrowSource.h>
    #include <vtkCellArray.h>
    #include <vtkGlyph2D.h>
    #include <vtkImageData.h>
    #include <vtkImageSlice.h>
    #include <vtkImageSliceMapper.h>
    #include <vtkInteractorStyleImage.h>
    #include <vtkPointData.h>
    #include <vtkPoints.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSmartPointer.h>
    #include <vtkXMLPolyDataWriter.h>

    int main(int, char*[])
    {
      // Create an image
      auto image =
        vtkSmartPointer<vtkImageData>::New();

      // Specify the size of the image data
      image->SetDimensions(50, 50, 1);
      image->AllocateScalars(VTK_FLOAT, 3);

      int* dims = image->GetDimensions();

      // Zero the image
      for (auto z = 0; z < dims[2]; ++z)
      {
        for (auto y = 0; y < dims[1]; ++y)
        {
          for (auto x = 0; x < dims[0]; ++x)
          {
            auto pixel = static_cast<float*>(image->GetScalarPointer(x, y, z));
            pixel[0] = 0.0;
            pixel[1] = 0.0;
            pixel[2] = 0.0;
          }
        }
      }

      {
        auto pixel = static_cast<float*>(image->GetScalarPointer(20, 20, 0));
        pixel[0] = -10.0;
        pixel[1] = 5.0;
      }

      {
        auto pixel = static_cast<float*>(image->GetScalarPointer(30, 30, 0));
        pixel[0] = 10.0;
        pixel[1] = 10.0;
      }

      // A better way to do this is (should be tested for compatibility and
      // correctness).
      // std::cout << image->GetPointData()->GetScalars()->GetName() << std::endl;
      image->GetPointData()->SetActiveVectors(
          image->GetPointData()->GetScalars()->GetName());
      // image->GetPointData()->SetActiveVectors("ImageScalars");

      // Setup the arrows
      auto arrowSource =
        vtkSmartPointer<vtkArrowSource>::New();
      arrowSource->Update();

      auto glyphFilter =
        vtkSmartPointer<vtkGlyph2D>::New();
      glyphFilter->SetSourceConnection(arrowSource->GetOutputPort());
      glyphFilter->OrientOn();
      glyphFilter->SetVectorModeToUseVector();
      glyphFilter->SetInputData(image);
      glyphFilter->Update();

      // Create actors
      auto imageMapper =
        vtkSmartPointer<vtkImageSliceMapper>::New();
      imageMapper->SetInputData(image);

      auto imageSlice =
        vtkSmartPointer<vtkImageSlice>::New();
      imageSlice->SetMapper(imageMapper);

      auto vectorMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      vectorMapper->SetInputConnection(glyphFilter->GetOutputPort());
      auto vectorActor =
        vtkSmartPointer<vtkActor>::New();
      vectorActor->SetMapper(vectorMapper);

      // Setup renderer
      auto renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->AddViewProp(imageSlice);
      renderer->AddViewProp(vectorActor);
      renderer->ResetCamera();

      // Setup render window
      auto renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);

      // Setup render window interactor
      auto renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      // auto style =
      //  vtkSmartPointer<vtkInteractorStyleImage>::New();
      // renderWindowInteractor->SetInteractorStyle(style);

      // Render and start interaction
      renderWindowInteractor->SetRenderWindow(renderWindow);
      renderWindow->Render();
      renderWindowInteractor->Initialize();

      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(VectorField)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonDataModel
      vtkFiltersCore
      vtkFiltersSources
      vtkIOXML
      vtkInteractionStyle
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping VectorField: ${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(VectorField MACOSX_BUNDLE VectorField.cxx )
      target_link_libraries(VectorField PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(VectorField MACOSX_BUNDLE VectorField.cxx )
      target_link_libraries(VectorField PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS VectorField
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build VectorField

Click here to download VectorField and its CMakeLists.txt file. Once the tarball VectorField.tar has been downloaded and extracted,

    cd VectorField/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:

    ./VectorField

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.