2#ifndef VOLUMEBUILDER_HH
3#define VOLUMEBUILDER_HH
31#include <G4Material.hh>
33#include "G4VPhysicalVolume.hh"
34#include "G4UnionSolid.hh"
35#include <G4ThreeVector.hh>
38#include <unordered_map>
42 class VolumeBuilderReference;
158 G4Material *material =
nullptr, G4String name = std::
string());
172 DerivedPtr
SetName(const G4String &name);
195 const
Unit3Vec &offset = {CLHEP::mm, 0, 0, 0},
196 G4RotationMatrix *rotation =
nullptr);
199 const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0},
200 G4RotationMatrix *rotation =
nullptr);
203 const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0},
204 G4RotationMatrix *rotation =
nullptr);
220 bool is_subtraction =
false,
221 bool is_intersection =
false,
222 const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0},
223 G4RotationMatrix *rotation = nullptr
240 DerivedPtr
SetColor(
double r = 0.5,
double g = 0.5,
double b = 0.5,
double alpha = 1.0);
248 DerivedPtr
SetColor(
const G4Colour &color);
256 DerivedPtr
SetAlpha(G4double alpha);
338 DerivedPtr
SetPhysOffset(
const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0});
349 DerivedPtr
StackPhysOffset(
const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0});
384 return {placement_configs_->rotation, placement_configs_->translation};
494 virtual DerivedPtr
ForkAndReset(
const G4String &new_name)
const;
538 std::optional<int> copy_no = std::nullopt
539 ,
const G4String &name_override =
""
540 ,
bool parent_name_was_set =
false
660 return std::static_pointer_cast<U>(base_cloned_ptr);
664 template <
typename T,
typename std::enable_if_t<std::is_base_of_v<IStructureBuilder, T>,
669 DerivedPtr MakeFinalSolid(G4String boolean_name =
"");
674 G4VSolid *GetSolidPtr()
const {
680 ONCE_MUTABLE Linkable<BooleanConfigs> boolean_configs_{BooleanConfigs{}};
681 ONCE_MUTABLE Linkable<VolumeConfigs> lv_configs_{VolumeConfigs{}};
682 ONCE_MUTABLE Linkable<PlacementConfigs> placement_configs_{PlacementConfigs{}};
694 void StoreIStructurePtr(
const IStructurePtr &istructure_ptr);
696 void StoreBuilderView(
const BuilderView &builder_view);
700 bool explicit_copyno_set_ =
false;
701 bool explicit_physical_copy_name_set_ =
false;
702 G4String placement_name_override_ =
"";
704 void SetSolid_impl(G4VSolid *solid);
705 void SetLogicalVolume_impl(G4LogicalVolume *logical_volume);
709 static constexpr bool has_ownership_{
false};
710 static constexpr bool enable_full_lazy_builds{
true};
716 void PropagateTransform();
719 void ApplyAttributes_();
720 G4String GetPlacementBaseName()
const;
722 [[nodiscard]] G4String GetLogicVolName()
const;
725 void ValidateForPVBuild(std::string
const &site = std::string());
726 void ValidateForVolumeBuild(std::string
const &site = std::string());
727 void ValidateForBooleanBuild(std::string
const &site = std::string());
729 void ValidateSolidNotBuilt(
const std::string &operation)
const;
730 void ValidateBooleanNotBuilt(
const std::string &operation)
const;
731 void ValidateLogicalNotBuilt(
const std::string &operation)
const;
732 void ValidatePlacementNotBuilt(
const std::string &operation)
const;
735 static void make_persistent(
const std::shared_ptr<void> &obj);
737 G4ThreeVector ProvisionUnits(
const Unit3Vec &vec)
const;
742 static void NoNameCheck(
const std::string &name,
const std::string &site) {
744 throw std::runtime_error(
"Error in " + site +
" ,"
745 "for builder named: " + name +
"\n"
746 " Must provide a new name");
A type-erased (data shared view) view of a builder or assembly, ie a "structure.".
Builder class for simple Box solids.
A solid buider class that just wrap an existing G4VSolid.
Builder class for RZ mult-plane defined solids.
A type-erased (data shared view) view of a builder or assembly, ie a "structure.".
A type-erased (data shared view) view of a builder or assembly, ie a "structure.".
A 3D vector that carries its own unit information.
A polymorphic, type-erased builder referencing any specialized builder.
VolumeBuilder: Common functionality for volume builder classes.
DerivedPtr ReflectZFinalSolid()
Configure final solid to be flipped in z.
friend AssemblyPtr CreateAssembly(G4String names)
Assembly of strucures, ie builders and/or other assemblies.
BuilderView ToBuilderView() const override
~VolumeBuilder() override
Destructor Normally does not delete volume objects.
DerivedPtr SetName(const G4String &name)
SetName Sets name used for solid and derived product names.
SharedPtr< IStructureBuilder > clone_impl() const override
DerivedPtr StackPhysTransform(const UnitlessG4Transform3D &new_transform)
Like SetPhysTransform but stacks with previous transformations in order applied.
G4VSolid * SolidConstructor(const G4String &name) override=0
polymorphic access to solid construction
DerivedPtr MakePlacement()
Make a placed physical volume.
DerivedPtr AddTo(BuilderViewList &list) const
DerivedPtr MakeSolid()
Make the G4VSolid.
StructureView ToStructureView() const override
DerivedPtr StackPhysOffset(const Unit3Vec &offset={CLHEP::mm, 0, 0, 0})
Like SetPhysOffset but stacks with previous transformations in order applied.
DerivedPtr StackPhysRotation(const G4RotationMatrix &stacked_rot)
Like SetPhysRotation but stacks with previous transformations in order applied.
G4String GetBuilderName() const
friend FromG4VSolidPtr CreateFromG4VSolid(G4VSolid *solid)
Constructor to make a builder from an Existing Geant solid.
friend class VolumeBuilder
DerivedPtr MakeLogicalVolume(G4Material *material=nullptr, G4String name=std::string())
Basically never needed now.
DerivedPtr ReflectZBaseSolid()
Configure base solid to be flipped BEFORE applying booleans.
A wrapper for std::shared_ptr that allows and facilitates many implicit(i) type conversions.
DerivedPtr CopyVolumeConfigsFrom(const BuilderView &other)
Copies the LogicalVolume configuration (material, VisAttributes, etc.) from another builder.
DerivedPtr CopyPlacementConfigsFrom(const BuilderView &other)
This may help if reusing placement configs for a different shape.
DerivedPtr ForkForLogicalVolume(const G4String &new_name)
Makes final solid (including booleans) if unbuilt and copies builder with products cleared for furthe...
virtual DerivedPtr ForkAndReset(const G4String &new_name) const
Copy a configured/unbuilt Builder, with a new name.
DerivedPtr ForkForFinalSolid(const G4String &new_name)
Calls MakeSolid() and copies builder with products cleared for further construction.
DerivedPtr ForkForPlacement(std::optional< int > copy_no=std::nullopt, const G4String &name_override="", bool parent_name_was_set=false)
Calls MakeLogicalVolume() and creates a new builder instance with a copy of all placement-related con...
DerivedPtr SetColor(double r=0.5, double g=0.5, double b=0.5, double alpha=1.0)
DerivedPtr SetVisibility(bool x=true)
DerivedPtr SetAlpha(G4double alpha)
DerivedPtr SetMaterial(G4Material *material)
DerivedPtr AddBoolean(const BuilderView &other, bool is_subtraction=false, bool is_intersection=false, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
Add a boolean operation.
DerivedPtr ForceSolid(bool x=true)
DerivedPtr SetAutoCopyNo(bool set)
Enable (disable for false) auto Physical Volume numbering, on by default.
DerivedPtr SetBooleanName(const G4String &name)
Pre-set base name for logical and physical volumes.
DerivedPtr SetLogicalVolume(G4LogicalVolume *logical_volume)
Directly Provide a Logical Volume Instead of using the builder.
DerivedPtr SetSurfaceCheck(G4bool pSurfChk)
Set the surface check flag for the placement.
DerivedPtr SetMother(const BuilderView &mother)
Set the mother volume with a builder, or G4VPhysicalVolume or G4VLogicalVolume through implicit conve...
DerivedPtr SetPhysOffset(const Unit3Vec &offset={CLHEP::mm, 0, 0, 0})
Set the translation vector for placement.
DerivedPtr SetPhysTransform(const UnitlessG4Transform3D &new_transform)
Set the G4Transform3D for placment The tranformation provided is meant to be UNITLESS unless you SetD...
DerivedPtr SetCopyNo(G4int pCopyNo)
Set the copy number for the placement.
DerivedPtr OverridePlacementName(const G4String &pName)
Set the placement name for the volume.
DerivedPtr SetPhysRotation(const G4RotationMatrix &rot)
Set the rotation matrix for placement.
DerivedPtr SetAutoPlacementNaming(bool set)
Enable auto Physical Volume naming.
G4Transform3D GetPhysTransform() const final
A helper to get the Physical Volume transform with units applied This is built from rotation and tran...
DerivedPtr PlaceAndFork()
An alias for MakePlacement and ForkForPlacement Used for one or many unaltered placments in sequence,...
G4VSolid * GetFinalSolid() final
Gets a complete final built Solid, including configured Boolean operaions if any.
G4LogicalVolume * GetLogicalVolume() final
Gets a built LogicalVolume, builds default if not built yet.
G4VPhysicalVolume * GetPlacement() final
Gets the (last) placed physical volume.
G4VSolid * GetBaseSolid() final
You probably want GetSolid() instead!!! This Gets a built Solid, BUT NOT final Boolean.
G4double GetEffectiveDefaultUnit() const
Get the builder default unit or global if not set.
DerivedPtr SetDefaultUnit(G4double unit)
Set the per-Builder default unit for all later non-factory offsets.
DerivedPtr AddSubtraction(const BuilderView &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
Define combination another volume with present one, This does NOT immediately trigger a build on a pa...
DerivedPtr AddUnion(const BuilderView &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
Define combination another volume with present one, This does NOT immediately trigger a build on a pa...
DerivedPtr AddIntersection(const BuilderView &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
Define combination another volume with present one, This does NOT immediately trigger a build on a pa...
SharedPtr< VolumeBuilderReference > BuilderView
Common interface for all volume builder types.
SharedPtr< IStructureBuilder > IStructurePtr
True polymorphic class base view for all structures Mostly for internal use.
std::vector< StructureView > StructureViewList
a user type to hold many structures
G4Transform3D UnitlessG4Transform3D
std::vector< BuilderView > BuilderViewList
a user type to hold many builders