From 913a1f7b3a9fbbe34c0468b2dbb0df14a7631df5 Mon Sep 17 00:00:00 2001 From: AndreaRigoni Date: Wed, 25 Mar 2026 10:37:38 +0000 Subject: [PATCH] move vtk containerbox in math --- src/Vtk/CMakeLists.txt | 2 - src/Vtk/HEP/Detectors/vtkDetectorChamber.h | 2 +- .../testing/vtkCylinderEmitterPrimaryTest.cpp | 2 +- .../Geant/testing/vtkEmitterPrimaryTest.cpp | 2 +- .../HEP/Geant/testing/vtkGeantEventTest.cpp | 2 +- .../testing/vtkSkyPlaneEmitterPrimaryTest.cpp | 2 +- src/Vtk/Math/CMakeLists.txt | 2 + src/Vtk/Math/testing/CMakeLists.txt | 1 + src/Vtk/Math/testing/testing-prototype.h | 41 +++++++ src/Vtk/Math/testing/vtkAssemblyTest.cpp | 109 ++++++++++++++++++ .../testing/vtkContainerBoxTest.cpp | 2 +- src/Vtk/Math/testing/vtkContainerBoxTest2.cpp | 2 +- src/Vtk/{ => Math}/vtkContainerBox.cpp | 27 +++-- src/Vtk/{ => Math}/vtkContainerBox.h | 3 + src/Vtk/testing/CMakeLists.txt | 3 +- src/Vtk/testing/vtkHandlerWidget.cpp | 2 +- src/Vtk/vtkObjectsContext.cpp | 2 +- 17 files changed, 183 insertions(+), 23 deletions(-) create mode 100644 src/Vtk/Math/testing/testing-prototype.h create mode 100644 src/Vtk/Math/testing/vtkAssemblyTest.cpp rename src/Vtk/{ => Math}/testing/vtkContainerBoxTest.cpp (98%) rename src/Vtk/{ => Math}/vtkContainerBox.cpp (89%) rename src/Vtk/{ => Math}/vtkContainerBox.h (94%) diff --git a/src/Vtk/CMakeLists.txt b/src/Vtk/CMakeLists.txt index ef1277c..6517431 100644 --- a/src/Vtk/CMakeLists.txt +++ b/src/Vtk/CMakeLists.txt @@ -1,6 +1,5 @@ set(HEADERS uLibVtkInterface.h uLibVtkViewer.h - vtkContainerBox.h vtkHandlerWidget.h vtkQViewport.h vtkViewport.h @@ -10,7 +9,6 @@ set(HEADERS uLibVtkInterface.h set(SOURCES uLibVtkInterface.cxx uLibVtkViewer.cpp - vtkContainerBox.cpp vtkHandlerWidget.cpp vtkQViewport.cpp vtkViewport.cpp diff --git a/src/Vtk/HEP/Detectors/vtkDetectorChamber.h b/src/Vtk/HEP/Detectors/vtkDetectorChamber.h index 6c2f128..0dc53ee 100644 --- a/src/Vtk/HEP/Detectors/vtkDetectorChamber.h +++ b/src/Vtk/HEP/Detectors/vtkDetectorChamber.h @@ -34,7 +34,7 @@ #include "HEP/Detectors/DetectorChamber.h" #include "Math/Dense.h" #include "Vtk/uLibVtkInterface.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include #include #include diff --git a/src/Vtk/HEP/Geant/testing/vtkCylinderEmitterPrimaryTest.cpp b/src/Vtk/HEP/Geant/testing/vtkCylinderEmitterPrimaryTest.cpp index f794b76..653442c 100644 --- a/src/Vtk/HEP/Geant/testing/vtkCylinderEmitterPrimaryTest.cpp +++ b/src/Vtk/HEP/Geant/testing/vtkCylinderEmitterPrimaryTest.cpp @@ -19,7 +19,7 @@ #include "Vtk/uLibVtkViewer.h" #include "Vtk/HEP/Geant/vtkGeantEvent.h" #include "Vtk/HEP/Geant/vtkEmitterPrimary.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include #include diff --git a/src/Vtk/HEP/Geant/testing/vtkEmitterPrimaryTest.cpp b/src/Vtk/HEP/Geant/testing/vtkEmitterPrimaryTest.cpp index 9e708cc..982b073 100644 --- a/src/Vtk/HEP/Geant/testing/vtkEmitterPrimaryTest.cpp +++ b/src/Vtk/HEP/Geant/testing/vtkEmitterPrimaryTest.cpp @@ -8,7 +8,7 @@ #include "Vtk/uLibVtkViewer.h" #include "Vtk/HEP/Geant/vtkGeantEvent.h" #include "Vtk/HEP/Geant/vtkEmitterPrimary.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include "HEP/Detectors/DetectorChamber.h" #include "Vtk/HEP/Detectors/vtkDetectorChamber.h" diff --git a/src/Vtk/HEP/Geant/testing/vtkGeantEventTest.cpp b/src/Vtk/HEP/Geant/testing/vtkGeantEventTest.cpp index 6ef69af..373fb95 100644 --- a/src/Vtk/HEP/Geant/testing/vtkGeantEventTest.cpp +++ b/src/Vtk/HEP/Geant/testing/vtkGeantEventTest.cpp @@ -18,7 +18,7 @@ #include "Math/Units.h" #include "Vtk/uLibVtkViewer.h" #include "Vtk/HEP/Geant/vtkGeantEvent.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include #include diff --git a/src/Vtk/HEP/Geant/testing/vtkSkyPlaneEmitterPrimaryTest.cpp b/src/Vtk/HEP/Geant/testing/vtkSkyPlaneEmitterPrimaryTest.cpp index ee16257..ee86991 100644 --- a/src/Vtk/HEP/Geant/testing/vtkSkyPlaneEmitterPrimaryTest.cpp +++ b/src/Vtk/HEP/Geant/testing/vtkSkyPlaneEmitterPrimaryTest.cpp @@ -8,7 +8,7 @@ #include "Vtk/uLibVtkViewer.h" #include "Vtk/HEP/Geant/vtkGeantEvent.h" #include "Vtk/HEP/Geant/vtkEmitterPrimary.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include "HEP/Detectors/DetectorChamber.h" #include "Vtk/HEP/Detectors/vtkDetectorChamber.h" diff --git a/src/Vtk/Math/CMakeLists.txt b/src/Vtk/Math/CMakeLists.txt index c76d465..c722d94 100644 --- a/src/Vtk/Math/CMakeLists.txt +++ b/src/Vtk/Math/CMakeLists.txt @@ -8,6 +8,7 @@ set(MATH_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/vtkTriangleMesh.cpp ${CMAKE_CURRENT_SOURCE_DIR}/vtkQuadMesh.cpp ${CMAKE_CURRENT_SOURCE_DIR}/vtkVoxImage.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/vtkContainerBox.cpp ${CMAKE_CURRENT_SOURCE_DIR}/vtkCylinder.cpp PARENT_SCOPE) @@ -17,6 +18,7 @@ set(MATH_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/vtkTriangleMesh.h ${CMAKE_CURRENT_SOURCE_DIR}/vtkQuadMesh.h ${CMAKE_CURRENT_SOURCE_DIR}/vtkVoxImage.h + ${CMAKE_CURRENT_SOURCE_DIR}/vtkContainerBox.h ${CMAKE_CURRENT_SOURCE_DIR}/vtkCylinder.h PARENT_SCOPE) diff --git a/src/Vtk/Math/testing/CMakeLists.txt b/src/Vtk/Math/testing/CMakeLists.txt index 4199727..04eaec3 100644 --- a/src/Vtk/Math/testing/CMakeLists.txt +++ b/src/Vtk/Math/testing/CMakeLists.txt @@ -5,6 +5,7 @@ set(TESTS vtkQuadMeshTest vtkVoxImageTest vtkVoxImageInteractiveTest + vtkContainerBoxTest vtkContainerBoxTest2 ) diff --git a/src/Vtk/Math/testing/testing-prototype.h b/src/Vtk/Math/testing/testing-prototype.h new file mode 100644 index 0000000..c801261 --- /dev/null +++ b/src/Vtk/Math/testing/testing-prototype.h @@ -0,0 +1,41 @@ +/*////////////////////////////////////////////////////////////////////////////// +// 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 + +#include "Vtk/uLibVtkInterface.h" + + +#define BEGIN_TESTING(name) \ +static int _fail = 0; \ +printf("..:: Testing " #name " ::..\n"); + +#define TEST1(val) _fail += (val)==0 +#define TEST0(val) _fail += (val)!=0 +#define END_TESTING return _fail; + + diff --git a/src/Vtk/Math/testing/vtkAssemblyTest.cpp b/src/Vtk/Math/testing/vtkAssemblyTest.cpp new file mode 100644 index 0000000..713deb2 --- /dev/null +++ b/src/Vtk/Math/testing/vtkAssemblyTest.cpp @@ -0,0 +1,109 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova +// All rights reserved +//////////////////////////////////////////////////////////////////////////////*/ + +#include "Vtk/uLibVtkViewer.h" +#include "Vtk/Math/vtkAssembly.h" +#include "Vtk/Math/vtkCylinder.h" +#include "Vtk/Math/vtkContainerBox.h" +#include "Math/Assembly.h" +#include "Math/Cylinder.h" +#include "Math/ContainerBox.h" +#include "Math/Units.h" + +#include + +using namespace uLib; + +/** + * @brief This test verifies that uLib::Vtk::Assembly correctly visualizes a collection + * of objects and that transformations applied to the assembly are propagated to its children. + * It also checks that the assembly appears as a bounding box of its contents. + */ +int main() { + std::cout << "Starting vtkAssemblyTest..." << std::endl; + + // 1. Setup Core Geometry + std::cout << " - Creating core Assembly..." << std::endl; + uLib::Assembly* core_assembly = new uLib::Assembly(); + core_assembly->SetInstanceName("MainAssembly"); + + // Add a box + std::cout << " - Adding ChildBox (Red)..." << std::endl; + ContainerBox* box = new ContainerBox(); + box->SetInstanceName("ChildBox"); + box->Translate(Vector3f(1.0, 0, 0)); + core_assembly->AddObject(box); + + // Add a cylinder + // std::cout << " - Adding ChildCylinder (Blue)..." << std::endl; + // Cylinder* cyl = new Cylinder(0.5, 2.0); + // cyl->SetInstanceName("ChildCylinder"); + // cyl->Translate(Vector3f(-2.0, 0, 0)); + // core_assembly->AddObject(cyl); + + std::cout << " - Adding another box (Green)..." << std::endl; + ContainerBox* box2 = new ContainerBox(); + box2->Scale(Vector3f(1.0, 1.0, 2.0)); + box2->SetInstanceName("ChildBox2"); + box2->Translate(Vector3f(0, 0, 1.0)); + core_assembly->AddObject(box2); + + // 2. Setup VTK Representation + std::cout << " - Creating VTK Assembly representation..." << std::endl; + Vtk::Assembly vtk_assembly(core_assembly); + + // Find and colorized the children puppets + Vtk::Puppet* p_box = vtk_assembly.GetPuppet(box); + if (p_box) { + p_box->SetColor(1.0, 0.0, 0.0); // Red + } else { + std::cerr << "Warning: Could not find puppet for box!" << std::endl; + } + + // Vtk::Puppet* p_cyl = vtk_assembly.GetPuppet(cyl); + // if (p_cyl) { + // p_cyl->SetColor(0.0, 0.0, 1.0); // Blue + // } else { + // std::cerr << "Warning: Could not find puppet for cylinder!" << std::endl; + // } + + Vtk::Puppet* p_box2 = vtk_assembly.GetPuppet(box2); + if (p_box2) { + p_box2->SetColor(0.0, 1.0, 0.0); // Green + } else { + std::cerr << "Warning: Could not find puppet for box2!" << std::endl; + } + + // 3. Test Transformation Propagation + std::cout << " - Rotating Assembly 45 degrees around Z..." << std::endl; + core_assembly->Rotate(45.0_deg, Vector3f::UnitZ()); + + std::cout << " - Translating Assembly up (+Y)..." << std::endl; + core_assembly->Translate(Vector3f(0, 2.0, 0)); + + // Notify all puppets of the change + core_assembly->Updated(); + + // 4. Run Visualization + std::cout << "Starting viewer (close to exit)..." << std::endl; + std::cout << "Expected: A white bounding box containing a red box and a blue cylinder, " + << "all rotated and translated as a group." << std::endl; + + Vtk::Viewer viewer; + viewer.AddPuppet(*p_box); + viewer.AddPuppet(*p_box2); + viewer.AddPuppet(vtk_assembly); + viewer.Start(); + + // Clean up + delete core_assembly; + delete box; + delete box2; + // delete cyl; + + return 0; +} diff --git a/src/Vtk/testing/vtkContainerBoxTest.cpp b/src/Vtk/Math/testing/vtkContainerBoxTest.cpp similarity index 98% rename from src/Vtk/testing/vtkContainerBoxTest.cpp rename to src/Vtk/Math/testing/vtkContainerBoxTest.cpp index a525fb6..12ddf37 100644 --- a/src/Vtk/testing/vtkContainerBoxTest.cpp +++ b/src/Vtk/Math/testing/vtkContainerBoxTest.cpp @@ -27,7 +27,7 @@ #include "Math/ContainerBox.h" #include "Math/Units.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include "testing-prototype.h" diff --git a/src/Vtk/Math/testing/vtkContainerBoxTest2.cpp b/src/Vtk/Math/testing/vtkContainerBoxTest2.cpp index 4e76178..f8bcc62 100644 --- a/src/Vtk/Math/testing/vtkContainerBoxTest2.cpp +++ b/src/Vtk/Math/testing/vtkContainerBoxTest2.cpp @@ -8,7 +8,7 @@ #include "Vtk/uLibVtkViewer.h" #include "Math/ContainerBox.h" #include "Math/Units.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include using namespace uLib; diff --git a/src/Vtk/vtkContainerBox.cpp b/src/Vtk/Math/vtkContainerBox.cpp similarity index 89% rename from src/Vtk/vtkContainerBox.cpp rename to src/Vtk/Math/vtkContainerBox.cpp index 0ba9d9a..6de9944 100644 --- a/src/Vtk/vtkContainerBox.cpp +++ b/src/Vtk/Math/vtkContainerBox.cpp @@ -86,33 +86,40 @@ void vtkContainerBox::contentUpdate() { m_Axes->SetUserMatrix(nullptr); Matrix4f transform = m_Content->GetMatrix(); - for (int i = 0; i < 4; ++i) - for (int j = 0; j < 4; ++j) { - vmat->SetElement(i, j, transform(i, j)); - } + Matrix4fToVtk(transform, vmat); root->Modified(); + m_BlockUpdate = true; Puppet::Update(); } void vtkContainerBox::Update() { if (!m_Content) return; + + if (m_BlockUpdate) { + m_BlockUpdate = false; + return; + } + + // Use Targeted Blocking: only block the feedback connection to this puppet + // boost::signals2::shared_connection_block block(m_Connection); vtkProp3D* assembly = vtkProp3D::SafeDownCast(this->GetProp()); if (!assembly) return; vtkMatrix4x4* vmat = assembly->GetUserMatrix(); if (!vmat) return; + Matrix4f transform = VtkToMatrix4f(vmat); // Update uLib model's affine transform - if (m_Content->GetParent()) { - Matrix4f localT = m_Content->GetParent()->GetWorldMatrix().inverse() * transform; - m_Content->SetMatrix(localT); - } else { - m_Content->SetMatrix(transform); - } + // if (m_Content->GetParent()) { + // Matrix4f localT = m_Content->GetParent()->GetWorldMatrix().inverse() * transform; + // m_Content->SetMatrix(localT); + // } else { + m_Content->SetMatrix(transform); + // } m_Content->Updated(); // Notify change } diff --git a/src/Vtk/vtkContainerBox.h b/src/Vtk/Math/vtkContainerBox.h similarity index 94% rename from src/Vtk/vtkContainerBox.h rename to src/Vtk/Math/vtkContainerBox.h index 4501c7c..95e10ac 100644 --- a/src/Vtk/vtkContainerBox.h +++ b/src/Vtk/Math/vtkContainerBox.h @@ -30,6 +30,7 @@ #include "uLibVtkInterface.h" #include "vtkPolydata.h" #include +#include namespace uLib { namespace Vtk { @@ -55,6 +56,8 @@ protected: // vtkActor *m_Pivot; Content *m_Content; + bool m_BlockUpdate = false; + // boost::signals2::connection m_Connection; }; } // namespace Vtk diff --git a/src/Vtk/testing/CMakeLists.txt b/src/Vtk/testing/CMakeLists.txt index 26d9c02..bf15373 100644 --- a/src/Vtk/testing/CMakeLists.txt +++ b/src/Vtk/testing/CMakeLists.txt @@ -1,7 +1,6 @@ # TESTS -set( TESTS +set(TESTS vtkViewerTest - vtkContainerBoxTest vtkHandlerWidget PuppetPropertyTest # vtkVoxImageTest diff --git a/src/Vtk/testing/vtkHandlerWidget.cpp b/src/Vtk/testing/vtkHandlerWidget.cpp index fa269a4..2c70d92 100644 --- a/src/Vtk/testing/vtkHandlerWidget.cpp +++ b/src/Vtk/testing/vtkHandlerWidget.cpp @@ -25,7 +25,7 @@ #include "Math/ContainerBox.h" #include "Vtk/uLibVtkViewer.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include "Vtk/vtkHandlerWidget.h" #include "testing-prototype.h" diff --git a/src/Vtk/vtkObjectsContext.cpp b/src/Vtk/vtkObjectsContext.cpp index 578d07e..133f926 100644 --- a/src/Vtk/vtkObjectsContext.cpp +++ b/src/Vtk/vtkObjectsContext.cpp @@ -1,5 +1,5 @@ #include "vtkObjectsContext.h" -#include "Vtk/vtkContainerBox.h" +#include "Vtk/Math/vtkContainerBox.h" #include "Vtk/Math/vtkCylinder.h" #include "HEP/Detectors/vtkDetectorChamber.h"