starting udate geant sloid
This commit is contained in:
@@ -35,12 +35,14 @@
|
||||
#include <Geant4/G4TriangularFacet.hh>
|
||||
#include <Geant4/G4Box.hh>
|
||||
#include <Geant4/G4PVPlacement.hh>
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
#include "Math/Dense.h"
|
||||
#include "Math/Transform.h"
|
||||
|
||||
#include "Solid.h"
|
||||
#include "uLibInterface.hh"
|
||||
|
||||
namespace uLib {
|
||||
namespace Geant {
|
||||
@@ -55,18 +57,17 @@ public:
|
||||
};
|
||||
|
||||
Solid::Solid()
|
||||
: m_Name("unnamed_solid"), m_Material(NULL), m_Logical(NULL), m_Physical(NULL),
|
||||
m_Position(new G4ThreeVector(0,0,0)), m_Rotation(NULL) {}
|
||||
: m_Material(nullptr), m_Logical(nullptr) {
|
||||
}
|
||||
|
||||
Solid::Solid(const char *name)
|
||||
: m_Name(name), m_Material(NULL), m_Logical(NULL), m_Physical(NULL),
|
||||
m_Position(new G4ThreeVector(0,0,0)), m_Rotation(NULL) {}
|
||||
|
||||
Solid::~Solid() {
|
||||
if (m_Position) delete m_Position;
|
||||
if (m_Rotation) delete m_Rotation;
|
||||
: m_Name(name), m_Material(nullptr), m_Logical(nullptr) {
|
||||
}
|
||||
|
||||
Solid::~Solid() {}
|
||||
|
||||
void Solid::Update() {}
|
||||
|
||||
void Solid::SetNistMaterial(const char *name) {
|
||||
G4NistManager *nist = G4NistManager::Instance();
|
||||
G4Material *mat = nist->FindOrBuildMaterial(name);
|
||||
@@ -85,32 +86,12 @@ void Solid::SetMaterial(G4Material *material) {
|
||||
}
|
||||
|
||||
void Solid::SetTransform(Matrix4f transform) {
|
||||
uLib::AffineTransform t;
|
||||
t.SetMatrix(transform);
|
||||
|
||||
// 2. Extract position and rotation for Geant4
|
||||
Vector3f pos = t.GetPosition();
|
||||
if (!m_Position) m_Position = new G4ThreeVector();
|
||||
*m_Position = G4ThreeVector(pos(0), pos(1), pos(2));
|
||||
|
||||
// Create a G4 rotation matrix from the 4x4 matrix
|
||||
Matrix3f m = t.GetRotation();
|
||||
if (!m_Rotation) m_Rotation = new G4RotationMatrix();
|
||||
m_Rotation->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. If object is already placed, update its transformation
|
||||
if (m_Physical) {
|
||||
m_Physical->SetTranslation(*m_Position);
|
||||
m_Physical->SetRotation(m_Rotation);
|
||||
}
|
||||
|
||||
std::cout << "Solid " << GetName() << " position: " << pos << " rotation: " << m << std::endl;
|
||||
|
||||
this->Updated();
|
||||
m_Transform.FromMatrix(transform);
|
||||
m_Transform.Updated();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Solid::SetParent(Solid *parent) {
|
||||
if (!m_Logical) {
|
||||
std::cerr << "logical volume not created for solid " << GetName() << std::endl;
|
||||
@@ -133,8 +114,8 @@ void Solid::SetParent(Solid *parent) {
|
||||
|
||||
// G4PVPlacement
|
||||
m_Physical = new G4PVPlacement(
|
||||
m_Rotation, // Rotation
|
||||
*m_Position, // Position (translation) inside the parent
|
||||
ToG4Transform(m_Transform), // Rotation
|
||||
GetPosition(), // 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)
|
||||
@@ -182,15 +163,15 @@ void TessellatedSolid::Update() {
|
||||
|
||||
BoxSolid::BoxSolid(const char *name) :
|
||||
BaseClass(name),
|
||||
m_Density(1.0),
|
||||
m_ContainerBox(new ContainerBox()),
|
||||
m_Solid(new G4Box(name, 0.5, 0.5, 0.5))
|
||||
{
|
||||
Object::connect(m_ContainerBox, &ContainerBox::Updated, this, &BoxSolid::Update);
|
||||
Update();
|
||||
}
|
||||
m_Solid(new G4Box(name, 1, 1, 1))
|
||||
{}
|
||||
|
||||
BoxSolid::BoxSolid(const char *name, ContainerBox *box) : BaseClass(name) {
|
||||
m_Solid = new G4Box(name, 0.5, 0.5, 0.5);
|
||||
BoxSolid::BoxSolid(const char *name, ContainerBox *box) :
|
||||
BaseClass(name),
|
||||
m_Density(1.0) {
|
||||
m_Solid = new G4Box(name, 1, 1, 1);
|
||||
m_ContainerBox = box;
|
||||
Object::connect(box, &ContainerBox::Updated, this, &BoxSolid::Update);
|
||||
if (m_Logical) {
|
||||
@@ -199,6 +180,7 @@ BoxSolid::BoxSolid(const char *name, ContainerBox *box) : BaseClass(name) {
|
||||
Update();
|
||||
}
|
||||
|
||||
|
||||
void BoxSolid::Update() {
|
||||
if (m_ContainerBox) {
|
||||
Vector3f size = m_ContainerBox->GetSize();
|
||||
|
||||
@@ -28,11 +28,15 @@
|
||||
|
||||
#include "Core/Object.h"
|
||||
#include "Geant/Matter.h"
|
||||
#include "Math/Transform.h"
|
||||
#include <Geant4/G4LogicalVolume.hh>
|
||||
#include "Math/ContainerBox.h"
|
||||
#include "Math/Dense.h"
|
||||
#include "Math/TriangleMesh.h"
|
||||
|
||||
|
||||
|
||||
|
||||
class G4Material;
|
||||
class G4LogicalVolume;
|
||||
class G4TessellatedSolid;
|
||||
@@ -42,28 +46,17 @@ namespace uLib {
|
||||
namespace Geant {
|
||||
|
||||
class Solid : public Object {
|
||||
public:
|
||||
|
||||
uLibTypeMacro(Solid, Object)
|
||||
ULIB_SERIALIZE_ACCESS
|
||||
ULIB_DECLARE_PROPERTIES(Solid)
|
||||
|
||||
public:
|
||||
|
||||
Solid();
|
||||
Solid(const char *name);
|
||||
virtual ~Solid();
|
||||
|
||||
void SetNistMaterial(const char *name);
|
||||
void SetMaterial(G4Material *material);
|
||||
|
||||
void SetSizeUnit(const char *unit);
|
||||
|
||||
// Implementiamo SetParent qui, per tutti.
|
||||
virtual void SetParent(Solid *parent);
|
||||
|
||||
// Setters per la posizione (necessari per il piazzamento)
|
||||
void SetTransform(Matrix4f transform);
|
||||
|
||||
uLibGetMacro(Material, G4Material *)
|
||||
uLibGetSetMacro(Logical, G4LogicalVolume *)
|
||||
uLibGetSetMacro(Physical, G4VPhysicalVolume *)
|
||||
|
||||
virtual G4VSolid* GetG4Solid() const { return nullptr; }
|
||||
|
||||
inline const char *GetName() const {
|
||||
@@ -72,20 +65,57 @@ public:
|
||||
|
||||
template < typename Ar >
|
||||
void serialize(Ar &ar, const unsigned int version) {
|
||||
ar & boost::serialization::make_nvp("Name", m_Name);
|
||||
ar & HRP("Name", m_Name);
|
||||
ar & HRP("Material", m_Material);
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
public slots:
|
||||
virtual void Update();
|
||||
|
||||
protected:
|
||||
std::string m_Name;
|
||||
G4Material *m_Material;
|
||||
G4LogicalVolume *m_Logical;
|
||||
G4VPhysicalVolume *m_Physical; // <-- Memorizza l'oggetto posizionato
|
||||
G4ThreeVector *m_Position; // <-- Offset rispetto al centro del padre
|
||||
G4RotationMatrix* m_Rotation; // <-- Rotazione rispetto al padre
|
||||
};
|
||||
|
||||
|
||||
class LogicalVolume : public Object {
|
||||
|
||||
uLibTypeMacro(LogicalVolume, Object)
|
||||
ULIB_SERIALIZE_ACCESS
|
||||
ULIB_DECLARE_PROPERTIES(LogicalVolume)
|
||||
|
||||
public:
|
||||
|
||||
LogicalVolume();
|
||||
LogicalVolume(const char *name);
|
||||
virtual ~LogicalVolume();
|
||||
|
||||
virtual G4VSolid* GetG4Solid() const { return nullptr; }
|
||||
|
||||
inline const char *GetName() const {
|
||||
return m_Logical ? m_Logical->GetName().c_str() : m_Name.c_str();
|
||||
}
|
||||
|
||||
template < typename Ar >
|
||||
void serialize(Ar &ar, const unsigned int version) {
|
||||
ar & HRP("Name", m_Name);
|
||||
ar & HRP("Material", m_Material);
|
||||
}
|
||||
|
||||
|
||||
public slots:
|
||||
virtual void Update();
|
||||
|
||||
protected:
|
||||
std::string m_Name;
|
||||
Material *m_Material;
|
||||
|
||||
G4LogicalVolume *m_Logical;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -95,8 +125,10 @@ public:
|
||||
|
||||
TessellatedSolid();
|
||||
TessellatedSolid(const char *name);
|
||||
|
||||
void SetMesh(TriangleMesh &mesh);
|
||||
uLibGetMacro(Solid, G4TessellatedSolid *)
|
||||
|
||||
virtual G4VSolid* GetG4Solid() const override { return (G4VSolid*)m_Solid; }
|
||||
|
||||
const TriangleMesh& GetMesh() const { return m_Mesh; }
|
||||
@@ -115,9 +147,11 @@ private :
|
||||
|
||||
|
||||
class BoxSolid : public Solid {
|
||||
uLibTypeMacro(BoxSolid, Solid)
|
||||
ULIB_SERIALIZE_ACCESS
|
||||
ULIB_DECLARE_PROPERTIES(BoxSolid)
|
||||
|
||||
public:
|
||||
uLibTypeMacro(BoxSolid, Solid)
|
||||
|
||||
BoxSolid(const char *name = "");
|
||||
BoxSolid(const char *name, ContainerBox *box);
|
||||
@@ -128,14 +162,15 @@ public:
|
||||
|
||||
template < typename Ar >
|
||||
void serialize(Ar &ar, const unsigned int version) {
|
||||
ar & boost::serialization::base_object<BaseClass>(*this);
|
||||
ar & m_ContainerBox;
|
||||
// ar & boost::serialization::base_object<BaseClass>(*this);
|
||||
ar & HRP("Density", m_Density, "g/cm3");
|
||||
}
|
||||
|
||||
public slots:
|
||||
void Update();
|
||||
|
||||
private:
|
||||
float m_Density;
|
||||
ContainerBox *m_ContainerBox;
|
||||
G4Box *m_Solid;
|
||||
};
|
||||
|
||||
51
src/HEP/Geant/uLibInterface.hh
Normal file
51
src/HEP/Geant/uLibInterface.hh
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef ULIB_GEANT_ULIB_INTERFACE_HH
|
||||
#define ULIB_GEANT_ULIB_INTERFACE_HH
|
||||
|
||||
#include "Math/Transform.h"
|
||||
#include <Geant4/G4Transform3D.hh>
|
||||
#include <Geant4/G4ThreeVector.hh>
|
||||
#include <Geant4/G4RotationMatrix.hh>
|
||||
|
||||
namespace uLib {
|
||||
namespace Geant {
|
||||
|
||||
/**
|
||||
* @brief Converts a uLib::Matrix4f to a Geant4 G4Transform3D.
|
||||
*/
|
||||
inline G4Transform3D ToG4Transform(const Matrix4f& m) {
|
||||
return G4Transform3D(
|
||||
m(0, 0), m(0, 1), m(0, 2), m(0, 3),
|
||||
m(1, 0), m(1, 1), m(1, 2), m(1, 3),
|
||||
m(2, 0), m(2, 1), m(2, 2), m(2, 3)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts a uLib::AffineTransform to a Geant4 G4Transform3D.
|
||||
*/
|
||||
inline G4Transform3D ToG4Transform(const AffineTransform& at) {
|
||||
return ToG4Transform(at.GetMatrix());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts a uLib::Vector3f to a Geant4 G4ThreeVector.
|
||||
*/
|
||||
inline G4ThreeVector ToG4Vector(const Vector3f& v) {
|
||||
return G4ThreeVector(v(0), v(1), v(2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts a uLib::Matrix3f to a Geant4 G4RotationMatrix.
|
||||
*/
|
||||
inline G4RotationMatrix ToG4Rotation(const Matrix3f& m) {
|
||||
G4RotationMatrix rot;
|
||||
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)));
|
||||
return rot;
|
||||
}
|
||||
|
||||
} // namespace Geant
|
||||
} // namespace uLib
|
||||
|
||||
#endif // ULIB_GEANT_ULIB_INTERFACE_HH
|
||||
Reference in New Issue
Block a user