3 Commits

Author SHA1 Message Date
AndreaRigoni
010927714f not working yet 2026-04-13 07:36:19 +00:00
AndreaRigoni
bf4006ff91 feat: add TRS serialization and display property support to MultiSelectionProp 2026-04-10 21:44:18 +00:00
AndreaRigoni
e320c932d2 feat: implement MultiSelectionProp to support grouped object transformation and selection in Viewport 2026-04-10 20:42:24 +00:00
30 changed files with 485 additions and 216 deletions

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,6 +13,7 @@
#include <QPushButton>
#include <QMenu>
#include <QAction>
#include <QShortcut>
#include <QApplication>
#include <QFileDialog>
#include <QFileInfo>
@@ -104,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) {

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

@@ -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

@@ -9,7 +9,7 @@ dependencies:
- root
- vtk=9.4 # VTK 9.4
- pybind11
# - boost=1.86.0 # requested by VTK 9.4
#- boost=1.86.0 # requested by VTK 9.4
- ninja
- clang
- clangxx

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

@@ -208,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

@@ -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

@@ -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

@@ -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

@@ -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

@@ -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());
}
this->UpdateBoundingBox();
// Sync the group-level transformation from VTK to the domain model
this->Prop3D::SyncFromVtk();
// 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

@@ -56,7 +56,9 @@ struct ContainerBoxData {
: m_Cube(vtkSmartPointer<vtkActor>::New()),
m_Axes(vtkSmartPointer<vtkActor>::New()),
m_VtkAsm(vtkSmartPointer<vtkAssembly>::New()) {}
~ContainerBoxData() {}
~ContainerBoxData() {
m_UpdateSignal.disconnect();
}
};
ContainerBox::ContainerBox(ContainerBox::Content *content)
@@ -93,27 +95,7 @@ void ContainerBox::Update() {
this->Prop3D::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)

View File

@@ -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

@@ -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

@@ -97,11 +97,24 @@ public:
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;
@@ -128,6 +141,8 @@ public:
int m_HighlightMode; // 0: Plain, 1: Corners
vtkSmartPointer<vtkMatrix4x4> m_PrevMatrix;
//
TRS m_Transform;
@@ -289,6 +304,7 @@ public:
vtkNew<vtkMatrix4x4> vwm;
Matrix4fToVtk(tr->GetWorldMatrix(), vwm);
m_HighlightActor->SetUserMatrix(vwm);
m_PrevMatrix->DeepCopy(vwm);
}
}
}
@@ -569,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;
}
@@ -608,6 +636,7 @@ void Prop3D::ApplyProp3DTransform(vtkProp3D* prop)
Matrix4fToVtk(tr->GetMatrix(), m);
prop->SetUserMatrix(m);
prop->Modified();
pd->m_PrevMatrix->DeepCopy(m);
}
}
}
@@ -618,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();
}
}

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;

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

@@ -104,6 +104,9 @@ QViewport::QViewport(QWidget* parent)
QViewport::~QViewport()
{
if (m_VtkWidget && m_VtkWidget->renderWindow()) {
m_VtkWidget->renderWindow()->RemoveRenderer(this->GetRenderer());
}
}
void QViewport::SetupPipeline()

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 (pv->m_HandlerWidget) {
if (multi) {
if (prop) {
vtkProp3D* prop3d = prop->GetProxyProp();
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 (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;
};