VTK/Examples/Broken/Cxx/Widgets/BorderWorldCoords

From KitwarePublic
< VTK‎ | Examples
Jump to navigationJump to search

If you set the Coordinates to World, the widget no longer follows the mouse.

BorderWorldCoords.cxx

#include <vtkSmartPointer.h>
#include <vtkWidgetCallbackMapper.h>
#include <vtkCommand.h>
#include <vtkWidgetEvent.h>
#include <vtkObjectFactory.h>
#include <vtkActor.h>
#include <vtkBorderRepresentation.h>
#include <vtkBorderWidget.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>

#include "RegionSelectionWidget.h"

int main(int, char *[])
{
  // Sphere
  vtkSmartPointer<vtkSphereSource> sphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->SetRadius(4.0);
  sphereSource->Update();

  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(sphereSource->GetOutputPort());

  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  // A renderer and render window
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  // An interactor
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  vtkSmartPointer<RegionSelectionWidget> regionSelectionWidget =
    vtkSmartPointer<RegionSelectionWidget>::New();
  regionSelectionWidget->SetInteractor(renderWindowInteractor);
  regionSelectionWidget->CreateDefaultRepresentation();
  regionSelectionWidget->SelectableOff();
  regionSelectionWidget->Renderer = renderer;

  // Add the actors to the scene
  renderer->AddActor(actor);
  renderer->Render();

  renderWindowInteractor->Initialize();

  // Ideally we would do this:
   vtkBorderRepresentation* representation = static_cast<vtkBorderRepresentation*>(regionSelectionWidget->GetRepresentation());
   representation->GetPositionCoordinate()->SetCoordinateSystemToWorld();
   representation->GetPosition2Coordinate()->SetCoordinateSystemToWorld();
   representation->SetPosition(0, 0);
   representation->SetPosition2(5, 5);
  
  regionSelectionWidget->On();

  renderer->ResetCamera();
  renderWindow->Render();
  
  // Begin mouse interaction
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

RegionSelectionWidget.h

#ifndef REGIONSELECTIONWIDGET_H
#define REGIONSELECTIONWIDGET_H

#include <vtkBorderWidget.h>

class RegionSelectionWidget : public vtkBorderWidget
{
public:
  static RegionSelectionWidget *New();
  vtkTypeMacro(RegionSelectionWidget, vtkBorderWidget);

  int SubclassEndSelectAction();
  
  RegionSelectionWidget();
  vtkRenderer* Renderer;
};

#endif

RegionSelectionWidget.cpp

#include "RegionSelectionWidget.h"

#include <vtkBorderRepresentation.h>
#include <vtkCommand.h>
#include <vtkObjectFactory.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkWidgetCallbackMapper.h>
#include <vtkWidgetEvent.h>
#include <vtkViewport.h>
#include <vtkRenderer.h>

vtkStandardNewMacro(RegionSelectionWidget);

RegionSelectionWidget::RegionSelectionWidget()
{
  this->CallbackMapper->SetCallbackMethod(vtkCommand::MiddleButtonReleaseEvent,
                                        vtkWidgetEvent::EndSelect,
                                        this, RegionSelectionWidget::EndSelectAction);
}

int RegionSelectionWidget::SubclassEndSelectAction()
{
  
  
  // Ideal method - coordinates must have already been set to World
//   vtkCoordinate* lowerLeft = static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())->GetPositionCoordinate();
//   double lowerLeftCoord[3];
//   lowerLeft->GetValue(lowerLeftCoord);
//   std::cout << "Lower left: " << lowerLeftCoord[0] << " " << lowerLeftCoord[1] << " " << lowerLeftCoord[2] << std::endl;
//   
//   vtkCoordinate* upperRight = static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())->GetPosition2Coordinate();
//   double upperRightCoord[3];
//   upperRight->GetValue(upperRightCoord);
//   std::cout << "Upper right: " << upperRightCoord[0] << " " << upperRightCoord[1] << " " << upperRightCoord[2] << std::endl;
  
  
  // Get the bottom left corner
//   double* lowerLeft;
//   lowerLeft = static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())->GetPosition();
//   std::cout << "Lower left: " << lowerLeft[0] << " " << lowerLeft[1] << std::endl;
// 
//   double* upperRight;
//   upperRight = static_cast<vtkBorderRepresentation*>(borderWidget->GetRepresentation())->GetPosition2();
//   std::cout << "Upper right: " << upperRight[0] << " " << upperRight[1] << std::endl;

  double* lowerLeft = static_cast<vtkBorderRepresentation*>(this->GetRepresentation())->GetPositionCoordinate()->GetComputedWorldValue (this->Renderer);
  std::cout << "Lower left: " << lowerLeft[0] << " " << lowerLeft[1] << std::endl;

  double* upperRight = static_cast<vtkBorderRepresentation*>(this->GetRepresentation())->GetPosition2Coordinate()->GetComputedWorldValue (this->Renderer);
  std::cout << "Upper right: " << upperRight[0] << " " << upperRight[1] << std::endl;

  return vtkBorderWidget::SubclassSelectAction(); // works
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(BorderWorldCoords)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(BorderWorldCoords MACOSX_BUNDLE BorderWorldCoords.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(BorderWorldCoords ${VTK_LIBRARIES})
else()
  target_link_libraries(BorderWorldCoords vtkHybrid vtkWidgets)
endif()

Download and Build BorderWorldCoords

Click here to download BorderWorldCoords. and its CMakeLists.txt file.

Once the tarball BorderWorldCoords.tar has been downloaded and extracted,

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

./BorderWorldCoords

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