add units

This commit is contained in:
AndreaRigoni
2026-03-13 21:40:14 +00:00
parent 61052f80bc
commit a142c5d060
7 changed files with 160 additions and 10 deletions

View File

@@ -1,6 +1,8 @@
#include "Math/ContainerBox.h"
#include <HEP/Geant/Scene.h>
#include "HEP/Detectors/DetectorChamber.h"
#include "Vtk/HEP/Detectors/vtkDetectorChamber.h"
#include <Vtk/uLibVtkViewer.h>
#include <Vtk/vtkContainerBox.h>
@@ -11,6 +13,7 @@
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <iostream>
using namespace uLib;
@@ -18,16 +21,36 @@ using namespace uLib;
int main(int argc, char** argv) {
std::cout << "Starting gcompose application..." << std::endl;
uLib::ContainerBox world_box(Vector3f(100, 100, 100));
ContainerBox world_box(Vector3f(1, 1, 1));
world_box.Scale(Vector3f(20_mm, 20_mm, 20_mm));
uLib::Scene scene;
DetectorChamber d1, d2;
d1.SetSize(Vector3f(1, 1, 1));
d1.SetPosition(Vector3f(0, 0, 0));
d1.Scale(Vector3f(5, 10, 2));
d1.Translate(Vector3f(0, 0, 0));
d2.SetSize(Vector3f(1, 1, 1));
d2.SetPosition(Vector3f(0, 0, 0));
d2.Scale(Vector3f(5, 10, 2));
d2.Translate(Vector3f(0, 0, 10));
Scene scene;
scene.ConstructWorldBox(&world_box, "G4_AIR");
scene.Initialize();
// 2. Initialize VTK Viewer
Vtk::Viewer viewer;
uLib::Vtk::vtkContainerBox vtk_box(&world_box);
Vtk::vtkDetectorChamber vtk_d1(&d1);
viewer.AddPuppet(vtk_d1);
Vtk::vtkDetectorChamber vtk_d2(&d2);
viewer.AddPuppet(vtk_d2);
Vtk::vtkContainerBox vtk_box(&world_box);
viewer.AddPuppet(vtk_box);
viewer.GetRenderer()->Render();

View File

@@ -24,6 +24,7 @@
//////////////////////////////////////////////////////////////////////////////*/
// G4 Solid //
#include <CLHEP/Units/SystemOfUnits.h>
#include <Geant4/G4LogicalVolume.hh>
#include <Geant4/G4Material.hh>
#include <Geant4/G4NistManager.hh>
@@ -43,11 +44,13 @@
namespace uLib {
class DetectorsSolidPimpl {
class DetectorsSolidImpl {
public:
static G4ThreeVector getG4Vector3f(const Vector3f &vector) {
return G4ThreeVector(vector(0), vector(1), vector(2));
}
};
Solid::Solid()
@@ -139,9 +142,9 @@ void TessellatedSolid::SetMesh(TriangleMesh &mesh) {
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);
DetectorsSolidImpl::getG4Vector3f(mesh.Points().at(trg(0))),
DetectorsSolidImpl::getG4Vector3f(mesh.Points().at(trg(1))),
DetectorsSolidImpl::getG4Vector3f(mesh.Points().at(trg(2))), ABSOLUTE);
ts->AddFacet((G4VFacet *)facet);
}
this->m_Logical->SetSolid(ts);

View File

@@ -47,7 +47,7 @@ public:
void SetNistMaterial(const char *name);
void SetMaterial(G4Material *material);
void SetSizeUnit(const char *unit);
// Implementiamo SetParent qui, per tutti.
virtual void SetParent(Solid *parent);
@@ -65,7 +65,6 @@ protected:
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
};

51
src/Math/Units.h Normal file
View File

