/*========================================================================= Program: Visualization Toolkit Module: TestAffineWidget.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 vtkAffineWidget. // First include the required header files for the VTK classes we are using. #include "vtkSmartPointer.h" #include "vtkAffineRepresentation2D.h" #include "vtkAffineWidget.h" #include "vtkCommand.h" #include "vtkImageActor.h" #include "vtkImageData.h" #include "vtkImageMapper3D.h" #include "vtkImageShiftScale.h" #include "vtkInteractorEventRecorder.h" #include "vtkInteractorStyleImage.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkRenderer.h" #include "vtkTestUtilities.h" #include "vtkTransform.h" #include "vtkVolume16Reader.h" // This callback is responsible for adjusting the point position. // It looks in the region around the point and finds the maximum or // minimum value. class vtkAffineCallback : public vtkCommand { public: static vtkAffineCallback* New() { return new vtkAffineCallback; } void Execute(vtkObject* caller, unsigned long, void*) override; vtkAffineCallback() : ImageActor(nullptr) , AffineRep(nullptr) { this->Transform = vtkTransform::New(); } ~vtkAffineCallback() override { this->Transform->Delete(); } vtkSmartPointer ImageActor; vtkAffineRepresentation2D* AffineRep; vtkTransform* Transform; }; // Method re-positions the points using random perturbation void vtkAffineCallback::Execute(vtkObject*, unsigned long, void*) { this->AffineRep->GetTransform(this->Transform); this->ImageActor->SetUserTransform(this->Transform); } int TestAffineWidget(int argc, char* argv[]) { // Create the pipeline char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/headsq/quarter"); vtkSmartPointer v16 = vtkSmartPointer::New(); v16->SetDataDimensions(64, 64); v16->SetDataByteOrderToLittleEndian(); v16->SetImageRange(1, 93); v16->SetDataSpacing(3.2, 3.2, 1.5); v16->SetFilePrefix(fname); v16->ReleaseDataFlagOn(); v16->SetDataMask(0x7fff); v16->Update(); delete[] fname; double range[2]; v16->GetOutput()->GetScalarRange(range); vtkSmartPointer shifter = vtkSmartPointer::New(); shifter->SetShift(-1.0 * range[0]); shifter->SetScale(255.0 / (range[1] - range[0])); shifter->SetOutputScalarTypeToUnsignedChar(); shifter->SetInputConnection(v16->GetOutputPort()); shifter->ReleaseDataFlagOff(); shifter->Update(); vtkSmartPointer imageActor = vtkSmartPointer::New(); imageActor->GetMapper()->SetInputConnection(shifter->GetOutputPort()); imageActor->VisibilityOn(); imageActor->SetDisplayExtent(0, 63, 0, 63, 46, 46); imageActor->InterpolateOn(); double bounds[6]; imageActor->GetBounds(bounds); // 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 style = vtkSmartPointer::New(); iren->SetInteractorStyle(style); // VTK widgets consist of two parts: the widget part that handles event processing; // and the widget representation that defines how the widget appears in the scene // (i.e., matters pertaining to geometry). vtkSmartPointer rep = vtkSmartPointer::New(); rep->SetBoxWidth(100); rep->SetCircleWidth(75); rep->SetAxesWidth(60); rep->DisplayTextOn(); rep->PlaceWidget(bounds); vtkSmartPointer widget = vtkSmartPointer::New(); widget->SetInteractor(iren); widget->SetRepresentation(rep); vtkSmartPointer acbk = vtkSmartPointer::New(); acbk->AffineRep = rep; acbk->ImageActor = imageActor; widget->AddObserver(vtkCommand::InteractionEvent, acbk); widget->AddObserver(vtkCommand::EndInteractionEvent, acbk); // Add the actors to the renderer, set the background and size ren1->AddActor(imageActor); 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(); // recorder->Play(); // Remove the observers so we can go interactive. Without this the "-I" // testing option fails. recorder->Off(); iren->Start(); return EXIT_SUCCESS; }