132 lines
4.2 KiB
C++
132 lines
4.2 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.
|
||
|
||
//////////////////////////////////////////////////////////////////////////////*/
|
||
|
||
#include "Vtk/Math/vtkCylinder.h"
|
||
#include <vtkActor.h>
|
||
#include <vtkAssembly.h>
|
||
#include <vtkCylinderSource.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 {
|
||
|
||
Cylinder::Cylinder(Cylinder::Content *content)
|
||
: ObjectWrapper(content), m_Actor(nullptr), m_VtkAsm(nullptr) {
|
||
this->InstallPipe();
|
||
m_UpdateSignal = Object::connect(this->m_model.get(), &uLib::Object::Updated, this, &Cylinder::Update);
|
||
}
|
||
|
||
Cylinder::~Cylinder() {
|
||
if (m_Actor) m_Actor->Delete();
|
||
if (m_VtkAsm) m_VtkAsm->Delete();
|
||
}
|
||
|
||
void Cylinder::Update() {
|
||
if (!this->m_model)
|
||
return;
|
||
|
||
vtkProp3D* root = vtkProp3D::SafeDownCast(this->GetProp());
|
||
if (root) {
|
||
// 1. Placement handled specifically from content (use TRS GetMatrix, not World)
|
||
vtkNew<vtkMatrix4x4> m;
|
||
Matrix4fToVtk(this->m_model->GetMatrix(), m);
|
||
root->SetUserMatrix(m);
|
||
|
||
// 2. Shape-local properties (Radius, Height, Axis alignment) go to the internal actor
|
||
// These are relative to the root assembly
|
||
vtkTransform* alignment = vtkTransform::SafeDownCast(m_Actor->GetUserTransform());
|
||
if (alignment) {
|
||
alignment->Identity();
|
||
alignment->PostMultiply();
|
||
|
||
// Initial source is centered Y-cylinder (Radial XZ [-1,1], Height Y [-0.5, 0.5])
|
||
// Apply Radius and Height scaling
|
||
alignment->Scale(this->m_model->GetRadius(), this->m_model->GetHeight(), this->m_model->GetRadius());
|
||
|
||
// Apply Axis alignment
|
||
int axis = this->m_model->GetAxis();
|
||
if (axis == 0) alignment->RotateZ(-90); // Y -> X
|
||
else if (axis == 1) ; // Y -> Y (identity)
|
||
else if (axis == 2) alignment->RotateX(90); // Y -> Z
|
||
}
|
||
|
||
root->Modified();
|
||
}
|
||
|
||
// Delegate rest of update (appearance, render, etc)
|
||
ConnectionBlock blocker(m_UpdateSignal);
|
||
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();
|
||
this->SetProp(m_VtkAsm);
|
||
|
||
vtkNew<vtkCylinderSource> cylinder;
|
||
cylinder->SetRadius(1.0);
|
||
cylinder->SetHeight(1.0);
|
||
cylinder->SetResolution(32);
|
||
|
||
m_Actor = vtkActor::New();
|
||
vtkNew<vtkTransform> alignment;
|
||
m_Actor->SetUserTransform(alignment);
|
||
|
||
vtkNew<vtkPolyDataMapper> mapper;
|
||
mapper->SetInputConnection(cylinder->GetOutputPort());
|
||
m_Actor->SetMapper(mapper);
|
||
m_Actor->GetProperty()->SetRepresentationToWireframe();
|
||
m_Actor->GetProperty()->SetAmbient(0.6);
|
||
|
||
m_VtkAsm->AddPart(m_Actor);
|
||
|
||
this->Update();
|
||
}
|
||
|
||
} // namespace Vtk
|
||
} // namespace uLib
|