g4system
Loading...
Searching...
No Matches
buildSolid.cc
Go to the documentation of this file.
1
7// gemc
8#include "gutilities.h"
9
10// g4system
11#include "cadSystemFactory.h"
12
13// ───────────────── CADMesh single-header library ────────────────
14// https://github.com/christopherpoole/CADMesh
15//
16// *Modifications applied to the vendor header:*
17// - Marked the following classes `final`
18// • BuiltInReader
19// • TessellatedMesh
20// - Reason: https://devblogs.microsoft.com/oldnewthing/20200619-00/?p=103877
21//
22// *Optional Dependencies*
23// Define `USE_CADMESH_ASSIMP_READER` **before** including `CADMesh.hh`
24// to enable Assimp-based file loading.
25//
26// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27// !!! SET THIS BEFORE INCLUDING CADMESH.HH TO USE THE ASSIMP READER
28// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
29#define USE_CADMESH_ASSIMP_READER
30// If you want Assimp to be the default reader, uncomment:
31// #define CADMESH_DEFAULT_READER ASSIMP
32
33#include "CADMesh.hh"
34
35// Geant4 units
36#include "CLHEP/Units/SystemOfUnits.h"
37
39 std::unordered_map<std::string,
40 G4Volume*>* g4s) {
41 std::string g4name = s->getG4Name();
42
43 // Dependency check: solids can require other solids (copy/boolean operations).
44 if (!checkSolidDependencies(s, g4s)) return nullptr;
45
46 // Locate or allocate the wrapper used to cache solid/logical/physical pointers.
47 auto thisG4Volume = getOrCreateG4Volume(g4name, g4s);
48 if (thisG4Volume->getSolid() != nullptr) return thisG4Volume->getSolid();
49
50 // CAD file handling:
51 // - file path is stored in the volume "description" field
52 // - extension determines which CADMesh reader path is used
53 std::string fileName = s->getDescription(); // full file path from DB
54 G4String g4filename = fileName;
55
56 // File extension (last token after '.').
57 std::string extension =
59
60 // PLY / STL via CADMesh & Assimp reader.
61 if (extension == "ply" || extension == "stl") {
62 auto mesh = CADMesh::TessellatedMesh::From(g4filename,
63 CADMesh::File::ASSIMP());
64
65 // The CAD file is interpreted in millimeters to match typical detector CAD conventions.
66 mesh->SetScale(CLHEP::mm);
67
68 // Do not flip vertex winding unless the CAD source requires it.
69 mesh->SetReverse(false);
70
71 thisG4Volume->setSolid(mesh->GetSolid(), log);
72 return thisG4Volume->getSolid();
73 }
74
75 // Unsupported extension: return nullptr so the caller can decide whether to treat it as fatal.
76 log->warning("G4CadSystemFactory: file <", fileName,
77 "> has unsupported extension <", extension, ">");
78 return nullptr;
79}
Factory that converts CAD files (PLY / STL) into Geant4 tessellated solids via CADMesh.
G4VSolid * buildSolid(const GVolume *s, std::unordered_map< std::string, G4Volume * > *g4s) override
Create (or fetch) a tessellated solid from a CAD file.
Definition buildSolid.cc:38
G4Volume * getOrCreateG4Volume(const std::string &volume_name, std::unordered_map< std::string, G4Volume * > *g4s)
Get or create a G4Volume wrapper entry in the map.
bool checkSolidDependencies(const GVolume *s, std::unordered_map< std::string, G4Volume * > *g4s)
Check whether all prerequisites to build a solid are satisfied.
Convenience container holding a Geant4 solid, logical, and physical volume.
Definition g4volume.h:50
std::shared_ptr< GLogger > log
void warning(Args &&... args) const
std::string getDescription() const
std::string getG4Name() const
vector< string > getStringVectorFromStringWithDelimiter(const string &input, const string &x)