refactor: Simplify vtkDetectorChamber by removing redundant transform management and improve vtkHandlerWidget rotation calculation using ray-plane intersection.

This commit is contained in:
AndreaRigoni
2026-03-18 23:08:22 +00:00
parent a9b66a4e12
commit 3b02bb26ac
7 changed files with 142 additions and 184 deletions

View File

@@ -46,100 +46,15 @@ namespace uLib {
namespace Vtk {
vtkDetectorChamber::vtkDetectorChamber(DetectorChamber *content)
: vtkContainerBox(content), m_Actor(vtkActor::New()) {
m_InitialTransform = vtkSmartPointer<vtkTransform>::New();
m_RelativeTransform = vtkSmartPointer<vtkTransform>::New();
m_TotalTransform = vtkSmartPointer<vtkTransform>::New();
this->InstallPipe();
: vtkContainerBox(content) {
}
vtkDetectorChamber::~vtkDetectorChamber() {
m_Actor->Delete();
}
DetectorChamber *vtkDetectorChamber::GetContent() {
return static_cast<DetectorChamber *>(m_Content);
}
void vtkDetectorChamber::PrintSelf(std::ostream &o) const {
vtkContainerBox::PrintSelf(o);
}
void vtkDetectorChamber::SetTransform(vtkTransform *t) {
if (!t) return;
m_RelativeTransform->SetMatrix(t->GetMatrix());
m_RelativeTransform->Update();
// Set content global transform (BaseClass of ContainerBox) //
vtkMatrix4x4 *vmat = m_TotalTransform->GetMatrix();
Matrix4f transform;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
transform(i, j) = vmat->GetElement(i, j);
this->GetContent()->SetMatrix(transform);
this->GetContent()->Updated(); // emit signal
this->Update();
}
void vtkDetectorChamber::Update() {
if (m_Actor->GetMapper())
m_Actor->GetMapper()->Update();
// If the actor has a UserMatrix, we update the content.
if (m_Actor->GetUserMatrix()) {
vtkMatrix4x4* vmat = m_Actor->GetUserMatrix();
Matrix4f transform;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
transform(i, j) = vmat->GetElement(i, j);
this->GetContent()->SetMatrix(transform);
this->GetContent()->Updated();
}
BaseClass::Update();
}
void vtkDetectorChamber::InstallPipe() {
if (!m_Content)
return;
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
cube->SetBounds(0, 1, 0, 1, 0, 1);
// 1. Initialize Global Transform (m_Transform) from Content's matrix (Base
// class AffineTransform)
vtkSmartPointer<vtkMatrix4x4> vmatGlobal =
vtkSmartPointer<vtkMatrix4x4>::New();
Matrix4f matGlobal = this->GetContent()->GetMatrix();
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
vmatGlobal->SetElement(i, j, matGlobal(i, j));
m_InitialTransform->SetMatrix(vmatGlobal);
m_InitialTransform->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cube->GetOutputPort());
m_Actor->SetMapper(mapper);
m_Actor->GetProperty()->SetRepresentationToSurface();
m_Actor->GetProperty()->SetEdgeVisibility(true);
m_Actor->GetProperty()->SetOpacity(0.4);
m_Actor->GetProperty()->SetAmbient(0.7);
m_TotalTransform->SetInput(m_RelativeTransform);
m_TotalTransform->Concatenate(m_InitialTransform);
m_Actor->SetUserTransform(m_TotalTransform);
m_TotalTransform->Update();
this->SetProp(m_Actor);
this->Update();
}
} // namespace Vtk
} // namespace uLib