VTK/Examples/Broken/RectangularButtonSource

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

RectangularButtonSource.cxx

#include <vtkSmartPointer.h>
#include <vtkWeakPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkRectangularButtonSource.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkObjectFactory.h>
#include <vtkProperty.h>


class MouseInteractorStyle : public vtkInteractorStyleTrackballActor
{
  public:
    static MouseInteractorStyle* New();
    vtkTypeRevisionMacro(MouseInteractorStyle,vtkInteractorStyleTrackballActor);
   
    virtual void OnLeftButtonDown() 
    {
      cout << "Pressed left mouse button." << endl;
      
      if(this->InteractionProp)
        {
        vtkActor::SafeDownCast(this->InteractionProp)->GetProperty()->SetColor(1,0,0);
        if (this->RenderWindow)
           this->RenderWindow->Render();
        }
      
      // forward events
      vtkInteractorStyleTrackballActor::OnLeftButtonDown();
    }
    
    void SetRenderWindow(vtkRenderWindow* renderWindow) {this->RenderWindow = renderWindow;}

  private:
    vtkWeakPointer<vtkRenderWindow> RenderWindow;
};
 
vtkCxxRevisionMacro(MouseInteractorStyle, "$Revision: 1.1 $");
vtkStandardNewMacro(MouseInteractorStyle);


int main (int argc, char* argv[])
{

  vtkSmartPointer<vtkRectangularButtonSource> button = 
      vtkSmartPointer<vtkRectangularButtonSource>::New();
  
  //create a mapper
  vtkSmartPointer<vtkPolyDataMapper> mapper = 
      vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInput(button->GetOutput());

	// create an actor
  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<MouseInteractorStyle> style = 
      vtkSmartPointer<MouseInteractorStyle>::New();
  renderWindowInteractor->SetInteractorStyle( style );
  style->SetRenderWindow(renderWindow);
  
  // add the actors to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(1,1,1); // Background color white

  //setup the text and add it to the window
  vtkSmartPointer<vtkTextActor> textActor = 
      vtkSmartPointer<vtkTextActor>::New();
  textActor->GetTextProperty()->SetFontSize ( 24 );
  textActor->SetPosition2 ( 10, 40 );
  renderer->AddActor2D ( textActor );
  textActor->SetInput ( "Hello world" );
  textActor->GetTextProperty()->SetColor ( 1.0,0.0,0.0 );

  // render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(RectangularButtonSource)

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

add_executable(RectangularButtonSource MACOSX_BUNDLE RectangularButtonSource.cxx)

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

Download and Build RectangularButtonSource

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

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

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

./RectangularButtonSource

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.