refactor: unify vtkBoxSolid architecture with Puppet base and update build documentation
This commit is contained in:
@@ -10,9 +10,11 @@
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
#include "vtkBoxSolid.h"
|
||||
#include "Core/Monitor.h"
|
||||
#include <vtkCubeSource.h>
|
||||
#include <vtkPolyDataMapper.h>
|
||||
#include <vtkActor.h>
|
||||
#include <vtkProperty.h>
|
||||
#include <vtkAssembly.h>
|
||||
#include <vtkTransform.h>
|
||||
#include <vtkMatrix4x4.h>
|
||||
@@ -23,95 +25,59 @@ namespace uLib {
|
||||
namespace Vtk {
|
||||
|
||||
vtkBoxSolid::vtkBoxSolid(Geant::BoxSolid *content)
|
||||
: 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());
|
||||
}
|
||||
|
||||
: vtkGeantSolid(content), m_BoxContent(content) {
|
||||
this->InstallPipe();
|
||||
|
||||
// Connect the model's Updated event to updateTransform to ensure VTK sync
|
||||
Object::connect(m_BoxContent, &uLib::Object::Updated, this, &vtkBoxSolid::UpdateTransform);
|
||||
m_UpdateConnection = Object::connect(m_BoxContent, &uLib::Object::Updated, this, &vtkBoxSolid::Update);
|
||||
|
||||
// Initial sync
|
||||
this->Update();
|
||||
}
|
||||
|
||||
vtkBoxSolid::~vtkBoxSolid() {
|
||||
if (m_BoxPuppet) {
|
||||
delete m_BoxPuppet;
|
||||
}
|
||||
}
|
||||
|
||||
void vtkBoxSolid::Update() {
|
||||
ConnectionBlock blocker(m_UpdateConnection);
|
||||
this->UpdateGeometry();
|
||||
this->UpdateTransform();
|
||||
// Ensure base Puppet properties (color, opacity, etc) are applied
|
||||
// Ensure base Puppet properties (color, opacity, etc) and transform 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();
|
||||
}
|
||||
this->Puppet::SyncFromVtk();
|
||||
if (auto* proxy = vtkProp3D::SafeDownCast(this->GetProxyProp())) {
|
||||
if (vtkMatrix4x4* mat = proxy->GetUserMatrix()) {
|
||||
m_BoxContent->SetTransform(VtkToMatrix4f(mat));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vtkBoxSolid::UpdateGeometry() {
|
||||
if (!m_BoxContent || !m_BoxContent->GetObject() || !m_BoxPuppet) {
|
||||
// Fallback to base tessellation if no model object is available
|
||||
vtkGeantSolid::UpdateGeometry();
|
||||
return;
|
||||
}
|
||||
|
||||
// The vtkContainerBox manages its own geometry update
|
||||
m_BoxPuppet->Update();
|
||||
// Sync geometry from G4VSolid provided by vtkGeantSolid (tessellation)
|
||||
vtkGeantSolid::UpdateGeometry();
|
||||
}
|
||||
|
||||
void vtkBoxSolid::UpdateTransform() {
|
||||
if (!m_BoxContent || !m_BoxPuppet) {
|
||||
vtkGeantSolid::UpdateTransform();
|
||||
return;
|
||||
// Take transform from Puppet base (which uses GetContent() -> ContainerBox TRS)
|
||||
this->Puppet::Update();
|
||||
}
|
||||
|
||||
void vtkBoxSolid::serialize_display(uLib::Archive::display_properties_archive &ar,
|
||||
const unsigned int version) {
|
||||
// Expose Geant solid properties and underlying Box/TRS properties
|
||||
this->Puppet::serialize_display(ar, version);
|
||||
if (m_BoxContent) {
|
||||
ar & NVP("Box", *m_BoxContent);
|
||||
if (m_BoxContent->GetObject()) {
|
||||
ar & NVP("Container", *m_BoxContent->GetObject());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
void vtkBoxSolid::InstallPipe() {
|
||||
vtkGeantSolid::InstallPipe();
|
||||
}
|
||||
|
||||
} // namespace Vtk
|
||||
|
||||
Reference in New Issue
Block a user