BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSGeometryFactoryBase.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2024.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSColours.hh"
20#include "BDSColourFromMaterial.hh"
21#include "BDSGeometryExternal.hh"
22#include "BDSGeometryFactoryBase.hh"
23#include "BDSGlobalConstants.hh"
24#include "BDSSDType.hh"
25#include "BDSUtilities.hh"
26
27#include "globals.hh"
28#include "G4AssemblyVolume.hh"
29#include "G4Colour.hh"
30#include "G4LogicalVolume.hh"
31#include "G4RotationMatrix.hh"
32#include "G4String.hh"
33#include "G4VisAttributes.hh"
34#include "G4VPhysicalVolume.hh"
35
36#include <algorithm>
37#include <map>
38#include <set>
39#include <vector>
40
41BDSGeometryFactoryBase::BDSGeometryFactoryBase()
42{
44}
45
46BDSGeometryFactoryBase::~BDSGeometryFactoryBase()
47{;}
48
49std::set<G4VisAttributes*> BDSGeometryFactoryBase::ApplyColourMapping(const std::set<G4LogicalVolume*>& lvsIn,
50 const std::map<G4String, G4Colour*>& mapping,
51 G4bool autoColour,
52 const G4String& prefixToStripFromName)
53{
54 std::set<G4VisAttributes*> visAttributes; // empty set
55
56 // no mapping, just return.
57 if (mapping.empty() && !autoColour)
58 {return visAttributes;}
59
60 if (mapping.size() == 1)
61 {// only one colour for all - simpler
62 G4VisAttributes* vis = new G4VisAttributes(*BDSColours::Instance()->GetColour("gdml"));
63 vis->SetVisibility(true);
64 visAttributes.insert(vis);
65 for (auto lv : lvsIn)
66 {lv->SetVisAttributes(*vis);}
67 return visAttributes;
68 }
69
70 // else iterate over all lvs and required vis attributes
71 // prepare required vis attributes
72 std::map<G4String, G4VisAttributes*> attMap;
73 for (const auto& it : mapping)
74 {
75 G4VisAttributes* vis = new G4VisAttributes(*(it.second));
76 vis->SetVisibility(true);
77 visAttributes.insert(vis);
78 attMap[it.first] = vis;
79 }
80
81 for (auto lv : lvsIn)
82 {// iterate over all volumes
83 const G4String& name = lv->GetName();
84 for (const auto& it : attMap)
85 {// iterate over all mappings to find first one that matches substring
86 if (BDS::StrContains(name, it.first))
87 {
88 lv->SetVisAttributes(it.second);
89 break;
90 }
91 }
92 }
93
94 if (autoColour)
95 {
96 for (auto lv : lvsIn)
97 {
98 const G4VisAttributes* existingVis = lv->GetVisAttributes();
99 if (!existingVis)
100 {
101 G4Colour* c = BDSColourFromMaterial::Instance()->GetColour(lv->GetMaterial(), prefixToStripFromName);
102 G4VisAttributes* vis = new G4VisAttributes(*c);
103 vis->SetVisibility(true);
104 visAttributes.insert(vis);
105 lv->SetVisAttributes(vis);
106 }
107 }
108 }
109 return visAttributes;
110}
111
112void BDSGeometryFactoryBase::ApplyUserLimits(const std::set<G4LogicalVolume*>& lvsIn,
113 G4UserLimits* userLimits)
114{
115 for (auto& lv : lvsIn)
116 {lv->SetUserLimits(userLimits);}
117}
118
120 const std::set<G4LogicalVolume*>& allLogicalVolumesIn,
121 BDSSDType generalSensitivity,
122 const std::set<G4LogicalVolume*>& vacuumLogicalVolumes,
123 BDSSDType vacuumSensitivity)
124{
125 std::map<G4LogicalVolume*, BDSSDType> sensitivityMapping;
126 std::set<G4LogicalVolume*> notVacuumVolumes;
127 std::set_difference(allLogicalVolumesIn.begin(),
128 allLogicalVolumesIn.end(),
129 vacuumLogicalVolumes.begin(),
130 vacuumLogicalVolumes.end(),
131 std::inserter(notVacuumVolumes, notVacuumVolumes.begin()));
132 for (auto lv : notVacuumVolumes)
133 {sensitivityMapping[lv] = generalSensitivity;}
134 for (auto lv : vacuumLogicalVolumes)
135 {sensitivityMapping[lv] = vacuumSensitivity;}
136 result->RegisterSensitiveVolume(sensitivityMapping);
137}
138
143
144G4String BDSGeometryFactoryBase:: PreprocessedName(const G4String& objectName,
145 const G4String& /*acceleratorComponentName*/) const
146{return objectName;}
147
148std::set<G4LogicalVolume*> BDSGeometryFactoryBase::GetVolumes(const std::set<G4LogicalVolume*>& allLVs,
149 std::vector<G4String>* volumeNames,
150 G4bool preprocessFile,
151 const G4String& componentName) const
152{
153 if (!volumeNames)
154 {return std::set<G4LogicalVolume*>();}
155
156 std::vector<G4String> expectedVolumeNames;
157 if (preprocessFile)
158 {
159 // transform the names to those the preprocessor would produce
160 expectedVolumeNames.resize(volumeNames->size());
161 std::transform(volumeNames->begin(),
162 volumeNames->end(),
163 expectedVolumeNames.begin(),
164 [&](const G4String& n){return PreprocessedName(n, componentName);});
165 }
166 else
167 {expectedVolumeNames = *volumeNames;}
168
169 std::set<G4LogicalVolume*> volsMatched;
170 for (const auto& en : expectedVolumeNames)
171 {
172 for (auto lv : allLVs)
173 {
174 if (lv->GetName() == en)
175 {volsMatched.insert(lv);}
176 }
177 }
178 return volsMatched;
179}
180
182{
184 xmin = 0;
185 xmax = 0;
186 ymin = 0;
187 ymax = 0;
188 zmin = 0;
189 zmax = 0;
190}
191
193{
194 xmin = std::min(ext.XNeg(), xmin);
195 xmax = std::max(ext.XPos(), xmax);
196 ymin = std::min(ext.YNeg(), ymin);
197 ymax = std::max(ext.YPos(), ymax);
198 zmin = std::min(ext.ZNeg(), zmin);
199 zmax = std::max(ext.ZPos(), zmax);
200}
201
202void BDSGeometryFactoryBase::ExpandExtent(G4double x0, G4double rx,
203 G4double y0, G4double ry,
204 G4double z0, G4double rz)
205{
206 xmin = std::min(x0-rx, xmin);
207 xmax = std::max(x0+rx, xmax);
208 ymin = std::min(y0-ry, ymin);
209 ymax = std::max(y0+ry, ymax);
210 zmin = std::min(z0-rz, zmin);
211 zmax = std::max(z0+rz, zmax);
212}
213
214void BDSGeometryFactoryBase::ExpandExtent(G4double x0, G4double xLow, G4double xHigh,
215 G4double y0, G4double yLow, G4double yHigh,
216 G4double z0, G4double zLow, G4double zHigh)
217{
218 xmin = std::min(x0+xLow, xmin);
219 xmax = std::max(x0+xHigh, xmax);
220 ymin = std::min(y0+yLow, ymin);
221 ymax = std::max(y0+yHigh, ymax);
222 zmin = std::min(z0+zLow, zmin);
223 zmax = std::max(z0+zHigh, zmax);
224}
static BDSColourFromMaterial * Instance()
Singleton pattern.
G4Colour * GetColour(const G4Material *material, const G4String &prefixToStripFromName="")
Get colour from name.
static BDSColours * Instance()
singleton pattern
Definition BDSColours.cc:33
Holder for +- extents in 3 dimensions.
Definition BDSExtent.hh:39
G4double XPos() const
Accessor.
Definition BDSExtent.hh:66
G4double ZPos() const
Accessor.
Definition BDSExtent.hh:70
G4double XNeg() const
Accessor.
Definition BDSExtent.hh:65
G4double YNeg() const
Accessor.
Definition BDSExtent.hh:67
G4double YPos() const
Accessor.
Definition BDSExtent.hh:68
G4double ZNeg() const
Accessor.
Definition BDSExtent.hh:69
virtual void FactoryBaseCleanUp()
Empty containers for next use - factories are never deleted so can't rely on scope.
void RegisterSensitiveVolume(G4LogicalVolume *sensitiveVolume, BDSSDType sensitivityType)
A loaded piece of externally provided geometry.
G4double ymax
Extent in one dimension.
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.
void ExpandExtent(const BDSExtent &extent)
Expand the acuumulated extents using a (possibly asymmetric) extent instance.
virtual G4String PreprocessedName(const G4String &objectName, const G4String &acceleratorComponentName) const
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().
G4double xmin
Extent in one dimension.
G4double ymin
Extent in one dimension.
G4double zmin
Extent in one dimension.
G4double zmax
Extent in one dimension.
G4double xmax
Extent in one dimension.
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="")
Improve type-safety of native enum data type in C++.
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.