starting udate geant sloid

This commit is contained in:
AndreaRigoni
2026-04-16 11:07:06 +00:00
parent 83af9a180f
commit 987d783fdb
10 changed files with 220 additions and 132 deletions

View File

@@ -26,36 +26,11 @@ namespace Vtk {
BoxSolid::BoxSolid(Geant::BoxSolid *content)
: GeantSolid(content), m_BoxContent(content) {
this->InstallPipe();
// Connect the model's Updated event to updateTransform to ensure VTK sync
m_UpdateConnection = Object::connect(m_BoxContent, &uLib::Object::Updated, this, &BoxSolid::Update);
// Initial sync
this->Update();
}
BoxSolid::~BoxSolid() {
}
void BoxSolid::Update() {
ConnectionBlock blocker(m_UpdateConnection);
// Ensure base GeantSolid logic (G4Polyhedron and Transform) runs
this->GeantSolid::Update();
// Ensure base Prop3D properties (color, opacity, etc) are applied
this->Prop3D::Update();
}
void BoxSolid::SyncFromVtk() {
this->Prop3D::SyncFromVtk();
if (auto* proxy = vtkProp3D::SafeDownCast(this->GetProxyProp())) {
if (vtkMatrix4x4* mat = proxy->GetUserMatrix()) {
m_BoxContent->SetTransform(VtkToMatrix4f(mat));
}
}
}
void BoxSolid::serialize_display(uLib::Archive::display_properties_archive &ar,
const unsigned int version) {
// Expose Geant solid properties and underlying Box/TRS properties

View File

@@ -48,13 +48,6 @@ public:
BoxSolid(Geant::BoxSolid *content);
virtual ~BoxSolid();
virtual void Update() override;
virtual void SyncFromVtk() override;
virtual uLib::Object *GetContent() const override {
return m_BoxContent ? (::uLib::Object*)m_BoxContent->GetObject() : nullptr;
}
virtual void serialize_display(uLib::Archive::display_properties_archive &ar,
const unsigned int version = 0) override;
@@ -67,7 +60,6 @@ protected:
virtual void InstallPipe() override;
Geant::BoxSolid *m_BoxContent;
uLib::Connection m_UpdateConnection;
ULIB_DECLARE_PROPERTIES(BoxSolid)
};

View File

@@ -24,6 +24,7 @@
//////////////////////////////////////////////////////////////////////////////*/
#include "vtkGeantSolid.h"
#include "Core/Monitor.h"
#include <vtkActor.h>
#include <vtkPolyData.h>
@@ -34,6 +35,7 @@
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
#include <vtkMatrix4x4.h>
#include "Vtk/Math/vtkDense.h"
#include <Geant4/G4VSolid.hh>
#include <Geant4/G4Polyhedron.hh>
@@ -45,6 +47,14 @@ namespace Vtk {
GeantSolid::GeantSolid(Content *content)
: m_SolidActor(vtkActor::New()), m_Content(content) {
this->InstallPipe();
if (m_Content) {
m_UpdateConnection = Object::connect(m_Content, &uLib::Object::Updated, [this]() {
std::cout << "GeantSolid: Signal Updated received." << std::endl;
this->Update();
});
// Mandatory initial update to generate geometry and apply initial transform
this->Update();
}
}
GeantSolid::~GeantSolid() {
@@ -58,8 +68,21 @@ vtkPolyData *GeantSolid::GetPolyData() const {
}
void GeantSolid::Update() {
ConnectionBlock blocker(m_UpdateConnection);
this->UpdateGeometry();
this->UpdateTransform();
this->Prop3D::Update();
}
void GeantSolid::SyncFromVtk() {
if (!m_Content) return;
this->Prop3D::SyncFromVtk();
if (auto* proxy = vtkProp3D::SafeDownCast(this->GetProxyProp())) {
if (vtkMatrix4x4* mat = proxy->GetUserMatrix()) {
m_Content->SetTransform(VtkToMatrix4f(mat));
}
}
}
void GeantSolid::UpdateGeometry() {
@@ -114,33 +137,36 @@ void GeantSolid::UpdateTransform() {
if (!m_Content || !m_SolidActor)
return;
// Apply the Geant4 transform (position/rotation) if placed
if (m_Content->GetPhysical()) {
auto *phys = m_Content->GetPhysical();
G4ThreeVector pos = phys->GetTranslation();
const G4RotationMatrix *rot = phys->GetRotation();
// Apply the Geant4 transform (position/rotation/scaling)
G4ThreeVector pos = m_Content->GetPosition();
const G4RotationMatrix *rot = m_Content->GetRotation();
G4ThreeVector scale = m_Content->GetScaling();
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
transform->Identity();
transform->Translate(pos.x(), pos.y(), pos.z());
std::cout << "GeantSolid: Updating transform. Pos: " << pos.x() << " " << pos.y() << " " << pos.z()
<< " Scale: " << scale.x() << " " << scale.y() << " " << scale.z() << std::endl;
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);
}
vtkNew<vtkTransform> transform;
transform->Identity();
transform->Translate(pos.x(), pos.y(), pos.z());
if (rot) {
G4RotationMatrix invRot = *rot;
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
};
vtkNew<vtkMatrix4x4> mat;
mat->DeepCopy(elements);
transform->Concatenate(mat);
}
transform->Scale(scale.x(), scale.y(), scale.z());
m_SolidActor->SetUserTransform(transform);
m_SolidActor->Modified();
}
}
void GeantSolid::InstallPipe() {
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();

View File

@@ -52,15 +52,19 @@ public:
virtual class vtkPolyData *GetPolyData() const override;
virtual void Update() override;
virtual void SyncFromVtk() override;
virtual void UpdateGeometry();
virtual void UpdateTransform();
virtual uLib::Object *GetContent() const override { return (uLib::Object*)m_Content; }
protected:
virtual void InstallPipe();
vtkActor *m_SolidActor;
Content *m_Content;
vtkActor *m_SolidActor;
uLib::Connection m_UpdateConnection;
};
} // namespace Vtk

View File

@@ -33,6 +33,8 @@ public:
/** @brief Synchronizes all managed prop3ds back to their models. */
virtual void SyncFromVtk() override;
virtual Object* GetContent() const override { return (Object*)m_Context; }
public:
virtual void Prop3DAdded(Prop3D* prop3d);
virtual void Prop3DRemoved(Prop3D* prop3d);