refactor: Simplify vtkDetectorChamber by removing redundant transform management and improve vtkHandlerWidget rotation calculation using ray-plane intersection.
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "Vtk/HEP/Detectors/vtkDetectorChamber.h"
|
||||
#include "HEP/Detectors/DetectorChamber.h"
|
||||
#include "Math/Units.h"
|
||||
|
||||
#include "Vtk/uLibVtkViewer.h"
|
||||
|
||||
@@ -35,29 +36,31 @@ using namespace uLib;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(vtkDetectorChamberTest) {
|
||||
DetectorChamber d1, d2;
|
||||
d1.SetSize(Vector3f(1, 1, 1));
|
||||
d1.SetPosition(Vector3f(0, 0, 0));
|
||||
d1.Scale(Vector3f(5, 10, 2));
|
||||
// d1.SetSize(Vector3f(1, 1, 1));
|
||||
// d1.SetPosition(Vector3f(0, 0, 0));
|
||||
d1.Scale(Vector3f(5_m, 10_m, 2_m));
|
||||
d1.Translate(Vector3f(0, 0, 0));
|
||||
|
||||
d2.SetSize(Vector3f(1, 1, 1));
|
||||
d2.SetPosition(Vector3f(0, 0, 0));
|
||||
d2.Scale(Vector3f(5, 10, 2));
|
||||
d2.Translate(Vector3f(0, 0, 10));
|
||||
// d2.SetSize(Vector3f(1, 1, 1));
|
||||
// d2.SetPosition(Vector3f(0, 0, 0));
|
||||
d2.Scale(Vector3f(5_m, 10_m, 2_m));
|
||||
d2.Translate(Vector3f(0, 0, 10_m));
|
||||
|
||||
|
||||
Vtk::vtkDetectorChamber vtkDetectorChamber(&d1);
|
||||
Vtk::vtkDetectorChamber vtkDetectorChamber2(&d2);
|
||||
Vtk::vtkDetectorChamber v_d1(&d1);
|
||||
Vtk::vtkDetectorChamber v_d2(&d2);
|
||||
v_d1.SetRepresentation(Vtk::Puppet::Surface);
|
||||
v_d2.SetRepresentation(Vtk::Puppet::Surface);
|
||||
|
||||
if (!vtkDetectorChamber.GetProp()) {
|
||||
if (!v_d1.GetProp()) {
|
||||
BOOST_FAIL("vtkDetectorChamber::GetProp() returned NULL");
|
||||
}
|
||||
|
||||
if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {
|
||||
Vtk::Viewer viewer;
|
||||
viewer.SetGridAxis(Vtk::Viewport::Y);
|
||||
viewer.AddPuppet(vtkDetectorChamber);
|
||||
viewer.AddPuppet(vtkDetectorChamber2);
|
||||
viewer.AddPuppet(v_d1);
|
||||
viewer.AddPuppet(v_d2);
|
||||
viewer.Start();
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -50,25 +50,9 @@ class vtkDetectorChamber : public vtkContainerBox {
|
||||
|
||||
public:
|
||||
vtkDetectorChamber(DetectorChamber *content);
|
||||
~vtkDetectorChamber();
|
||||
virtual ~vtkDetectorChamber();
|
||||
|
||||
Content *GetContent();
|
||||
|
||||
void SetTransform(class vtkTransform *t);
|
||||
|
||||
void Update() override;
|
||||
|
||||
void PrintSelf(std::ostream &o) const;
|
||||
|
||||
protected:
|
||||
void InstallPipe() override;
|
||||
|
||||
private:
|
||||
vtkActor *m_Actor;
|
||||
|
||||
vtkSmartPointer<vtkTransform> m_InitialTransform;
|
||||
vtkSmartPointer<vtkTransform> m_RelativeTransform;
|
||||
vtkSmartPointer<vtkTransform> m_TotalTransform;
|
||||
};
|
||||
|
||||
} // namespace Vtk
|
||||
|
||||
Reference in New Issue
Block a user