Files
uLib/src/Vtk/testing/vtkHandlerWidget.cpp
2026-03-25 18:47:52 +00:00

119 lines
4.4 KiB
C++

/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#include "Math/ContainerBox.h"
#include "Vtk/uLibVtkViewer.h"
#include "Vtk/Math/vtkContainerBox.h"
#include "Vtk/vtkHandlerWidget.h"
#include "testing-prototype.h"
#include <vtkProp3D.h>
#include <vtkPropAssembly.h>
#include <vtkPropCollection.h>
#include <vtkSmartPointer.h>
#include <vtkCallbackCommand.h>
#include <vtkRenderWindowInteractor.h>
using namespace uLib;
int main() {
BEGIN_TESTING(vtkHandlerWidget with ContainerBox);
// 1. Create a ContainerBox (Math object)
ContainerBox box;
box.Scale(Vector3f(2.0, 3.0, 4.0));
box.SetPosition(Vector3f(1.0, 1.0, 1.0));
// 3. Setup the Viewer
Vtk::Viewer viewer;
// 2. Wrap it in a Vtk::vtkContainerBox (Vtk Puppet)
Vtk::vtkContainerBox v_box(&box);
v_box.SetRepresentation(Vtk::Puppet::Surface);
v_box.SetOpacity(0.5);
viewer.AddPuppet(v_box);
// 4. Create and setup the vtkHandlerWidget
vtkSmartPointer<Vtk::vtkHandlerWidget> handler =
vtkSmartPointer<Vtk::vtkHandlerWidget>::New();
handler->SetInteractor(viewer.GetInteractor());
// Get the prop from the puppet and cast it to vtkProp3D
vtkProp *v_prop = v_box.GetProp();
vtkProp3D *prop = vtkProp3D::SafeDownCast(v_prop);
if (!prop) {
// If it's a PropAssembly, try to get the first part (the cube)
::vtkPropAssembly *assembly = ::vtkPropAssembly::SafeDownCast(v_prop);
if (assembly && assembly->GetParts()->GetNumberOfItems() > 0) {
assembly->GetParts()->InitTraversal();
prop = vtkProp3D::SafeDownCast(assembly->GetParts()->GetNextProp());
}
}
TEST1(prop != nullptr);
handler->SetProp3D(prop);
handler->PlaceWidget();
handler->EnabledOn();
// 5. Add a key callback to switch modes
auto key_callback = vtkSmartPointer<vtkCallbackCommand>::New();
key_callback->SetCallback([](vtkObject *caller, unsigned long, void *clientData, void *) {
auto interactor = static_cast<vtkRenderWindowInteractor *>(caller);
auto h = static_cast<Vtk::vtkHandlerWidget *>(clientData);
std::string key = interactor->GetKeySym();
if (key == "g") {
std::cout << "Switching to GLOBAL frame" << std::endl;
h->SetReferenceFrame(Vtk::vtkHandlerWidget::GLOBAL);
} else if (key == "l") {
std::cout << "Switching to LOCAL frame" << std::endl;
h->SetReferenceFrame(Vtk::vtkHandlerWidget::LOCAL);
} else if (key == "c") {
std::cout << "Switching to CENTER frame" << std::endl;
h->SetReferenceFrame(Vtk::vtkHandlerWidget::CENTER);
} else if (key == "k") {
std::cout << "Switching to CENTER_LOCAL frame" << std::endl;
h->SetReferenceFrame(Vtk::vtkHandlerWidget::CENTER_LOCAL);
}
});
key_callback->SetClientData(handler.GetPointer());
viewer.GetInteractor()->AddObserver(vtkCommand::CharEvent, key_callback);
// 6. Start interaction if not in a continuous integration environment
if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {
std::cout << "Interactive test: use the gizmos to transform the ContainerBox"
<< std::endl;
std::cout << "Keys: [g] GLOBAL, [l] LOCAL, [c] CENTER, [k] CENTER_LOCAL" << std::endl;
viewer.Start();
} else {
std::cout << "Non-interactive test: widget initialized successfully"
<< std::endl;
}
handler->EnabledOff();
END_TESTING;
}