refactor: update transformation system, improve template readability, and reorganize VTK assembly management

This commit is contained in:
AndreaRigoni
2026-03-31 16:04:03 +00:00
parent 22d0041942
commit d4fd2d3914
30 changed files with 568 additions and 501 deletions

View File

@@ -40,7 +40,7 @@ namespace Vtk {
vtkCylinder::vtkCylinder(vtkCylinder::Content *content)
: m_Content(content), m_Actor(nullptr), m_VtkAsm(nullptr) {
this->InstallPipe();
Object::connect(m_Content, &Content::Updated, this, &vtkCylinder::contentUpdate);
Object::connect(m_Content, &uLib::Object::Updated, this, &vtkCylinder::Update);
}
vtkCylinder::~vtkCylinder() {
@@ -48,59 +48,56 @@ vtkCylinder::~vtkCylinder() {
if (m_VtkAsm) m_VtkAsm->Delete();
}
void vtkCylinder::contentUpdate() {
void vtkCylinder::Update() {
if (!m_Content)
return;
vtkProp3D* root = vtkProp3D::SafeDownCast(this->GetProp());
if (!root) return;
if (root) {
// 1. Placement handled specifically from content (use TRS GetMatrix, not World)
vtkNew<vtkMatrix4x4> m;
Matrix4fToVtk(m_Content->GetMatrix(), m);
root->SetUserMatrix(m);
// 1. Placement handled by base Puppet class via Sync / Update logic
// Update internal pd->m_Transform from content
Puppet::Update();
// 2. Shape-local properties (Radius, Height, Axis alignment) go to the internal actor
// These are relative to the root assembly
vtkTransform* alignment = vtkTransform::SafeDownCast(m_Actor->GetUserTransform());
if (alignment) {
alignment->Identity();
alignment->PostMultiply();
// Initial source is centered Y-cylinder (Radial XZ [-1,1], Height Y [-0.5, 0.5])
// Apply Radius and Height scaling
alignment->Scale(m_Content->GetRadius(), m_Content->GetHeight(), m_Content->GetRadius());
// 2. Shape-local properties (Radius, Height, Axis alignment) go to the internal actor
// These are relative to the root assembly
vtkTransform* alignment = vtkTransform::SafeDownCast(m_Actor->GetUserTransform());
if (alignment) {
alignment->Identity();
alignment->PostMultiply();
// Initial source is centered Y-cylinder (Radial XZ [-1,1], Height Y [-0.5, 0.5])
// Apply Radius and Height scaling
alignment->Scale(m_Content->GetRadius(), m_Content->GetHeight(), m_Content->GetRadius());
// Apply Axis alignment
int axis = m_Content->GetAxis();
if (axis == 0) alignment->RotateZ(-90); // Y -> X
else if (axis == 1) ; // Y -> Y (identity)
else if (axis == 2) alignment->RotateX(90); // Y -> Z
}
// Apply Axis alignment
int axis = m_Content->GetAxis();
if (axis == 0) alignment->RotateZ(-90); // Y -> X
else if (axis == 1) ; // Y -> Y (identity)
else if (axis == 2) alignment->RotateX(90); // Y -> Z
root->Modified();
}
root->Modified();
}
void vtkCylinder::Update() {
this->contentUpdate();
// Use base class sync, which handles appearance and children
this->Puppet::Update();
}
void vtkCylinder::SyncFromVtk() {
if (!m_Content) return;
vtkProp3D* assembly = vtkProp3D::SafeDownCast(this->GetProp());
vtkProp3D* assembly = this->GetProxyProp();
if (!assembly) return;
double pos[3], ori[3], scale[3];
assembly->GetPosition(pos);
assembly->GetOrientation(ori);
assembly->GetScale(scale);
// VTK -> Model: Update TRS properties from VTK matrix via world transform
m_Content->SetWorldMatrix(VtkToMatrix4f(assembly->GetUserMatrix()));
m_Content->SetPosition(Vector3f(pos[0], pos[1], pos[2]));
// Convert VTK degrees to model radians
m_Content->SetOrientation(Vector3f(ori[0], ori[1], ori[2]) * CLHEP::degree);
m_Content->SetScale(Vector3f(scale[0], scale[1], scale[2]));
// Resync TRS property members (pos/rot/scale) from the newly set local matrix
m_Content->FromMatrix(m_Content->GetMatrix());
m_Content->Updated(); // Notify change
// Since we modified the model, notify observers, but block the loop back to VTK
m_Content->Updated();
}
void vtkCylinder::InstallPipe() {
@@ -127,7 +124,7 @@ void vtkCylinder::InstallPipe() {
m_VtkAsm->AddPart(m_Actor);
this->contentUpdate();
this->Update();
}
} // namespace Vtk