Skip to content

LineWidget2

vtk-examples/Cxx/Widgets/LineWidget2


Question

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

Code

LineWidget2.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCommand.h>
#include <vtkLineRepresentation.h>
#include <vtkLineWidget2.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

namespace {
// This does the actual work.
// Callback for the interaction
class vtkLineCallback : public vtkCommand
{
public:
  static vtkLineCallback* New()
  {
    return new vtkLineCallback;
  }

  virtual void Execute(vtkObject* caller, unsigned long, void*)
  {

    vtkLineWidget2* lineWidget = reinterpret_cast<vtkLineWidget2*>(caller);

    // Get the actual box coordinates of the line
    vtkNew<vtkPolyData> polydata;
    static_cast<vtkLineRepresentation*>(lineWidget->GetRepresentation())
        ->GetPolyData(polydata);

    // Display one of the points, just so we know it's working
    double p[3];
    polydata->GetPoint(0, p);
    std::cout << "P: " << p[0] << " " << p[1] << " " << p[2] << std::endl;
  }
  vtkLineCallback()
  {
  }
};
} // namespace

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

  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->Update();

  // Create a mapper and actor
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

  // A renderer and render window
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("LineWidget2");

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

  // An interactor
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  vtkNew<vtkLineWidget2> lineWidget;
  lineWidget->SetInteractor(renderWindowInteractor);
  lineWidget->CreateDefaultRepresentation();

  // You could do this if you want to set properties at this point:
  // vtkNew<vtkLineRepresentation> lineRepresentation;
  // lineWidget->SetRepresentation(lineRepresentation);

  vtkNew<vtkLineCallback> lineCallback;

  lineWidget->AddObserver(vtkCommand::InteractionEvent, lineCallback);

  // Render
  renderWindow->Render();

  renderWindowInteractor->Initialize();
  renderWindow->Render();
  lineWidget->On();

  // Begin mouse interaction
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(LineWidget2)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersSources
  InteractionStyle
  InteractionWidgets
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "LineWidget2: 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(LineWidget2 MACOSX_BUNDLE LineWidget2.cxx )
  target_link_libraries(LineWidget2 PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS LineWidget2
  MODULES ${VTK_LIBRARIES}
)

Download and Build LineWidget2

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

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

./LineWidget2

WINDOWS USERS

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