Files
uLib/src/Vtk/Math/vtkContainerBox.cpp
2026-03-25 18:47:52 +00:00

186 lines
5.0 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 "Math/vtkDense.h"
namespace uLib {
namespace Vtk {
struct ContainerBoxData {
vtkSmartPointer<vtkActor> m_Cube;
vtkSmartPointer<vtkActor> m_Axes;
ContainerBoxData() : m_Cube(vtkSmartPointer<vtkActor>::New()), m_Axes(vtkSmartPointer<vtkActor>::New()) {}
~ContainerBoxData() {
}
};
vtkContainerBox::vtkContainerBox(vtkContainerBox::Content *content)
: d(new ContainerBoxData()), m_Content(content) {
this->InstallPipe();
Object::connect(m_Content, &Content::Updated, this, &vtkContainerBox::contentUpdate);
}
vtkContainerBox::~vtkContainerBox() {
delete d;
}
vtkPolyData *vtkContainerBox::GetPolyData() const {
// TODO
return NULL;
}
void vtkContainerBox::contentUpdate() {
RecursiveMutex::ScopedLock lock(this->m_UpdateMutex);
if (!m_Content)
return;
vtkProp3D* root = vtkProp3D::SafeDownCast(this->GetProp());
if (!root) return;
vtkMatrix4x4* vmat = root->GetUserMatrix();
if (!vmat) {
// Should have been set in InstallPipe, but let's be safe
vtkNew<vtkMatrix4x4> mat;
root->SetUserMatrix(mat);
vmat = mat;
}
d->m_Cube->SetUserMatrix(nullptr);
d->m_Axes->SetUserMatrix(nullptr);
Matrix4f transform = m_Content->GetMatrix();
Matrix4fToVtk(transform, vmat);
root->Modified();
m_BlockUpdate = true;
Puppet::Update();
}
void vtkContainerBox::Update() {
RecursiveMutex::ScopedLock lock(this->m_UpdateMutex);
if (!m_Content) return;
if (m_BlockUpdate) {
m_BlockUpdate = false;
return;
}
// Use Targeted Blocking: only block the feedback connection to this puppet
// boost::signals2::shared_connection_block block(m_Connection);
vtkProp3D* assembly = vtkProp3D::SafeDownCast(this->GetProp());
if (!assembly) return;
vtkMatrix4x4* vmat = assembly->GetUserMatrix();
if (!vmat) return;
Matrix4f transform = VtkToMatrix4f(vmat);
// Update uLib model's affine transform
// if (m_Content->GetParent()) {
// Matrix4f localT = m_Content->GetParent()->GetWorldMatrix().inverse() * transform;
// m_Content->SetMatrix(localT);
// } else {
m_Content->SetMatrix(transform);
// }
m_Content->Updated(); // Notify change
}
void vtkContainerBox::InstallPipe() {
if (!m_Content)
return;
Content *c = m_Content;
// CUBE
vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cube->SetBounds(0, 1, 0, 1, 0, 1);
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();
this->SetProp(d->m_Cube);
this->SetProp(d->m_Axes);
vtkProp3D* root = vtkProp3D::SafeDownCast(this->GetProp());
if (root) {
vtkNew<vtkMatrix4x4> vmat;
Matrix4fToVtk(c->GetMatrix(), vmat);
root->SetUserMatrix(vmat);
}
}
} // namespace Vtk
} // namespace uLib