ViewportBorders
Repository source: ViewportBorders
Description¶
Sometimes multiple vtkRenderer viewports can be difficult to differentiate.
This example draws a border around each viewport. The example creates a vtkPolyLine that outlines the viewport. The coordinates for the vtkPoints are specified in normalized viewport coordinates. The coordinate selection is made with a vtkCoordinate. The vtkPolyDataMapper2D and vtkActor2D render the border.
To run the example, provide 1-N vtkPolyDataReader files.
This example uses the data, src/Testing/Data/v.vtk,, src/Testing/Data/t.vtk and src/Testing/Data/k.vtk.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ViewportBorders.cxx
#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkCellArray.h>
#include <vtkCoordinate.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkPolyDataReader.h>
#include <vtkPolyLine.h>
#include <vtkProperty.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtk_cli11.h>
#include <iostream>
#include <string>
namespace fs = std::filesystem;
namespace {
/** Specify the rules for drawing the borders around a viewport.
*
* Here the borders for the viewports are defined in the order
* [top, left, bottom, right].
*
* Names for adjacent sides reflect anticlockwise ordering.
*/
static constexpr struct ViewportBorderSpecifier
{
std::array<bool, 4> t{true, false, false, false};
std::array<bool, 4> l{false, true, false, false};
std::array<bool, 4> b{false, false, true, false};
std::array<bool, 4> r{false, false, false, true};
std::array<bool, 4> lb{false, true, true, false};
std::array<bool, 4> lbr{false, true, true, true};
std::array<bool, 4> tlb{true, true, true, false};
std::array<bool, 4> tlbr{true, true, true, true};
std::array<bool, 4> rtl{true, true, false, true};
std::array<bool, 4> tl{true, true, false, false};
} viewportBorderSpecifier;
/** Draw a border around a viewport.
*
* @param renderer: The renderer corresponding to the viewport.
* @param sides: An array of boolean corresponding to [top, left, bottom, right]
* @param border_color: The color of the border.
* @param border_width: The width of the border.
* @return The border actor.
*/
vtkNew<vtkActor2D> DrawViewportBorder(std::array<bool, 4> const& sides,
std::string const& border_color,
unsigned int const& border_width);
} // namespace
int main(int argc, char* argv[])
{
CLI::App app{"Draws a border around each viewport."};
std::vector<std::string> filenames;
app.add_option("fileName", filenames, "List of files")
->check(CLI::ExistingFile);
CLI11_PARSE(app, argc, argv);
if (filenames.empty())
{
std::cout << "No filenames have been specified.\n"
"Try: src/Testing/Data/v.vtk src/Testing/Data/t.vtk"
" src/Testing/Data/k.vtk"
<< std::endl;
return EXIT_FAILURE;
}
size_t numberOfFiles = filenames.size();
vtkNew<vtkNamedColors> colors;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(300 * numberOfFiles, 300);
renderWindow->SetWindowName("ViewportBorders");
auto borderWidth = 4;
double size = 1.0 / numberOfFiles;
for (unsigned int i = 0; static_cast<int>(i) < numberOfFiles; ++i)
{
vtkNew<vtkPolyDataReader> reader;
reader->SetFileName(argv[i + 1]);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(reader->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("Silver").GetData());
vtkNew<vtkRenderer> renderer;
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
double viewport[4];
viewport[0] = size * i;
viewport[1] = 0.0;
viewport[2] = size * (i + 1);
viewport[3] = 1.0;
renderer->SetViewport(viewport);
if (i < numberOfFiles - 1)
{
auto borderActor =
DrawViewportBorder(viewportBorderSpecifier.tlb, "Gold", borderWidth);
renderer->AddViewProp(borderActor);
}
else
{
auto borderActor =
DrawViewportBorder(viewportBorderSpecifier.tlbr, "Gold", borderWidth);
renderer->AddViewProp(borderActor);
}
renderWindow->AddRenderer(renderer);
}
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Initialize();
interactor->Start();
return EXIT_SUCCESS;
}
namespace {
vtkNew<vtkActor2D> DrawViewportBorder(std::array<bool, 4> const& sides,
std::string const& border_color,
unsigned int const& border_width)
{
vtkNew<vtkNamedColors> colors;
// Points start at upper right and proceed anti-clockwise.
vtkNew<vtkPoints> points;
points->InsertPoint(0, 1, 1, 0);
points->InsertPoint(1, 0, 1, 0);
points->InsertPoint(2, 0, 0, 0);
points->InsertPoint(3, 1, 0, 0);
vtkNew<vtkCellArray> cells;
if (sides[0])
{
// Top
vtkNew<vtkPolyLine> top;
top->GetPointIds()->SetNumberOfIds(2);
top->GetPointIds()->SetId(0, 0);
top->GetPointIds()->SetId(1, 1);
cells->InsertNextCell(top);
}
if (sides[1])
{
// Left
vtkNew<vtkPolyLine> left;
left->GetPointIds()->SetNumberOfIds(2);
left->GetPointIds()->SetId(0, 1);
left->GetPointIds()->SetId(1, 2);
cells->InsertNextCell(left);
}
if (sides[2])
{
// Bottom
vtkNew<vtkPolyLine> bottom;
bottom->GetPointIds()->SetNumberOfIds(2);
bottom->GetPointIds()->SetId(0, 2);
bottom->GetPointIds()->SetId(1, 3);
cells->InsertNextCell(bottom);
}
if (sides[3])
{
// Right
vtkNew<vtkPolyLine> right;
right->GetPointIds()->SetNumberOfIds(2);
right->GetPointIds()->SetId(0, 3);
right->GetPointIds()->SetId(1, 0);
cells->InsertNextCell(right);
}
// Now make the polydata and display it.
vtkNew<vtkPolyData> poly;
poly->SetPoints(points);
poly->SetLines(cells);
// Use normalized viewport coordinates since
// they are independent of window size.
vtkNew<vtkCoordinate> coordinate;
coordinate->SetCoordinateSystemToNormalizedViewport();
vtkNew<vtkPolyDataMapper2D> mapper;
mapper->SetInputData(poly);
mapper->SetTransformCoordinate(coordinate);
vtkNew<vtkActor2D> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d(border_color).GetData());
// Line width should be at least 2 to be visible at extremes.
actor->GetProperty()->SetLineWidth(border_width);
return actor;
}
} // namespace```
###CMakeLists.txt
```cmake
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ViewportBorders)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
IOLegacy
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ViewportBorders: 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(ViewportBorders MACOSX_BUNDLE ViewportBorders.cxx )
target_link_libraries(ViewportBorders PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ViewportBorders
MODULES ${VTK_LIBRARIES}
)
Download and Build ViewportBorders¶
Click here to download ViewportBorders and its CMakeLists.txt file. Once the tarball ViewportBorders.tar has been downloaded and extracted,
cd ViewportBorders/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:
./ViewportBorders
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.
