add QCanvas Root and viewport pane in gcompose

This commit is contained in:
AndreaRigoni
2026-03-21 15:41:58 +00:00
parent 033fb598c7
commit bbd7493d9f
19 changed files with 721 additions and 61 deletions

View File

@@ -157,7 +157,10 @@ else()
GUISupportQt)
endif()
find_package(Qt6 COMPONENTS Widgets REQUIRED)
find_package(Qt6 COMPONENTS Widgets)
if(Qt6_FOUND)
add_compile_definitions(HAVE_QT)
endif()
find_package(Geant4)
if(Geant4_FOUND)

View File

@@ -3,6 +3,10 @@ add_executable(gcompose
src/main.cpp
src/MainWindow.h
src/MainWindow.cpp
src/QViewportPane.h
src/QViewportPane.cpp
src/MainPanel.h
src/MainPanel.cpp
)
set_target_properties(gcompose PROPERTIES
@@ -29,6 +33,7 @@ target_link_libraries(gcompose
mutomMath
mutomGeant
mutomVtk
mutomRoot
${Geant4_LIBS_FILTERED}
${VTK_LIBRARIES}
Qt6::Widgets

View File

@@ -0,0 +1,49 @@
#include "MainPanel.h"
#include "QViewportPane.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSplitter>
#include <QLabel>
#include <QPushButton>
MainPanel::MainPanel(QWidget* parent) : QWidget(parent) {
auto* mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
// 1. Top Menu Panel
auto* menuPanel = new QWidget(this);
menuPanel->setFixedHeight(36);
menuPanel->setStyleSheet("background-color: #2b2b2b; border-bottom: 1px solid #111;");
auto* menuLayout = new QHBoxLayout(menuPanel);
menuLayout->setContentsMargins(10, 0, 10, 0);
menuLayout->setSpacing(15);
auto* logo = new QLabel("G-COMPOSE", menuPanel);
logo->setStyleSheet("font-weight: bold; color: #0078d7; font-size: 14px; letter-spacing: 1px;");
auto* btnOpen = new QPushButton("Open", menuPanel);
auto* btnSave = new QPushButton("Save", menuPanel);
QString btnStyle = "QPushButton { background: transparent; color: #ccc; border: none; padding: 5px 10px; }"
"QPushButton:hover { background: #3c3c3c; color: white; border-radius: 4px; }";
btnOpen->setStyleSheet(btnStyle);
btnSave->setStyleSheet(btnStyle);
menuLayout->addWidget(logo);
menuLayout->addWidget(btnOpen);
menuLayout->addWidget(btnSave);
menuLayout->addStretch();
mainLayout->addWidget(menuPanel);
// 2. Central Splitter Area
m_rootSplitter = new QSplitter(Qt::Horizontal, this);
m_firstPane = new QViewportPane(m_rootSplitter);
m_rootSplitter->addWidget(m_firstPane);
mainLayout->addWidget(m_rootSplitter, 1);
}
MainPanel::~MainPanel() {}

View File

@@ -0,0 +1,22 @@
#ifndef MAINPANEL_H
#define MAINPANEL_H
#include <QWidget>
class QSplitter;
class QViewportPane;
class MainPanel : public QWidget {
Q_OBJECT
public:
explicit MainPanel(QWidget* parent = nullptr);
virtual ~MainPanel();
QViewportPane* getFirstPane() const { return m_firstPane; }
private:
QSplitter* m_rootSplitter;
QViewportPane* m_firstPane;
};
#endif // MAINPANEL_H

View File

@@ -1,11 +1,12 @@
#include "MainWindow.h"
#include <Vtk/vtkQViewport.h>
#include <QSplitter>
#include "MainPanel.h"
using namespace uLib;
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
m_viewport = new Vtk::QViewport(this);
setCentralWidget(m_viewport);
m_panel = new MainPanel(this);
setCentralWidget(m_panel);
setWindowTitle("gcompose - Qt VTK Interface");
resize(1200, 800);

View File

@@ -4,9 +4,10 @@
#include <QMainWindow>
#include <QVTKOpenGLNativeWidget.h>
class MainPanel;
namespace uLib {
namespace Vtk {
class QViewport;
}
}
@@ -16,10 +17,10 @@ public:
MainWindow(QWidget* parent = nullptr);
virtual ~MainWindow();
uLib::Vtk::QViewport* getViewport() { return m_viewport; }
MainPanel* getPanel() { return m_panel; }
private:
uLib::Vtk::QViewport* m_viewport;
MainPanel* m_panel;
};
#endif

View File

@@ -0,0 +1,169 @@
#include "QViewportPane.h"
#include <Vtk/vtkQViewport.h>
#include <Root/QCanvas.h>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QToolButton>
#include <QMenu>
#include <QAction>
#include <QSplitter>
#include <vtkCamera.h>
QViewportPane::QViewportPane(QWidget* parent) : QWidget(parent), m_viewport(nullptr) {
m_layout = new QVBoxLayout(this);
m_layout->setContentsMargins(0, 0, 0, 0);
m_layout->setSpacing(0);
// Title bar setup
m_titleBar = new QWidget(this);
m_titleBar->setFixedHeight(22);
m_titleBar->setStyleSheet("background-color: #333; color: white;");
auto* titleLayout = new QHBoxLayout(m_titleBar);
titleLayout->setContentsMargins(5, 0, 5, 0);
m_titleLabel = new QLabel("Viewport", m_titleBar);
auto* closeBtn = new QToolButton(m_titleBar);
closeBtn->setText("X");
closeBtn->setFixedSize(18, 18);
closeBtn->setStyleSheet("QToolButton { border: none; font-weight: bold; background: transparent; color: #ccc; } "
"QToolButton:hover { color: white; background: red; }");
titleLayout->addWidget(m_titleLabel);
titleLayout->addStretch();
titleLayout->addWidget(closeBtn);
m_layout->addWidget(m_titleBar);
m_titleBar->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_titleBar, &QWidget::customContextMenuRequested, this, &QViewportPane::showContextMenu);
connect(closeBtn, &QToolButton::clicked, this, &QViewportPane::onCloseRequested);
addVtkViewport(); // Initialize with a default VTK viewport
}
QViewportPane::~QViewportPane() {}
void QViewportPane::setViewport(QWidget* viewport, const QString& title) {
if (m_viewport) {
m_layout->removeWidget(m_viewport);
delete m_viewport;
}
m_viewport = viewport;
m_titleLabel->setText(title);
m_viewport->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_layout->addWidget(m_viewport);
}
void QViewportPane::addVtkViewport() {
auto* viewport = new uLib::Vtk::QViewport(this);
setViewport(viewport, "VTK Viewport");
}
void QViewportPane::addRootCanvas() {
auto* canvas = new uLib::Root::QCanvas(this);
setViewport(canvas, "ROOT Canvas");
}
void QViewportPane::onCloseRequested() {
QSplitter* parentSplitter = qobject_cast<QSplitter*>(parentWidget());
if (parentSplitter && parentSplitter->count() > 1) {
deleteLater();
} else {
// Can't close the last viewport in the splitter safely. Re-initialize to default VTK canvas.
addVtkViewport();
}
}
void QViewportPane::showContextMenu(const QPoint& pos) {
QMenu menu(this);
QAction* hSplit = menu.addAction("H split");
QAction* vSplit = menu.addAction("V split");
menu.addSeparator();
bool isVtk = (qobject_cast<uLib::Vtk::QViewport*>(m_viewport) != nullptr);
QAction* changeType = menu.addAction(isVtk ? "Change to ROOT Canvas" : "Change to VTK Viewport");
QAction* selected = menu.exec(m_titleBar->mapToGlobal(pos));
if (selected == hSplit) {
AttemptSplit(Qt::Horizontal);
} else if (selected == vSplit) {
AttemptSplit(Qt::Vertical);
} else if (selected == changeType) {
if (isVtk) {
addRootCanvas();
} else {
addVtkViewport();
}
}
}
void QViewportPane::AttemptSplit(Qt::Orientation orientation) {
QWidget* p = parentWidget();
if (!p) return;
QSplitter* parentSplitter = qobject_cast<QSplitter*>(p);
if (!parentSplitter) return;
QViewportPane* newPane = new QViewportPane();
// 1. Synchronize viewport content and camera (VTK Viewport only for now)
auto* currentVtk = qobject_cast<uLib::Vtk::QViewport*>(m_viewport);
if (currentVtk) {
auto* newVtk = qobject_cast<uLib::Vtk::QViewport*>(newPane->currentViewport());
if (newVtk) {
// Copy puppets
for (auto* puppet : currentVtk->getPuppets()) {
newVtk->AddPuppet(*puppet);
}
// Copy camera
if (currentVtk->GetRenderer() && newVtk->GetRenderer()) {
vtkCamera* currentCam = currentVtk->GetRenderer()->GetActiveCamera();
vtkCamera* newCam = newVtk->GetRenderer()->GetActiveCamera();
if (currentCam && newCam) {
newCam->DeepCopy(currentCam);
}
}
// Sync grid visible and axis
newVtk->SetGridVisible(currentVtk->GetGridVisible());
newVtk->SetGridAxis(currentVtk->GetGridAxis());
}
}
// 2. Adjust for ROOT Canvas if that was the active view
bool isRoot = (qobject_cast<uLib::Root::QCanvas*>(m_viewport) != nullptr);
if (isRoot) {
newPane->addRootCanvas();
}
if (parentSplitter->orientation() == orientation) {
int index = parentSplitter->indexOf(this);
QList<int> sizes = parentSplitter->sizes();
int currentSize = sizes.value(index, 0);
int half = currentSize / 2;
sizes[index] = half;
sizes.insert(index + 1, currentSize - half);
parentSplitter->insertWidget(index + 1, newPane);
parentSplitter->setSizes(sizes);
} else {
int index = parentSplitter->indexOf(this);
QList<int> parentSizes = parentSplitter->sizes();
QSplitter* newSplitter = new QSplitter(orientation);
newSplitter->addWidget(this);
newSplitter->addWidget(newPane);
QList<int> subSizes;
subSizes << 500 << 500;
newSplitter->setSizes(subSizes);
parentSplitter->insertWidget(index, newSplitter);
parentSplitter->setSizes(parentSizes);
}
}

