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