Skip to content

ScalarBarWidget

vtk-examples/Cxx/Widgets/ScalarBarWidget


Other languages

See (Python)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ScalarBarWidget.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSet.h>
#include <vtkDataSetMapper.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkScalarBarActor.h>
#include <vtkScalarBarWidget.h>
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridReader.h>

int main(int argc, char* argv[])
{
  vtkNew<vtkNamedColors> colors;

  // Parse command line arguments
  if (argc != 2)
  {
    std::cerr << "Usage: " << argv[0] << " Filename(.vtk) e.g.uGridEx.vtk"
              << std::endl;
    return EXIT_FAILURE;
  }

  // Create a custom lut. The lut is used for both the mapper and the
  // scalarBar
  vtkNew<vtkLookupTable> lut;
  lut->Build();

  // Read the source file.
  vtkNew<vtkUnstructuredGridReader> reader;
  reader->SetFileName(argv[1]);
  reader->Update(); // Needed because of GetScalarRange
  auto output = reader->GetOutput();
  auto scalarRange = output->GetScalarRange();

  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputData(output);
  mapper->SetScalarRange(scalarRange);
  mapper->SetLookupTable(lut);

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("MidnightBlue").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(300, 300);
  renderWindow->SetWindowName("ScalarBarWidget");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  // create the scalarBar
  vtkNew<vtkScalarBarActor> scalarBar;
  scalarBar->SetOrientationToHorizontal();
  scalarBar->SetLookupTable(lut);

  // create the scalarBarWidget
  vtkNew<vtkScalarBarWidget> scalarBarWidget;
  scalarBarWidget->SetInteractor(interactor);
  scalarBarWidget->SetScalarBarActor(scalarBar);
  scalarBarWidget->On();

  interactor->Initialize();
  renderWindow->Render();
  renderer->GetActiveCamera()->SetPosition(-6.4, 10.3, 1.4);
  renderer->GetActiveCamera()->SetFocalPoint(1.0, 0.5, 3.0);
  renderer->GetActiveCamera()->SetViewUp(0.6, 0.4, -0.7);
  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ScalarBarWidget)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkIOLegacy
  vtkInteractionStyle
  vtkInteractionWidgets
  vtkRenderingAnnotation
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ScalarBarWidget: ${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(ScalarBarWidget MACOSX_BUNDLE ScalarBarWidget.cxx )
  target_link_libraries(ScalarBarWidget PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ScalarBarWidget MACOSX_BUNDLE ScalarBarWidget.cxx )
  target_link_libraries(ScalarBarWidget PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ScalarBarWidget
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ScalarBarWidget

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

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

./ScalarBarWidget

WINDOWS USERS

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