add geant4 scene and gcompose app

This commit is contained in:
AndreaRigoni
2026-03-13 17:19:51 +00:00
parent f2133c31d5
commit 61052f80bc
34 changed files with 1341 additions and 580 deletions

View File

@@ -23,20 +23,21 @@
//////////////////////////////////////////////////////////////////////////////*/
// G4 Solid //
#include <Geant4/G4LogicalVolume.hh>
#include <Geant4/G4Material.hh>
#include <Geant4/G4NistManager.hh>
#include <Geant4/G4LogicalVolume.hh>
// Tessellated solid //
#include <Geant4/G4TessellatedSolid.hh>
#include <Geant4/G4TriangularFacet.hh>
#include <Geant4/G4ThreeVector.hh>
#include <Geant4/G4TriangularFacet.hh>
#include <Geant4/G4Box.hh>
#include <Geant4/G4PVPlacement.hh>
#include "Math/Dense.h"
#include "Math/Transform.h"
#include "Solid.h"
@@ -44,71 +45,130 @@ namespace uLib {
class DetectorsSolidPimpl {
public:
static G4ThreeVector getG4Vector3f(const Vector3f &vector) {
return G4ThreeVector( vector(0), vector(1), vector(2) );
}
static G4ThreeVector getG4Vector3f(const Vector3f &vector) {
return G4ThreeVector(vector(0), vector(1), vector(2));
}
};
Solid::Solid()
: m_Logical(new G4LogicalVolume(NULL, NULL, "unnamed_solid")),
m_Material(NULL) {}
Solid::Solid(const char *name)
: m_Logical(new G4LogicalVolume(NULL, NULL, name)), m_Material(NULL) {}
Solid::Solid() :
m_Logical (new G4LogicalVolume(NULL,NULL,"unnamed_solid")),
m_Material(NULL)
{}
Solid::Solid(const char *name) :
m_Logical(new G4LogicalVolume(NULL,NULL,name)),
m_Material(NULL)
{}
void Solid::SetNistMaterial(const char *name)
{
G4NistManager *nist = G4NistManager::Instance();
if (m_Material) delete m_Material;
m_Material = nist->FindOrBuildMaterial(name);
m_Logical->SetMaterial(m_Material);
void Solid::SetNistMaterial(const char *name) {
G4NistManager *nist = G4NistManager::Instance();
m_Material = nist->FindOrBuildMaterial(name);
m_Logical->SetMaterial(m_Material);
}
void Solid::SetMaterial(G4Material *material)
{
if(material)
{
m_Material = material;
m_Logical->SetMaterial(material);
void Solid::SetMaterial(G4Material *material) {
if (material) {
m_Material = material;
m_Logical->SetMaterial(material);
}
}
void Solid::SetTransform(Matrix4f transform) {
uLib::AffineTransform t;
t.SetMatrix(transform);
// 2. Extracto position and rotation for Geant4
Vector3f pos = t.GetPosition();
G4ThreeVector g4pos(pos(0), pos(1), pos(2));
// Create a G4 rotation matrix from the 4x4 matrix
Matrix3f m = t.GetRotation();
G4RotationMatrix* rot = new G4RotationMatrix();
rot->set(G4ThreeVector(m(0,0), m(1,0), m(2,0)),
G4ThreeVector(m(0,1), m(1,1), m(2,1)),
G4ThreeVector(m(0,2), m(1,2), m(2,2)));
// 3. Se l'oggetto è già stato piazzato, aggiorniamo la sua trasformazione
if (m_Physical) {
m_Physical->SetTranslation(g4pos);
m_Physical->SetRotation(rot);
}
}
void Solid::SetParent(Solid *parent) {
if (!m_Logical) {
std::cerr << "logical volume not created for solid " << GetName() << std::endl;
return;
}
if(m_Physical) {
std::cerr << "physical volume already created for solid " << GetName() << std::endl;
return;
}
TessellatedSolid::TessellatedSolid(const char *name) :
BaseClass(name),
m_Solid(new G4TessellatedSolid(name))
{}
void TessellatedSolid::SetMesh(TriangleMesh &mesh)
{
G4TessellatedSolid *ts = this->m_Solid;
for (int i=0; i<mesh.Triangles().size(); ++i) {
const Vector3i &trg = mesh.Triangles().at(i);
G4TriangularFacet *facet = new G4TriangularFacet(
DetectorsSolidPimpl::getG4Vector3f(mesh.Points().at(trg(0))),
DetectorsSolidPimpl::getG4Vector3f(mesh.Points().at(trg(1))),
DetectorsSolidPimpl::getG4Vector3f(mesh.Points().at(trg(2))),
ABSOLUTE);
ts->AddFacet((G4VFacet *)facet);
G4LogicalVolume* parentLogical = nullptr;
if (parent) {
parentLogical = parent->GetLogical();
if (!parentLogical) {
std::cerr << "parent logical volume not created for solid " << parent->GetName() << std::endl;
return;
}
this->m_Logical->SetSolid(ts);
}
// G4PVPlacement
m_Physical = new G4PVPlacement(
nullptr, // Rotation
G4ThreeVector(0,0,0), // Position (translation) inside the parent
m_Logical, // The logical volume of this solid (the child)
GetName(), // Name of the physical volume
parentLogical, // The logical volume of the parent (nullptr if it's the World volume)
false, // Boolean operations (usually false)
0, // Copy number
true // Check overlaps (useful to enable in debug phase)
);
}
TessellatedSolid::TessellatedSolid(const char *name)
: BaseClass(name), m_Solid(new G4TessellatedSolid(name)) {}
void TessellatedSolid::SetMesh(TriangleMesh &mesh) {
G4TessellatedSolid *ts = this->m_Solid;
for (int i = 0; i < mesh.Triangles().size(); ++i) {
const Vector3i &trg = mesh.Triangles().at(i);
G4TriangularFacet *facet = new G4TriangularFacet(
DetectorsSolidPimpl::getG4Vector3f(mesh.Points().at(trg(0))),
DetectorsSolidPimpl::getG4Vector3f(mesh.Points().at(trg(1))),
DetectorsSolidPimpl::getG4Vector3f(mesh.Points().at(trg(2))), ABSOLUTE);
ts->AddFacet((G4VFacet *)facet);
}
this->m_Logical->SetSolid(ts);
}
BoxSolid::BoxSolid(const char *name, ContainerBox *box) : BaseClass(name) {
m_Solid = new G4Box(name, 0.5, 0.5, 0.5);
m_Object = box;
Object::connect(box, &ContainerBox::Updated, this, &BoxSolid::Update);
this->m_Logical->SetSolid(m_Solid);
}
void BoxSolid::Update() {
if (m_Object) {
Vector3f size = m_Object->GetSize();
m_Solid->SetXHalfLength(size(0) * 0.5);
m_Solid->SetYHalfLength(size(1) * 0.5);
m_Solid->SetZHalfLength(size(2) * 0.5);
this->SetTransform(m_Object->GetMatrix());
// this->m_Logical->SetSolid(m_Solid);
}
}
} // namespace uLib