refactor: extend Object property system and implement recursive property discovery in Vtk::Puppet archive

This commit is contained in:
AndreaRigoni
2026-04-03 08:54:37 +00:00
parent 6396bdfebf
commit a6a1539663
12 changed files with 459 additions and 272 deletions

View File

@@ -61,8 +61,8 @@ int main(int argc, char** argv) {
vtkTess.AddToViewer(viewer);
// Color them differently
vtkActor::SafeDownCast(vtkBox.GetProp())->GetProperty()->SetColor(0.8, 0.2, 0.2); // Redish box
vtkActor::SafeDownCast(vtkTess.GetProp())->GetProperty()->SetColor(0.2, 0.8, 0.2); // Greenish tess
vtkBox.SetColor(0.8, 0.2, 0.2); // Redish box
vtkTess.SetColor(0.2, 0.8, 0.2); // Greenish tess
// Position tessellated solid away from box
Matrix4f trans = Matrix4f::Identity();

View File

@@ -13,43 +13,104 @@
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkAssembly.h>
#include <vtkTransform.h>
#include <vtkMatrix4x4.h>
#include <Geant4/G4VPhysicalVolume.hh>
#include "Vtk/Math/vtkDense.h"
namespace uLib {
namespace Vtk {
vtkBoxSolid::vtkBoxSolid(Geant::BoxSolid *content)
: vtkGeantSolid(content), m_BoxContent(content) {
// Re-run Update for box-specific pipe
: vtkGeantSolid(content), m_BoxContent(content), m_BoxPuppet(nullptr) {
if (m_BoxContent && m_BoxContent->GetObject()) {
m_BoxPuppet = new vtkContainerBox(m_BoxContent->GetObject());
// Use the specialized box puppet's representation as our main prop
this->SetProp(m_BoxPuppet->GetProp());
}
// Connect the model's Updated event to updateTransform to ensure VTK sync
Object::connect(m_BoxContent, &uLib::Object::Updated, this, &vtkBoxSolid::UpdateTransform);
// Initial sync
this->Update();
}
vtkBoxSolid::~vtkBoxSolid() {}
vtkBoxSolid::~vtkBoxSolid() {
if (m_BoxPuppet) {
delete m_BoxPuppet;
}
}
void vtkBoxSolid::Update() {
this->UpdateGeometry();
this->UpdateTransform();
// Ensure base Puppet properties (color, opacity, etc) are applied
this->Puppet::Update();
}
void vtkBoxSolid::SyncFromVtk() {
vtkProp3D *root = vtkProp3D::SafeDownCast(this->GetProp());
if (root && m_BoxContent) {
vtkMatrix4x4 *rootMat = root->GetUserMatrix();
if (rootMat) {
Matrix4f vtkWorld = VtkToMatrix4f(rootMat);
m_BoxContent->SetTransform(vtkWorld);
m_BoxContent->Updated();
}
}
}
void vtkBoxSolid::UpdateGeometry() {
if (!m_BoxContent || !m_BoxContent->GetObject()) {
// Fallback to base tessellation if no model object
if (!m_BoxContent || !m_BoxContent->GetObject() || !m_BoxPuppet) {
// Fallback to base tessellation if no model object is available
vtkGeantSolid::UpdateGeometry();
return;
}
// Use the underlying ContainerBox for precise geometry
Vector3f size = m_BoxContent->GetObject()->GetSize();
vtkNew<vtkCubeSource> cube;
cube->SetXLength(size(0));
cube->SetYLength(size(1));
cube->SetZLength(size(2));
cube->Update();
// The vtkContainerBox manages its own geometry update
m_BoxPuppet->Update();
}
vtkPolyData *poly = GetPolyData();
if (poly) {
poly->ShallowCopy(cube->GetOutput());
poly->Modified();
void vtkBoxSolid::UpdateTransform() {
if (!m_BoxContent || !m_BoxPuppet) {
vtkGeantSolid::UpdateTransform();
return;
}
// 1. Sync the inner box TRS (local box properties like size and offset)
m_BoxPuppet->Update();
// 2. Sync the Geant4-level placement (world position/rotation)
vtkProp3D* root = vtkProp3D::SafeDownCast(this->GetProp());
if (root && m_BoxContent->GetPhysical()) {
auto *phys = m_BoxContent->GetPhysical();
G4ThreeVector pos = phys->GetTranslation();
const G4RotationMatrix *rot = phys->GetRotation();
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
transform->Identity();
transform->Translate(pos.x(), pos.y(), pos.z());
if (rot) {
// G4RotationMatrix stores the inverse of the rotation for placement
G4RotationMatrix invRot = rot->inverse();
double elements[16] = {
invRot.xx(), invRot.xy(), invRot.xz(), 0,
invRot.yx(), invRot.yy(), invRot.yz(), 0,
invRot.zx(), invRot.zy(), invRot.zz(), 0,
0, 0, 0, 1
};
vtkSmartPointer<vtkMatrix4x4> mat = vtkSmartPointer<vtkMatrix4x4>::New();
mat->DeepCopy(elements);
transform->Concatenate(mat);
}
// Apply the Geant4 transform on top of the local box's UserMatrix
root->SetUserTransform(transform);
} else if (root) {
root->SetUserTransform(nullptr);
}
}

View File

@@ -26,7 +26,12 @@
#ifndef U_VTKBOXSOLID_H
#define U_VTKBOXSOLID_H
#include "Core/Types.h"
#include "Core/Property.h"
#include "Core/Serializable.h"
#include "vtkGeantSolid.h"
#include "Vtk/Math/vtkContainerBox.h"
namespace uLib {
namespace Vtk {
@@ -35,15 +40,27 @@ namespace Vtk {
* @brief VTK Puppet for visualizing a Geant::BoxSolid.
*/
class vtkBoxSolid : public vtkGeantSolid {
uLibTypeMacro(vtkBoxSolid, uLib::Vtk::vtkGeantSolid)
public:
vtkBoxSolid(Geant::BoxSolid *content);
virtual ~vtkBoxSolid();
virtual void Update() override;
virtual void UpdateGeometry() override;
virtual void UpdateTransform() override;
virtual void SyncFromVtk() override;
template <typename Ar>
void serialize(Ar &ar, const unsigned int version) {
ar & NVP("BoxSolid", *m_BoxContent);
}
protected:
Geant::BoxSolid *m_BoxContent;
vtkContainerBox *m_BoxPuppet;
ULIB_DECLARE_PROPERTIES(vtkBoxSolid)
};
} // namespace Vtk

View File

@@ -54,6 +54,7 @@ struct ContainerBoxData {
vtkSmartPointer<vtkMatrix4x4> m_Affine;
uLib::Connection m_UpdateSignal;
ContainerBoxData() : m_Cube(vtkSmartPointer<vtkActor>::New()),
m_Axes(vtkSmartPointer<vtkActor>::New()),
m_VtkAsm(vtkSmartPointer<vtkAssembly>::New()),

View File

@@ -62,9 +62,9 @@ protected:
virtual void InstallPipe();
struct ContainerBoxData *d;
Content *m_Content;
bool m_BlockUpdate = false;
ContainerBox *m_Content;
ULIB_DECLARE_PROPERTIES(vtkContainerBox)
};
} // namespace Vtk

View File

@@ -560,28 +560,55 @@ bool Puppet::IsSelected() const
return pd->m_Selected;
}
void Puppet::ApplyPuppetTransform(vtkProp3D* prop)
{
if (!prop) return;
if (auto* content = this->GetContent()) {
if (auto* tr = dynamic_cast<uLib::TRS*>(content)) {
vtkNew<vtkMatrix4x4> m;
Matrix4fToVtk(tr->GetMatrix(), m);
prop->SetUserMatrix(m);
prop->Modified();
}
}
}
void Puppet::SyncFromVtk()
{
if (auto* content = this->GetContent()) {
if (auto* tr = dynamic_cast<uLib::TRS*>(content)) {
if (auto* proxy = this->GetProxyProp()) {
if (vtkMatrix4x4* mat = proxy->GetUserMatrix()) {
tr->FromMatrix(VtkToMatrix4f(mat));
content->Updated();
}
}
}
}
}
void Puppet::Update()
{
// Derived classes should have updated the transform if they override Update()
// or we can apply base transform if it's default:
// pd->ApplyTransform(pd->m_Prop);
// Apply content transform via virtual GetProp() / ApplyPuppetTransform(),
// so all derived classes benefit without duplicating the matrix code.
this->ApplyPuppetTransform(vtkProp3D::SafeDownCast(this->GetProp()));
pd->ApplyAppearance(pd->m_Prop);
// Use virtual GetProp() for appearance so overriders (e.g. vtkVoxImage)
// that never call SetProp() are handled correctly.
pd->ApplyAppearance(this->GetProp());
if (pd->m_Selected) {
pd->UpdateHighlight();
}
if (pd->m_Prop) {
if (pd->m_ShowBoundingBox) {
double* bounds = pd->m_Prop->GetBounds();
if (auto* prop = this->GetProp()) {
if (pd->m_ShowBoundingBox && pd->m_OutlineSource) {
double* bounds = prop->GetBounds();
pd->m_OutlineSource->SetBounds(bounds);
pd->m_OutlineSource->Update();
}
if (pd->m_ShowScaleMeasures) {
double* bounds = pd->m_Prop->GetBounds();
pd->m_CubeAxesActor->SetBounds(bounds);
if (pd->m_ShowScaleMeasures && pd->m_CubeAxesActor) {
pd->m_CubeAxesActor->SetBounds(prop->GetBounds());
}
}
@@ -598,6 +625,7 @@ void Puppet::Update()
}
void Puppet::ConnectInteractor(vtkRenderWindowInteractor *interactor)
{
}

View File

@@ -35,6 +35,8 @@
#include <iomanip>
#include <ostream>
#include <vector>
#include <set>
#include <boost/type_traits/is_base_of.hpp>
// vtk classes forward declaration //
class vtkProp;
@@ -106,7 +108,7 @@ uLibTypeMacro(Puppet, uLib::Object)
* This method should be called when the VTK representation has been modified
* (e.g., via a gizmo) and the changes need to be pushed back to the model.
*/
virtual void SyncFromVtk() {}
virtual void SyncFromVtk();
enum Representation {
Points = 0,
@@ -149,6 +151,7 @@ protected:
void ApplyAppearance(vtkProp *prop);
void ApplyTransform(vtkProp3D *p3d);
void ApplyPuppetTransform(vtkProp3D *p3d);
std::vector<uLib::PropertyBase *> m_DisplayProperties;
mutable uLib::RecursiveMutex m_UpdateMutex;
@@ -179,10 +182,17 @@ class display_properties_archive
: public boost::archive::detail::common_oarchive<
display_properties_archive> {
public:
display_properties_archive(Vtk::Puppet *puppet)
friend class boost::archive::detail::interface_oarchive<display_properties_archive>;
friend class boost::archive::save_access;
using boost::archive::detail::common_oarchive<display_properties_archive>::save_override;
display_properties_archive(Vtk::Puppet *p)
: boost::archive::detail::common_oarchive<display_properties_archive>(
boost::archive::no_header),
m_Puppet(puppet) {}
m_Puppet(p) {
if (p)
m_Visited.insert(dynamic_cast<const void *>(p));
}
std::string GetCurrentGroup() const {
std::string group;
@@ -234,6 +244,24 @@ public:
m_GroupStack.pop_back();
}
// Follow pointers to discover properties in child objects
template<class T>
void save_override(T * const & t) {
if (!t) return;
this->save_pointer_helper(t, typename boost::is_base_of<uLib::Object, T>::type());
}
template<class T>
void save_pointer_helper(T* t, boost::mpl::true_) {
const void* ptr = dynamic_cast<const void*>(t);
if (m_Visited.find(ptr) != m_Visited.end()) return;
m_Visited.insert(ptr);
this->save_override(*t);
}
template<class T>
void save_pointer_helper(T* t, boost::mpl::false_) {}
// Recursion for nested classes, ignore primitives
template <class T> void save_override(const T &t) {
this->save_helper(t, typename boost::is_class<T>::type());
@@ -243,6 +271,8 @@ public:
boost::serialization::serialize_adl(*this, const_cast<T &>(t), 0);
}
void save_helper(const std::string &t, boost::mpl::true_) {}
template <class T> void save_helper(const T &t, boost::mpl::false_) {}
void save_override(const boost::archive::object_id_type &t) {}
@@ -257,6 +287,7 @@ public:
private:
Vtk::Puppet *m_Puppet;
std::vector<std::string> m_GroupStack;
std::set<const void *> m_Visited;
};
} // namespace Archive