Files
uLib/src/Vtk/HEP/Geant/vtkGeantScene.cpp

107 lines
3.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 "vtkGeantScene.h"
#include "vtkGeantSolid.h"
#include "vtkBoxSolid.h"
#include "vtkTessellatedSolid.h"
#include "Vtk/vtkViewport.h"
namespace uLib {
namespace Vtk {
GeantScene::GeantScene(Geant::Scene *scene)
: m_Scene(scene), m_WorldProp3D(nullptr) {
if (!m_Scene)
return;
// 1. Create the world box wireframe prop3d
uLib::ContainerBox *worldBox = m_Scene->GetWorldBox();
if (worldBox) {
m_WorldProp3D = new ContainerBox(worldBox);
m_WorldProp3D->SetRepresentation(Prop3D::Wireframe);
m_WorldProp3D->ShowScaleMeasures(true);
}
// 2. Create prop3ds for each physical volume in the scene
const Vector<SmartPointer<Geant::PhysicalVolume>> &volumes = m_Scene->GetVolumes();
for (auto &volume : volumes) {
if (!volume || !volume->GetLogical() || !volume->GetLogical()->GetSolid())
continue;
Geant::PhysicalVolume *pv = volume.Get();
Geant::Solid *solid = pv->GetLogical()->GetSolid();
// Skip the world volume itself — it's already shown as the wireframe box
if (solid == m_Scene->GetWorld())
continue;
// Only create a prop3d if the solid has a valid G4VSolid
if (solid->GetG4Solid()) {
GeantSolid *vtkSolid = nullptr;
if (auto *box = dynamic_cast<Geant::BoxSolid *>(solid)) {
vtkSolid = new BoxSolid(pv);
} else if (auto *tess = dynamic_cast<Geant::TessellatedSolid *>(solid)) {
vtkSolid = new TessellatedSolid(pv);
} else {
vtkSolid = new GeantSolid(pv);
}
if (vtkSolid) {
m_SolidProp3Ds.push_back(vtkSolid);
}
}
}
}
GeantScene::~GeantScene() {
delete m_WorldProp3D;
for (auto *p : m_SolidProp3Ds) {
delete p;
}
}
void GeantScene::AddToViewer(Viewport &viewer) {
if (m_WorldProp3D) {
viewer.AddProp3D(*m_WorldProp3D);
}
for (auto *p : m_SolidProp3Ds) {
viewer.AddProp3D(*p);
}
}
void GeantScene::RemoveFromViewer(Viewport &viewer) {
if (m_WorldProp3D) {
viewer.RemoveProp3D(*m_WorldProp3D);
}
for (auto *p : m_SolidProp3Ds) {
viewer.RemoveProp3D(*p);
}
}
} // namespace Vtk
} // namespace uLib