20#include "BDSAcceleratorModel.hh"
21#include "BDSColourFromMaterial.hh"
22#include "BDSColours.hh"
24#include "BDSException.hh"
25#include "BDSGeometryExternal.hh"
26#include "BDSGeometryFactoryGDML.hh"
27#include "BDSGeometryInspector.hh"
28#include "BDSGeometryWriter.hh"
29#include "BDSGDMLPreprocessor.hh"
30#include "BDSGlobalConstants.hh"
31#include "BDSMaterials.hh"
32#include "BDSWarning.hh"
36#include "G4GDMLParser.hh"
37#include "G4LogicalVolume.hh"
38#include "G4UserLimits.hh"
39#include "G4VisAttributes.hh"
40#include "G4VPhysicalVolume.hh"
57BDSGeometryFactoryGDML::BDSGeometryFactoryGDML()
62 std::map<G4String, G4Colour*>* mapping,
66 std::vector<G4String>* namedVacuumVolumes,
70 G4UserLimits* userLimitsToAttachToAllLVs)
76 G4String processedFile;
80 {processedFile = BDS::PreprocessGDML(fileName, componentName, preprocessGDMLSchema);}
81 else if (preprocessGDMLSchema)
82 {processedFile = BDS::PreprocessGDMLSchemaOnly(fileName);}
84 {processedFile = fileName;}
85 G4String preprocessNameToStrip = preprocessGDML ? componentName+
"_" :
"";
87 G4GDMLParser* parser =
new G4GDMLParser();
89 parser->Read(processedFile,
true);
91 G4VPhysicalVolume* containerPV = parser->GetWorldVolume();
92 G4LogicalVolume* containerLV = containerPV->GetLogicalVolume();
93 G4VSolid* containerSolid = containerLV->GetSolid();
94 G4ThreeVector gdmlWorldOrigin = G4ThreeVector();
95 if (containerPV->GetName() ==
"world_volume_lv_PV")
97 gdmlWorldOrigin = parser->GetPosition(
"PygdmlOrigin");
98 gdmlWorldOrigin[2] = 0.0;
102 std::set<G4VPhysicalVolume*> pvsGDML;
103 std::set<G4LogicalVolume*> lvsGDML;
104 std::map<G4String, G4Material*> materialsGDML;
109 std::map<G4String, G4Colour*> gdmlColours = BuildColourMap(lvsGDML, parser, componentName);
111 {gdmlColours.insert(mapping->begin(), mapping->end());}
113 G4cout <<
"Loaded GDML file \"" << processedFile <<
"\" containing:" << G4endl;
114 G4cout << pvsGDML.size() <<
" physical volumes, and " << lvsGDML.size() <<
" logical volumes" << G4endl;
116 auto visesGDML =
ApplyColourMapping(lvsGDML, gdmlColours, autoColour, preprocessNameToStrip);
123 G4VisAttributes* vis =
new G4VisAttributes(*c);
124 vis->SetVisibility(
true);
125 visesGDML.insert(vis);
126 containerLV->SetVisAttributes(vis);
146 vacuumSensitivityType);
153std::map<G4String, G4Colour*> BDSGeometryFactoryGDML::BuildColourMap(
const std::set<G4LogicalVolume*>& lvsGDML,
154 G4GDMLParser* parser,
155 const G4String& componentName)
157 std::map<G4String, G4Colour*> result;
159 for (
auto lv : lvsGDML)
161 auto auxInfo = parser->GetVolumeAuxiliaryInformation(lv);
162 for (
const auto& af : auxInfo)
164 if (af.type ==
"colour")
166 std::stringstream ss(af.value);
167 std::vector<G4String> colVals((std::istream_iterator<G4String>(ss)), std::istream_iterator<G4String>());
168 if (colVals.size() != 4)
169 {BDS::Warning(__METHOD_NAME__,
"invalid number of colour values for logical volume " + lv->GetName());}
170 G4String colourName = componentName +
"_colour_"+std::to_string(iColour);
172 G4String colourString = colourName +
":";
173 for (
const auto& c : colVals)
174 {colourString +=
" " + c;}
177 result[lv->GetName()] = colour;
181 G4bool visible =
false;
186 result[lv->GetName()] = colour;
197 std::array<G4double, 4> result{0,0,0,0};
198 std::stringstream ss(value);
212 catch (std::exception& e)
213 {
throw BDSException(__METHOD_NAME__,
"error in colour definition " + value);}
218 const G4String& acceleratorComponentName)
const
222 std::set<G4VPhysicalVolume*>& pvsIn,
223 std::set<G4LogicalVolume*>& lvsIn,
224 std::map<G4String, G4Material*>& materialsGDML)
226 const auto& lv = volume->GetLogicalVolume();
228 G4Material* mat = lv->GetMaterial();
229 materialsGDML[mat->GetName()] = mat;
230 for (G4int i = 0; i < (G4int)lv->GetNoDaughters(); i++)
232 const auto& pv = lv->GetDaughter(i);
239 const G4String& outputFileName,
241 const G4String& replacement)
244 std::ifstream ifs(fileName);
248 {
throw BDSException(__METHOD_NAME__,
"Cannot open file \"" + fileName +
"\"");}
250 std::ofstream fout(outputFileName);
252 G4cout << __METHOD_NAME__ <<
"Original file: " << fileName << G4endl;
253 G4cout << __METHOD_NAME__ <<
"Temporary file: " << outputFileName << G4endl;
256 int lenOfKey = (int)key.size();
260 while (std::getline(ifs, buffer))
262 int f = (int)buffer.find(key);
265 std::string outputString = std::string(buffer);
266 outputString.replace(f, lenOfKey, replacement);
267 fout << outputString <<
"\n";
270 {fout << buffer <<
"\n";}
280void _SymbolToPreventWarningGeomFacGDML(){;}
static BDSColourFromMaterial * Instance()
Singleton pattern.
G4Colour * GetColour(const G4Material *material, const G4String &prefixToStripFromName="")
Get colour from name.
G4Colour * GetColour(G4double red, G4double green, G4double blue, G4double alpha=1)
Get a cached anonymous colour by values.
static BDSColours * Instance()
singleton pattern
General exception with possible name of object and message.
static G4String ProcessedNodeName(const G4String &nodeName, const G4String &prefix)
void RegisterLogicalVolume(G4LogicalVolume *logicalVolume)
virtual std::set< G4LogicalVolume * > GetAllLogicalVolumes() const
Access all logical volumes belonging to this component.
void RegisterPhysicalVolume(G4VPhysicalVolume *physicalVolume)
void RegisterVisAttributes(G4VisAttributes *visAttribute)
A loaded piece of externally provided geometry.
void RegisterVacuumVolumes(const std::set< G4LogicalVolume * > &vacuumVolumesIn)
Register a set of volumes to be identified as vacuum volumes for the BDSAcceleratorComponent.
const std::set< G4LogicalVolume * > & VacuumVolumes() const
Access the vacuum volumes.
virtual void ApplySensitivity(BDSGeometryExternal *result, const std::set< G4LogicalVolume * > &allLogicalVolumesIn, BDSSDType generalSensitivity, const std::set< G4LogicalVolume * > &vacuumLogicalVolumes, BDSSDType vacuumSensitivity)
Attach the relevant general and vacuum sensitivity to each volume.
std::set< G4LogicalVolume * > GetVolumes(const std::set< G4LogicalVolume * > &allLVs, std::vector< G4String > *volumeNames, G4bool preprocessGDML, const G4String &componentName) const
Get the volumes that match the name. Volume names are matched exactly and are case sensitive.
virtual void CleanUp()
Virtual clean up that derived classes can override that calls CleanUpBase().
virtual void ApplyUserLimits(const std::set< G4LogicalVolume * > &lvsIn, G4UserLimits *userLimits)
Attach a set of user limits to every logical volume supplied.
virtual std::set< G4VisAttributes * > ApplyColourMapping(const std::set< G4LogicalVolume * > &lvs, const std::map< G4String, G4Colour * > &mapping, G4bool autoColour, const G4String &preprocessPrefix="")
void GetAllLogicalPhysicalAndMaterials(const G4VPhysicalVolume *volume, std::set< G4VPhysicalVolume * > &pvs, std::set< G4LogicalVolume * > &lvs, std::map< G4String, G4Material * > &materialsGDML)
static std::array< G4double, 4 > VRGBAStringAndUnitToColourAndStyle(const G4String &value, G4bool &visible)
virtual BDSGeometryExternal * Build(G4String componentName, G4String fileName, std::map< G4String, G4Colour * > *colourMapping=nullptr, G4bool autoColour=true, G4double suggestedLength=0, G4double suggestedHorizontalWidth=0, std::vector< G4String > *namedVacuumVolumes=nullptr, G4bool makeSensitive=true, BDSSDType sensitivityType=BDSSDType::energydep, BDSSDType vacuumSensitivityType=BDSSDType::energydepvacuum, G4UserLimits *userLimitsToAttachToAllLVs=nullptr)
virtual G4String PreprocessedName(const G4String &objectName, const G4String &acceleratorComponentName) const
Use the GDML preprocessing scheme to prepare the preprocessed volume names.
void ReplaceStringInFile(const G4String &filename, const G4String &outputFileName, const G4String &key, const G4String &replacement)
static const G4String auxType
"bds_vrgba" - short to minimise output file size: "bdsim visibility rgba"
static BDSGlobalConstants * Instance()
Access method.
static BDSMaterials * Instance()
Singleton pattern access.
void CacheMaterialsFromGDML(const std::map< G4String, G4Material * > &materialsGDML)
Introduce materials loaded from GDML into this instance.
void CheckForConflictingMaterialsAfterLoad(const G4String &geometryFileName, const G4String &componentName) const
Improve type-safety of native enum data type in C++.
std::pair< BDSExtent, BDSExtent > DetermineExtents(const G4VSolid *solid)