View File

@@ -0,0 +1,34 @@
#ifndef QVIEWPORTPANE_H
#define QVIEWPORTPANE_H
#include <QWidget>
class QVBoxLayout;
class QLabel;
class QViewportPane : public QWidget {
Q_OBJECT
public:
explicit QViewportPane(QWidget* parent = nullptr);
virtual ~QViewportPane();
void addVtkViewport();
void addRootCanvas();
QWidget* currentViewport() const { return m_viewport; }
private slots:
void onCloseRequested();
void showContextMenu(const QPoint& pos);
private:
void AttemptSplit(Qt::Orientation orientation);
void setViewport(QWidget* viewport, const QString& title);
QVBoxLayout* m_layout;
QWidget* m_titleBar;
QLabel* m_titleLabel;
QWidget* m_viewport;
};
#endif // QVIEWPORTPANE_H

View File

@@ -1,5 +1,7 @@
#include <QApplication>
#include "MainWindow.h"
#include "MainPanel.h"
#include "QViewportPane.h"
#include "Math/ContainerBox.h"
#include <HEP/Geant/Scene.h>
@@ -27,19 +29,7 @@ int main(int argc, char** argv) {
std::cout << "Starting gcompose Qt application..." << std::endl;
ContainerBox world_box(Vector3f(1, 1, 1));
world_box.Scale(Vector3f(20_mm, 20_mm, 20_mm));
DetectorChamber d1, d2;
d1.SetSize(Vector3f(1, 1, 1));
d1.SetPosition(Vector3f(0, 0, 0));
d1.Scale(Vector3f(5, 10, 2));
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));
world_box.Scale(Vector3f(20_mm, 20_mm, 20_mm));
Geant::Scene scene;
scene.ConstructWorldBox(world_box.GetSize(), "G4_AIR");
@@ -47,17 +37,12 @@ int main(int argc, char** argv) {
// 2. Initialize MainWindow (contains embedded VTK QViewport)
MainWindow window;
Vtk::QViewport* viewport = window.getViewport();
Vtk::vtkDetectorChamber vtk_d1(&d1);
viewport->AddPuppet(vtk_d1);
Vtk::vtkDetectorChamber vtk_d2(&d2);
viewport->AddPuppet(vtk_d2);
MainPanel* panel = window.getPanel();
QViewportPane* pane = panel->getFirstPane();
Vtk::QViewport* viewport = qobject_cast<Vtk::QViewport*>(pane->currentViewport());
Vtk::vtkContainerBox vtk_box(&world_box);
viewport->AddPuppet(vtk_box);
viewport->ZoomAuto();
std::cout << "Geant4 and VTK scenes are ready." << std::endl;

View File

@@ -14,14 +14,7 @@ MuonEvent DetectorChamber::ProjectMuonEvent(const MuonEvent &muon) const {
HPoint3f X_in = muon.LineIn().origin;
HPoint3f X_out = muon.LineOut().origin;
// Calculate squared distances to the plane normal point for comparison
// Actually, we should probably follow the user's description literally:
// "closest ... with the projection plane ( so the colsest direction point with the point of the normal defining the plane )"
// This could mean point-to-plane or point-to-point.
// Given "closest with the projection plane", point-to-plane is more natural.
// However, "closest direction point with the point of the normal" strongly suggests point-to-point distance.
// Let's use distance to the plane for the first part and keep the logic consistent.
// Let's use distance to the plane for the first part and keep the logic consistent.
float dist_in = std::abs((X_in - P).dot(N));
float dist_out = std::abs((X_out - P).dot(N));

View File

@@ -8,6 +8,9 @@ set(HEADERS RootMathDense.h
muCastorPrimaryVertex.h
muCastorMuDetDIGI.h
SkinDetectorWriter.h)
if(Qt6_FOUND)
list(APPEND HEADERS QCanvas.h)
endif()
set(SOURCES ${HEADERS} RootMuonScatter.cpp
muCastorMCTrack.cpp
@@ -17,6 +20,9 @@ set(SOURCES ${HEADERS} RootMuonScatter.cpp
muCastorPrimaryVertex.cpp
muCastorMuDetDIGI.cpp
SkinDetectorWriter.cpp)
if(Qt6_FOUND)
list(APPEND SOURCES QCanvas.cpp)
endif()
set(DICTIONARY_HEADERS muCastorMCTrack.h
muCastorHit.h
@@ -32,11 +38,17 @@ set(LIBRARIES ${ROOT_LIBRARIES}
set(rDictName ${PACKAGE_LIBPREFIX}RootDict)
root_generate_dictionary(${rDictName} ${DICTIONARY_HEADERS}
LINKDEF Linkdef.h)
set_source_files_properties(${rDictName}.cxx
PROPERTIES GENERATED TRUE)
set_source_files_properties(${rDictName}.h
PROPERTIES GENERATED TRUE)
list(APPEND SOURCES ${rDictName}.cxx)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${rDictName}.cxx
PROPERTIES GENERATED TRUE
SKIP_AUTOMOC TRUE
SKIP_AUTOUIC TRUE
SKIP_AUTORCC TRUE)
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${rDictName}.h
PROPERTIES GENERATED TRUE
SKIP_AUTOMOC TRUE
SKIP_AUTOUIC TRUE
SKIP_AUTORCC TRUE)
list(APPEND SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${rDictName}.cxx)
# TODO use a custom target linked to root_generate_dictionary
set(R_ARTIFACTS ${CMAKE_CURRENT_BINARY_DIR}/lib${rDictName}_rdict.pcm
@@ -55,6 +67,11 @@ set_target_properties(${libname} PROPERTIES
CXX_STANDARD 17)
target_link_libraries(${libname} ${LIBRARIES})
if(Qt6_FOUND)
set_target_properties(${libname} PROPERTIES AUTOMOC ON)
target_link_libraries(${libname} Qt6::Widgets)
endif()
install(TARGETS ${libname}
EXPORT "uLibTargets"
RUNTIME DESTINATION ${INSTALL_BIN_DIR} COMPONENT bin

View File

@@ -1,20 +0,0 @@
SUBDIRS = .
ROOTCINT = `which rootcint`
include $(top_srcdir)/Common.am
library_includedir = $(includedir)/libmutom-${PACKAGE_VERSION}/Root
library_include_HEADERS = TestTObject.h
_CORE_SOURCES = TestTObject.cpp
MutomDict.cxx: $(library_include_HEADERS) Linkdef.h
$(ROOTCINT) -f $@ -c $(AM_CXXFLAGS) -p $^
noinst_LTLIBRARIES = libmutomroot.la
libmutomroot_la_SOURCES = MutomDict.cxx ${_CORE_SOURCES}

153
src/Root/QCanvas.cpp Normal file
View File

@@ -0,0 +1,153 @@
#include "QCanvas.h"
#include <iostream>
#include <cstdio>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QMouseEvent>
#include <TCanvas.h>
#include <TApplication.h>
#include <TSystem.h>
#include <Buttons.h>
#include <Gtypes.h>
#include <TVirtualX.h>
#include <TEnv.h>
namespace uLib {
namespace Root {
QCanvas::QCanvas(QWidget *parent) : QWidget(parent), m_canvas(nullptr) {
if (gEnv) gEnv->SetValue("Canvas.UseDoubleBuffer", 0);
setMinimumSize(400, 300);
resize(400, 300);
setUpdatesEnabled(false);
setAttribute(Qt::WA_NativeWindow, true);
setAttribute(Qt::WA_OpaquePaintEvent, true);
setAttribute(Qt::WA_PaintOnScreen, true);
setMouseTracking(true); // Required so that TCanvas can get mouse hover events
if (!gApplication) {
static int argc = 1;
static char* argv[] = {(char*)"App", nullptr};
new TApplication("App", &argc, argv);
}
// Create the TCanvas associated with this QWidget
m_canvas = nullptr;
}
QCanvas::~QCanvas() {
delete m_canvas;
}
void QCanvas::SetCanvas(TCanvas* c) {
m_canvas = c;
}
void QCanvas::paintEvent(QPaintEvent* /*event*/) {
EnsureCanvasCreated();
if (m_canvas) {
m_canvas->Update();
}
}
void QCanvas::resizeEvent(QResizeEvent* event) {
EnsureCanvasCreated();
if (m_canvas) {
m_canvas->SetWindowSize(event->size().width(), event->size().height());
m_canvas->Resize();
m_canvas->Update();
}
}
TCanvas* QCanvas::GetCanvas() {
EnsureCanvasCreated();
return m_canvas;
}
void QCanvas::EnsureCanvasCreated() {
if (!m_canvas) {
WId wid = this->winId();
std::cout << "DEBUG: QCanvas winId: " << wid << " (hex: 0x" << std::hex << wid << std::dec << ")" << std::endl;
if (gSystem) gSystem->ProcessEvents();
// Ensure backend is loaded before creating the embedded canvas
if (!gVirtualX) {
TCanvas dummy("dummy", "dummy", 10, 10);
}
// Add the X11 window to ROOT's window table and get its internal ROOT index.
Int_t root_wid = -1;
if (gVirtualX) {
root_wid = gVirtualX->AddWindow((ULongptr_t)wid, (UInt_t)width(), (UInt_t)height());
}
char name[100];
sprintf(name, "RootQCanvas_%p", (void*)this);
m_canvas = new TCanvas(name, (Int_t)width(), (Int_t)height(), root_wid);
}
}
void QCanvas::mouseMoveEvent(QMouseEvent* event) {
if (m_canvas) {
auto pos = event->position();
if (event->buttons() & Qt::LeftButton) {
m_canvas->HandleInput(kButton1Motion, pos.x(), pos.y());
} else if (event->buttons() & Qt::MiddleButton) {
m_canvas->HandleInput(kButton2Motion, pos.x(), pos.y());
} else if (event->buttons() & Qt::RightButton) {
m_canvas->HandleInput(kButton3Motion, pos.x(), pos.y());
} else {
m_canvas->HandleInput(kMouseMotion, pos.x(), pos.y());
}
}
}
void QCanvas::mousePressEvent(QMouseEvent* event) {
if (m_canvas) {
auto pos = event->position();
switch (event->button()) {
case Qt::LeftButton:
m_canvas->HandleInput(kButton1Down, pos.x(), pos.y());
break;
case Qt::RightButton:
m_canvas->HandleInput(kButton3Down, pos.x(), pos.y());
break;
case Qt::MiddleButton:
m_canvas->HandleInput(kButton2Down, pos.x(), pos.y());
break;
default:
break;
}
}
}
void QCanvas::mouseReleaseEvent(QMouseEvent* event) {
if (m_canvas) {
auto pos = event->position();
switch (event->button()) {
case Qt::LeftButton:
m_canvas->HandleInput(kButton1Up, pos.x(), pos.y());
break;
case Qt::RightButton:
m_canvas->HandleInput(kButton3Up, pos.x(), pos.y());
break;
case Qt::MiddleButton:
m_canvas->HandleInput(kButton2Up, pos.x(), pos.y());
break;
default:
break;
}
}
}
} // namespace Root
} // namespace uLib

38
src/Root/QCanvas.h Normal file
View File

@@ -0,0 +1,38 @@
#pragma once
#ifdef HAVE_QT
#include <QWidget>
class TCanvas;
class QPaintEvent;
class QResizeEvent;
class QMouseEvent;
namespace uLib {
namespace Root {
class QCanvas : public QWidget {
Q_OBJECT
public:
explicit QCanvas(QWidget *parent = nullptr);
virtual ~QCanvas();
TCanvas* GetCanvas();
void SetCanvas(TCanvas* c);
protected:
void paintEvent(QPaintEvent* event) override;
void resizeEvent(QResizeEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;
void mousePressEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
private:
void EnsureCanvasCreated();
TCanvas* m_canvas;
};
} // namespace Root
} // namespace uLib
#endif // HAVE_QT

View File

@@ -3,6 +3,9 @@ set( TESTS
# RootDebugTest
# muBlastMCTrackTest
)
if(Qt6_FOUND)
list(APPEND TESTS QCanvasTest)
endif()
set(LIBRARIES
${PACKAGE_LIBPREFIX}Core
@@ -12,4 +15,7 @@ set(LIBRARIES
Boost::program_options
${ROOT_LIBRARIES}
)
if(Qt6_FOUND)
list(APPEND LIBRARIES Qt6::Widgets)
endif()
uLib_add_tests(Root)

View File

@@ -0,0 +1,46 @@
#ifdef HAVE_QT
#include <iostream>
#include <QApplication>
#include <QMainWindow>
#include <TH1F.h>
#include <TCanvas.h>
#include <TRandom.h>
#include <TEnv.h>
#include "Root/QCanvas.h"
int main(int argc, char **argv) {
if (gEnv) gEnv->SetValue("Canvas.UseDoubleBuffer", 0);
QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
QApplication app(argc, argv);
QMainWindow mainWindow;
mainWindow.setWindowTitle("uLib Root QCanvas Test");
// Create our custom Root QCanvas widget
uLib::Root::QCanvas *qcanvas = new uLib::Root::QCanvas(&mainWindow);
mainWindow.setCentralWidget(qcanvas);
mainWindow.resize(800, 600);
mainWindow.show();
app.processEvents();
// Now let's draw something in the ROOT canvas
TCanvas *canvas = qcanvas->GetCanvas();
if (canvas) {
canvas->cd();
TH1F *h1 = new TH1F("h1", "Test Histogram;X-axis;Events", 100, -5, 5);
h1->FillRandom("gaus", 10000);
h1->SetFillColor(kBlue-10);
h1->Draw();
canvas->Modified();
canvas->Update();
app.processEvents();
} else {
std::cerr << "FAIL: Canvas is still NULL after show() and processEvents()" << std::endl;
}
return app.exec();
}
#else
int main() { return 0; }
#endif

View File

@@ -2,6 +2,7 @@
set(TESTS
vtkMuonScatterTest
vtkDetectorChamberTest
vtkDetectorMuonProjectionTest
)
set(LIBRARIES

View File

@@ -0,0 +1,156 @@
/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#include "HEP/Detectors/DetectorChamber.h"
#include "HEP/Detectors/MuonScatter.h"
#include "Math/Units.h"
#include "Vtk/HEP/Detectors/vtkDetectorChamber.h"
#include "Vtk/HEP/Detectors/vtkMuonScatter.h"
#include "Vtk/uLibVtkViewer.h"
#include <vtkActor.h>
#include <vtkArrowSource.h>
#include <vtkMath.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkTransform.h>
#define BOOST_TEST_MODULE vtkDetectorMuonProjectionTest
#include <boost/test/unit_test.hpp>
using namespace uLib;
// A simple puppet class to represent an arrow indicative of a projected muon hit
class vtkArrowPuppet : public Vtk::Puppet {
public:
vtkArrowPuppet() : m_Actor(vtkActor::New()) {
vtkNew<vtkArrowSource> arrow;
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(arrow->GetOutputPort());
m_Actor->SetMapper(mapper);
this->SetProp(m_Actor);
m_Actor->GetProperty()->SetColor(1, 1, 0); // Yellow color for visibility
}
virtual ~vtkArrowPuppet() { m_Actor->Delete(); }
void SetProjection(const HLine3f &line) {
HPoint3f p = line.origin;
HVector3f d = line.direction;
d.normalize();
vtkNew<vtkTransform> t;
t->Translate(p.x(), p.y(), p.z());
// vtkArrowSource initially points along X+ (1, 0, 0)
double vtk_x[3] = {1, 0, 0};
double vtk_d[3] = {d.x(), d.y(), d.z()};
double axis[3];
vtkMath::Cross(vtk_x, vtk_d, axis);
double dot = vtkMath::Dot(vtk_x, vtk_d);
double angle = vtkMath::DegreesFromRadians(acos(dot));
// Handle parallel or anti-parallel cases
if (vtkMath::Norm(axis) < 1e-6) {
if (dot < 0) {
t->RotateY(180); // Reverse direction if anti-parallel
}
} else {
t->RotateWXYZ(angle, axis);
}
t->Scale(100, 100, 100); // Scale the arrow for visual prominence
m_Actor->SetUserTransform(t);
}
private:
vtkActor *m_Actor;
};
BOOST_AUTO_TEST_CASE(vtkDetectorMuonProjectionTest) {
using namespace uLib::literals;
// 1. Prepare a scene with two detector chambers
DetectorChamber d1, d2;
d1.Scale(Vector3f(1_m, 1_m, 10_cm));
d1.Translate(Vector3f(0, 0, -0.5_m));
d2.Scale(Vector3f(1_m, 1_m, 10_cm));
d2.Translate(Vector3f(0, 0, 0.5_m));
// 2. Setup a muon event that crosses the detectors
MuonScatter event;
// Set an origin slightly offset and point it toward (0,0,0)
HPoint3f mu_origin(20_cm, 30_cm, -1_m);
event.LineIn().origin = mu_origin;
// Direction is toward (0,0,0)
HVector3f mu_dir = (HPoint3f(0,0,0) - mu_origin);
mu_dir.normalize();
event.LineIn().direction = mu_dir;
// Let's make the track continue straight for a simple projection test
event.LineOut().origin = mu_origin - mu_dir * 3_m;
event.LineOut().direction = -mu_dir;
MuonEvent mu_event;
mu_event.LineIn() = event.LineIn();
mu_event.LineOut() = event.LineOut();
// 3. Project the muon on each detector to find the projection points
MuonEvent mu_proj1 = d1.ProjectMuonEvent(mu_event);
MuonEvent mu_proj2 = d2.ProjectMuonEvent(mu_event);
Vtk::vtkDetectorChamber v_d1(&d1);
Vtk::vtkDetectorChamber v_d2(&d2);
Vtk::vtkMuonScatter v_event(event);
v_event.AddPocaPoint(HPoint3f(0, 0, 0));
v_event.SetColor(1, 0, 0); // Red muon event
v_d1.SetRepresentation(Vtk::Puppet::Surface);
v_d1.SetOpacity(0.3);
v_d2.SetRepresentation(Vtk::Puppet::Surface);
v_d2.SetOpacity(0.3);
// 5. Add two arrows to mark where the projection is located on the chambers
vtkArrowPuppet v_p1, v_p2;
v_p1.SetProjection(mu_proj1.LineIn());
v_p2.SetProjection(mu_proj2.LineIn());
if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {
Vtk::Viewer viewer;
viewer.SetGridAxis(Vtk::Viewport::Z);
viewer.AddPuppet(v_d1);
viewer.AddPuppet(v_d2);
viewer.AddPuppet(v_event);
viewer.AddPuppet(v_p1);
viewer.AddPuppet(v_p2);
viewer.Start();
}
BOOST_CHECK(true);
}

View File

@@ -61,6 +61,7 @@ public:
virtual vtkRenderWindowInteractor* GetInteractor() = 0;
vtkCornerAnnotation* GetAnnotation() { return m_Annotation; }
vtkCameraOrientationWidget* GetCameraWidget(){ return m_CameraWidget; }
const std::vector<Puppet*>& getPuppets() const { return m_Puppets; }
// Grid control
void SetGridVisible(bool visible);