8 Commits

52 changed files with 1183 additions and 650 deletions

View File

@@ -19,6 +19,18 @@ endif()
project(uLib)
option(ULIB_USE_CCACHE "Use ccache for build acceleration" ON)
if(ULIB_USE_CCACHE)
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif()
else()
set(CMAKE_CXX_COMPILER_LAUNCHER "")
set(CMAKE_C_COMPILER_LAUNCHER "")
endif()
# Applica la flag SOLO se il compilatore è GCC
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-fno-merge-constants)
@@ -130,8 +142,11 @@ find_package(Boost 1.45.0 COMPONENTS program_options serialization unit_test_fra
include_directories(${Boost_INCLUDE_DIRS})
find_package(Eigen3 CONFIG REQUIRED)
get_target_property(EIGEN3_INCLUDE_DIRS Eigen3::Eigen INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${EIGEN3_INCLUDE_DIRS})
# if(NOT EIGEN3_INCLUDE_DIRS)
# get_target_property(EIGEN3_INCLUDE_DIRS Eigen3::Eigen INTERFACE_INCLUDE_DIRECTORIES)
# else()
# include_directories(${EIGEN3_INCLUDE_DIRS})
# endif()
find_package(OpenMP)

View File

@@ -17,6 +17,8 @@ add_executable(gcompose
src/PropertyWidgets.cpp
src/PropertiesPanel.h
src/PropertiesPanel.cpp
src/PreferencesDialog.h
src/PreferencesDialog.cpp
)
set_target_properties(gcompose PROPERTIES

View File

@@ -148,6 +148,7 @@ QVariant ContextModel::data(const QModelIndex& index, int role) const {
if (!index.isValid()) return QVariant();
uLib::Object* obj = static_cast<uLib::Object*>(index.internalPointer());
if (!obj) return QVariant();
if (role == Qt::DisplayRole) {
QString typeName = getDemangledName(typeid(*obj));

View File

@@ -13,11 +13,14 @@
#include <QPushButton>
#include <QMenu>
#include <QAction>
#include <QShortcut>
#include <QApplication>
#include <QFileDialog>
#include <QFileInfo>
#include "StyleManager.h"
#include "Math/VoxImage.h"
#include "PreferencesDialog.h"
#include "Settings.h"
MainPanel::MainPanel(QWidget* parent) : QWidget(parent), m_context(nullptr), m_mainVtkContext(nullptr) {
this->setObjectName("MainPanel");
@@ -45,17 +48,12 @@ MainPanel::MainPanel(QWidget* parent) : QWidget(parent), m_context(nullptr), m_m
fileMenu->addAction("Open", this, &MainPanel::onOpen);
fileMenu->addAction("Save", this, &MainPanel::onSave);
fileMenu->addAction("Save As", this, &MainPanel::onSaveAs);
fileMenu->addSeparator();
fileMenu->addAction("Preferences", this, &MainPanel::onPreferences);
fileMenu->addSeparator();
fileMenu->addAction("Exit", this, &MainPanel::onExit);
btnFile->setMenu(fileMenu);
// Theme Menu Button
auto* btnTheme = new QPushButton("Theme", menuPanel);
btnTheme->setObjectName("MenuButton");
auto* themeMenu = new QMenu(btnTheme);
themeMenu->addAction("Dark", this, &MainPanel::onDarkTheme);
themeMenu->addAction("Bright", this, &MainPanel::onBrightTheme);
btnTheme->setMenu(themeMenu);
// New Menu Button
auto* btnNew = new QPushButton("Add", menuPanel);
btnNew->setObjectName("MenuButton");
@@ -73,7 +71,6 @@ MainPanel::MainPanel(QWidget* parent) : QWidget(parent), m_context(nullptr), m_m
menuLayout->addWidget(logo);
menuLayout->addWidget(btnFile);
menuLayout->addWidget(btnNew);
menuLayout->addWidget(btnTheme);
menuLayout->addStretch();
mainLayout->addWidget(menuPanel);
@@ -108,6 +105,14 @@ MainPanel::MainPanel(QWidget* parent) : QWidget(parent), m_context(nullptr), m_m
m_rootSplitter->setSizes(sizes);
mainLayout->addWidget(m_rootSplitter, 1);
// Shortcuts
auto* groupShortcut = new QShortcut(QKeySequence("Ctrl+G"), this);
connect(groupShortcut, &QShortcut::activated, [this]() {
if (auto* viewport = qobject_cast<uLib::Vtk::QViewport*>(m_firstPane->currentViewport())) {
viewport->GroupSelection(m_context);
}
});
}
void MainPanel::setContext(uLib::ObjectsContext* context) {
@@ -236,12 +241,21 @@ void MainPanel::onExit() {
qApp->quit();
}
void MainPanel::onDarkTheme() {
StyleManager::applyStyle(qApp, "dark");
}
void MainPanel::onPreferences() {
uLib::Qt::PreferencesDialog dlg(this);
if (dlg.exec() == QDialog::Accepted) {
// Apply theme
auto theme = uLib::Qt::Settings::Instance().GetTheme();
StyleManager::applyStyle(qApp, theme == uLib::Qt::Settings::Dark ? "dark" : "bright");
void MainPanel::onBrightTheme() {
StyleManager::applyStyle(qApp, "bright");
// Apply rendering preference to all viewports
bool throttled = uLib::Qt::Settings::Instance().GetThrottledRendering();
auto viewports = this->findChildren<uLib::Vtk::QViewport*>();
for (auto* vp : viewports) {
vp->SetThrottledRendering(throttled);
vp->Render();
}
}
}
MainPanel::~MainPanel() {}

View File

@@ -30,8 +30,7 @@ private slots:
void onSaveAs();
void onExit();
void onDarkTheme();
void onBrightTheme();
void onPreferences();
void onCreateObject(const std::string& className);

View File

@@ -0,0 +1,99 @@
#include "PreferencesDialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QPushButton>
#include <QLabel>
#include <QGroupBox>
namespace uLib {
namespace Qt {
PreferencesDialog::PreferencesDialog(QWidget* parent) : QDialog(parent) {
setWindowTitle("Preferences");
setMinimumWidth(400);
auto* mainLayout = new QVBoxLayout(this);
mainLayout->setSpacing(20);
mainLayout->setContentsMargins(20, 20, 20, 20);
// ── General / Rendering Settings ────────────────────────────────────────
auto* renderingGroup = new QGroupBox("Appearance & Performance", this);
auto* renderingLayout = new QVBoxLayout(renderingGroup);
auto* themeLayout = new QHBoxLayout();
themeLayout->addWidget(new QLabel("Color Theme:"));
m_themeCombo = new QComboBox(renderingGroup);
m_themeCombo->addItem("Dark");
m_themeCombo->addItem("Bright");
m_themeCombo->setCurrentIndex(Settings::Instance().GetTheme() == Settings::Dark ? 0 : 1);
themeLayout->addWidget(m_themeCombo);
themeLayout->addStretch();
renderingLayout->addLayout(themeLayout);
renderingLayout->addSpacing(10);
m_throttledRendering = new QCheckBox("Enable throttled rendering (recommended for performance)", renderingGroup);
m_throttledRendering->setChecked(Settings::Instance().GetThrottledRendering());
m_throttledRendering->setToolTip("Limits framerate to ~60fps to reduce CPU/GPU usage.");
renderingLayout->addWidget(m_throttledRendering);
mainLayout->addWidget(renderingGroup);
// ── Units Settings ──────────────────────────────────────────────────────
auto* unitsGroup = new QGroupBox("Preferred Units", this);
auto* unitsLayout = new QFormLayout(unitsGroup);
unitsLayout->setLabelAlignment(::Qt::AlignRight);
unitsLayout->setSpacing(10);
auto addUnitRow = [&](const QString& label, Settings::Dimension dim, const QStringList& units) {
auto* combo = new QComboBox(unitsGroup);
combo->addItems(units);
std::string current = Settings::Instance().GetPreferredUnit(dim);
int idx = combo->findText(QString::fromStdString(current));
if (idx >= 0) combo->setCurrentIndex(idx);
unitsLayout->addRow(label + ":", combo);
m_unitCombos[dim] = combo;
};
addUnitRow("Length", Settings::Length, {"m", "cm", "mm", "um", "nm"});
addUnitRow("Angle", Settings::Angle, {"deg", "rad"});
addUnitRow("Energy", Settings::Energy, {"MeV", "GeV", "eV", "keV", "TeV"});
addUnitRow("Time", Settings::Time, {"ns", "s", "ms", "us"});
mainLayout->addWidget(unitsGroup);
mainLayout->addStretch();
// ── Buttons ─────────────────────────────────────────────────────────────
auto* buttonLayout = new QHBoxLayout();
buttonLayout->addStretch();
auto* btnCancel = new QPushButton("Cancel", this);
connect(btnCancel, &QPushButton::clicked, this, &QDialog::reject);
auto* btnOk = new QPushButton("Apply", this);
btnOk->setDefault(true);
btnOk->setObjectName("DisplayToggleBtn"); // Reusing high-contrast style
btnOk->setMinimumWidth(100);
connect(btnOk, &QPushButton::clicked, this, &PreferencesDialog::onAccept);
buttonLayout->addWidget(btnCancel);
buttonLayout->addWidget(btnOk);
mainLayout->addLayout(buttonLayout);
}
void PreferencesDialog::onAccept() {
Settings::Instance().SetThrottledRendering(m_throttledRendering->isChecked());
Settings::Instance().SetTheme(m_themeCombo->currentIndex() == 0 ? Settings::Dark : Settings::Bright);
for (auto const& pair : m_unitCombos) {
Settings::Instance().SetPreferredUnit(pair.first, pair.second->currentText().toStdString());
}
accept();
}
} // namespace Qt
} // namespace uLib

View File

@@ -0,0 +1,31 @@
#ifndef GCOMPOSE_PREFERENCESDIALOG_H
#define GCOMPOSE_PREFERENCESDIALOG_H
#include <QDialog>
#include <QCheckBox>
#include <QComboBox>
#include <map>
#include <string>
#include "Settings.h"
namespace uLib {
namespace Qt {
class PreferencesDialog : public QDialog {
Q_OBJECT
public:
explicit PreferencesDialog(QWidget* parent = nullptr);
private slots:
void onAccept();
private:
QCheckBox* m_throttledRendering;
QComboBox* m_themeCombo;
std::map<Settings::Dimension, QComboBox*> m_unitCombos;
};
} // namespace Qt
} // namespace uLib
#endif

View File

@@ -94,8 +94,8 @@ void QViewportPane::setObject(uLib::Object* obj) {
void QViewportPane::setViewport(QWidget* viewport, const QString& title) {
if (m_viewport) {
m_viewport->parentWidget()->layout()->removeWidget(m_viewport);
delete m_viewport;
// Use deleteLater() instead of delete to avoid crashes during repaint cycles
m_viewport->deleteLater();
}
m_viewport = viewport;
m_titleLabel->setText(title);
@@ -105,21 +105,8 @@ void QViewportPane::setViewport(QWidget* viewport, const QString& title) {
mainAreaLayout->insertWidget(0, m_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");

View File

@@ -23,6 +23,11 @@ public:
Dimensionless
};
enum Theme {
Dark,
Bright
};
void SetPreferredUnit(Dimension dim, const std::string& unit) {
m_PreferredUnits[dim] = unit;
}
@@ -64,9 +69,17 @@ public:
return Dimensionless;
}
bool GetThrottledRendering() const { return m_ThrottledRendering; }
void SetThrottledRendering(bool enabled) { m_ThrottledRendering = enabled; }
Theme GetTheme() const { return m_Theme; }
void SetTheme(Theme theme) { m_Theme = theme; }
private:
Settings() {}
Settings() : m_ThrottledRendering(true), m_Theme(Dark) {}
std::map<Dimension, std::string> m_PreferredUnits;
bool m_ThrottledRendering;
Theme m_Theme;
};
} // namespace Qt

View File

@@ -115,7 +115,7 @@ void ViewportPane::setObject(uLib::Object* obj) {
void ViewportPane::setViewport(QWidget* viewport, const QString& title) {
if (m_viewport) {
delete m_viewport;
m_viewport->deleteLater();
}
m_viewport = viewport;
m_titleLabel->setText(title);

View File

@@ -1,36 +1 @@
make: Entering directory '/home/rigoni/devel/cmt/ulib/build'
[ 30%] Building CXX object src/Math/CMakeFiles/mutomMath.dir/VoxImage.cpp.o
[ 30%] Building CXX object src/Math/CMakeFiles/mutomMath.dir/TriangleMesh.cpp.o
[ 30%] Building CXX object src/Core/CMakeFiles/mutomCore.dir/Options.cpp.o
[ 30%] Building CXX object src/Math/CMakeFiles/mutomMath.dir/Dense.cpp.o
[ 30%] Building CXX object src/Math/CMakeFiles/mutomMath.dir/StructuredGrid.cpp.o
[ 30%] Building CXX object src/Math/CMakeFiles/mutomMath.dir/VoxRaytracer.cpp.o
[ 30%] Building CXX object src/Math/CMakeFiles/mutomMath.dir/StructuredData.cpp.o
[ 30%] Building CXX object src/Math/CMakeFiles/mutomMath.dir/Structured2DGrid.cpp.o
[ 30%] Building CXX object src/Math/CMakeFiles/mutomMath.dir/Structured4DGrid.cpp.o
[ 33%] Linking CXX shared library libmutomCore.so
[ 33%] Built target mutomCore
[ 36%] Linking CXX shared library libmutomMath.so
[ 36%] Built target mutomMath
[ 63%] Building CXX object src/Vtk/CMakeFiles/mutomVtk.dir/vtkContainerBox.cpp.o
[ 63%] Building CXX object src/Vtk/CMakeFiles/mutomVtk.dir/uLibVtkInterface.cxx.o
[ 63%] Building CXX object src/Vtk/CMakeFiles/mutomVtk.dir/vtkStructuredGrid.cpp.o
[ 63%] Building CXX object src/Vtk/CMakeFiles/mutomVtk.dir/vtkMuonScatter.cxx.o
[ 63%] Building CXX object src/Vtk/CMakeFiles/mutomVtk.dir/uLibVtkViewer.cpp.o
[ 63%] Generating mutomRootDict.cxx, libmutomRootDict_rdict.pcm, libmutomRootDict.rootmap
[ 63%] Building CXX object src/Vtk/CMakeFiles/mutomVtk.dir/vtkVoxImage.cpp.o
[ 63%] Building CXX object src/Vtk/CMakeFiles/mutomVtk.dir/vtkVoxRaytracerRepresentation.cpp.o
[ 90%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/muCastorSkinHit.cpp.o
[ 90%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/muCastorHit.cpp.o
[ 90%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/muCastorMCTrack.cpp.o
[ 90%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/muCastorInfo.cpp.o
[ 90%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/RootMuonScatter.cpp.o
[ 90%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/muCastorPrimaryVertex.cpp.o
[ 90%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/muCastorMuDetDIGI.cpp.o
[ 90%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/SkinDetectorWriter.cpp.o
[ 93%] Building CXX object src/Root/CMakeFiles/mutomRoot.dir/mutomRootDict.cxx.o
[ 96%] Linking CXX shared library libmutomVtk.so
[ 96%] Built target mutomVtk
[100%] Linking CXX shared library libmutomRoot.so
[100%] Built target mutomRoot
make: Leaving directory '/home/rigoni/devel/cmt/ulib/build'
ninja: error: loading 'build.ninja': No such file or directory

View File

@@ -1,5 +1,5 @@
[requires]
eigen/3.4.0
# eigen/5.0.1
boost/1.86.0
# pybind11/3.0.2
hdf5/1.14.3

View File

@@ -0,0 +1,48 @@
# Prop3D
`uLib::Vtk::Prop3D` is a bridge class that wraps VTK 3D representations (`vtkProp`, `vtkProp3D`) and integrates them into the `uLib` object model. It allows the framework to manage visual objects, synchronize them with underlying data models, and expose display-specific properties to the GUI.
## Inheritance
`uLib::Vtk::Prop3D` : `uLib::Object`
## Key Functionalities
### VTK Integration
The class provides access to the underlying VTK objects:
- `GetProp()`: Returns the `vtkProp`.
- `GetProxyProp()`: Returns the `vtkProp3D`.
- `GetParts()` / `GetProps()`: Returns `vtkPropCollection` for composite objects.
### Model-View Synchronization
`Prop3D` ensures that the visual representation stays in sync with the domain model:
- `Update()`: Synchronizes the VTK representation based on current internal state and properties. Should be called when model data changes.
- `SyncFromVtk()`: Updates internal state using data from the VTK representation (e.g., after user interaction via gizmos in the 3D view).
- `GetContent()`: Returns the `uLib::Object` that this `Prop3D` represents visually.
### Visual Appearance
- **Color & Opacity**: `SetColor(r, g, b)` and `SetOpacity(alpha)`.
- **Selection**: `SetSelectable(bool)` and `SetSelected(bool)` to manage interactivity and highlighting.
- **BBox/Scale**: `ShowBoundingBox(bool)` and `ShowScaleMeasures(bool)`.
### Rendering Modes
The rendering style can be controlled via the `Representation` enum:
- `Points`
- `Wireframe`
- `Surface`
- `SurfaceWithEdges`
- `Volume`
- `Outline`
- `Slice`
### Display Properties System
`Prop3D` implements a system to expose specific properties (often marked as `hrp` - human readable properties) to a property editor in the GUI.
- `GetDisplayProperties()`: Returns the list of properties registered for display.
- `RegisterDisplayProperty(uLib::PropertyBase*)`: Adds a property to the display list.
- `serialize_display(...)`: A virtual method that subclasses implement to define which properties should be exposed.
#### Activating Display Properties
To automatically populate the display properties list, the `ULIB_ACTIVATE_DISPLAY_PROPERTIES` macro should be called in the constructor. This triggers `serialize_display` using a `display_properties_archive`.
## Implementation Details
`Prop3D` uses the Pimpl idiom (via `Prop3DData *pd`) to hide VTK-specific implementation details and reduce header dependencies.

View File

@@ -46,6 +46,8 @@ public:
DataAllocator(size_t size = 0, bool owns_objects = true)
: m_Size(size), m_RamData(nullptr), m_VramData(nullptr),
m_Device(MemoryDevice::RAM), m_OwnsObjects(owns_objects) {
if (m_Size >= (static_cast<size_t>(1) << 60))
throw std::invalid_argument("DataAllocator: absurdly large size requested");
if (m_Size > 0) {
if (m_OwnsObjects)
m_RamData = new T[m_Size]();
@@ -139,7 +141,7 @@ public:
void MoveToRAM() {
if (m_Device == MemoryDevice::RAM)
return;
if (!m_RamData && m_Size > 0) {
if (!m_RamData && m_Size > 0 && m_Size < (static_cast<size_t>(1) << 60)) {
if (m_OwnsObjects)
m_RamData = new T[m_Size]();
else
@@ -178,7 +180,7 @@ public:
T *newRam = nullptr;
T *newVram = nullptr;
if (size > 0) {
if (size > 0 && size < (static_cast<size_t>(1) << 60)) {
if (m_OwnsObjects)
newRam = new T[size]();
else

View File

@@ -60,6 +60,10 @@ public:
std::string slostr;
};
~ObjectPrivate() {
for (auto& s : sigv) delete s.signal;
}
std::string m_InstanceName;
std::vector<Signal> sigv;
std::vector<Slot> slov;
@@ -71,7 +75,13 @@ public:
// Implementations of Property methods
void Object::RegisterDisplayProperty(PropertyBase* prop) {
if (prop) d->m_DisplayProperties.push_back(prop);
if (prop) {
for (auto* existing : d->m_DisplayProperties) {
if (existing == prop) return;
if (existing->GetName() == prop->GetName()) return;
}
d->m_DisplayProperties.push_back(prop);
}
}
const std::vector<PropertyBase*>& Object::GetDisplayProperties() const {
@@ -80,6 +90,10 @@ const std::vector<PropertyBase*>& Object::GetDisplayProperties() const {
void Object::RegisterProperty(PropertyBase* prop) {
if (prop) {
for (auto* existing : d->m_Properties) {
if (existing == prop) return;
if (existing->GetName() == prop->GetName()) return;
}
d->m_Properties.push_back(prop);
}
}
@@ -105,16 +119,19 @@ PropertyBase* Object::GetProperty(const std::string& name) const {
for (auto* p : d->m_DynamicProperties) {
if (p->GetName() == name || p->GetQualifiedName() == name) return p;
}
for (auto* p : d->m_DisplayProperties) {
if (p->GetName() == name || p->GetQualifiedName() == name) return p;
}
return nullptr;
}
void Object::NotifyPropertiesUpdated() {
// Only notify properties in the primary list to avoid duplicates,
// as all registered properties should be there.
for (auto* p : d->m_Properties) p->Updated();
for (auto* p : d->m_DynamicProperties) p->Updated();
}
void Object::Updated() { ULIB_SIGNAL_EMIT(Object::Updated); }
void Object::PropertyUpdated() { ULIB_SIGNAL_EMIT(Object::PropertyUpdated); }
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@@ -147,6 +164,9 @@ Object::~Object() {
for (auto* p : d->m_DynamicProperties) {
delete p;
}
for (auto* p : d->m_DisplayProperties) {
delete p;
}
delete d;
}
@@ -188,7 +208,8 @@ void Object::PrintSelf(std::ostream &o) const {
bool Object::addSignalImpl(SignalBase *sig, GenericMFPtr fptr,
const char *name) {
ObjectPrivate::Signal s = {fptr, std::string(name), sig};
if (!d) return false;
ObjectPrivate::Signal s = {fptr, std::string(name ? name : "unnamed"), sig};
d->sigv.push_back(s);
return true;
}

View File

@@ -84,7 +84,8 @@ public:
const std::string &GetInstanceName() const;
void SetInstanceName(const std::string &name);
/** @brief Temporarily blocks all signal emissions from this object. Returns previous state. */
/** @brief Temporarily blocks all signal emissions from this object. Returns
* previous state. */
bool blockSignals(bool block);
/** @brief Checks if signals are currently blocked. */
@@ -99,7 +100,8 @@ public:
virtual const std::vector<PropertyBase *> &GetDisplayProperties() const;
PropertyBase *GetProperty(const std::string &name) const;
/** @brief Sends an Updated signal for all properties of this object. useful for real-time UI refresh. */
/** @brief Sends an Updated signal for all properties of this object. useful
* for real-time UI refresh. */
void NotifyPropertiesUpdated();
////////////////////////////////////////////////////////////////////////////
@@ -117,13 +119,20 @@ public:
template <class ArchiveT>
void serialize(ArchiveT &ar, const unsigned int version);
virtual void serialize(Archive::xml_oarchive & ar, const unsigned int version) {}
virtual void serialize(Archive::xml_iarchive & ar, const unsigned int version) {}
virtual void serialize(Archive::text_oarchive & ar, const unsigned int version) {}
virtual void serialize(Archive::text_iarchive & ar, const unsigned int version) {}
virtual void serialize(Archive::hrt_oarchive & ar, const unsigned int version) {}
virtual void serialize(Archive::hrt_iarchive & ar, const unsigned int version) {}
virtual void serialize(Archive::log_archive & ar, const unsigned int version) {}
virtual void serialize(Archive::xml_oarchive &ar,
const unsigned int version) {}
virtual void serialize(Archive::xml_iarchive &ar,
const unsigned int version) {}
virtual void serialize(Archive::text_oarchive &ar,
const unsigned int version) {}
virtual void serialize(Archive::text_iarchive &ar,
const unsigned int version) {}
virtual void serialize(Archive::hrt_oarchive &ar,
const unsigned int version) {}
virtual void serialize(Archive::hrt_iarchive &ar,
const unsigned int version) {}
virtual void serialize(Archive::log_archive &ar, const unsigned int version) {
}
template <class ArchiveT>
void save_override(ArchiveT &ar, const unsigned int version) {}
@@ -139,7 +148,6 @@ public:
signals:
virtual void Updated();
virtual void PropertyUpdated();
// Qt4 style connector //
static bool connect(const Object *ob1, const char *signal_name,
@@ -160,8 +168,8 @@ public:
connect(typename FunctionPointer<Func1>::Object *sender, Func1 sigf,
typename FunctionPointer<Func2>::Object *receiver, Func2 slof) {
SignalBase *sigb = sender->findOrAddSignal(sigf);
return ConnectSignal<typename FunctionPointer<Func1>::SignalSignature>(sigb, slof,
receiver);
return ConnectSignal<typename FunctionPointer<Func1>::SignalSignature>(
sigb, slof, receiver);
}
// Lambda/Function object connector //
@@ -183,9 +191,10 @@ public:
}
template <typename FuncT>
static inline Connection connect(SignalBase *sigb, FuncT slof, Object *receiver) {
return ConnectSignal<typename FunctionPointer<FuncT>::SignalSignature>(sigb, slof,
receiver);
static inline Connection connect(SignalBase *sigb, FuncT slof,
Object *receiver) {
return ConnectSignal<typename FunctionPointer<FuncT>::SignalSignature>(
sigb, slof, receiver);
}
template <typename FuncT>

View File

@@ -8,7 +8,9 @@ ObjectsContext::ObjectsContext() : Object() {}
ObjectsContext::~ObjectsContext() {}
void ObjectsContext::AddObject(Object* obj) {
if (obj && std::find(m_objects.begin(), m_objects.end(), obj) == m_objects.end()) {
if (!obj || obj == this) return;
if (std::find(m_objects.begin(), m_objects.end(), obj) == m_objects.end()) {
m_objects.push_back(obj);
// Connect child's update to context's update to trigger re-renders
Object::connect(obj, &Object::Updated, this, &Object::Updated);

View File

@@ -53,9 +53,6 @@ public:
return GetGroup() + "." + GetName();
}
// Signal support
signals:
virtual void Updated() override { ULIB_SIGNAL_EMIT(PropertyBase::Updated); }
// Serialization support for different uLib archives
virtual void serialize(Archive::xml_oarchive & ar, const unsigned int version) override = 0;
@@ -106,6 +103,7 @@ public:
// Accessors
const T& Get() const { return *m_value; }
void Set(const T& value) {
if (!m_value) return;
T val = value;
if constexpr (std::is_arithmetic<T>::value) {
if (m_HasRange) { if (val < m_Min) val = m_Min; if (val > m_Max) val = m_Max; }
@@ -169,7 +167,6 @@ public:
virtual void serialize(Archive::property_register_archive & ar, const unsigned int v) override;
virtual void Updated() override { PropertyBase::Updated(); this->PropertyChanged(); }
protected:
std::string m_name;
@@ -263,7 +260,7 @@ public:
if (m_DisplayOnly) {
m_Object->RegisterDisplayProperty(newP);
Object* obj = m_Object;
Object::connect(newP, &PropertyBase::Updated, [obj]() { obj->Updated(); });
Object::connect(newP, &Object::Updated, [obj]() { obj->Updated(); });
} else {
m_Object->RegisterDynamicProperty(newP);
}
@@ -277,7 +274,7 @@ public:
if (m_DisplayOnly) {
m_Object->RegisterDisplayProperty(p);
Object* obj = m_Object;
Object::connect(p, &PropertyBase::Updated, [obj]() { obj->Updated(); });
Object::connect(p, &Object::Updated, [obj]() { obj->Updated(); });
} else {
m_Object->RegisterDynamicProperty(p);
}
@@ -300,7 +297,7 @@ public:
if (m_Object) {
EnumProperty* p = new EnumProperty(m_Object, t.name(), (int*)&const_cast<boost::serialization::hrp_enum<T>&>(t).value(), t.labels(), t.units() ? t.units() : "", GetCurrentGroup());
p->SetReadOnly(t.is_read_only());
if (m_DisplayOnly) { m_Object->RegisterDisplayProperty(p); Object* obj = m_Object; Object::connect(p, &PropertyBase::Updated, [obj]() { obj->Updated(); }); }
if (m_DisplayOnly) { m_Object->RegisterDisplayProperty(p); Object* obj = m_Object; Object::connect(p, &Object::Updated, [obj]() { obj->Updated(); }); }
else { m_Object->RegisterDynamicProperty(p); }
}
}
@@ -308,7 +305,7 @@ public:
if (m_Object) {
EnumProperty* p = new EnumProperty(m_Object, t.name(), (int*)&const_cast<boost::serialization::hrp_enum_val<T>&>(t).value(), t.labels(), t.units() ? t.units() : "", GetCurrentGroup());
p->SetReadOnly(t.is_read_only());
if (m_DisplayOnly) { m_Object->RegisterDisplayProperty(p); Object* obj = m_Object; Object::connect(p, &PropertyBase::Updated, [obj]() { obj->Updated(); }); }
if (m_DisplayOnly) { m_Object->RegisterDisplayProperty(p); Object* obj = m_Object; Object::connect(p, &Object::Updated, [obj]() { obj->Updated(); }); }
else { m_Object->RegisterDynamicProperty(p); }
}
}

View File

@@ -24,3 +24,5 @@ int main() {
std::cout << "Tests passed (compilation and manual instantiation)!" << std::endl;
return 0;
}

View File

@@ -37,27 +37,18 @@ Assembly::Assembly(const Assembly &copy)
m_GroupSelection(copy.m_GroupSelection) {}
Assembly::~Assembly() {
for (auto const& [obj, conn] : m_ChildConnections) {
conn.disconnect();
}
m_ChildConnections.clear();
}
void Assembly::AddObject(Object *obj) {
if (!obj || obj == this) return;
if (auto *at = dynamic_cast<AffineTransform *>(obj)) {
at->SetParent(this);
}
// Base class already handles the list and child->parent update connection
ObjectsContext::AddObject(obj);
// Connect to child updates to recompute AABB
m_ChildConnections[obj] = Object::connect(obj, &Object::Updated, [this](){
this->ComputeBoundingBox();
this->Updated(); // Signal that assembly itself changed (AABB-wise)
});
// Parent -> Child propagation for world matrix updates
Object::connect(this, &Object::Updated, obj, &Object::Updated);
this->ComputeBoundingBox();
}
@@ -67,12 +58,6 @@ void Assembly::RemoveObject(Object *obj) {
at->SetParent(nullptr);
}
auto itConn = m_ChildConnections.find(obj);
if (itConn != m_ChildConnections.end()) {
itConn->second.disconnect();
m_ChildConnections.erase(itConn);
}
ObjectsContext::RemoveObject(obj);
this->ComputeBoundingBox();
}

View File

@@ -31,6 +31,7 @@
#include "Math/Transform.h"
namespace uLib {
namespace Vtk { class Assembly; }
/**
* @brief Assembly groups geometric objects (ContainerBox, Cylinder, etc.)
@@ -46,6 +47,7 @@ namespace uLib {
class Assembly : public ObjectsContext, public TRS {
public:
uLibTypeMacro(Assembly, ObjectsContext, TRS)
friend class Vtk::Assembly;
Assembly();
@@ -112,7 +114,6 @@ private:
bool m_ShowBoundingBox;
bool m_GroupSelection;
bool m_InUpdated = false;
std::map<Object*, Connection> m_ChildConnections;
ULIB_DECLARE_PROPERTIES(Assembly)
};

View File

@@ -50,9 +50,9 @@
// #include <Eigen/src/Core/Matrix.h>
#include <stdlib.h>
#include <Eigen/Dense>
#include "Core/Types.h"
#include "Core/Property.h"
#include "Core/Types.h"
#include <Eigen/Dense>
//// BOOST SERIALIZATION ///////////////////////////////////////////////////////
@@ -150,7 +150,6 @@ typedef Eigen::MatrixXi MatrixXi;
typedef Eigen::MatrixXf MatrixXf;
typedef Eigen::MatrixXd MatrixXd;
////////////////////////////////////////////////////////////////////////////////
// Vector String interaction ///////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@@ -205,9 +204,7 @@ public:
typedef Eigen::Matrix<Scalarf, 4, 1> BaseClass;
_HPoint3f() : BaseClass(0, 0, 0, p) {}
_HPoint3f(int rows, int cols) : BaseClass() {
this->operator()(3) = p;
}
_HPoint3f(int rows, int cols) : BaseClass() { this->operator()(3) = p; }
_HPoint3f(float x, float y, float z) : BaseClass(x, y, z, p) {}
_HPoint3f(Vector3f &in) : BaseClass(in.homogeneous()) {
this->operator()(3) = p;

View File

@@ -23,8 +23,6 @@
//////////////////////////////////////////////////////////////////////////////*/
/*
* Copyright (C) 2012 Andrea Rigoni Garola <andrea.rigoni@pd.infn.it>
*
@@ -45,63 +43,43 @@
*
*/
#ifndef U_TRANSFORM_H
#define U_TRANSFORM_H
#include <Eigen/Geometry>
#include "Math/Units.h"
#include "Math/Dense.h"
#include "Math/Units.h"
#include <Eigen/Geometry>
namespace uLib {
using Eigen::Isometry3f;
using Eigen::Isometry3d;
using Eigen::Isometry3f;
using Eigen::Affine3f;
using Eigen::Affine3d;
using Eigen::Affine3f;
using Eigen::Projective3f;
using Eigen::Projective3d;
using Eigen::Projective3f;
////////////////////////////////////////////////////////////////////////////////
///////// AFFINE TRANSFORM WRAPPER //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
class AffineTransform : virtual public Object {
public:
uLibTypeMacro(AffineTransform, Object)
protected:
uLibTypeMacro(AffineTransform, Object) protected :
Affine3f m_T;
AffineTransform *m_Parent;
public:
AffineTransform() :
m_T(Matrix4f::Identity()),
m_Parent(NULL)
{}
AffineTransform() : m_T(Matrix4f::Identity()), m_Parent(NULL) {}
AffineTransform(AffineTransform *parent) :
m_T(Matrix4f::Identity()),
m_Parent(parent)
{}
AffineTransform(AffineTransform *parent)
: m_T(Matrix4f::Identity()), m_Parent(parent) {}
AffineTransform(const AffineTransform &copy) :
m_T(copy.m_T),
m_Parent(copy.m_Parent)
{}
AffineTransform(const AffineTransform &copy)
: m_T(copy.m_T), m_Parent(copy.m_Parent) {}
Affine3f &GetTransform() { return m_T; }
@@ -113,16 +91,18 @@ public:
Matrix4f &GetMatrix() { return m_T.matrix(); }
const Matrix4f &GetMatrix() const { return m_T.matrix(); }
Matrix4f GetWorldMatrix() const
{
if(!m_Parent) return m_T.matrix();
else return m_Parent->GetWorldMatrix() * m_T.matrix(); // T = B * A //
Matrix4f GetWorldMatrix() const {
if (!m_Parent)
return m_T.matrix();
else
return m_Parent->GetWorldMatrix() * m_T.matrix(); // T = B * A //
}
void SetWorldMatrix(const Matrix4f &mat)
{
if(!m_Parent) m_T.matrix() = mat;
else m_T.matrix() = m_Parent->GetWorldMatrix().inverse() * mat;
void SetWorldMatrix(const Matrix4f &mat) {
if (!m_Parent)
m_T.matrix() = mat;
else
m_T.matrix() = m_Parent->GetWorldMatrix().inverse() * mat;
}
void SetPosition(const Vector3f &v) { this->m_T.translation() = v; }
@@ -143,11 +123,9 @@ public:
this->m_T.linear().col(2).norm());
}
void Rotate(const Matrix3f &m) { this->m_T.rotate(m); }
void Rotate(const float angle, Vector3f axis)
{
void Rotate(const float angle, Vector3f axis) {
axis.normalize(); // prehaps not necessary ( see eigens )
Eigen::AngleAxisf ax(angle, axis);
this->m_T.rotate(Eigen::Quaternion<float>(ax));
@@ -160,27 +138,25 @@ public:
void PreRotate(const Matrix3f &m) { this->m_T.prerotate(m); }
void QuaternionRotate(const Vector4f &q)
{ this->m_T.rotate(Eigen::Quaternion<float>(q)); }
void QuaternionRotate(const Vector4f &q) {
this->m_T.rotate(Eigen::Quaternion<float>(q));
}
void EulerYZYRotate(const Vector3f &e) {
Matrix3f mat;
mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY())
* Eigen::AngleAxisf(e.y(), Vector3f::UnitZ())
* Eigen::AngleAxisf(e.z(), Vector3f::UnitY());
mat = Eigen::AngleAxisf(e.x(), Vector3f::UnitY()) *
Eigen::AngleAxisf(e.y(), Vector3f::UnitZ()) *
Eigen::AngleAxisf(e.z(), Vector3f::UnitY());
m_T.rotate(mat);
}
void FlipAxes(int first, int second)
{
void FlipAxes(int first, int second) {
Matrix3f mat = Matrix3f::Identity();
mat.col(first).swap(mat.col(second));
m_T.rotate(mat);
}
};
////////////////////////////////////////////////////////////////////////////////
///////// TRS PARAMETERS /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
@@ -189,8 +165,7 @@ typedef Eigen::Affine3f AffineMatrix;
class TRS : public AffineTransform {
uLibTypeMacro(TRS, AffineTransform)
ULIB_SERIALIZE_ACCESS
uLibTypeMacro(TRS, AffineTransform) ULIB_SERIALIZE_ACCESS
// ULIB_DECLARE_PROPERTIES(TRS)
public :
@@ -201,13 +176,9 @@ public:
TRS() = default;
TRS(const class AffineTransform& at) {
this->FromMatrix(at.GetMatrix());
}
TRS(const class AffineTransform &at) { this->FromMatrix(at.GetMatrix()); }
TRS(const Matrix4f& mat) {
this->FromMatrix(mat);
}
TRS(const Matrix4f &mat) { this->FromMatrix(mat); }
void FromMatrix(const Matrix4f &mat) {
this->position = mat.block<3, 1>(0, 3);
@@ -218,11 +189,14 @@ public:
this->scaling(2) = linear.col(2).norm();
Matrix3f rot = linear;
if (this->scaling(0) > 1e-6) rot.col(0) /= this->scaling(0);
if (this->scaling(1) > 1e-6) rot.col(1) /= this->scaling(1);
if (this->scaling(2) > 1e-6) rot.col(2) /= this->scaling(2);
if (this->scaling(0) > 1e-6)
rot.col(0) /= this->scaling(0);
if (this->scaling(1) > 1e-6)
rot.col(1) /= this->scaling(1);
if (this->scaling(2) > 1e-6)
rot.col(2) /= this->scaling(2);
Vector3f euler = rot.eulerAngles(2, 1, 0);
Vector3f euler = rot.canonicalEulerAngles(2, 1, 0);
this->rotation = Vector3f(euler(2), euler(1), euler(0));
this->SetMatrix(mat);
@@ -246,9 +220,7 @@ public:
this->SyncMatrix();
}
void SyncMatrix() {
this->GetTransform() = GetAffineMatrix();
}
void SyncMatrix() { this->GetTransform() = GetAffineMatrix(); }
void Updated() override {
this->SyncMatrix();
@@ -263,7 +235,6 @@ public:
ar &HRP(scaling);
}
AffineMatrix GetAffineMatrix() const {
AffineMatrix m = AffineMatrix::Identity();
m.translate(position);
@@ -274,19 +245,9 @@ public:
return m;
}
Matrix4f GetMatrix() const {
return this->GetAffineMatrix().matrix();
}
Matrix4f GetMatrix() const { return this->GetAffineMatrix().matrix(); }
};
inline std::ostream &operator<<(std::ostream &os, const TRS &trs) {
os << trs.position << " " << trs.rotation << " " << trs.scaling;
return os;
@@ -297,8 +258,6 @@ inline std::istream& operator>>(std::istream& is, TRS& trs) {
return is;
}
} // uLib
} // namespace uLib
#endif // U_TRANSFORM_H

View File

@@ -33,7 +33,11 @@ set(DICTIONARY_HEADERS muCastorMCTrack.h
SkinDetectorWriter.h)
set(LIBRARIES ${ROOT_LIBRARIES}
${PACKAGE_LIBPREFIX}Math)
${PACKAGE_LIBPREFIX}Math
Eigen3::Eigen)
get_target_property(EIGEN3_INC Eigen3::Eigen INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${EIGEN3_INC})
set(rDictName ${PACKAGE_LIBPREFIX}RootDict)
root_generate_dictionary(${rDictName} ${DICTIONARY_HEADERS}

View File

@@ -9,19 +9,17 @@
// Martin Subieta martin.subieta@ing.unibs.it
// ########################################
#include <iostream>
#include "vector"
#include "Detectors/MuonScatter.h"
#include "TObject.h"
#include "TParticle.h"
#include "Detectors/MuonScatter.h"
#include "vector"
#include <iostream>
class TClonesArray;
class muCastorMCTrack : public TObject
{
class muCastorMCTrack : public TObject {
public:
/** Default constructor **/
muCastorMCTrack();
@@ -32,7 +30,6 @@ class muCastorMCTrack : public TObject
virtual ~muCastorMCTrack();
void Reset();
/** Accessors **/
Int_t GetFirstDaughter() const { return fDaughter[0]; }
Int_t GetMother() const { return fMother[0]; }
@@ -52,7 +49,6 @@ class muCastorMCTrack : public TObject
}
public:
/* Private variables - copying private variables of TParticle */
Int_t fPdgCode; // PDG code of the particle
@@ -81,8 +77,8 @@ public:
Double_t *fPntE; //[fNpoints] array of points (E) belonging to this track
ClassDef(muCastorMCTrack, 1);
};
uLib::MuonScatter &operator << (uLib::MuonScatter &mu, const muCastorMCTrack &bsmu);
uLib::MuonScatter &operator<<(uLib::MuonScatter &mu,
const muCastorMCTrack &bsmu);
#endif // muCastor_MCTRACK_H

View File

@@ -4,6 +4,7 @@ set(HEADERS uLibVtkInterface.h
vtkQViewport.h
vtkViewport.h
vtkObjectsContext.h
vtkMultiSelectionProp.h
)
set(SOURCES uLibVtkInterface.cxx
@@ -12,6 +13,7 @@ set(SOURCES uLibVtkInterface.cxx
vtkQViewport.cpp
vtkViewport.cpp
vtkObjectsContext.cpp
vtkMultiSelectionProp.cpp
)
## Pull in Math VTK wrappers (sets MATH_SOURCES / MATH_HEADERS)

View File

@@ -61,9 +61,9 @@ public:
Content &GetModel();
uLib::Object* GetContent() const override;
void PrintSelf(std::ostream &o) const;
void PrintSelf(std::ostream &o) const override;
virtual vtkPolyData *GetPolyData() const;
vtkPolyData *GetPolyData() const override;
void AddPocaPoint(HPoint3f poca);

View File

@@ -28,33 +28,33 @@ using namespace uLib;
int main(int argc, char **argv) {
bool interactive = (argc > 1 && std::string(argv[1]) == "-i");
// ---- 1. Build model objects ----
ContainerBox box1;
box1.Scale(Vector3f(1_m, 2_m, 0.5_m));
box1.SetPosition(Vector3f(0, 0, 0));
// ---- 1. Build model objects on the heap (expected by uLib SmartPointer) ----
auto* box1 = new ContainerBox();
box1->Scale(Vector3f(1, 2, 0.5));
// box1->SetPosition(Vector3f(0, 0, 0));
ContainerBox box2;
box2.Scale(Vector3f(0.5_m, 0.5_m, 3_m));
box2.SetPosition(Vector3f(2_m, 0, 0));
auto* box2 = new ContainerBox();
box2->Scale(Vector3f(0.5, 0.5, 3));
box2->SetPosition(Vector3f(2, 0, 0));
Cylinder cyl(0.3_m, 1.5_m, 1);
cyl.SetPosition(Vector3f(0, 3_m, 0));
auto* cyl = new Cylinder(1, 1.5, 1);
cyl->SetPosition(Vector3f(0, 3, 0));
// ---- 2. Create an Assembly and add objects ----
Assembly assembly;
assembly.AddObject(&box1);
assembly.AddObject(&box2);
assembly.AddObject(&cyl);
assembly.SetShowBoundingBox(true);
auto* assembly = new Assembly();
assembly->AddObject(box1);
assembly->AddObject(box2);
assembly->AddObject(cyl);
assembly->SetShowBoundingBox(true);
// ---- 3. Apply a group transform ----
assembly.SetPosition(Vector3f(1_m, 1_m, 0));
// assembly->SetPosition(Vector3f(1_m, 1_m, 0));
// ---- 5. Visualize (create prop3ds to set properties) ----
Vtk::Assembly vtkAsm(&assembly);
Vtk::Assembly vtkAsm(assembly); // Vtk::Assembly takes ownership of the model wrapper
Vtk::Viewer viewer;
vtkAsm.AddToViewer(viewer); // This triggers prop3d creation via ConnectRenderer which eventually calls Prop3D::GetProp
vtkAsm.AddToViewer(viewer);
// Explicitly update to ensure prop3ds exist and are added to assemblies
vtkAsm.Update();
@@ -74,16 +74,16 @@ int main(int argc, char **argv) {
}
};
setProps(childCtx->GetProp3D(&box1), 1.0, 0.0, 0.0); // Red
setProps(childCtx->GetProp3D(&box2), 0.0, 1.0, 0.0); // Green
setProps(childCtx->GetProp3D(&cyl), 0.0, 0.0, 1.0); // Blue
setProps(childCtx->GetProp3D(box1), 1.0, 0.0, 0.0); // Red
setProps(childCtx->GetProp3D(box2), 0.0, 1.0, 0.0); // Green
setProps(childCtx->GetProp3D(cyl), 0.0, 0.0, 1.0); // Blue
}
std::cout << "Prop3Ds in viewport: " << viewer.getProp3Ds().size() << " (Expected 4: 1 assembly + 3 children)" << std::endl;
// ---- 4. Query the bounding box for terminal output ----
Vector3f bbMin, bbMax;
assembly.GetBoundingBox(bbMin, bbMax);
assembly->GetBoundingBox(bbMin, bbMax);
std::cout << "Assembly bounding box:" << std::endl;
std::cout << " min = " << bbMin.transpose() << std::endl;
std::cout << " max = " << bbMax.transpose() << std::endl;

View File

@@ -25,7 +25,6 @@
#include "Vtk/uLibVtkViewer.h"
#include "Vtk/Math/vtkContainerBox.h"
#include "Math/Units.h"
#include "Vtk/Math/vtkContainerBox.h"
@@ -36,19 +35,23 @@ using namespace uLib;
int main() {
BEGIN_TESTING(vtk ContainerBox Test);
ContainerBox box;
box.Scale(Vector3f(1_m,2_m,1_m));
box.SetPosition(Vector3f(0,0,0));
{
ContainerBox* box = new ContainerBox();
box->Scale(Vector3f(1_m, 2_m, 1_m));
box->SetPosition(Vector3f(0, 0, 0));
Vtk::ContainerBox v_box(&box);
Vtk::ContainerBox v_box(box);
v_box.Update();
// v_box.SetRepresentation(Vtk::Prop3D::Surface);
// v_box.SetOpacity(0.5);
// v_box.SetSelectable(true);
v_box.SetRepresentation(Vtk::Prop3D::Surface);
v_box.SetOpacity(0.5);
v_box.SetSelectable(true);
}
box.findOrAddSignal(&Object::Updated)->connect([&box](){
std::cout << "box updated: " << box.GetWorldPoint(HPoint3f(1,1,1)) << std::endl;
Vtk::ContainerBox v_box;
v_box.findOrAddSignal(&Object::Updated)->connect([&v_box]() {
std::cout << "box updated: "
<< v_box.get()->GetWorldPoint(HPoint3f(1, 1, 1)) << std::endl;
});
if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {

View File

@@ -15,11 +15,11 @@ using namespace uLib;
int main() {
std::cout << "Creating ContainerBox..." << std::endl;
ContainerBox box(Vector3f(1.0, 1.0, 1.0)); // 1x1x1 unit box
box.SetInstanceName("MyTestBox");
ContainerBox* box = new ContainerBox(Vector3f(1.0, 1.0, 1.0)); // 1x1x1 unit box
box->SetInstanceName("MyTestBox");
std::cout << "Creating VTK representation..." << std::endl;
Vtk::ContainerBox v_box(&box);
Vtk::ContainerBox v_box(box);
v_box.SetRepresentation(Vtk::Prop3D::Wireframe);
v_box.SetColor(1.0, 0.0, 0.0); // Red

View File

@@ -45,15 +45,13 @@ Assembly::Assembly(uLib::Assembly *content)
Assembly::~Assembly() {
delete m_ChildContext;
if (m_BBoxActor) m_BBoxActor->Delete();
if (m_VtkAsm) m_VtkAsm->Delete();
}
// ------------------------------------------------------------------ //
void Assembly::InstallPipe() {
// 1. Create the VTK library assembly that groups everything
m_VtkAsm = ::vtkAssembly::New();
m_VtkAsm->PickableOff();
// 1. Setup the internal VTK assembly
m_VtkAsm = vtkSmartPointer<::vtkAssembly>::New();
m_BBoxActor = vtkSmartPointer<::vtkActor>::New();
this->SetProp(m_VtkAsm);
// 2. Create the bounding-box wireframe actor
@@ -64,7 +62,6 @@ void Assembly::InstallPipe() {
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(cube->GetOutputPort());
m_BBoxActor = vtkActor::New();
m_BBoxActor->SetMapper(mapper);
m_BBoxActor->GetProperty()->SetRepresentationToWireframe();
m_BBoxActor->GetProperty()->SetColor(1.0, 0.85, 0.0); // gold wireframe
@@ -93,14 +90,7 @@ void Assembly::Update() {
if (m_InUpdate) return;
m_InUpdate = true;
if (this->m_model && m_VtkAsm) {
// Apply world matrix from the assembly content
vtkNew<vtkMatrix4x4> m;
Matrix4fToVtk(this->m_model->GetMatrix(), m);
m_VtkAsm->SetUserMatrix(m);
m_VtkAsm->Modified();
}
// Delegate to Prop3D to handle standard transformation application (uses GetContent())
this->Prop3D::Update();
this->UpdateBoundingBox();
if (m_ChildContext)
@@ -110,25 +100,29 @@ void Assembly::Update() {
void Assembly::SyncFromVtk() {
if (m_InUpdate) return;
if (!this->m_model || !m_VtkAsm) return;
m_InUpdate = true;
// VTK -> Model: Update world matrix (accounting for model parents)
if (vtkProp3D* proxy = this->GetProxyProp()) {
this->m_model->SetWorldMatrix(VtkToMatrix4f(proxy->GetUserMatrix()));
this->m_model->FromMatrix(this->m_model->GetMatrix());
}
// Sync the group-level transformation from VTK to the domain model
this->Prop3D::SyncFromVtk();
this->UpdateBoundingBox();
// Propagate sync to children
if (m_ChildContext)
m_ChildContext->SyncFromVtk();
this->m_model->Updated(); // Notify change in model
m_InUpdate = false;
}
void Assembly::serialize_display(uLib::Archive::display_properties_archive &ar, const unsigned int version) {
// 1. Register base class appearance/transform
this->Prop3D::serialize_display(ar, version);
// 2. Map domain model properties (Bounding Box visibility, etc.)
if (this->m_model) {
ar & HRP("ShowBoundingBox", m_model->m_ShowBoundingBox);
}
}
// ------------------------------------------------------------------ //
void Assembly::UpdateBoundingBox() {
if (!this->m_model || !m_BBoxActor) return;
@@ -188,10 +182,6 @@ void Assembly::UpdateBoundingBox() {
m_BBoxActor->Modified();
}
// ------------------------------------------------------------------ //
ObjectsContext *Assembly::GetChildrenContext() const {
return m_ChildContext;
}
} // namespace Vtk
} // namespace uLib

View File

@@ -16,8 +16,9 @@
#include "Math/Assembly.h"
#include "Vtk/uLibVtkInterface.h"
class vtkActor;
class vtkAssembly; // VTK library forward declaration (must be before namespace)
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkAssembly.h>
namespace uLib {
namespace Vtk {
@@ -39,33 +40,36 @@ class Assembly : public Prop3D, public uLib::ObjectWrapper<uLib::Assembly> {
public:
uLibTypeMacro(Assembly, Prop3D)
Assembly(uLib::Assembly *content);
/**
* @brief Constructor.
* @param content Pointer to the domain assembly model.
*/
explicit Assembly(uLib::Assembly *content);
virtual ~Assembly();
/** @brief Updates the VTK representation from the model (model→VTK). */
virtual void Update() override;
/** @brief Synchronizes the model from the VTK representation (VTK→model). */
virtual void SyncFromVtk() override;
virtual uLib::Object* GetContent() const override { return (uLib::Object*)m_model.get(); }
virtual uLib::ObjectsContext* GetChildren() override { return (uLib::ObjectsContext*)m_model.get(); }
/**
* @brief Returns the prop3d managing child objects.
*/
/** @brief Returns the visualization context for children. */
uLib::Vtk::ObjectsContext* GetChildrenContext() const { return m_ChildContext; }
/** @brief Returns the prop3d managing child objects. */
ObjectsContext *GetChildrenContext() const;
/** @brief Property serialization for Display Properties Panel. */
void serialize_display(Archive::display_properties_archive &ar, const unsigned int version = 0) override;
private:
void UpdateBoundingBox();
void InstallPipe();
ObjectsContext *m_ChildContext;
vtkActor *m_BBoxActor;
::vtkAssembly *m_VtkAsm; // VTK library assembly — NOT this class
bool m_InUpdate; // re-entrancy guard
vtkSmartPointer<::vtkAssembly> m_VtkAsm;
vtkSmartPointer<::vtkActor> m_BBoxActor;
uLib::Vtk::ObjectsContext *m_ChildContext;
bool m_InUpdate;
};
} // namespace Vtk

View File

@@ -43,7 +43,6 @@
#include "Vtk/Math/vtkDense.h"
namespace uLib {
namespace Vtk {
@@ -53,38 +52,34 @@ struct ContainerBoxData {
vtkSmartPointer<vtkAssembly> m_VtkAsm;
uLib::Connection m_UpdateSignal;
ContainerBoxData() : m_Cube(vtkSmartPointer<vtkActor>::New()),
ContainerBoxData()
: m_Cube(vtkSmartPointer<vtkActor>::New()),
m_Axes(vtkSmartPointer<vtkActor>::New()),
m_VtkAsm(vtkSmartPointer<vtkAssembly>::New()) {}
~ContainerBoxData() {
m_UpdateSignal.disconnect();
}
};
ContainerBox::ContainerBox(ContainerBox::Content *content)
: d(new ContainerBoxData()), ObjectWrapper(content) {
: d(new ContainerBoxData()),
ObjectWrapper(content ? content : new Content()) {
this->InstallPipe();
d->m_UpdateSignal =
Object::connect(this->m_model.get(), &uLib::Object::Updated, this, &ContainerBox::Update);
d->m_UpdateSignal = Object::connect(
this->m_model.get(), &uLib::Object::Updated, this, &ContainerBox::Update);
}
ContainerBox::~ContainerBox() {
delete d;
}
ContainerBox::~ContainerBox() { delete d; }
vtkPolyData *ContainerBox::GetPolyData() const {
// TODO
return NULL;
}
void ContainerBox::Update() {
RecursiveMutex::ScopedLock lock(this->m_UpdateMutex);
if (!this->m_model) return;
if (!this->m_model)
return;
vtkProp3D *prop = vtkProp3D::SafeDownCast(this->GetProp());
if (prop) {
@@ -102,30 +97,6 @@ void ContainerBox::Update() {
void ContainerBox::SyncFromVtk() {
RecursiveMutex::ScopedLock lock(this->m_UpdateMutex);
if (!this->m_model) return;
vtkProp3D* root = this->GetProxyProp();
if (!root) return;
// VTK -> Model: Extract new world TRS from proxy, which matches the model's TRS center
vtkMatrix4x4* rootMat = root->GetUserMatrix();
Matrix4f vtkWorld = VtkToMatrix4f(rootMat);
// Synchronize TRS property members from the updated local matrix
this->m_model->FromMatrix(vtkWorld);
// Since we modified the model, notify observers, but block the loop back to VTK
// ConnectionBlock blocker(d->m_UpdateSignal);
this->m_model->Updated();
}
void ContainerBox::InstallPipe() {
if (!this->m_model)
return;

View File

@@ -42,11 +42,11 @@ class ContainerBox : public Prop3D,
public Polydata,
public uLib::ObjectWrapper<uLib::ContainerBox> {
uLibTypeMacro(ContainerBox, Prop3D, Polydata)
typedef uLib::ContainerBox Content;
uLibTypeMacro(ContainerBox, Prop3D,
Polydata) typedef uLib::ContainerBox Content;
public:
ContainerBox(Content *content);
ContainerBox(Content *content = nullptr);
~ContainerBox();
virtual class vtkPolyData *GetPolyData() const override;
@@ -59,7 +59,7 @@ public:
/**
* @brief Synchronizes the model from the VTK representation (VTK→model).
*/
virtual void SyncFromVtk() override;
virtual uLib::Object *GetContent() const override {
return (uLib::Object *)m_model.get();

View File

@@ -44,8 +44,7 @@ Cylinder::Cylinder(Cylinder::Content *content)
}
Cylinder::~Cylinder() {
if (m_Actor) m_Actor->Delete();
if (m_VtkAsm) m_VtkAsm->Delete();
m_UpdateSignal.disconnect();
}
void Cylinder::Update() {
@@ -85,26 +84,13 @@ void Cylinder::Update() {
this->Prop3D::Update();
}
void Cylinder::SyncFromVtk() {
if (!this->m_model) return;
vtkProp3D* root = this->GetProxyProp();
if (!root) return;
// VTK -> Model: Extract new world TRS from proxy
vtkMatrix4x4* rootMat = root->GetUserMatrix();
Matrix4f vtkWorld = VtkToMatrix4f(rootMat);
// Directly sync model from the world matrix
this->m_model->FromMatrix(vtkWorld);
this->m_model->Updated();
}
void Cylinder::InstallPipe() {
if (!this->m_model)
return;
m_VtkAsm = ::vtkAssembly::New();
m_VtkAsm = vtkSmartPointer<::vtkAssembly>::New();
this->SetProp(m_VtkAsm);
vtkNew<vtkCylinderSource> cylinder;
@@ -112,7 +98,7 @@ void Cylinder::InstallPipe() {
cylinder->SetHeight(1.0);
cylinder->SetResolution(32);
m_Actor = vtkActor::New();
m_Actor = vtkSmartPointer<vtkActor>::New();
vtkNew<vtkTransform> alignment;
m_Actor->SetUserTransform(alignment);

View File

@@ -30,6 +30,7 @@
#include "Math/Cylinder.h"
#include "Vtk/uLibVtkInterface.h"
#include <vtkActor.h>
#include <vtkSmartPointer.h>
class vtkAssembly;
namespace uLib {
@@ -53,7 +54,7 @@ public:
virtual void Update() override;
/** Synchronizes the uLib model matrix with the VTK actor specifically for gizmo interactions */
virtual void SyncFromVtk() override;
virtual uLib::Object *GetContent() const override {
return (uLib::Object *)m_model.get();
@@ -63,8 +64,8 @@ protected:
/** Sets up the VTK visualization pipeline */
virtual void InstallPipe();
vtkActor *m_Actor;
::vtkAssembly *m_VtkAsm;
vtkSmartPointer<vtkActor> m_Actor;
vtkSmartPointer<::vtkAssembly> m_VtkAsm;
uLib::Connection m_UpdateSignal;
};

View File

@@ -54,11 +54,11 @@ public:
void ReadFromStlFile(const char *filename);
virtual class vtkPolyData *GetPolyData() const;
vtkPolyData *GetPolyData() const override;
virtual void contentUpdate();
virtual void Update();
void Update() override;
uLib::Object *GetContent() const override {
return (uLib::Object *)m_model.get();
}

View File

@@ -54,11 +54,11 @@ public:
void ReadFromStlFile(const char *filename);
virtual class vtkPolyData *GetPolyData() const;
vtkPolyData *GetPolyData() const override;
virtual void contentUpdate();
virtual void Update();
void Update() override;
uLib::Object *GetContent() const override {
return (uLib::Object *)m_model.get();
}

View File

@@ -308,17 +308,7 @@ void VoxImage::serialize_display(uLib::Archive::display_properties_archive & ar,
{"MIP", "Composite", "Composite Shaded", "MIP Bone", "MIP Hot", "Additive"});
}
void VoxImage::SyncFromVtk() {
if (auto *root = this->GetProxyProp()) {
vtkMatrix4x4 *rootMat = root->GetUserMatrix();
if (rootMat) {
Matrix4f vtkLocal = VtkToMatrix4f(rootMat);
// Synchronize TRS from VTK, compensating for local volume offset
this->m_model->FromMatrix(vtkLocal); // * this->m_model->GetLocalMatrix().inverse());
this->m_model->Updated();
}
}
}
void VoxImage::Update() {
if (auto *root = vtkProp3D::SafeDownCast(this->GetProp())) {

View File

@@ -77,7 +77,7 @@ public:
void RescaleShaderRange();
void Update() override;
void SyncFromVtk() override;
void serialize_display(uLib::Archive::display_properties_archive &ar,
const unsigned int version = 0) override;

View File

@@ -11,6 +11,7 @@
#include "Vtk/uLibVtkInterface.h"
#include "Core/Property.h"
#include "Math/Dense.h"
#include <iostream>
#include <cassert>
#include "testing-prototype.h"
@@ -33,7 +34,7 @@ int main() {
// Verify specific properties exist
Property<double>* opacityProp = nullptr;
Property<double>* colorRProp = nullptr;
Property<Vector3d>* colorProp = nullptr;
for (auto* prop : props) {
std::cout << " - [" << prop->GetTypeName() << "] " << prop->GetName()
@@ -42,13 +43,13 @@ int main() {
if (prop->GetName() == "Opacity") {
opacityProp = dynamic_cast<Property<double>*>(prop);
}
if (prop->GetName() == "ColorR") {
colorRProp = dynamic_cast<Property<double>*>(prop);
if (prop->GetName() == "Color") {
colorProp = dynamic_cast<Property<Vector3d>*>(prop);
}
}
assert(opacityProp != nullptr && "Opacity property not registered!");
assert(colorRProp != nullptr && "ColorR property not registered!");
assert(colorProp != nullptr && "Color property not registered!");
// Test modification via uLib Property interface
std::cout << "Modifying Opacity via property proxy (0.25)..." << std::endl;
@@ -58,9 +59,9 @@ int main() {
assert(opacityProp->Get() == 0.25);
assert(opacityProp->GetValueAsString().find("0.25") != std::string::npos);
std::cout << "Modifying ColorR via property proxy (0.9)..." << std::endl;
*colorRProp = 0.9;
assert(colorRProp->Get() == 0.9);
std::cout << "Modifying Color via property proxy (0.9, 0.1, 0.1)..." << std::endl;
*colorProp = Vector3d(0.9, 0.1, 0.1);
assert(colorProp->Get().x() == 0.9);
std::cout << "All Prop3D Property Registration Tests PASSED!" << std::endl;

View File

@@ -93,14 +93,28 @@ public:
m_Selectable(true),
m_Selected(false),
m_Visibility(true),
m_Dragable(true)
m_Dragable(true),
m_HighlightMode(Prop3D::HighlightPlain)
{
m_Color = Vector3d(-1, -1, -1);
m_PrevMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
m_PrevMatrix->Identity();
}
~Prop3DData() {
// No manual Delete needed for smart pointers
if (m_Renderers) {
m_Renderers->InitTraversal();
for (int i = 0; i < m_Renderers->GetNumberOfItems(); ++i) {
vtkRenderer* ren = m_Renderers->GetNextItem();
if (ren) {
if (m_Prop) ren->RemoveViewProp(m_Prop);
if (m_OutlineActor) ren->RemoveActor(m_OutlineActor);
if (m_CubeAxesActor) ren->RemoveActor(m_CubeAxesActor);
if (m_HighlightActor) ren->RemoveActor(m_HighlightActor);
}
}
m_Renderers->RemoveAllItems();
}
}
Prop3D *m_Prop3D;
@@ -125,6 +139,10 @@ public:
bool m_Visibility;
bool m_Dragable;
int m_HighlightMode; // 0: Plain, 1: Corners
vtkSmartPointer<vtkMatrix4x4> m_PrevMatrix;
//
TRS m_Transform;
@@ -212,28 +230,17 @@ public:
}
if (!m_HighlightActor) {
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
double bounds[6];
polydata->GetBounds(bounds);
// Add a small padding to prevent z-fighting
double maxDim = std::max({bounds[1]-bounds[0], bounds[3]-bounds[2], bounds[5]-bounds[4]});
double pad = maxDim * 0.02;
if(pad < 1e-4) pad = 0.05;
cube->SetBounds(bounds[0]-pad, bounds[1]+pad,
bounds[2]-pad, bounds[3]+pad,
bounds[4]-pad, bounds[5]+pad);
m_HighlightActor = vtkSmartPointer<vtkActor>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(cube->GetOutputPort());
m_HighlightActor->SetMapper(mapper);
m_HighlightActor->GetProperty()->SetRepresentationToWireframe();
m_HighlightActor->GetProperty()->SetColor(1.0, 0.0, 0.0); // Red
m_HighlightActor->GetProperty()->SetLineWidth(2.0);
m_HighlightActor->GetProperty()->SetLighting(0);
} else {
if (auto* mapper = vtkPolyDataMapper::SafeDownCast(m_HighlightActor->GetMapper())) {
if (auto* cube = vtkCubeSource::SafeDownCast(mapper->GetInputAlgorithm())) {
}
if (m_HighlightMode == Prop3D::HighlightPlain) {
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
double bounds[6];
polydata->GetBounds(bounds);
double maxDim = std::max({bounds[1]-bounds[0], bounds[3]-bounds[2], bounds[5]-bounds[4]});
@@ -242,9 +249,52 @@ public:
cube->SetBounds(bounds[0]-pad, bounds[1]+pad,
bounds[2]-pad, bounds[3]+pad,
bounds[4]-pad, bounds[5]+pad);
cube->Modified();
cube->Update();
m_HighlightActor->GetMapper()->SetInputConnection(cube->GetOutputPort());
} else {
// Corners mode logic
double bounds[6];
polydata->GetBounds(bounds);
double maxDim = std::max({bounds[1]-bounds[0], bounds[3]-bounds[2], bounds[5]-bounds[4]});
double pad = maxDim * 0.02;
if(pad < 1e-4) pad = 0.05;
double b[6] = {bounds[0]-pad, bounds[1]+pad, bounds[2]-pad, bounds[3]+pad, bounds[4]-pad, bounds[5]+pad};
vtkNew<vtkPoints> points;
vtkNew<vtkCellArray> lines;
float len[3] = {
(float)(b[1] - b[0]) * 0.15f,
(float)(b[3] - b[2]) * 0.15f,
(float)(b[5] - b[4]) * 0.15f
};
for (int i = 0; i < 8; ++i) {
double p[3];
p[0] = b[(i & 1) ? 1 : 0];
p[1] = b[(i & 2) ? 1 : 0];
p[2] = b[(i & 4) ? 1 : 0];
for (int axis = 0; axis < 3; ++axis) {
double p2[3] = {p[0], p[1], p[2]};
double delta = (i & (1 << axis)) ? -len[axis] : len[axis];
p2[axis] += delta;
vtkIdType id1 = points->InsertNextPoint(p);
vtkIdType id2 = points->InsertNextPoint(p2);
lines->InsertNextCell(2);
lines->InsertCellPoint(id1);
lines->InsertCellPoint(id2);
}
}
vtkNew<vtkPolyData> cornerPoly;
cornerPoly->SetPoints(points);
cornerPoly->SetLines(lines);
if (auto* mapper = vtkPolyDataMapper::SafeDownCast(m_HighlightActor->GetMapper())) {
mapper->SetInputData(cornerPoly);
}
}
// Update highlight matrix from the model world matrix
@@ -254,6 +304,7 @@ public:
vtkNew<vtkMatrix4x4> vwm;
Matrix4fToVtk(tr->GetWorldMatrix(), vwm);
m_HighlightActor->SetUserMatrix(vwm);
m_PrevMatrix->DeepCopy(vwm);
}
}
}
@@ -290,9 +341,6 @@ public:
Prop3D::Prop3D() : Object(), pd(new Prop3DData(this)) {
ULIB_ACTIVATE_DISPLAY_PROPERTIES;
for (auto* p : this->GetDisplayProperties()) {
uLib::Object::connect(p, &uLib::PropertyBase::Updated, this, &Prop3D::Update);
}
}
Prop3D::~Prop3D()
@@ -517,6 +565,12 @@ void Prop3D::SetRepresentation(const char *mode)
else if (s == "slice") SetRepresentation(Slice);
}
void Prop3D::SetHighlightMode(HighlightMode mode)
{
pd->m_HighlightMode = static_cast<int>(mode);
pd->UpdateHighlight();
}
void Prop3D::SetColor(double r, double g, double b)
{
pd->m_Color[0] = r;
@@ -531,6 +585,18 @@ void Prop3D::SetOpacity(double alpha)
pd->ApplyAppearance(pd->m_Prop);
}
void Prop3D::GetColor(double &r, double &g, double &b) const
{
r = pd->m_Color[0];
g = pd->m_Color[1];
b = pd->m_Color[2];
}
double Prop3D::GetOpacity() const
{
return pd->m_Opacity;
}
@@ -570,6 +636,7 @@ void Prop3D::ApplyProp3DTransform(vtkProp3D* prop)
Matrix4fToVtk(tr->GetMatrix(), m);
prop->SetUserMatrix(m);
prop->Modified();
pd->m_PrevMatrix->DeepCopy(m);
}
}
}
@@ -580,7 +647,18 @@ void Prop3D::SyncFromVtk()
if (auto* tr = dynamic_cast<uLib::TRS*>(content)) {
if (auto* proxy = this->GetProxyProp()) {
if (vtkMatrix4x4* mat = proxy->GetUserMatrix()) {
tr->FromMatrix(VtkToMatrix4f(mat));
// Calculate Delta: currentMatrix * Inv(m_PrevMatrix)
vtkNew<vtkMatrix4x4> invPrev;
vtkMatrix4x4::Invert(pd->m_PrevMatrix, invPrev);
vtkNew<vtkMatrix4x4> delta;
vtkMatrix4x4::Multiply4x4(mat, invPrev, delta);
// Apply delta to world matrix
Matrix4f nextWorldMatrix = VtkToMatrix4f(delta) * tr->GetWorldMatrix();
tr->SetWorldMatrix(nextWorldMatrix);
pd->m_PrevMatrix->DeepCopy(mat);
content->Updated();
}
}
@@ -658,6 +736,8 @@ struct AppearanceProxy {
ar & boost::serialization::make_hrp("Dragable", pd->m_Dragable);
ar & boost::serialization::make_hrp("ShowBoundingBox", pd->m_ShowBoundingBox);
ar & boost::serialization::make_hrp("ShowScaleMeasures", pd->m_ShowScaleMeasures);
ar & boost::serialization::make_hrp_enum("HighlightMode",
pd->m_HighlightMode, {"Plain", "Corners"});
}
};

View File

@@ -86,8 +86,10 @@ public:
void DisonnectViewer(Viewer *viewer);
void SetColor(double r, double g, double b);
void GetColor(double &r, double &g, double &b) const;
void SetOpacity(double alpha);
double GetOpacity() const;
void SetSelectable(bool selectable = true);
bool IsSelectable() const;
@@ -123,6 +125,12 @@ public:
void SetRepresentation(Representation mode);
void SetRepresentation(const char *mode);
enum HighlightMode {
HighlightPlain = 0,
HighlightCorners = 1
};
void SetHighlightMode(HighlightMode mode);
virtual void PrintSelf(std::ostream &o) const;
void ShowBoundingBox(bool show);
@@ -222,7 +230,7 @@ public:
m_Prop3D->RegisterDisplayProperty(p);
Vtk::Prop3D *prop3d = m_Prop3D;
uLib::Object::connect(p, &uLib::PropertyBase::Updated,
uLib::Object::connect(p, &uLib::Object::Updated,
[prop3d]() { prop3d->Update(); });
}
}
@@ -236,7 +244,7 @@ public:
t.labels(), t.units() ? t.units() : "", GetCurrentGroup());
m_Prop3D->RegisterDisplayProperty(p);
Vtk::Prop3D *prop3d = m_Prop3D;
uLib::Object::connect(p, &uLib::PropertyBase::Updated,
uLib::Object::connect(p, &uLib::Object::Updated,
[prop3d]() { prop3d->Update(); });
}
}
@@ -295,7 +303,7 @@ public:
if (m_Prop3D) {
m_Prop3D->RegisterDisplayProperty(&p);
Vtk::Prop3D *prop3d = m_Prop3D;
uLib::Object::connect(&p, &uLib::PropertyBase::Updated,
uLib::Object::connect(&p, &uLib::Object::Updated,
[prop3d]() { prop3d->Update(); });
}
}

View File

@@ -0,0 +1,175 @@
#include "uLibVtkInterface.h"
#include "vtkMultiSelectionProp.h"
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkCubeSource.h>
#include <vtkProperty.h>
#include <vtkMatrix4x4.h>
#include <vtkRenderer.h>
#include <vtkRendererCollection.h>
#include "Math/Transform.h"
#include "Vtk/Math/vtkDense.h"
namespace uLib {
namespace Vtk {
MultiSelectionProp::MultiSelectionProp() : Prop3D() {
((::uLib::Object*)this)->SetInstanceName("Selection Group");
m_PrevMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
m_GroupHighlightActor = vtkSmartPointer<vtkActor>::New();
vtkNew<vtkPolyDataMapper> mapper;
m_GroupHighlightActor->SetMapper(mapper);
m_GroupHighlightActor->GetProperty()->SetRepresentationToWireframe();
m_GroupHighlightActor->GetProperty()->SetLineWidth(2.0);
m_GroupHighlightActor->GetProperty()->SetLighting(0);
m_GroupHighlightActor->PickableOff();
// Set default display color in Prop3D state
((Prop3D*)this)->SetColor(0.0, 1.0, 0.0);
}
MultiSelectionProp::~MultiSelectionProp() {
}
MultiSelectionProp* MultiSelectionProp::Clone() const {
auto* copy = new MultiSelectionProp();
copy->SetMembers(this->m_Members);
((::uLib::Object*)copy)->SetInstanceName(((::uLib::Object*)this)->GetInstanceName());
return copy;
}
void MultiSelectionProp::SetMembers(const std::vector<Prop3D*>& members) {
m_Members = members;
Update();
// Reset prev matrix to current highlight position
if (m_GroupHighlightActor->GetUserMatrix()) {
m_PrevMatrix->DeepCopy(m_GroupHighlightActor->GetUserMatrix());
} else {
m_PrevMatrix->Identity();
}
}
void MultiSelectionProp::Update() {
if (m_Members.empty()) {
m_GroupHighlightActor->VisibilityOff();
return;
}
m_GroupHighlightActor->VisibilityOn();
double combinedBounds[6] = {VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
VTK_DOUBLE_MAX, VTK_DOUBLE_MIN};
for (auto* member : m_Members) {
if (vtkProp* prop = member->GetProp()) {
double* b = prop->GetBounds();
if (b) {
for (int i = 0; i < 3; ++i) {
if (b[2*i] < combinedBounds[2*i]) combinedBounds[2*i] = b[2*i];
if (b[2*i+1] > combinedBounds[2*i+1]) combinedBounds[2*i+1] = b[2*i+1];
}
}
}
}
if (combinedBounds[0] > combinedBounds[1]) return;
vtkNew<vtkCubeSource> cube;
double maxDim = std::max({combinedBounds[1]-combinedBounds[0],
combinedBounds[3]-combinedBounds[2],
combinedBounds[5]-combinedBounds[4]});
double pad = maxDim * 0.02;
if (pad < 1e-4) pad = 0.05;
cube->SetBounds(combinedBounds[0]-pad, combinedBounds[1]+pad,
combinedBounds[2]-pad, combinedBounds[3]+pad,
combinedBounds[4]-pad, combinedBounds[5]+pad);
cube->Update();
if (auto* mapper = vtkPolyDataMapper::SafeDownCast(m_GroupHighlightActor->GetMapper())) {
mapper->SetInputConnection(cube->GetOutputPort());
}
// Apply TRS from m_SelectionTransform
vtkNew<vtkMatrix4x4> trsMatrix;
Matrix4fToVtk(m_SelectionTransform.GetWorldMatrix(), trsMatrix);
m_GroupHighlightActor->SetUserMatrix(trsMatrix);
// Apply Display Properties (Color, Opacity)
double r, g, b;
((Prop3D*)this)->GetColor(r, g, b);
m_GroupHighlightActor->GetProperty()->SetColor(r, g, b);
m_GroupHighlightActor->GetProperty()->SetOpacity(((Prop3D*)this)->GetOpacity());
// Update Prev Matrix for delta calculations
m_PrevMatrix->DeepCopy(trsMatrix);
// Ensure it's in the renderers
vtkRendererCollection* rens = ((Prop3D*)this)->GetRenderers();
rens->InitTraversal();
for (int i = 0; i < rens->GetNumberOfItems(); ++i) {
vtkRenderer* ren = rens->GetNextItem();
ren->AddActor(m_GroupHighlightActor);
}
}
void MultiSelectionProp::SyncFromVtk() {
if (m_Members.empty()) return;
vtkMatrix4x4* currentMatrix = m_GroupHighlightActor->GetUserMatrix();
if (!currentMatrix) return;
// Calculate Delta: currentMatrix * Inv(m_PrevMatrix)
vtkNew<vtkMatrix4x4> invPrev;
vtkMatrix4x4::Invert(m_PrevMatrix, invPrev);
vtkNew<vtkMatrix4x4> delta;
vtkMatrix4x4::Multiply4x4(currentMatrix, invPrev, delta);
// Apply delta to all members
for (auto* member : m_Members) {
if (auto* content = member->GetContent()) {
if (auto* tr = dynamic_cast<uLib::TRS*>(content)) {
vtkNew<vtkMatrix4x4> memberWorldMatrix;
Matrix4fToVtk(tr->GetWorldMatrix(), memberWorldMatrix);
vtkNew<vtkMatrix4x4> nextWorldMatrix;
vtkMatrix4x4::Multiply4x4(delta, memberWorldMatrix, nextWorldMatrix);
// Set the new world matrix.
if (tr->GetParent()) {
Matrix4f invParentWorld = tr->GetParent()->GetWorldMatrix().inverse();
Matrix4f nextLocalMatrix = invParentWorld * VtkToMatrix4f(nextWorldMatrix);
tr->FromMatrix(nextLocalMatrix);
} else {
tr->FromMatrix(VtkToMatrix4f(nextWorldMatrix));
}
member->Update();
}
}
}
m_PrevMatrix->DeepCopy(currentMatrix);
m_SelectionTransform.FromMatrix(VtkToMatrix4f(currentMatrix));
}
vtkProp* MultiSelectionProp::GetProp() {
return m_GroupHighlightActor;
}
vtkProp3D* MultiSelectionProp::GetProxyProp() {
return m_GroupHighlightActor;
}
void MultiSelectionProp::serialize(Archive::property_register_archive &ar, const unsigned int version) {
ar & boost::serialization::make_nvp("Transform", m_SelectionTransform);
}
void MultiSelectionProp::serialize_display(uLib::Archive::display_properties_archive &ar, const unsigned int version) {
// Call base class to register standard display properties (Color, Opacity)
((Prop3D*)this)->serialize_display(ar, version);
}
} // namespace Vtk
} // namespace uLib

View File

@@ -0,0 +1,61 @@
#ifndef ULIB_VTK_MULTISELECTIONPROP_H
#define ULIB_VTK_MULTISELECTIONPROP_H
#include "uLibVtkInterface.h"
#include "Math/Transform.h"
#include <vector>
#include <vtkSmartPointer.h>
#include <vtkMatrix4x4.h>
class vtkActor;
class vtkProp;
class vtkProp3D;
namespace uLib {
namespace Vtk {
/**
* @class MultiSelectionProp
* @brief A proxy Prop3D that represents a group of selected Prop3Ds.
* It manages a combined highlight and propagates transformations to its members.
*/
class MultiSelectionProp : public Prop3D {
public:
uLibTypeMacro(MultiSelectionProp, Prop3D)
MultiSelectionProp();
virtual ~MultiSelectionProp();
/** @brief Creates a new instance that is a copy of this one's selection state. */
MultiSelectionProp* Clone() const;
void SetMembers(const std::vector<Prop3D*>& members);
const std::vector<Prop3D*>& GetMembers() const { return m_Members; }
virtual void Update() override;
virtual void SyncFromVtk() override;
virtual vtkProp* GetProp() override;
virtual vtkProp3D* GetProxyProp() override;
// Serialization for Properties Panel (TRS)
void serialize(Archive::property_register_archive &ar, const unsigned int version);
// Serialization for Display Properties Panel (Color, Opacity)
void serialize_display(uLib::Archive::display_properties_archive &ar, const unsigned int version = 0) override;
virtual uLib::Object* GetContent() const override { return const_cast<MultiSelectionProp*>(this); }
private:
std::vector<Prop3D*> m_Members;
uLib::TRS m_SelectionTransform;
vtkSmartPointer<vtkMatrix4x4> m_PrevMatrix;
vtkSmartPointer<vtkActor> m_GroupHighlightActor;
};
} // namespace Vtk
} // namespace uLib
#endif // ULIB_VTK_MULTISELECTIONPROP_H

View File

@@ -26,15 +26,17 @@ ObjectsContext::ObjectsContext(uLib::ObjectsContext *context)
: m_Context(context), m_Assembly(::vtkAssembly::New()) {
this->SetProp(m_Assembly);
if (m_Context) {
Object::connect(m_Context, &uLib::ObjectsContext::ObjectAdded, this,
m_AddedConnection = Object::connect(m_Context, &uLib::ObjectsContext::ObjectAdded, this,
&ObjectsContext::OnObjectAdded);
Object::connect(m_Context, &uLib::ObjectsContext::ObjectRemoved, this,
m_RemovedConnection = Object::connect(m_Context, &uLib::ObjectsContext::ObjectRemoved, this,
&ObjectsContext::OnObjectRemoved);
this->Synchronize();
}
}
ObjectsContext::~ObjectsContext() {
m_AddedConnection.disconnect();
m_RemovedConnection.disconnect();
for (auto const &[obj, prop3d] : m_Prop3Ds) {
delete prop3d;
}
@@ -136,6 +138,10 @@ Prop3D *ObjectsContext::CreateProp3D(uLib::Object *obj) {
if (!obj)
return nullptr;
if (auto* p3d = dynamic_cast<Prop3D*>(obj)) {
return p3d;
}
if (auto *vox = dynamic_cast<uLib::Abstract::VoxImage *>(obj)) {
return new VoxImage(vox);
} else if (auto *box = dynamic_cast<uLib::ContainerBox *>(obj)) {

View File

@@ -51,6 +51,8 @@ private:
uLib::ObjectsContext *m_Context;
std::map<uLib::Object*, Prop3D*> m_Prop3Ds;
vtkAssembly *m_Assembly;
uLib::Connection m_AddedConnection;
uLib::Connection m_RemovedConnection;
};
} // namespace Vtk

View File

@@ -20,6 +20,8 @@ QViewport::QViewport(QWidget* parent)
, m_VtkWidget(nullptr)
, m_GridButton(nullptr)
, m_ProjButton(nullptr)
, m_renderTimer(nullptr)
, m_renderPending(false)
{
// Build the layout zero margins so VTK fills the entire widget
auto* layout = new QVBoxLayout(this);
@@ -29,8 +31,14 @@ QViewport::QViewport(QWidget* parent)
m_VtkWidget = new QVTKOpenGLNativeWidget(this);
layout->addWidget(m_VtkWidget);
// Initialize render timer
m_renderTimer = new QTimer(this);
m_renderTimer->setSingleShot(true);
connect(m_renderTimer, &QTimer::timeout, this, &QViewport::doRender);
// Grid Toggle Button
m_GridButton = new QPushButton(m_VtkWidget);
m_GridButton->setText("#");
m_GridButton->setFixedSize(40, 40);
m_GridButton->setToolTip("Toggle Grid");
@@ -96,6 +104,9 @@ QViewport::QViewport(QWidget* parent)
QViewport::~QViewport()
{
if (m_VtkWidget && m_VtkWidget->renderWindow()) {
m_VtkWidget->renderWindow()->RemoveRenderer(this->GetRenderer());
}
}
void QViewport::SetupPipeline()
@@ -112,6 +123,18 @@ void QViewport::SetupPipeline()
void QViewport::Render()
{
if (!m_throttledRendering) {
doRender();
return;
}
if (m_renderPending) return;
m_renderPending = true;
m_renderTimer->start(16);
}
void QViewport::doRender()
{
m_renderPending = false;
if (m_VtkWidget && m_VtkWidget->renderWindow())
m_VtkWidget->renderWindow()->Render();
}

View File

@@ -4,6 +4,7 @@
#include <QWidget>
#include <QVTKOpenGLNativeWidget.h>
#include <QPushButton>
#include <QTimer>
#include <vtkCornerAnnotation.h>
#include <vtkOrientationMarkerWidget.h>
@@ -39,6 +40,9 @@ public:
// Render scene
virtual void Render() override;
void SetThrottledRendering(bool enabled) { m_throttledRendering = enabled; }
bool GetThrottledRendering() const { return m_throttledRendering; }
// Direct access to VTK internals
virtual vtkRenderWindow* GetRenderWindow() override;
virtual vtkRenderWindowInteractor* GetInteractor() override;
@@ -52,6 +56,7 @@ protected:
private slots:
void onGridButtonClicked();
void onProjButtonClicked();
void doRender();
private:
void SetupPipeline();
@@ -59,8 +64,12 @@ private:
QVTKOpenGLNativeWidget* m_VtkWidget;
QPushButton* m_GridButton;
QPushButton* m_ProjButton;
QTimer* m_renderTimer;
bool m_renderPending = false;
bool m_throttledRendering = true;
};
} // namespace Vtk
} // namespace uLib

View File

@@ -36,6 +36,8 @@
#include "Vtk/Math/vtkCylinder.h"
#include "Math/Transform.h"
#include "Vtk/Math/vtkAssembly.h"
#include "vtkMultiSelectionProp.h"
#include <vtkRendererCollection.h>
namespace uLib {
namespace Vtk {
@@ -69,6 +71,7 @@ struct ViewportData {
Viewport::Viewport()
: pv(new ViewportData())
, m_GridAxis(Y)
, m_MultiSelectionProp(new MultiSelectionProp())
{
}
@@ -100,6 +103,10 @@ Viewport::~Viewport()
pv->m_CameraWidget->Off();
pv->m_CameraWidget->SetInteractor(nullptr);
}
if (m_MultiSelectionProp) {
delete m_MultiSelectionProp;
m_MultiSelectionProp = nullptr;
}
delete pv;
}
@@ -192,6 +199,11 @@ void Viewport::SetupPipeline(vtkRenderWindowInteractor* iren)
pv->m_Renderer->SetLayer(0);
}
// Connect MultiSelectionProp
if (m_MultiSelectionProp) {
m_MultiSelectionProp->ConnectRenderer(pv->m_Renderer);
}
// Setup Handler Widget
if (!std::getenv("CTEST_PROJECT_NAME")) {
pv->m_HandlerWidget = vtkSmartPointer<HandlerWidget>::New();
@@ -206,8 +218,10 @@ void Viewport::SetupPipeline(vtkRenderWindowInteractor* iren)
widgetInteractionCallback->SetClientData(this);
widgetInteractionCallback->SetCallback([](vtkObject*, unsigned long, void* clientdata, void*){
auto* self = static_cast<Viewport*>(clientdata);
for (auto* p : self->m_Prop3Ds) {
if (p->IsSelected()) {
if (self->m_SelectedProps.size() > 1 && self->m_MultiSelectionProp) {
self->m_MultiSelectionProp->SyncFromVtk();
} else {
for (auto* p : self->m_SelectedProps) {
p->SyncFromVtk();
}
}
@@ -222,6 +236,7 @@ void Viewport::SetupPipeline(vtkRenderWindowInteractor* iren)
clickCallback->SetCallback([](vtkObject* caller, unsigned long, void* clientdata, void*){
auto* iren = static_cast<vtkRenderWindowInteractor*>(caller);
auto* self = static_cast<Viewport*>(clientdata);
bool multiSelect = iren->GetShiftKey() != 0;
int* pos = iren->GetEventPosition();
self->pv->m_Picker->Pick(pos[0], pos[1], 0, self->pv->m_Renderer);
@@ -291,7 +306,7 @@ void Viewport::SetupPipeline(vtkRenderWindowInteractor* iren)
}
}
}
self->SelectProp3D(target);
self->SelectProp3D(target, multiSelect);
});
iren->AddObserver(vtkCommand::LeftButtonPressEvent, clickCallback);
@@ -446,6 +461,11 @@ void Viewport::RegisterProp3D(Prop3D* p, bool isPart) {
m_Prop3Ds.push_back(p);
p->ConnectRenderer(pv->m_Renderer);
// Ensure m_MultiSelectionProp also has the same renderers
if (m_MultiSelectionProp) {
m_MultiSelectionProp->GetRenderers()->AddItem(pv->m_Renderer);
}
// If it's a part of an assembly, we don't want to draw it twice.
// Assembly itself already draws its parts.
// But we need ConnectRenderer above to allow highliting and property updates.
@@ -497,28 +517,59 @@ void Viewport::ObserveContext(ObjectsContext* ctx) {
});
}
void Viewport::SelectProp3D(Prop3D* prop)
void Viewport::SelectProp3D(Prop3D* prop, bool multi)
{
for (auto* p : m_Prop3Ds) {
p->SetSelected(p == prop);
if (multi) {
if (prop) {
auto it = std::find(m_SelectedProps.begin(), m_SelectedProps.end(), prop);
if (it != m_SelectedProps.end()) {
prop->SetSelected(false);
m_SelectedProps.erase(it);
} else {
prop->SetSelected(true);
m_SelectedProps.push_back(prop);
}
}
} else {
for (auto* p : m_SelectedProps) {
p->SetSelected(false);
}
m_SelectedProps.clear();
if (prop) {
prop->SetSelected(true);
m_SelectedProps.push_back(prop);
}
}
// Update HandlerWidget
if (pv->m_HandlerWidget) {
if (prop) {
vtkProp3D* prop3d = prop->GetProxyProp();
if (m_SelectedProps.empty()) {
pv->m_HandlerWidget->SetEnabled(0);
pv->m_HandlerWidget->SetProp3D(nullptr);
if (m_MultiSelectionProp) m_MultiSelectionProp->SetMembers({});
} else if (m_SelectedProps.size() == 1) {
Prop3D* selected = m_SelectedProps[0];
vtkProp3D* prop3d = selected->GetProxyProp();
if (prop3d) {
pv->m_HandlerWidget->SetProp3D(prop3d);
pv->m_HandlerWidget->SetEnabled(1);
pv->m_HandlerWidget->PlaceWidget(prop3d->GetBounds()); //TODO: FIX !
pv->m_HandlerWidget->PlaceWidget(prop3d->GetBounds());
}
if (m_MultiSelectionProp) m_MultiSelectionProp->SetMembers({});
} else {
pv->m_HandlerWidget->SetEnabled(0);
pv->m_HandlerWidget->SetProp3D(nullptr);
// Multi-selection
if (m_MultiSelectionProp) {
m_MultiSelectionProp->SetMembers(m_SelectedProps);
vtkProp3D* proxy = m_MultiSelectionProp->GetProxyProp();
pv->m_HandlerWidget->SetProp3D(proxy);
pv->m_HandlerWidget->SetEnabled(1);
pv->m_HandlerWidget->PlaceWidget(proxy->GetBounds());
}
}
}
Render();
OnSelectionChanged(prop);
OnSelectionChanged(m_SelectedProps.empty() ? nullptr : m_SelectedProps.back());
}
void Viewport::SetGridVisible(bool visible)
@@ -657,5 +708,19 @@ void Viewport::UpdateGrid()
pv->m_Annotation->SetText(1, gridLabel);
}
void Viewport::GroupSelection(uLib::ObjectsContext* targetCtx) {
if (!targetCtx || m_SelectedProps.size() <= 1 || !m_MultiSelectionProp) return;
// Clone the current multi-selection proxy
MultiSelectionProp* group = m_MultiSelectionProp->Clone();
// Add it to the context
targetCtx->AddObject(group);
// Select the new group and clear multi-selection
m_SelectedProps.clear();
SelectProp3D(group);
}
} // namespace Vtk
} // namespace uLib

View File

@@ -29,6 +29,7 @@ namespace Vtk {
struct ViewportData;
class HandlerWidget;
class MultiSelectionProp;
class ObjectsContext;
/**
@@ -49,7 +50,11 @@ public:
// Prop3D / prop management
void AddProp3D(Prop3D &prop);
void RemoveProp3D(Prop3D &prop);
void SelectProp3D(Prop3D *prop);
/** @brief Selects a specific Prop3D. If multi is true, it toggles selection in a group. */
void SelectProp3D(Prop3D* target, bool multi = false);
/** @brief Creates a persistent Selection Group from the current multi-selection. */
void GroupSelection(uLib::ObjectsContext* targetCtx);
void addProp(vtkProp *prop);
void RemoveProp(vtkProp *prop);
@@ -91,6 +96,8 @@ protected:
struct ViewportData *pv;
Axis m_GridAxis;
std::vector<Prop3D*> m_Prop3Ds;
std::vector<Prop3D*> m_SelectedProps;
MultiSelectionProp* m_MultiSelectionProp;
std::map<uLib::Object*, Prop3D*> m_ObjectToProp3D;
};