@@ -0,0 +1,51 @@
#ifndef ULIB_MATH_UNITS_H
#define ULIB_MATH_UNITS_H
#include <CLHEP/Units/SystemOfUnits.h>
namespace uLib {
using namespace CLHEP;
inline namespace literals {
constexpr double operator"" _m(long double v) { return static_cast<double>(v) * CLHEP::meter; }
constexpr double operator"" _cm(long double v) { return static_cast<double>(v) * CLHEP::centimeter; }
constexpr double operator"" _mm(long double v) { return static_cast<double>(v) * CLHEP::millimeter; }
constexpr double operator"" _um(long double v) { return static_cast<double>(v) * CLHEP::micrometer; }
constexpr double operator"" _nm(long double v) { return static_cast<double>(v) * CLHEP::nanometer; }
constexpr double operator"" _km(long double v) { return static_cast<double>(v) * CLHEP::kilometer; }
constexpr double operator"" _m(unsigned long long v) { return static_cast<double>(v) * CLHEP::meter; }
constexpr double operator"" _cm(unsigned long long v) { return static_cast<double>(v) * CLHEP::centimeter; }
constexpr double operator"" _mm(unsigned long long v) { return static_cast<double>(v) * CLHEP::millimeter; }
constexpr double operator"" _um(unsigned long long v) { return static_cast<double>(v) * CLHEP::micrometer; }
constexpr double operator"" _nm(unsigned long long v) { return static_cast<double>(v) * CLHEP::nanometer; }
constexpr double operator"" _km(unsigned long long v) { return static_cast<double>(v) * CLHEP::kilometer; }
constexpr double operator"" _deg(long double v) { return static_cast<double>(v) * CLHEP::degree; }
constexpr double operator"" _rad(long double v) { return static_cast<double>(v) * CLHEP::radian; }
constexpr double operator"" _deg(unsigned long long v) { return static_cast<double>(v) * CLHEP::degree; }
constexpr double operator"" _rad(unsigned long long v) { return static_cast<double>(v) * CLHEP::radian; }
constexpr double operator"" _ns(long double v) { return static_cast<double>(v) * CLHEP::nanosecond; }
constexpr double operator"" _s(long double v) { return static_cast<double>(v) * CLHEP::second; }
constexpr double operator"" _ms(long double v) { return static_cast<double>(v) * CLHEP::millisecond; }
constexpr double operator"" _ns(unsigned long long v) { return static_cast<double>(v) * CLHEP::nanosecond; }
constexpr double operator"" _s(unsigned long long v) { return static_cast<double>(v) * CLHEP::second; }
constexpr double operator"" _ms(unsigned long long v) { return static_cast<double>(v) * CLHEP::millisecond; }
constexpr double operator"" _MeV(long double v) { return static_cast<double>(v) * CLHEP::megaelectronvolt; }
constexpr double operator"" _eV(long double v) { return static_cast<double>(v) * CLHEP::electronvolt; }
constexpr double operator"" _keV(long double v) { return static_cast<double>(v) * CLHEP::kiloelectronvolt; }
constexpr double operator"" _GeV(long double v) { return static_cast<double>(v) * CLHEP::gigaelectronvolt; }
constexpr double operator"" _TeV(long double v) { return static_cast<double>(v) * CLHEP::teraelectronvolt; }
constexpr double operator"" _MeV(unsigned long long v) { return static_cast<double>(v) * CLHEP::megaelectronvolt; }
constexpr double operator"" _eV(unsigned long long v) { return static_cast<double>(v) * CLHEP::electronvolt; }
constexpr double operator"" _keV(unsigned long long v) { return static_cast<double>(v) * CLHEP::kiloelectronvolt; }
constexpr double operator"" _GeV(unsigned long long v) { return static_cast<double>(v) * CLHEP::gigaelectronvolt; }
}
}
#endif

View File

@@ -13,6 +13,7 @@ set(TESTS
VoxImageCopyTest
TriangleMeshTest
BitCodeTest
UnitsTest
)
set(LIBRARIES

View File

@@ -14,7 +14,9 @@ TESTS = MathVectorTest \
PolicyTest \
AccumulatorTest \
VoxImageCopyTest \
TriangleMeshTest
TriangleMeshTest \
BitCodeTest \
UnitsTest
# else
# TEST =

View File

@@ -0,0 +1,71 @@
/*//////////////////////////////////////////////////////////////////////////////
// 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 "testing-prototype.h"
#include "Math/Units.h"
#include <cmath>
using namespace uLib;
int main()
{
BEGIN_TESTING(Math Units);
// Length
TEST0(1.0_m != 1000.0);
TEST0(1_m != 1000.0);
TEST0(1.0_cm != 10.0);
TEST0(1_cm != 10.0);
TEST0(1.0_mm != 1.0);
TEST0(1_mm != 1.0);
TEST0(1.0_km != 1000000.0);
TEST0(1_km != 1000000.0);
TEST0(std::abs(1.0_um - 1e-3) > 1e-12);
TEST0(std::abs(1.0_nm - 1e-6) > 1e-15);
// Angle
TEST0(std::abs(180.0_deg - M_PI) > 1e-9);
TEST0(std::abs(180_deg - M_PI) > 1e-9);
TEST0(std::abs(1.0_rad - 1.0) > 1e-9);
// Time
TEST0(1.0_s != 1e9);
TEST0(1_s != 1e9);
TEST0(1.0_ms != 1e6);
TEST0(1_ms != 1e6);
TEST0(1.0_ns != 1.0);
TEST0(1_ns != 1.0);
// Energy
TEST0(1.0_MeV != 1.0);
TEST0(1_MeV != 1.0);
TEST0(1.0_GeV != 1000.0);
TEST0(1_GeV != 1000.0);
TEST0(1.0_TeV != 1000000.0);
TEST0(1.0_keV != 1e-3);
TEST0(1.0_eV != 1e-6);
END_TESTING;
}