From f2133c31d579f2fb3634af47e6ccbcc23c02c500 Mon Sep 17 00:00:00 2001 From: AndreaRigoni Date: Tue, 10 Mar 2026 08:18:17 +0000 Subject: [PATCH] DetectorChamber vtk handler --- src/Math/ContainerBox.h | 98 +++++++++++- src/Math/Geometry.h | 4 +- src/Math/Transform.h | 20 +-- src/Math/testing/ContainerBoxTest.cpp | 92 ++++++++--- src/Python/math_bindings.cpp | 11 +- src/Vtk/HEP/Detectors/testing/CMakeLists.txt | 1 + .../testing/vtkDetectorChamberTest.cpp | 11 +- src/Vtk/HEP/Detectors/vtkDetectorChamber.cxx | 143 +++++++++--------- src/Vtk/HEP/Detectors/vtkDetectorChamber.h | 17 ++- src/Vtk/Vtk.am | 93 ------------ src/Vtk/uLibVtkInterface.h | 65 +++----- src/Vtk/uLibVtkViewer.cpp | 1 + src/Vtk/vtkContainerBox.cpp | 70 +++++++-- src/Vtk/vtkContainerBox.h | 4 + 14 files changed, 358 insertions(+), 272 deletions(-) delete mode 100644 src/Vtk/Vtk.am diff --git a/src/Math/ContainerBox.h b/src/Math/ContainerBox.h index 12bd4cf..04a7a89 100644 --- a/src/Math/ContainerBox.h +++ b/src/Math/ContainerBox.h @@ -29,63 +29,147 @@ #define U_CONTAINERBOX_H #include "Geometry.h" +#include "Math/Dense.h" +#include "Math/Transform.h" +#include namespace uLib { +/** + * @brief Represents an oriented bounding box (OBB) within a hierarchical transformation system. + * + * ContainerBox inherits from AffineTransform, which defines its parent coordinate system. + * It contains an internal local transformation (m_LocalT) that defines the box's + * specific origin and size relative to its own coordinate system. + */ class ContainerBox : public AffineTransform { -public: - ContainerBox() : m_LocalT(this) {} + typedef AffineTransform BaseClass; + +public: + /** + * @brief Default constructor. + * Initializes the local transformation with this instance as its parent. + */ + ContainerBox() : + m_LocalT(this) // BaseClass is Parent of m_LocalTransform + {} + + /** + * @brief Copy constructor. + * @param copy The ContainerBox instance to copy from. + */ ContainerBox(const ContainerBox ©) : - m_LocalT(this), + m_LocalT(this), // BaseClass is Parent of m_LocalTransform AffineTransform(copy) { - // FIX for performance // this->SetOrigin(copy.GetOrigin()); this->SetSize(copy.GetSize()); } + /** + * @brief Sets the box origin relative to its coordinate system. + * @param v The origin position vector. + */ inline void SetOrigin(const Vector3f &v) { m_LocalT.SetPosition(v); } + /** + * @brief Gets the box origin relative to its coordinate system. + * @return The origin position vector. + */ inline Vector3f GetOrigin() const { return m_LocalT.GetPosition(); } + /** + * @brief Sets the size of the box. + * Re-initializes the local transformation and applies the new scale. + * @param v The size vector (width, height, depth). + */ void SetSize(const Vector3f &v) { Vector3f pos = this->GetOrigin(); - m_LocalT = AffineTransform(this); + m_LocalT = AffineTransform(this); // regenerate local transform m_LocalT.Scale(v); m_LocalT.SetPosition(pos); } + /** + * @brief Gets the current size (scale) of the box. + * @return The size vector. + */ inline Vector3f GetSize() const { return m_LocalT.GetScale(); } - // FIX... // + /** + * @brief Swaps two local axes of the box. + * @param first Index of the first axis (0=X, 1=Y, 2=Z). + * @param second Index of the second axis (0=X, 1=Y, 2=Z). + */ inline void FlipLocalAxes(int first, int second) { m_LocalT.FlipAxes(first,second); } + /** + * @brief Returns the world transformation matrix of the box's volume. + * @return A 4x4 transformation matrix. + */ Matrix4f GetWorldMatrix() const { return m_LocalT.GetWorldMatrix(); } + /** + * @brief Returns the local transformation matrix of the box's volume. + * @return A 4x4 transformation matrix. + */ + Matrix4f GetLocalMatrix() const { return m_LocalT.GetMatrix(); } + + /** + * @brief Transforms a point from box-local space to world space. + * @param v The local point (4D homogeneous vector). + * @return The transformed point in world space. + */ inline Vector4f GetWorldPoint(const Vector4f &v) const { return m_LocalT.GetWorldMatrix() * v; } + /** + * @brief Transforms a point from box-local space coordinates to world space. + * @param x X coordinate in local space. + * @param y Y coordinate in local space. + * @param z Z coordinate in local space. + * @return The transformed point in world space. + */ inline Vector4f GetWorldPoint(const float x, const float y, const float z) { return this->GetWorldPoint(Vector4f(x,y,z,1)); } + /** + * @brief Transforms a point from world space to box-local space. + * @param v The world point (4D homogeneous vector). + * @return The transformed point in box-local space. + */ inline Vector4f GetLocalPoint(const Vector4f &v) const { return m_LocalT.GetWorldMatrix().inverse() * v; } + /** + * @brief Transforms a point from world space coordinates to box-local space. + * @param x X coordinate in world space. + * @param y Y coordinate in world space. + * @param z Z coordinate in world space. + * @return The transformed point in box-local space. + */ inline Vector4f GetLocalPoint(const float x, const float y, const float z) { return this->GetLocalPoint(Vector4f(x,y,z,1)); } + /** Translate using transformation chain */ + using BaseClass::Translate; -protected: + /** Rotate using transformation chain */ + using BaseClass::Rotate; + /** Scale using transformation chain */ + using BaseClass::Scale; + + private: AffineTransform m_LocalT; }; diff --git a/src/Math/Geometry.h b/src/Math/Geometry.h index 5c78cf6..3d1ff70 100644 --- a/src/Math/Geometry.h +++ b/src/Math/Geometry.h @@ -36,7 +36,7 @@ namespace uLib { class Geometry : public AffineTransform { public: - inline Vector4f GetWorldPoint(const Vector4f &v) const { + inline Vector4f GetWorldPoint(const Vector4f v) const { return this->GetWorldMatrix() * v; } @@ -44,7 +44,7 @@ public: return this->GetWorldPoint(Vector4f(x,y,z,1)); } - inline Vector4f GetLocalPoint(const Vector4f &v) const { + inline Vector4f GetLocalPoint(const Vector4f v) const { return this->GetWorldMatrix().inverse() * v; } diff --git a/src/Math/Transform.h b/src/Math/Transform.h index 8b34d38..9cca0df 100644 --- a/src/Math/Transform.h +++ b/src/Math/Transform.h @@ -84,8 +84,8 @@ public: inline void SetParent(AffineTransform *name) { this->m_Parent = name; } - inline void SetMatrix (Matrix4f &mat) { m_T.matrix() = mat; } - inline Matrix4f& GetMatrix () { return m_T.matrix(); } + inline void SetMatrix (Matrix4f mat) { m_T.matrix() = mat; } + inline Matrix4f GetMatrix() const { return m_T.matrix(); } Matrix4f GetWorldMatrix() const { @@ -93,22 +93,22 @@ public: else return m_Parent->GetWorldMatrix() * m_T.matrix(); // T = B * A // } - inline void SetPosition(const Vector3f &v) { this->m_T.translation() = v; } + inline void SetPosition(const Vector3f v) { this->m_T.translation() = v; } inline Vector3f GetPosition() const { return this->m_T.translation(); } - inline void SetRotation(const Matrix3f &m) { this->m_T.linear() = m; } + inline void SetRotation(const Matrix3f m) { this->m_T.linear() = m; } inline Matrix3f GetRotation() const { return this->m_T.rotation(); } - inline void Translate(const Vector3f &v) { this->m_T.translate(v); } + inline void Translate(const Vector3f v) { this->m_T.translate(v); } - inline void Scale(const Vector3f &v) { this->m_T.scale(v); } + inline void Scale(const Vector3f v) { this->m_T.scale(v); } inline Vector3f GetScale() const { return this->m_T.linear() * Vector3f(1,1,1); } // FIXXXXXXX - inline void Rotate(const Matrix3f &m) { this->m_T.rotate(m); } + inline void Rotate(const Matrix3f m) { this->m_T.rotate(m); } inline void Rotate(const float angle, Vector3f axis) { @@ -122,12 +122,12 @@ public: Rotate(angle,euler_axis); } - inline void PreRotate(const Matrix3f &m) { this->m_T.prerotate(m); } + inline void PreRotate(const Matrix3f m) { this->m_T.prerotate(m); } - inline void QuaternionRotate(const Vector4f &q) + inline void QuaternionRotate(const Vector4f q) { this->m_T.rotate(Eigen::Quaternion(q)); } - inline void EulerYZYRotate(const Vector3f &e) { + inline void EulerYZYRotate(const Vector3f e) { Matrix3f mat; mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY()) * Eigen::AngleAxisf(e.y(), Vector3f::UnitZ()) diff --git a/src/Math/testing/ContainerBoxTest.cpp b/src/Math/testing/ContainerBoxTest.cpp index 98fb27e..2c0b391 100644 --- a/src/Math/testing/ContainerBoxTest.cpp +++ b/src/Math/testing/ContainerBoxTest.cpp @@ -31,6 +31,7 @@ #include "Math/Dense.h" #include "Math/ContainerBox.h" +#include #include #include @@ -52,41 +53,82 @@ int main() BEGIN_TESTING(Math ContainerBox); - ContainerBox Cnt; - -// // Local transform: - Cnt.SetOrigin(Vector3f(-1,-1,-1)); - Cnt.SetSize(Vector3f(2,2,2)); // scaling // - std::cout << "Container scale is: " << Cnt.GetSize().transpose() << "\n"; - std::cout << "Container scale is: " << Cnt.GetSize().transpose() << "\n"; - TEST0( Vector4f0(Cnt.GetSize().homogeneous() - HVector3f(2,2,2)) ); + { + ContainerBox Cnt; + Cnt.SetOrigin(Vector3f(0,0,0)); + Cnt.SetSize(Vector3f(2,2,2)); + TEST0( Vector4f0(Cnt.GetOrigin().homogeneous() - HVector3f(0,0,0)) ); + TEST0( Vector4f0(Cnt.GetSize().homogeneous() - HVector3f(2,2,2)) ); - ContainerBox Box; + HPoint3f pt = Cnt.GetLocalPoint(HPoint3f(0,0,0)); + HPoint3f wp = Cnt.GetWorldPoint(pt); + TEST0( Vector4f0(wp - HPoint3f(0,0,0)) ); - Box.SetPosition(Vector3f(1,1,1)); - Box.SetSize(Vector3f(2,2,2)); - Box.EulerYZYRotate(Vector3f(0,0,0)); - HPoint3f pt = Box.GetLocalPoint(HPoint3f(2,3,2)); - HPoint3f wp = Box.GetWorldPoint(pt); - TEST0( Vector4f0(wp - HPoint3f(2,3,2)) ); + HPoint3f pt2 = Cnt.GetLocalPoint(HPoint3f(2,2,2)); + HPoint3f wp2 = Cnt.GetWorldPoint(pt2); + TEST0( Vector4f0(wp2 - HPoint3f(2,2,2)) ); + HPoint3f pt3 = Cnt.GetLocalPoint(HPoint3f(1,1,1)); + HPoint3f wp3 = Cnt.GetWorldPoint(pt3); + TEST0( Vector4f0(wp3 - HPoint3f(1,1,1)) ); -//// // Global -// Cnt.SetPosition(Vector3f(1,1,1)); -// Cnt.EulerYZYRotate(Vector3f(M_PI_2,M_PI_2,0)); -// HPoint3f p = Cnt.GetWorldPoint(1,1,1); -// //std::cout << p.transpose() << "\n"; -// TEST0( Vector4f0(p - HVector3f(2,1,2)) ); -// p = Cnt.GetWorldPoint(1,2,3); -// //std::cout << p.transpose() << "\n"; -// TEST0( Vector4f0(p - HVector3f(4,1,3)) ); + HPoint3f pt4 = Cnt.GetLocalPoint(HPoint3f(1,2,3)); + HPoint3f wp4 = Cnt.GetWorldPoint(pt4); + TEST0( Vector4f0(wp4 - HPoint3f(1,2,3)) ); + } + { + ContainerBox Cnt; + Cnt.SetOrigin(Vector3f(0,0,0)); + Cnt.SetSize(Vector3f(2,2,2)); + Cnt.EulerYZYRotate(Vector3f(M_PI,0,0)); + + HPoint3f pt = Cnt.GetLocalPoint(HPoint3f(0,0,0)); + HPoint3f wp = Cnt.GetWorldPoint(pt); + TEST0( Vector4f0(wp - HPoint3f(0,0,0)) ); -// // scaling // + HPoint3f pt2 = Cnt.GetLocalPoint(HPoint3f(2,2,2)); + HPoint3f wp2 = Cnt.GetWorldPoint(pt2); + TEST0( Vector4f0(wp2 - HPoint3f(2,2,2)) ); + pt2 = HPoint3f(1,1,1); + wp2 = Cnt.GetWorldPoint(pt2); + TEST0( Vector4f0(wp2 - HPoint3f(-2,2,-2)) ); + pt2 = HPoint3f(1,2,3); + wp2 = Cnt.GetWorldPoint(pt2); + TEST0( Vector4f0(wp2 - HPoint3f(-2,4,-6)) ); + } + { + ContainerBox Cnt; + Cnt.SetOrigin(Vector3f(-1,-1,-1)); + Cnt.SetSize(Vector3f(2,2,2)); // scaling // + + HPoint3f pt2 = HPoint3f(.5,.5,.5); + HPoint3f wp2 = Cnt.GetWorldPoint(pt2); + TEST0( Vector4f0(wp2 - HPoint3f(0,0,0)) ); + + pt2 = HPoint3f(0,0,0); + wp2 = Cnt.GetWorldPoint(pt2); + TEST0( Vector4f0(wp2 - HPoint3f(-1,-1,-1)) ); + + Cnt.EulerYZYRotate(Vector3f(M_PI,0,0)); + pt2 = HPoint3f(0,0,0); + wp2 = Cnt.GetWorldPoint(pt2); + TEST0( Vector4f0(wp2 - HPoint3f(1,-1,1)) ); + } + + { + ContainerBox Box; + Box.SetPosition(Vector3f(1,1,1)); + Box.SetSize(Vector3f(2,2,2)); + Box.EulerYZYRotate(Vector3f(0,0,0)); + HPoint3f pt = Box.GetLocalPoint(HPoint3f(2,3,2)); + HPoint3f wp = Box.GetWorldPoint(pt); + TEST0( Vector4f0(wp - HPoint3f(2,3,2)) ); + } END_TESTING; } diff --git a/src/Python/math_bindings.cpp b/src/Python/math_bindings.cpp index cdf2eed..746466b 100644 --- a/src/Python/math_bindings.cpp +++ b/src/Python/math_bindings.cpp @@ -243,15 +243,14 @@ void init_math(py::module_ &m) { .def("Scale", &AffineTransform::Scale) .def("SetRotation", &AffineTransform::SetRotation) .def("GetRotation", &AffineTransform::GetRotation) - .def("Rotate", py::overload_cast(&AffineTransform::Rotate)) - .def("Rotate", py::overload_cast(&AffineTransform::Rotate)) + .def("Rotate", &AffineTransform::Rotate) .def("EulerYZYRotate", &AffineTransform::EulerYZYRotate) .def("FlipAxes", &AffineTransform::FlipAxes); py::class_(m, "Geometry") .def(py::init<>()) - .def("GetWorldPoint", py::overload_cast(&Geometry::GetWorldPoint, py::const_)) - .def("GetLocalPoint", py::overload_cast(&Geometry::GetLocalPoint, py::const_)); + .def("GetWorldPoint", &Geometry::GetWorldPoint) + .def("GetLocalPoint", &Geometry::GetLocalPoint); py::class_(m, "ContainerBox") .def(py::init<>()) @@ -260,8 +259,8 @@ void init_math(py::module_ &m) { .def("SetSize", &ContainerBox::SetSize) .def("GetSize", &ContainerBox::GetSize) .def("GetWorldMatrix", &ContainerBox::GetWorldMatrix) - .def("GetWorldPoint", py::overload_cast(&ContainerBox::GetWorldPoint, py::const_)) - .def("GetLocalPoint", py::overload_cast(&ContainerBox::GetLocalPoint, py::const_)); + .def("GetWorldPoint", &ContainerBox::GetWorldPoint) + .def("GetLocalPoint", &ContainerBox::GetLocalPoint); py::enum_(m, "StructuredDataOrder") .value("CustomOrder", StructuredData::CustomOrder) diff --git a/src/Vtk/HEP/Detectors/testing/CMakeLists.txt b/src/Vtk/HEP/Detectors/testing/CMakeLists.txt index 6e49e62..8364108 100644 --- a/src/Vtk/HEP/Detectors/testing/CMakeLists.txt +++ b/src/Vtk/HEP/Detectors/testing/CMakeLists.txt @@ -13,3 +13,4 @@ set(LIBRARIES ) uLib_add_tests(VtkDetectors) + diff --git a/src/Vtk/HEP/Detectors/testing/vtkDetectorChamberTest.cpp b/src/Vtk/HEP/Detectors/testing/vtkDetectorChamberTest.cpp index cb5da5c..0573cc5 100644 --- a/src/Vtk/HEP/Detectors/testing/vtkDetectorChamberTest.cpp +++ b/src/Vtk/HEP/Detectors/testing/vtkDetectorChamberTest.cpp @@ -33,11 +33,16 @@ BOOST_AUTO_TEST_CASE(vtkDetectorChamberTest) { uLib::DetectorChamber d1, d2; - d1.SetSize(uLib::Vector3f(10, 20, 2)); + d1.SetSize(uLib::Vector3f(1, 1, 1)); d1.SetPosition(uLib::Vector3f(0, 0, 0)); + d1.Scale(uLib::Vector3f(5, 10, 2)); + d1.Translate(uLib::Vector3f(0, 0, 0)); + + d2.SetSize(uLib::Vector3f(1, 1, 1)); + d2.SetPosition(uLib::Vector3f(0, 0, 0)); + d2.Scale(uLib::Vector3f(5, 10, 2)); + d2.Translate(uLib::Vector3f(0, 0, 10)); - d2.SetSize(uLib::Vector3f(10, 20, 2)); - d2.SetPosition(uLib::Vector3f(0, 0, 20)); uLib::Vtk::vtkDetectorChamber vtkDetectorChamber(&d1); uLib::Vtk::vtkDetectorChamber vtkDetectorChamber2(&d2); diff --git a/src/Vtk/HEP/Detectors/vtkDetectorChamber.cxx b/src/Vtk/HEP/Detectors/vtkDetectorChamber.cxx index a75d09c..86fa751 100644 --- a/src/Vtk/HEP/Detectors/vtkDetectorChamber.cxx +++ b/src/Vtk/HEP/Detectors/vtkDetectorChamber.cxx @@ -23,6 +23,7 @@ //////////////////////////////////////////////////////////////////////////////*/ +#include #include #include #include @@ -38,23 +39,31 @@ #include #include "Vtk/HEP/Detectors/vtkDetectorChamber.h" -#include "Vtk/vtkHandlerWidget.h" +#include +#include namespace uLib { namespace Vtk { vtkDetectorChamber::vtkDetectorChamber(DetectorChamber *content) - : vtkContainerBox(content) { - m_Widget = vtkHandlerWidget::New(); + : vtkContainerBox(content), m_Actor(vtkActor::New()), + m_Widget(vtkBoxWidget::New()) { m_Callback = vtkWidgetCallback::New(); m_PickerCallback = vtkSelectionCallback::New(); m_Callback->SetChamber(this); m_PickerCallback->SetChamber(this); m_Widget->AddObserver(vtkCommand::InteractionEvent, m_Callback); + + m_InitialTransform = vtkSmartPointer::New(); + m_RelativeTransform = vtkSmartPointer::New(); + m_TotalTransform = vtkSmartPointer::New(); + + this->InstallPipe(); } vtkDetectorChamber::~vtkDetectorChamber() { + m_Actor->Delete(); m_Widget->Delete(); m_Callback->Delete(); m_PickerCallback->Delete(); @@ -68,94 +77,97 @@ void vtkDetectorChamber::PrintSelf(std::ostream &o) const { vtkContainerBox::PrintSelf(o); } +/** + * Connect the interactor to the widget + */ void vtkDetectorChamber::ConnectInteractor( vtkRenderWindowInteractor *interactor) { if (!interactor) return; m_Widget->SetInteractor(interactor); - m_Widget->SetProp3D(m_Cube); - m_Widget->PlaceWidget(); + m_Widget->SetProp3D(m_Actor); interactor->AddObserver(vtkCommand::LeftButtonPressEvent, m_PickerCallback); } -void vtkDetectorChamber::Update() { - if (!m_Content) - return; - - Matrix4f mat = static_cast(m_Content)->GetWorldMatrix(); - vtkSmartPointer vmat = vtkSmartPointer::New(); +void vtkDetectorChamber::SetTransform(vtkTransform *t) { + + + 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) - vmat->SetElement(i, j, mat(i, j)); + transform(i, j) = vmat->GetElement(i, j); + this->GetContent()->SetMatrix(transform); - m_Cube->SetUserMatrix(vmat); - m_Axes->SetUserMatrix(vmat); - m_Pivot->SetUserMatrix(vmat); + this->Update(); +} + +vtkBoxWidget *vtkDetectorChamber::GetWidget() { return m_Widget; } + +void vtkDetectorChamber::Update() { + if (m_Actor->GetMapper()) + m_Actor->GetMapper()->Update(); + BaseClass::Update(); } void vtkDetectorChamber::InstallPipe() { if (!m_Content) return; - // Cube: unit box (0,0,0) -> (1,1,1) vtkSmartPointer cube = vtkSmartPointer::New(); cube->SetBounds(0, 1, 0, 1, 0, 1); + + // 1. Initialize Global Transform (m_Transform) from Content's matrix (Base + // class AffineTransform) + vtkSmartPointer vmatGlobal = + vtkSmartPointer::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 mapper = vtkSmartPointer::New(); mapper->SetInputConnection(cube->GetOutputPort()); - m_Cube->SetMapper(mapper); - m_Cube->GetProperty()->SetRepresentationToWireframe(); - m_Cube->GetProperty()->SetAmbient(0.7); + m_Actor->SetMapper(mapper); - // Axes: corner origin - vtkSmartPointer axes = vtkSmartPointer::New(); - axes->SetOrigin(0, 0, 0); - axes->SetScaleFactor(0.2); - mapper = vtkSmartPointer::New(); - mapper->SetInputConnection(axes->GetOutputPort()); - m_Axes->SetMapper(mapper); - m_Axes->GetProperty()->SetLineWidth(3); - m_Axes->GetProperty()->SetAmbient(0.4); + m_Actor->GetProperty()->SetRepresentationToSurface(); + m_Actor->GetProperty()->SetEdgeVisibility(true); + m_Actor->GetProperty()->SetOpacity(0.4); + m_Actor->GetProperty()->SetAmbient(0.7); - // Pivot: center of unit box - vtkSmartPointer pivot = vtkSmartPointer::New(); - pivot->SetOrigin(0.5, 0.5, 0.5); - pivot->SetScaleFactor(0.1); - mapper = vtkSmartPointer::New(); - mapper->SetInputConnection(pivot->GetOutputPort()); - m_Pivot->SetMapper(mapper); - m_Pivot->GetProperty()->SetLineWidth(3); - m_Pivot->GetProperty()->SetAmbient(0.4); + // Temporarily disable UserTransform to place widget on local base + m_Widget->SetProp3D(m_Actor); - this->SetProp(m_Cube); - this->SetProp(m_Axes); - this->SetProp(m_Pivot); + m_TotalTransform->SetInput(m_RelativeTransform); + m_TotalTransform->Concatenate(m_InitialTransform); + m_Actor->SetUserTransform(m_TotalTransform); + m_TotalTransform->Update(); + m_Widget->PlaceWidget(); + m_Widget->SetPlaceFactor(2); + + this->SetProp(m_Actor); this->Update(); } void vtkDetectorChamber::vtkWidgetCallback::Execute(vtkObject *caller, unsigned long, void *) { - vtkHandlerWidget *widget = reinterpret_cast(caller); + vtkBoxWidget *widget = reinterpret_cast(caller); + + // Get the Relative transform from the widget // vtkSmartPointer t = vtkSmartPointer::New(); widget->GetTransform(t); + chamber->SetTransform(t); - vtkMatrix4x4 *vmat = t->GetMatrix(); - Matrix4f mat; - for (int i = 0; i < 4; ++i) - for (int j = 0; j < 4; ++j) - mat(i, j) = vmat->GetElement(i, j); - - chamber->GetContent()->SetMatrix(mat); - - // Reset internal matrix to identity to avoid double-transformation - vtkProp3D *prop = widget->GetProp3D(); - if (prop) { - prop->SetPosition(0, 0, 0); - prop->SetOrientation(0, 0, 0); - prop->SetScale(1, 1, 1); - } - + // Apply to both the content and the actor state // chamber->Update(); } @@ -170,22 +182,9 @@ void vtkDetectorChamber::vtkSelectionCallback::Execute(vtkObject *caller, interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()); vtkProp *picked = picker->GetViewProp(); - if (picked == chamber->m_Cube || picked == chamber->m_Axes || - picked == chamber->m_Pivot) { + if (picked == chamber->m_Actor) { if (!chamber->m_Widget->GetEnabled()) { - // Register current position/rotation/scale from content // - Matrix4f mat = chamber->GetContent()->GetWorldMatrix(); - vtkSmartPointer vmat = vtkSmartPointer::New(); - for (int i = 0; i < 4; ++i) - for (int j = 0; j < 4; ++j) - vmat->SetElement(i, j, mat(i, j)); - - vtkSmartPointer t = vtkSmartPointer::New(); - t->SetMatrix(vmat); - chamber->m_Widget->SetCurrentRenderer( - interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()); - chamber->m_Widget->SetTransform(t); - chamber->m_Widget->PlaceWidget(); + chamber->m_Widget->SetInteractor(interactor); chamber->m_Widget->On(); } } else { diff --git a/src/Vtk/HEP/Detectors/vtkDetectorChamber.h b/src/Vtk/HEP/Detectors/vtkDetectorChamber.h index 6446d9f..b7564cc 100644 --- a/src/Vtk/HEP/Detectors/vtkDetectorChamber.h +++ b/src/Vtk/HEP/Detectors/vtkDetectorChamber.h @@ -35,14 +35,18 @@ #include "Math/Dense.h" #include "Vtk/uLibVtkInterface.h" #include "Vtk/vtkContainerBox.h" +#include +#include +#include namespace uLib { namespace Vtk { -class vtkHandlerWidget; +// class vtkHandlerWidget; // Removed as we use vtkBoxWidget now class vtkDetectorChamber : public vtkContainerBox { typedef DetectorChamber Content; + typedef vtkContainerBox BaseClass; public: vtkDetectorChamber(DetectorChamber *content); @@ -50,6 +54,10 @@ public: Content *GetContent(); + void SetTransform(class vtkTransform *t); + + class vtkBoxWidget *GetWidget(); + void Update(); void ConnectInteractor(vtkRenderWindowInteractor *interactor) override; @@ -80,9 +88,14 @@ private: uLib::Vtk::vtkDetectorChamber *chamber; }; - vtkHandlerWidget *m_Widget; + vtkActor *m_Actor; + vtkBoxWidget *m_Widget; vtkWidgetCallback *m_Callback; vtkSelectionCallback *m_PickerCallback; + + vtkSmartPointer m_InitialTransform; + vtkSmartPointer m_RelativeTransform; + vtkSmartPointer m_TotalTransform; }; } // namespace Vtk diff --git a/src/Vtk/Vtk.am b/src/Vtk/Vtk.am deleted file mode 100644 index 27e030a..0000000 --- a/src/Vtk/Vtk.am +++ /dev/null @@ -1,93 +0,0 @@ - - -#if VTK_5_x -_VTK_LIBS = -lQVTK \ - -lLSDyna \ - -lMapReduceMPI \ - -lmpistubs \ - -lvtkalglib \ - -lvtkCharts \ - -lvtkCommon \ - -lvtkDICOMParser \ - -lvtkexoIIc \ - -lvtkFiltering \ - -lvtkftgl \ - -lvtkGenericFiltering \ - -lvtkGraphics \ - -lvtkHybrid \ - -lvtkImaging \ - -lvtkInfovis \ - -lvtkmetaio \ - -lvtkNetCDF_cxx \ - -lvtkNetCDF \ - -lvtkproj4 \ - -lvtkRendering \ - -lvtksqlite \ - -lvtksys \ - -lvtkverdict \ - -lvtkViews \ - -lvtkVolumeRendering \ - -lvtkWidgets - -AM_CFLAGS += $(VTK_CFLAGS) -AM_CXXFLAGS += $(VTK_CXXFLAGS) -AM_LDFLAGS += $(VTK_LDFLAGS) $(_VTK_LIBS) - -#else -#_VTK_LIBS = \ -# -lvtkRenderingOpenGL-6.0 \ -# -lvtkImagingHybrid-6.0 \ -# -lvtkIOImage-6.0 \ -# -lvtkCommonDataModel-6.0 \ -# -lvtkCommonMath-6.0 \ -# -lvtkCommonCore-6.0 \ -# -lvtksys-6.0 \ -# -lvtkCommonMisc-6.0 \ -# -lvtkCommonSystem-6.0 \ -# -lvtkCommonTransforms-6.0 \ -# -lvtkCommonExecutionModel-6.0 \ -# -lvtkDICOMParser-6.0 \ -# -lvtkIOCore-6.0 \ -# -lvtkzlib-6.0 \ -# -lvtkmetaio-6.0 \ -# -lvtkjpeg-6.0 \ -# -lvtkpng-6.0 \ -# -lvtktiff-6.0 \ -# -lvtkImagingCore-6.0 \ -# -lvtkRenderingCore-6.0 \ -# -lvtkFiltersExtraction-6.0 \ -# -lvtkFiltersCore-6.0 \ -# -lvtkFiltersGeneral-6.0 \ -# -lvtkCommonComputationalGeometry-6.0 \ -# -lvtkFiltersStatistics-6.0 \ -# -lvtkImagingFourier-6.0 \ -# -lvtkalglib-6.0 \ -# -lvtkFiltersGeometry-6.0 \ -# -lvtkFiltersSources-6.0 \ -# -lvtkIOXMLParser-6.0 \ -# -lvtkexpat-6.0 \ -# -lvtkRenderingAnnotation-6.0 \ -# -lvtkImagingColor-6.0 \ -# -lvtkRenderingFreeType-6.0 \ -# -lvtkfreetype-6.0 \ -# -lvtkftgl-6.0 \ -# -lvtkInteractionStyle-6.0 \ -# -lvtkRenderingVolumeOpenGL-6.0 \ -# -lvtkRenderingVolume-6.0 \ -# -lvtkIOXML-6.0 \ -# -lvtkIOGeometry-6.0 \ -# -lvtkjsoncpp-6.0 \ -# -lvtkInteractionWidgets-6.0 \ -# -lvtkFiltersHybrid-6.0 \ -# -lvtkImagingSources-6.0 \ -# -lvtkFiltersModeling-6.0 \ -# -lvtkImagingGeneral-6.0 \ -# -lvtkIOLegacy-6.0 \ -# -lvtkRenderingFreeTypeOpenGL-6.0 - - - -#AM_CFLAGS += $(VTK_CFLAGS) -#AM_CXXFLAGS += $(VTK_CXXFLAGS) -rdynamic -#AM_LDFLAGS += $(VTK_LDFLAGS) $(_VTK_LIBS) -lGLU -lSM -lICE -lX11 -lXext -lSM -lICE -lX11 -lXext -lXt -lm -ldl -lGL -#endif diff --git a/src/Vtk/uLibVtkInterface.h b/src/Vtk/uLibVtkInterface.h index 4dd9e00..ae6ff26 100644 --- a/src/Vtk/uLibVtkInterface.h +++ b/src/Vtk/uLibVtkInterface.h @@ -23,17 +23,11 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef ULIBVTKINTERFACE_H #define ULIBVTKINTERFACE_H - - -#include #include - - +#include // vtk classes forward declaration // class vtkProp; @@ -42,66 +36,57 @@ class vtkPropCollection; class vtkRenderer; class vtkRendererCollection; - namespace uLib { namespace Vtk { -class Puppet -{ +class Puppet { public: - Puppet(); - ~Puppet(); + Puppet(); + ~Puppet(); - virtual vtkProp * GetProp(); + virtual vtkProp *GetProp(); - virtual vtkPropCollection * GetProps(); + virtual vtkPropCollection *GetProps(); - void ConnectRenderer(vtkRenderer *renderer); + void ConnectRenderer(vtkRenderer *renderer); - void DisconnectRenderer(vtkRenderer *renderer); + void DisconnectRenderer(vtkRenderer *renderer); - void ConnectViewer(class Viewer *viewer); + void ConnectViewer(class Viewer *viewer); - void DisonnectViewer(class Viewer *viewer); + void DisonnectViewer(class Viewer *viewer); - vtkRendererCollection * GetRenderers() const; + vtkRendererCollection *GetRenderers() const; + virtual void PrintSelf(std::ostream &o) const; - virtual void PrintSelf(std::ostream &o) const; - - virtual void ConnectInteractor(class vtkRenderWindowInteractor *interactor) { (void)interactor; } + virtual void ConnectInteractor(class vtkRenderWindowInteractor *interactor) { + (void)interactor; + } protected: + void SetProp(vtkProp *prop); - void SetProp(vtkProp *prop); - - void RemoveProp(vtkProp *prop); + void RemoveProp(vtkProp *prop); private: - friend class PuppetData; - class PuppetData *d; + friend class PuppetData; + class PuppetData *d; }; - - - class Polydata { public: + virtual vtkPolyData *GetPolyData() const { return NULL; } - virtual vtkPolyData * GetPolyData() const { return NULL; } + virtual void SaveToFile(const char *vtk_file); - virtual void SaveToFile(const char *vtk_file); - - virtual void SaveToXMLFile(const char *vtp_file); + virtual void SaveToXMLFile(const char *vtp_file); protected: - virtual ~Polydata() {} + virtual ~Polydata() {} }; - -} // vtk -} // uLib - - +} // namespace Vtk +} // namespace uLib #endif // ULIBVTKINTERFACE_H diff --git a/src/Vtk/uLibVtkViewer.cpp b/src/Vtk/uLibVtkViewer.cpp index 847da36..24f6ee1 100644 --- a/src/Vtk/uLibVtkViewer.cpp +++ b/src/Vtk/uLibVtkViewer.cpp @@ -86,6 +86,7 @@ Viewer::~Viewer() { void Viewer::InstallPipe() { m_RenderWindow->AddRenderer(m_Renderer); + m_RenderWindow->SetSize(600,600); vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New(); renderWindowInteractor->SetRenderWindow(m_RenderWindow); diff --git a/src/Vtk/vtkContainerBox.cpp b/src/Vtk/vtkContainerBox.cpp index 6d11239..c3f2a90 100644 --- a/src/Vtk/vtkContainerBox.cpp +++ b/src/Vtk/vtkContainerBox.cpp @@ -27,23 +27,28 @@ #include "config.h" #endif +#include + +#include "vtkContainerBox.h" #include #include #include #include #include +#include #include #include #include +#include -#include "vtkContainerBox.h" namespace uLib { namespace Vtk { vtkContainerBox::vtkContainerBox(vtkContainerBox::Content *content) : m_Cube(vtkActor::New()), m_Axes(vtkActor::New()), - m_Pivot(vtkActor::New()), m_Content(content) { + m_Pivot(vtkActor::New()), + m_Content(content) { this->InstallPipe(); } @@ -58,6 +63,28 @@ vtkPolyData *vtkContainerBox::GetPolyData() const { return NULL; } +void vtkContainerBox::Update() { + if (!m_Content) + return; + vtkSmartPointer vmat = vtkSmartPointer::New(); + Matrix4f transform = m_Content->GetMatrix(); + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + vmat->SetElement(i, j, transform(i, j)); + + std::cout << "transform: " << transform << std::endl; + + // m_RelativeTransform->SetMatrix(vmat); + // m_RelativeTransform->Update(); + + m_Cube->SetUserMatrix(vmat); + m_Axes->SetUserMatrix(vmat); +} + + + + + void vtkContainerBox::InstallPipe() { if (!m_Content) return; @@ -65,17 +92,27 @@ void vtkContainerBox::InstallPipe() { // CUBE vtkSmartPointer cube = vtkSmartPointer::New(); + Vector3f p = c->GetPosition(); - cube->SetCenter(p(0), p(1), p(2)); - Vector4f p1 = c->GetWorldPoint(HPoint3f(0, 0, 0)); - Vector4f p2 = c->GetWorldPoint(HPoint3f(1, 1, 1)); - vtkSmartPointer line = vtkSmartPointer::New(); - line->SetPoint1(p1(0), p1(1), p1(2)); - line->SetPoint2(p2(0), p2(1), p2(2)); - line->Update(); - cube->SetBounds(line->GetOutput()->GetBounds()); + // cube->SetCenter(p(0), p(1), p(2)); + // Vector4f p1 = c->GetWorldPoint(HPoint3f(0, 0, 0)); + // Vector4f p2 = c->GetWorldPoint(HPoint3f(1, 1, 1)); + // vtkSmartPointer line = + // vtkSmartPointer::New(); line->SetPoint1(p1(0), p1(1), + // p1(2)); line->SetPoint2(p2(0), p2(1), p2(2)); line->Update(); + // cube->SetBounds(line->GetOutput()->GetBounds()); + + vtkSmartPointer vmat = vtkSmartPointer::New(); + Matrix4f transform = c->GetMatrix(); + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + vmat->SetElement(i, j, transform(i, j)); + + m_Cube->SetUserMatrix(vmat); + vtkSmartPointer mapper = vtkSmartPointer::New(); + cube->SetBounds(0, 1, 0, 1, 0, 1); mapper->SetInputConnection(cube->GetOutputPort()); mapper->Update(); m_Cube->SetMapper(mapper); @@ -84,11 +121,12 @@ void vtkContainerBox::InstallPipe() { // AXES // vtkSmartPointer axes = vtkSmartPointer::New(); - axes->SetOrigin(p1(0), p1(1), p1(2)); + axes->SetOrigin(0, 0, 0); mapper = vtkSmartPointer::New(); mapper->SetInputConnection(axes->GetOutputPort()); mapper->Update(); m_Axes->SetMapper(mapper); + m_Axes->SetUserMatrix(vmat); Vector3f s = c->GetSize(); // m_Axes->SetScale(s(0),s(1),s(2)); m_Axes->GetProperty()->SetLineWidth(3); @@ -97,11 +135,19 @@ void vtkContainerBox::InstallPipe() { // PIVOT // axes = vtkSmartPointer::New(); - axes->SetOrigin(p(0), p(1), p(2)); + axes->SetOrigin(0, 0, 0); mapper = vtkSmartPointer::New(); mapper->SetInputConnection(axes->GetOutputPort()); mapper->Update(); + m_Pivot->SetUserMatrix(vmat); m_Pivot->SetMapper(mapper); + + Matrix4f pivotTransform = c->AffineTransform::GetWorldMatrix(); + vtkSmartPointer pmat = vtkSmartPointer::New(); + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + pmat->SetElement(i, j, pivotTransform(i, j)); + m_Pivot->SetUserMatrix(pmat); s = c->GetScale(); // m_Pivot->SetScale(s(0),s(1),s(2)); m_Pivot->GetProperty()->SetLineWidth(3); diff --git a/src/Vtk/vtkContainerBox.h b/src/Vtk/vtkContainerBox.h index 23a3b0b..64dc701 100644 --- a/src/Vtk/vtkContainerBox.h +++ b/src/Vtk/vtkContainerBox.h @@ -28,6 +28,7 @@ #include "Math/ContainerBox.h" #include "uLibVtkInterface.h" +#include namespace uLib { namespace Vtk { @@ -41,6 +42,9 @@ public: virtual class vtkPolyData *GetPolyData() const; + virtual void Update(); + + protected: virtual void InstallPipe();