From a142c5d060ff6fd0109427411404d699cbadf1ea Mon Sep 17 00:00:00 2001 From: AndreaRigoni Date: Fri, 13 Mar 2026 21:40:14 +0000 Subject: [PATCH] add units --- app/gcompose/src/main.cpp | 29 ++++++++++++-- src/HEP/Geant/Solid.cpp | 11 +++-- src/HEP/Geant/Solid.h | 3 +- src/Math/Units.h | 51 +++++++++++++++++++++++ src/Math/testing/CMakeLists.txt | 1 + src/Math/testing/Makefile.am | 4 +- src/Math/testing/UnitsTest.cpp | 71 +++++++++++++++++++++++++++++++++ 7 files changed, 160 insertions(+), 10 deletions(-) create mode 100644 src/Math/Units.h create mode 100644 src/Math/testing/UnitsTest.cpp diff --git a/app/gcompose/src/main.cpp b/app/gcompose/src/main.cpp index c63ebbb..a6a6185 100644 --- a/app/gcompose/src/main.cpp +++ b/app/gcompose/src/main.cpp @@ -1,6 +1,8 @@ #include "Math/ContainerBox.h" #include +#include "HEP/Detectors/DetectorChamber.h" +#include "Vtk/HEP/Detectors/vtkDetectorChamber.h" #include #include @@ -11,6 +13,7 @@ #include #include + #include 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(); diff --git a/src/HEP/Geant/Solid.cpp b/src/HEP/Geant/Solid.cpp index 993d0e5..dfc77a1 100644 --- a/src/HEP/Geant/Solid.cpp +++ b/src/HEP/Geant/Solid.cpp @@ -24,6 +24,7 @@ //////////////////////////////////////////////////////////////////////////////*/ // G4 Solid // +#include #include #include #include @@ -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); diff --git a/src/HEP/Geant/Solid.h b/src/HEP/Geant/Solid.h index fc81ec3..f73fd38 100644 --- a/src/HEP/Geant/Solid.h +++ b/src/HEP/Geant/Solid.h @@ -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 }; diff --git a/src/Math/Units.h b/src/Math/Units.h new file mode 100644 index 0000000..8ad340d --- /dev/null +++ b/src/Math/Units.h @@ -0,0 +1,51 @@ +#ifndef ULIB_MATH_UNITS_H +#define ULIB_MATH_UNITS_H + + +#include + +namespace uLib { + using namespace CLHEP; + + inline namespace literals { + constexpr double operator"" _m(long double v) { return static_cast(v) * CLHEP::meter; } + constexpr double operator"" _cm(long double v) { return static_cast(v) * CLHEP::centimeter; } + constexpr double operator"" _mm(long double v) { return static_cast(v) * CLHEP::millimeter; } + constexpr double operator"" _um(long double v) { return static_cast(v) * CLHEP::micrometer; } + constexpr double operator"" _nm(long double v) { return static_cast(v) * CLHEP::nanometer; } + constexpr double operator"" _km(long double v) { return static_cast(v) * CLHEP::kilometer; } + + constexpr double operator"" _m(unsigned long long v) { return static_cast(v) * CLHEP::meter; } + constexpr double operator"" _cm(unsigned long long v) { return static_cast(v) * CLHEP::centimeter; } + constexpr double operator"" _mm(unsigned long long v) { return static_cast(v) * CLHEP::millimeter; } + constexpr double operator"" _um(unsigned long long v) { return static_cast(v) * CLHEP::micrometer; } + constexpr double operator"" _nm(unsigned long long v) { return static_cast(v) * CLHEP::nanometer; } + constexpr double operator"" _km(unsigned long long v) { return static_cast(v) * CLHEP::kilometer; } + + constexpr double operator"" _deg(long double v) { return static_cast(v) * CLHEP::degree; } + constexpr double operator"" _rad(long double v) { return static_cast(v) * CLHEP::radian; } + constexpr double operator"" _deg(unsigned long long v) { return static_cast(v) * CLHEP::degree; } + constexpr double operator"" _rad(unsigned long long v) { return static_cast(v) * CLHEP::radian; } + + constexpr double operator"" _ns(long double v) { return static_cast(v) * CLHEP::nanosecond; } + constexpr double operator"" _s(long double v) { return static_cast(v) * CLHEP::second; } + constexpr double operator"" _ms(long double v) { return static_cast(v) * CLHEP::millisecond; } + constexpr double operator"" _ns(unsigned long long v) { return static_cast(v) * CLHEP::nanosecond; } + constexpr double operator"" _s(unsigned long long v) { return static_cast(v) * CLHEP::second; } + constexpr double operator"" _ms(unsigned long long v) { return static_cast(v) * CLHEP::millisecond; } + + constexpr double operator"" _MeV(long double v) { return static_cast(v) * CLHEP::megaelectronvolt; } + constexpr double operator"" _eV(long double v) { return static_cast(v) * CLHEP::electronvolt; } + constexpr double operator"" _keV(long double v) { return static_cast(v) * CLHEP::kiloelectronvolt; } + constexpr double operator"" _GeV(long double v) { return static_cast(v) * CLHEP::gigaelectronvolt; } + constexpr double operator"" _TeV(long double v) { return static_cast(v) * CLHEP::teraelectronvolt; } + constexpr double operator"" _MeV(unsigned long long v) { return static_cast(v) * CLHEP::megaelectronvolt; } + constexpr double operator"" _eV(unsigned long long v) { return static_cast(v) * CLHEP::electronvolt; } + constexpr double operator"" _keV(unsigned long long v) { return static_cast(v) * CLHEP::kiloelectronvolt; } + constexpr double operator"" _GeV(unsigned long long v) { return static_cast(v) * CLHEP::gigaelectronvolt; } + } +} + +#endif + + diff --git a/src/Math/testing/CMakeLists.txt b/src/Math/testing/CMakeLists.txt index a88969b..6539c2e 100644 --- a/src/Math/testing/CMakeLists.txt +++ b/src/Math/testing/CMakeLists.txt @@ -13,6 +13,7 @@ set(TESTS VoxImageCopyTest TriangleMeshTest BitCodeTest + UnitsTest ) set(LIBRARIES diff --git a/src/Math/testing/Makefile.am b/src/Math/testing/Makefile.am index 22ea4f1..545b1bb 100644 --- a/src/Math/testing/Makefile.am +++ b/src/Math/testing/Makefile.am @@ -14,7 +14,9 @@ TESTS = MathVectorTest \ PolicyTest \ AccumulatorTest \ VoxImageCopyTest \ - TriangleMeshTest + TriangleMeshTest \ + BitCodeTest \ + UnitsTest # else # TEST = diff --git a/src/Math/testing/UnitsTest.cpp b/src/Math/testing/UnitsTest.cpp new file mode 100644 index 0000000..577b434 --- /dev/null +++ b/src/Math/testing/UnitsTest.cpp @@ -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 + +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; +}