Files
uLib/src/Vtk/Math/vtkContainerBox.cpp
2026-04-17 09:36:10 +00:00

172 lines
5.1 KiB
C++

/*//////////////////////////////////////////////////////////////////////////////
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
All rights reserved
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
------------------------------------------------------------------
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
//////////////////////////////////////////////////////////////////////////////*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include "vtkContainerBox.h"
#include <vtkActor.h>
#include <vtkAssembly.h>
#include <vtkAxes.h>
#include <vtkCubeSource.h>
#include <vtkLineSource.h>
#include <vtkMatrix4x4.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkTransform.h>
#include "Vtk/Math/vtkDense.h"
namespace uLib {
namespace Vtk {
struct ContainerBoxData {
vtkSmartPointer<vtkActor> m_Cube;
vtkSmartPointer<vtkActor> m_Axes;
vtkSmartPointer<vtkAssembly> m_VtkAsm;
uLib::Connection m_UpdateSignal;
ContainerBoxData()
: m_Cube(vtkSmartPointer<vtkActor>::New()),
m_Axes(vtkSmartPointer<vtkActor>::New()),
m_VtkAsm(vtkSmartPointer<vtkAssembly>::New()) {}
~ContainerBoxData() {}
};
ContainerBox::ContainerBox(ContainerBox::Content *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);
this->Update();
}
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;
vtkProp3D *prop = vtkProp3D::SafeDownCast(this->GetProp());
if (prop) {
// Apply the TRS matrix to the assembly
vtkNew<vtkMatrix4x4> m;
Matrix4fToVtk(this->m_model->GetMatrix(), m);
prop->SetUserMatrix(m);
prop->Modified();
}
// Apply the local shape transformation (Size/Origin) to the cube actor
vtkNew<vtkMatrix4x4> localM;
Matrix4fToVtk(this->m_model->GetLocalMatrix(), localM);
d->m_Cube->SetUserMatrix(localM);
// Delegate rest of update (appearance, render, etc)
ConnectionBlock blocker(d->m_UpdateSignal);
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)
return;
Content *c = this->m_model;
// CUBE
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
// cube->SetBounds(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
mapper->SetInputConnection(cube->GetOutputPort());
mapper->Update();
d->m_Cube->SetMapper(mapper);
d->m_Cube->GetProperty()->SetRepresentationToWireframe();
d->m_Cube->GetProperty()->SetAmbient(0.7);
// AXES //
vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();
axes->SetOrigin(0, 0, 0);
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(axes->GetOutputPort());
mapper->Update();
d->m_Axes->SetMapper(mapper);
d->m_Axes->GetProperty()->SetLineWidth(3);
d->m_Axes->GetProperty()->SetAmbient(0.4);
d->m_Axes->GetProperty()->SetSpecular(0);
// PIVOT //
axes = vtkSmartPointer<vtkAxes>::New();
axes->SetOrigin(0, 0, 0);
mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(axes->GetOutputPort());
mapper->Update();
d->m_VtkAsm->AddPart(d->m_Cube);
d->m_VtkAsm->AddPart(d->m_Axes);
this->SetProp(d->m_VtkAsm);
// vtkProp3D* root = d->m_VtkAsm;
// if (root) {
// this->ApplyProp3DTransform(root);
// }
this->Update();
}
} // namespace Vtk
} // namespace uLib