move vtk containerbox in math

This commit is contained in:
AndreaRigoni
2026-03-25 10:37:38 +00:00
parent 5397baa50c
commit 913a1f7b3a
17 changed files with 183 additions and 23 deletions

View File

@@ -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)

View File

@@ -5,6 +5,7 @@ set(TESTS
vtkQuadMeshTest
vtkVoxImageTest
vtkVoxImageInteractiveTest
vtkContainerBoxTest
vtkContainerBoxTest2
)

View File

@@ -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 <stdio.h>
#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;

View File

@@ -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 <iostream>
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;
}

View File

@@ -0,0 +1,58 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "Vtk/uLibVtkViewer.h"
#include "Math/ContainerBox.h"
#include "Math/Units.h"
#include "Vtk/Math/vtkContainerBox.h"
#include "testing-prototype.h"
using namespace uLib;
int main() {
BEGIN_TESTING(vtk ContainerBox Test);
ContainerBox box;
box.Scale(Vector3f(1_m,5_m,1_m));
box.SetPosition(Vector3f(0,1_m,0));
Vtk::vtkContainerBox v_box(&box);
v_box.SetRepresentation(Vtk::Puppet::Surface);
v_box.SetOpacity(0.5);
v_box.SetSelectable(true);
box.findOrAddSignal(&ContainerBox::Updated)->connect([&box](){
std::cout << "box updated: " << box.GetWorldPoint(HPoint3f(1,1,1)) << std::endl;
});
if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {
Vtk::Viewer v_viewer;
v_viewer.AddPuppet(v_box);
v_viewer.Start();
}
END_TESTING;
}

View File

@@ -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 <iostream>
using namespace uLib;

View File

@@ -0,0 +1,178 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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.
//////////////////////////////////////////////////////////////////////////////*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include "vtkContainerBox.h"
#include <vtkActor.h>
#include <vtkAssembly.h>
#include <vtkAxes.h>
#include <vtkCubeSource.h>
#include <vtkLineSource.h>
#include <vtkMatrix4x4.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
#include "Math/vtkDense.h"
namespace uLib {
namespace Vtk {
vtkContainerBox::vtkContainerBox(vtkContainerBox::Content *content)
: m_Cube(vtkActor::New()), m_Axes(vtkActor::New()),
// m_Pivot(vtkActor::New()),
m_Content(content) {
this->InstallPipe();
Object::connect(m_Content, &Content::Updated, this, &vtkContainerBox::contentUpdate);
}
vtkContainerBox::~vtkContainerBox() {
m_Cube->Delete();
m_Axes->Delete();
// m_Pivot->Delete();
}
vtkPolyData *vtkContainerBox::GetPolyData() const {
// TODO
return NULL;
}
void vtkContainerBox::contentUpdate() {
if (!m_Content)
return;
vtkProp3D* root = vtkProp3D::SafeDownCast(this->GetProp());
if (!root) return;
vtkMatrix4x4* vmat = root->GetUserMatrix();
if (!vmat) {
// Should have been set in InstallPipe, but let's be safe
vtkNew<vtkMatrix4x4> mat;
root->SetUserMatrix(mat);
vmat = mat;
}
m_Cube->SetUserMatrix(nullptr);
m_Axes->SetUserMatrix(nullptr);
Matrix4f transform = m_Content->GetMatrix();
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);
// }
m_Content->Updated(); // Notify change
}
void vtkContainerBox::InstallPipe() {
if (!m_Content)
return;
Content *c = m_Content;
// CUBE
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cube->SetBounds(0, 1, 0, 1, 0, 1);
mapper->SetInputConnection(cube->GetOutputPort());
mapper->Update();
m_Cube->SetMapper(mapper);
m_Cube->GetProperty()->SetRepresentationToWireframe();
m_Cube->GetProperty()->SetAmbient(0.7);
// AXES //
vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();
axes->SetOrigin(0, 0, 0);
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(axes->GetOutputPort());
mapper->Update();
m_Axes->SetMapper(mapper);
m_Axes->GetProperty()->SetLineWidth(3);
m_Axes->GetProperty()->SetAmbient(0.4);
m_Axes->GetProperty()->SetSpecular(0);
// PIVOT //
axes = vtkSmartPointer<vtkAxes>::New();
axes->SetOrigin(0, 0, 0);
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(axes->GetOutputPort());
mapper->Update();
this->SetProp(m_Cube);
this->SetProp(m_Axes);
vtkProp3D* root = vtkProp3D::SafeDownCast(this->GetProp());
if (root) {
vtkNew<vtkMatrix4x4> vmat;
Matrix4fToVtk(c->GetMatrix(), vmat);
root->SetUserMatrix(vmat);
}
}
} // namespace Vtk
} // namespace uLib

View File

@@ -0,0 +1,66 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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.
//////////////////////////////////////////////////////////////////////////////*/
#ifndef U_VTKCONTAINERBOX_H
#define U_VTKCONTAINERBOX_H
#include "Math/ContainerBox.h"
#include "uLibVtkInterface.h"
#include "vtkPolydata.h"
#include <vtkActor.h>
#include <boost/signals2/connection.hpp>
namespace uLib {
namespace Vtk {
class vtkContainerBox : public Puppet, public Polydata {
typedef ContainerBox Content;
public:
vtkContainerBox(Content *content);
~vtkContainerBox();
virtual class vtkPolyData *GetPolyData() const;
virtual void contentUpdate();
virtual void Update();
protected:
virtual void InstallPipe();
vtkActor *m_Cube;
vtkActor *m_Axes;
// vtkActor *m_Pivot;
Content *m_Content;
bool m_BlockUpdate = false;
// boost::signals2::connection m_Connection;
};
} // namespace Vtk
} // namespace uLib
#endif // VTKCONTAINERBOX_H