add detector chamber projection plane representation in vtk

This commit is contained in:
AndreaRigoni
2026-03-19 10:21:01 +00:00
parent c265adadfc
commit 12657167f1
3 changed files with 79 additions and 2 deletions

View File

@@ -43,8 +43,8 @@ BOOST_AUTO_TEST_CASE(vtkDetectorChamberTest) {
// d2.SetSize(Vector3f(1, 1, 1));
// d2.SetPosition(Vector3f(0, 0, 0));
d2.Scale(Vector3f(1000, 2000, 200));
d2.Translate(Vector3f(0, 0, 10));
d2.Scale(Vector3f(1_m, 2_m, 20_cm));
d2.Translate(Vector3f(0, 0, 10_m));
Vtk::vtkDetectorChamber v_d1(&d1);

View File

@@ -41,20 +41,89 @@
#include "Vtk/HEP/Detectors/vtkDetectorChamber.h"
#include <vtkBoxWidget.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkPlaneSource.h>
#include <vtkProperty.h>
#include <vtkNew.h>
namespace uLib {
namespace Vtk {
vtkDetectorChamber::vtkDetectorChamber(DetectorChamber *content)
: vtkContainerBox(content) {
m_PlaneSource = vtkPlaneSource::New();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(m_PlaneSource->GetOutputPort());
m_PlaneActor = vtkActor::New();
m_PlaneActor->SetMapper(mapper);
m_PlaneActor->GetProperty()->SetColor(0.2, 0.8, 0.2); // Light green
m_PlaneActor->GetProperty()->SetOpacity(0.3);
m_PlaneActor->GetProperty()->SetAmbient(1.0);
m_PlaneActor->GetProperty()->SetDiffuse(0.0);
this->SetProp(m_PlaneActor);
this->contentUpdate();
}
vtkDetectorChamber::~vtkDetectorChamber() {
m_PlaneSource->Delete();
m_PlaneActor->Delete();
}
DetectorChamber *vtkDetectorChamber::GetContent() {
return static_cast<DetectorChamber *>(m_Content);
}
void vtkDetectorChamber::contentUpdate() {
this->BaseClass::contentUpdate();
if (!m_Content) return;
DetectorChamber *c = this->GetContent();
Vector3f size = c->GetSize();
HLine3f plane = c->GetProjectionPlane();
// Normalized local space of the chamber
float Lx = plane.origin.x() / size.x();
float Ly = plane.origin.y() / size.y();
float Lz = plane.origin.z() / size.z();
float Dx = plane.direction.x() * size.x();
float Dy = plane.direction.y() * size.y();
float Dz = plane.direction.z() * size.z();
Vector3f normal(Dx, Dy, Dz);
normal.normalize();
// Find spans
Vector3f v1;
if (std::abs(normal.z()) < 0.9) v1 = Vector3f(0,0,1);
else v1 = Vector3f(1,0,0);
Vector3f p1 = normal.cross(v1).normalized();
Vector3f p2 = normal.cross(p1).normalized();
// Center the visual representation on the box extents (unit box [0,1]^3)
// instead of the plane origin (which might be a corner).
Vector3f boxCenter(0.5f, 0.5f, 0.5f);
Vector3f planePt(Lx, Ly, Lz);
Vector3f visualCenter = boxCenter - (boxCenter - planePt).dot(normal) * normal;
float scale = 1.5; // Slightly larger than the diagonal of a face (1.41)
m_PlaneSource->SetOrigin(visualCenter.x() - 0.5f*scale*p1.x() - 0.5f*scale*p2.x(),
visualCenter.y() - 0.5f*scale*p1.y() - 0.5f*scale*p2.y(),
visualCenter.z() - 0.5f*scale*p1.z() - 0.5f*scale*p2.z());
m_PlaneSource->SetPoint1(visualCenter.x() + 0.5f*scale*p1.x() - 0.5f*scale*p2.x(),
visualCenter.y() + 0.5f*scale*p1.y() - 0.5f*scale*p2.y(),
visualCenter.z() + 0.5f*scale*p1.z() - 0.5f*scale*p2.z());
m_PlaneSource->SetPoint2(visualCenter.x() - 0.5f*scale*p1.x() + 0.5f*scale*p2.x(),
visualCenter.y() - 0.5f*scale*p1.y() + 0.5f*scale*p2.y(),
visualCenter.z() - 0.5f*scale*p1.z() + 0.5f*scale*p2.z());
}
} // namespace Vtk
} // namespace uLib

View File

@@ -39,6 +39,8 @@
#include <vtkBoxWidget.h>
#include <vtkTransformPolyDataFilter.h>
class vtkPlaneSource;
namespace uLib {
namespace Vtk {
@@ -53,6 +55,12 @@ public:
virtual ~vtkDetectorChamber();
Content *GetContent();
virtual void contentUpdate() override;
protected:
vtkActor *m_PlaneActor;
vtkPlaneSource *m_PlaneSource;
};
} // namespace Vtk