/*========================================================================= Program: Visualization Toolkit Module: TestBalloonWidget.cxx Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ // // This example tests the vtkHoverWidget and vtkBalloonWidget. // First include the required header files for the VTK classes we are using. #include "vtkActor.h" #include "vtkBalloonRepresentation.h" #include "vtkBalloonWidget.h" #include "vtkCommand.h" #include "vtkConeSource.h" #include "vtkCylinderSource.h" #include "vtkInteractorEventRecorder.h" #include "vtkInteractorStyleTrackballCamera.h" #include "vtkPolyDataMapper.h" #include "vtkPropPicker.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkRenderer.h" #include "vtkSmartPointer.h" #include "vtkSphereSource.h" #include "vtkTIFFReader.h" #include "vtkTestUtilities.h" class vtkBalloonCallback : public vtkCommand { public: static vtkBalloonCallback* New() { return new vtkBalloonCallback; } void Execute(vtkObject* caller, unsigned long, void*) override { vtkBalloonWidget* balloonWidget = reinterpret_cast(caller); if (balloonWidget->GetCurrentProp() != nullptr) { std::cout << "Prop selected\n"; } } vtkActor* PickedActor; }; class vtkBalloonPickCallback : public vtkCommand { public: static vtkBalloonPickCallback* New() { return new vtkBalloonPickCallback; } void Execute(vtkObject* caller, unsigned long, void*) override { vtkPropPicker* picker = reinterpret_cast(caller); vtkProp* prop = picker->GetViewProp(); if (prop != nullptr) { this->BalloonWidget->UpdateBalloonString(prop, "Picked"); } } vtkBalloonWidget* BalloonWidget; }; int TestBalloonWidget(int argc, char* argv[]) { // Create the RenderWindow, Renderer and both Actors // vtkSmartPointer ren1 = vtkSmartPointer::New(); vtkSmartPointer renWin = vtkSmartPointer::New(); renWin->AddRenderer(ren1); vtkSmartPointer iren = vtkSmartPointer::New(); iren->SetRenderWindow(renWin); vtkSmartPointer picker = vtkSmartPointer::New(); vtkSmartPointer pcbk = vtkSmartPointer::New(); picker->AddObserver(vtkCommand::PickEvent, pcbk); iren->SetPicker(picker); // Create an image for the balloon widget char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/beach.tif"); vtkSmartPointer image1 = vtkSmartPointer::New(); image1->SetFileName(fname); image1->SetOrientationType(4); // Create a test pipeline // vtkSmartPointer ss = vtkSmartPointer::New(); vtkSmartPointer mapper = vtkSmartPointer::New(); mapper->SetInputConnection(ss->GetOutputPort()); vtkSmartPointer sph = vtkSmartPointer::New(); sph->SetMapper(mapper); vtkSmartPointer cs = vtkSmartPointer::New(); vtkSmartPointer csMapper = vtkSmartPointer::New(); csMapper->SetInputConnection(cs->GetOutputPort()); vtkSmartPointer cyl = vtkSmartPointer::New(); cyl->SetMapper(csMapper); cyl->AddPosition(5, 0, 0); vtkSmartPointer coneSource = vtkSmartPointer::New(); vtkSmartPointer coneMapper = vtkSmartPointer::New(); coneMapper->SetInputConnection(coneSource->GetOutputPort()); vtkSmartPointer cone = vtkSmartPointer::New(); cone->SetMapper(coneMapper); cone->AddPosition(0, 5, 0); // Create the widget vtkSmartPointer rep = vtkSmartPointer::New(); rep->SetBalloonLayoutToImageRight(); vtkSmartPointer widget = vtkSmartPointer::New(); widget->SetInteractor(iren); widget->SetRepresentation(rep); widget->AddBalloon(sph, "This is a sphere", nullptr); widget->AddBalloon(cyl, "This is a\ncylinder", image1->GetOutput()); widget->AddBalloon(cone, "This is a\ncone,\na really big cone,\nyou wouldn't believe how big", image1->GetOutput()); pcbk->BalloonWidget = widget; vtkSmartPointer cbk = vtkSmartPointer::New(); widget->AddObserver(vtkCommand::WidgetActivateEvent, cbk); // Add the actors to the renderer, set the background and size // ren1->AddActor(sph); ren1->AddActor(cyl); ren1->AddActor(cone); ren1->SetBackground(0.1, 0.2, 0.4); renWin->SetSize(300, 300); // record events vtkSmartPointer recorder = vtkSmartPointer::New(); recorder->SetInteractor(iren); recorder->SetFileName("c:/record.log"); // recorder->Record(); // recorder->ReadFromInputStringOn(); // recorder->SetInputString(eventLog); // render the image // iren->Initialize(); renWin->Render(); widget->On(); // recorder->Play(); // Remove the observers so we can go interactive. Without this the "-I" // testing option fails. recorder->Off(); iren->Start(); delete[] fname; return EXIT_SUCCESS; }