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 <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>
#include <iostream>
#include <string>
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.12 FATAL_ERROR)
project(ScalarBarWidget)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ScalarBarWidget: 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(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}
)
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.