refactor: improve vtkContainerBox constructor flexibility and clean up code formatting

This commit is contained in:
AndreaRigoni
2026-04-09 12:48:14 +00:00
parent db76513e79
commit 76f29328cd
3 changed files with 51 additions and 59 deletions

View File

@@ -25,7 +25,6 @@
#include "Vtk/uLibVtkViewer.h" #include "Vtk/uLibVtkViewer.h"
#include "Vtk/Math/vtkContainerBox.h"
#include "Math/Units.h" #include "Math/Units.h"
#include "Vtk/Math/vtkContainerBox.h" #include "Vtk/Math/vtkContainerBox.h"
@@ -36,19 +35,23 @@ using namespace uLib;
int main() { int main() {
BEGIN_TESTING(vtk ContainerBox Test); BEGIN_TESTING(vtk ContainerBox Test);
ContainerBox box; {
box.Scale(Vector3f(1_m,2_m,1_m)); ContainerBox box;
box.SetPosition(Vector3f(0,0,0)); box.Scale(Vector3f(1_m, 2_m, 1_m));
box.SetPosition(Vector3f(0, 0, 0));
Vtk::ContainerBox v_box(&box);
v_box.Update(); Vtk::ContainerBox v_box(&box);
v_box.Update();
// v_box.SetRepresentation(Vtk::Prop3D::Surface);
// v_box.SetOpacity(0.5); v_box.SetRepresentation(Vtk::Prop3D::Surface);
// v_box.SetSelectable(true); 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) { if (std::getenv("CTEST_PROJECT_NAME") == nullptr) {

View File

@@ -43,50 +43,43 @@
#include "Vtk/Math/vtkDense.h" #include "Vtk/Math/vtkDense.h"
namespace uLib { namespace uLib {
namespace Vtk { namespace Vtk {
struct ContainerBoxData { struct ContainerBoxData {
vtkSmartPointer<vtkActor> m_Cube; vtkSmartPointer<vtkActor> m_Cube;
vtkSmartPointer<vtkActor> m_Axes; vtkSmartPointer<vtkActor> m_Axes;
vtkSmartPointer<vtkAssembly> m_VtkAsm; vtkSmartPointer<vtkAssembly> m_VtkAsm;
uLib::Connection m_UpdateSignal; uLib::Connection m_UpdateSignal;
ContainerBoxData()
ContainerBoxData() : m_Cube(vtkSmartPointer<vtkActor>::New()), : m_Cube(vtkSmartPointer<vtkActor>::New()),
m_Axes(vtkSmartPointer<vtkActor>::New()), m_Axes(vtkSmartPointer<vtkActor>::New()),
m_VtkAsm(vtkSmartPointer<vtkAssembly>::New()) {} m_VtkAsm(vtkSmartPointer<vtkAssembly>::New()) {}
~ContainerBoxData() { ~ContainerBoxData() {}
}
}; };
ContainerBox::ContainerBox(ContainerBox::Content *content) ContainerBox::ContainerBox(ContainerBox::Content *content)
: d(new ContainerBoxData()), ObjectWrapper(content) { : d(new ContainerBoxData()),
ObjectWrapper(content ? content : new Content()) {
this->InstallPipe(); this->InstallPipe();
d->m_UpdateSignal = d->m_UpdateSignal = Object::connect(
Object::connect(this->m_model.get(), &uLib::Object::Updated, this, &ContainerBox::Update); this->m_model.get(), &uLib::Object::Updated, this, &ContainerBox::Update);
} }
ContainerBox::~ContainerBox() { ContainerBox::~ContainerBox() { delete d; }
delete d;
}
vtkPolyData *ContainerBox::GetPolyData() const { vtkPolyData *ContainerBox::GetPolyData() const {
// TODO // TODO
return NULL; return NULL;
} }
void ContainerBox::Update() { void ContainerBox::Update() {
RecursiveMutex::ScopedLock lock(this->m_UpdateMutex); RecursiveMutex::ScopedLock lock(this->m_UpdateMutex);
if (!this->m_model) return; if (!this->m_model)
return;
vtkProp3D* prop = vtkProp3D::SafeDownCast(this->GetProp()); vtkProp3D *prop = vtkProp3D::SafeDownCast(this->GetProp());
if (prop) { if (prop) {
// Apply the full volume matrix (TRS * m_LocalT) // Apply the full volume matrix (TRS * m_LocalT)
vtkNew<vtkMatrix4x4> m; vtkNew<vtkMatrix4x4> m;
@@ -100,32 +93,28 @@ void ContainerBox::Update() {
this->Prop3D::Update(); this->Prop3D::Update();
} }
void ContainerBox::SyncFromVtk() { void ContainerBox::SyncFromVtk() {
RecursiveMutex::ScopedLock lock(this->m_UpdateMutex); RecursiveMutex::ScopedLock lock(this->m_UpdateMutex);
if (!this->m_model) return; if (!this->m_model)
return;
vtkProp3D* root = this->GetProxyProp(); vtkProp3D *root = this->GetProxyProp();
if (!root) return; if (!root)
return;
// VTK -> Model: Extract new world TRS from proxy, which matches the model's TRS center // VTK -> Model: Extract new world TRS from proxy, which matches the model's
vtkMatrix4x4* rootMat = root->GetUserMatrix(); // TRS center
vtkMatrix4x4 *rootMat = root->GetUserMatrix();
Matrix4f vtkWorld = VtkToMatrix4f(rootMat); Matrix4f vtkWorld = VtkToMatrix4f(rootMat);
// Synchronize TRS property members from the updated local matrix // Synchronize TRS property members from the updated local matrix
this->m_model->FromMatrix(vtkWorld); this->m_model->FromMatrix(vtkWorld);
// Since we modified the model, notify observers, but block the loop back to VTK // Since we modified the model, notify observers, but block the loop back to
// ConnectionBlock blocker(d->m_UpdateSignal); // VTK ConnectionBlock blocker(d->m_UpdateSignal);
this->m_model->Updated(); this->m_model->Updated();
} }
void ContainerBox::InstallPipe() { void ContainerBox::InstallPipe() {
if (!this->m_model) if (!this->m_model)
return; return;
@@ -133,7 +122,7 @@ void ContainerBox::InstallPipe() {
// CUBE // CUBE
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New(); vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New(); vtkSmartPointer<vtkPolyDataMapper>::New();
cube->SetBounds(0, 1, 0, 1, 0, 1); cube->SetBounds(0, 1, 0, 1, 0, 1);
@@ -160,11 +149,11 @@ void ContainerBox::InstallPipe() {
mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(axes->GetOutputPort()); mapper->SetInputConnection(axes->GetOutputPort());
mapper->Update(); mapper->Update();
d->m_VtkAsm->AddPart(d->m_Cube); d->m_VtkAsm->AddPart(d->m_Cube);
d->m_VtkAsm->AddPart(d->m_Axes); d->m_VtkAsm->AddPart(d->m_Axes);
this->SetProp(d->m_VtkAsm); this->SetProp(d->m_VtkAsm);
// vtkProp3D* root = d->m_VtkAsm; // vtkProp3D* root = d->m_VtkAsm;
// if (root) { // if (root) {
// this->ApplyProp3DTransform(root); // this->ApplyProp3DTransform(root);

View File

@@ -42,11 +42,11 @@ class ContainerBox : public Prop3D,
public Polydata, public Polydata,
public uLib::ObjectWrapper<uLib::ContainerBox> { public uLib::ObjectWrapper<uLib::ContainerBox> {
uLibTypeMacro(ContainerBox, Prop3D, Polydata) uLibTypeMacro(ContainerBox, Prop3D,
typedef uLib::ContainerBox Content; Polydata) typedef uLib::ContainerBox Content;
public: public:
ContainerBox(Content *content); ContainerBox(Content *content = nullptr);
~ContainerBox(); ~ContainerBox();
virtual class vtkPolyData *GetPolyData() const override; virtual class vtkPolyData *GetPolyData() const override;