DLG4::VolumeBuilders
A fluent interface for Geant4 geometry definition.
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
VolumeBuilderBase< U > Class Template Referenceabstract
Collaboration diagram for VolumeBuilderBase< U >:
[legend]

Public Member Functions

G4VPhysicalVolume * GetPlacement () final
 
DerivedPtr MakePlacement ()
 
DerivedPtr PlaceAndFork ()
 
G4LogicalVolume * GetLogicalVolume () final
 
G4VSolid * GetBaseSolid () final
 
G4VSolid * GetFinalSolid () final
 
DerivedPtr MakeLogicalVolume (G4Material *material=nullptr, G4String name=std::string())
 
DerivedPtr MakeSolid ()
 
DerivedPtr SetName (const G4String &name)
 
DerivedPtr AddUnion (const VolumeBuilder &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
 
DerivedPtr AddSubtraction (const VolumeBuilder &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
 
DerivedPtr AddIntersection (const VolumeBuilder &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
 
DerivedPtr AddBoolean (const VolumeBuilder &other, bool is_subtraction=false, bool is_intersection=false, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
 
DerivedPtr SetMaterial (G4Material *material)
 
DerivedPtr SetColor (double r=0.5, double g=0.5, double b=0.5, double alpha=1.0)
 
DerivedPtr SetColor (const G4Colour &color)
 
DerivedPtr SetAlpha (G4double alpha)
 
DerivedPtr ForceSolid (bool x=true)
 
DerivedPtr SetVisibility (bool x=true)
 
DerivedPtr SetLogicalVolume (G4LogicalVolume *logical_volume)
 
DerivedPtr SetBooleanName (const G4String &name)
 
DerivedPtr SetPhysRotation (const G4RotationMatrix &rot)
 
DerivedPtr StackPhysRotation (const G4RotationMatrix &stacked_rot)
 
DerivedPtr SetPhysOffset (const Unit3Vec &offset={CLHEP::mm, 0, 0, 0})
 
DerivedPtr StackPhysOffset (const Unit3Vec &offset={CLHEP::mm, 0, 0, 0})
 
DerivedPtr SetPhysTransform (const UnitlessG4Transform3D &new_transform)
 
DerivedPtr StackPhysTransform (const UnitlessG4Transform3D &new_transform)
 
G4Transform3D GetPhysTransform () const final
 
DerivedPtr OverridePlacementName (const G4String &pName)
 
DerivedPtr SetCopyNo (G4int pCopyNo)
 
DerivedPtr SetSurfaceCheck (G4bool pSurfChk)
 
DerivedPtr SetMother (const VolumeBuilder &mother)
 
DerivedPtr SetAutoPlacementNaming (bool set)
 
DerivedPtr SetAutoCopyNo (bool set)
 
DerivedPtr ReflectZFinalSolid ()
 
DerivedPtr ReflectZBaseSolid ()
 
DerivedPtr ForkAndReset (const G4String &new_name) const
 
DerivedPtr ForkForFinalSolid (const G4String &new_name)
 
DerivedPtr ForkForLogicalVolume (const G4String &new_name)
 
DerivedPtr ForkForPlacement (std::optional< int > copy_no=std::nullopt, const G4String &name_override="", bool parent_name_was_set=false)
 
DerivedPtr CopyPlacementConfigsFrom (const VolumeBuilder &other)
 
DerivedPtr CopyVolumeConfigsFrom (const VolumeBuilder &other)
 
DerivedPtr SetDefaultUnit (G4double unit)
 
G4double GetEffectiveDefaultUnit () const
 
DerivedPtr AddTo (BuilderViewList &list) const
 
DerivedPtr AddTo (StructureViewList &list) const
 
DerivedPtr AddTo (Assembly &assembly) const
 
 ~VolumeBuilderBase () override
 
G4String GetBuilderName () const
 
template<typename U >
 VolumeBuilderBase (const VolumeBuilderBase< U > &other)
 
template<typename T , std::enable_if_t< std::is_base_of_v< IStructureBuilder, T >, int > >
 VolumeBuilderBase (const SharedPtr< T > &other, std::true_type)
 
std::shared_ptr< T > shared_from_this ()
 
std::shared_ptr< const T > shared_from_this () const
 
void set_shared_from_this_enabled (bool enabled)
 

Protected Member Functions

G4VSolid * SolidConstructor (const G4String &name) override=0
 
SharedPtr< IStructureBuilderclone_impl () const override
 

Protected Attributes

std::atomic< bool > is_enabled_ {true}
 

Friends

template<typename >
class VolumeBuilderBase
 
class VolumeBuilderCore
 
class AssemblyCore
 
template<typename >
class StructureBuilderBase
 
class StructureBuilderCore
 
class RZBuilderCore
 
class BoxBuilderCore
 
class FromG4VSolidCore
 
FromG4VSolid VB::CreateFromG4VSolid (G4VSolid *solid)
 
Assembly VB::CreateAssembly (G4String names)
 

Detailed Description

template<typename U>
class DLG4::VolumeBuilders::_internals_::VolumeBuilderBase< U >

VolumeBuilder: Common functionality for volume builder classes.

Includes material and attribute setting, and union/subtraction methods. This is Not a user class. Only usable through inheritance to extend class functionality. Return most things by pointer to the derived class So we can chain operations (fluent interface).

Template Parameters
UCRTP paramter for the derived class.

Definition at line 60 of file VolumeBuilderBase.hh.

Constructor & Destructor Documentation

◆ ~VolumeBuilderBase()

template<typename U >
~VolumeBuilderBase ( )
override

Destructor Normally does not delete volume objects.

Geant takes care of that.

Definition at line 114 of file VolumeBuilderBase.hpp.

114 {
115 // optionally release resources (default)
116 // ReSharper disable once CppIfCanBeReplacedByConstexprIf
117 if (!has_ownership_) {
118 // This is SO wrong. This should be done at CREATION of members,
119 // not deletion of class.
120 // But it works for now and we're super restrictive on creation.
121 final_solid_ptr_.make_persistent();
122 solid_ptr_.make_persistent();
123 logicvol_ptr_.make_persistent();
124 placement_.make_persistent();
125 }
126 }

◆ VolumeBuilderBase() [1/2]

template<typename U >
template<typename U >
VolumeBuilderBase ( const VolumeBuilderBase< U > &  other)

Definition at line 83 of file VolumeBuilderBase.hpp.

83 : builder_configs_(other.builder_configs_),
84 boolean_configs_(other.boolean_configs_),
85 lv_configs_(other.lv_configs_),
86 placement_configs_(other.placement_configs_) {
87 // avoid masked bugs from stale view:
88 StoreBuilderView(nullptr);
89 StoreIStructurePtr(nullptr);
90 // don't copy products since they can only be linked once.
91 // Copy methods will do that.
92 }

◆ VolumeBuilderBase() [2/2]

template<typename U >
template<typename T , std::enable_if_t< std::is_base_of_v< IStructureBuilder, T >, int > >
VolumeBuilderBase ( const SharedPtr< T > &  other,
std::true_type   
)

Definition at line 100 of file VolumeBuilderBase.hpp.

102 : builder_configs_(other->builder_configs_,SET_LINK),
103 boolean_configs_(other->boolean_configs_,SET_LINK),
104 lv_configs_(other->lv_configs_, SET_LINK),
105 placement_configs_(other->placement_configs_,SET_LINK),
106 solid_ptr_(other->solid_ptr_,SET_LINK),
107 logicvol_ptr_(other->logicvol_ptr_,SET_LINK),
108 placement_(other->placement_,SET_LINK) {
109 builder_configs_->istructure_ptr = IStructurePtr(other);
110 }
#define SET_LINK
Definition Linkable.hh:63
SharedPtr< IStructureBuilder > IStructurePtr
True polymorphic class base view for all structures Mostly for internal use.

Member Function Documentation

◆ AddTo()

template<typename U >
VolumeBuilderBase< U >::DerivedPtr AddTo ( BuilderViewList list) const

Definition at line 1005 of file VolumeBuilderBase.hpp.

1005 {
1006 list.emplace_back(this->ToVolumeBuilder());
1007 auto retval = shared_mutable_this(this);
1008 return retval;
1009 }
std::shared_ptr< T > shared_mutable_this(const std::enable_shared_from_this< T > *obj)

◆ clone_impl()

template<typename U >
SharedPtr< IStructureBuilder > clone_impl ( ) const
overrideprotectedvirtual

Implements IStructureBuilder.

Reimplemented in VolumeBuilderCore.

Definition at line 997 of file VolumeBuilderBase.hpp.

997 {
998 const U &derived_ref = static_cast<const U &>(*this); // downcast
999 auto retval = new U(derived_ref); // copy
1000 auto shared_ptr = i_shared_ptr<U>(retval);
1001 return shared_mutable_this(retval); // wrap and return.
1002 }
A wrapper for std::shared_ptr that allows and facilitates many implicit(i) type conversions.

◆ GetBuilderName()

template<typename U >
G4String GetBuilderName ( ) const

Definition at line 973 of file VolumeBuilderBase.hpp.

973 {
974 return this->builder_configs_->name;
975 }

◆ MakeLogicalVolume()

template<typename U >
VolumeBuilderBase< U >::DerivedPtr MakeLogicalVolume ( G4Material *  material = nullptr,
G4String  name = std::string() 
)

Basically never needed now.


Builds the logical volume and returns the builder. GetLogicalVolume(), or passing the builder to any consumer will trigger the build anyway.

Parameters
materialPointer to material to use.
nameOptional explicit name for logical volume. Normally already derived from builder/solid name.
Returns
This builder.

Definition at line 129 of file VolumeBuilderBase.hpp.

129 {
130 ValidateForVolumeBuild(STRINGIFY(BASE) "MakeLogicalVolume");
131 //if no volume was previously built, should be safe to reset material.
132 if (material) {
133 lv_configs_->material = material;
134 }
135 if (name.empty()) {
136 name = this->GetLogicVolName() + "_L";
137 }
138 // Implicit construction through
139 // ctor args..
140 logicvol_ptr_.ConstructAndLink(final_solid_ptr_,
141 lv_configs_->material,
142 name);
143
144 ApplyAttributes_();
145 return this->shared_from_this();
146 }
#define STRINGIFY(x)
#define BASE
void ConstructAndLink(Args &&... args)
Link and own a T and link to it.
Definition Linkable.hh:279

◆ MakePlacement()

template<typename U >
VolumeBuilderBase< U >::DerivedPtr MakePlacement ( )

Make a placed physical volume.

Retrieve it or make and retrieve with GetPlacement() Fails (throws) if already built. Make a copy first.

Returns
The builder

Definition at line 463 of file VolumeBuilderBase.hpp.

463 {
464 // the copy part comes from validate below, if we're already built:
465 ValidateForPVBuild(STRINGIFY(BASE) "MakePlacement");
466 // 1. Get the logical volume for *this* builder
467 auto *currentLogical = this->GetLogicalVolume();
468 if (!(explicit_physical_copy_name_set_
469 || explicit_copyno_set_
470 || placement_configs_->auto_copy_name
471 || placement_configs_->auto_copyno)
472 ) {
473 G4cout << "Warning: Placement Builder for " + builder_configs_->name +
474 " copied without setting name or copy number and auto naming and numbering have been disabled \n"
475 "Copy number auto-incrementing will be re-enabled \n"
476 " Use SetAutoCopyNo(true), SetAuCopyName(true), OverridePlacementName(\"name\"), SetCopyNo(num) \n"
477 " to pass a name or number to MakePlacement()" << G4endl;
478 placement_configs_->copy_no = PlacementNameRegistry::GetNameCount(
479 GetPlacementBaseName());
480 }
481
482 // 2. Resolve the Mother Logical Volume
483 G4LogicalVolume *effectiveMotherLogical = nullptr;
484
485 G4String base_name = this->GetPlacementBaseName();
486 G4String final_name;
487 if (placement_configs_->auto_copy_name) {
488 // auto naming incrementing
489 final_name = base_name + "_P" +
490 std::to_string(
491 PlacementNameRegistry::GetNameCount(GetPlacementBaseName()));
492 } else {
493 final_name = GetPlacementBaseName() + "_P";
494 }
495 // unless an override was set, then take it verbatim, user's blame :):
496 if (!placement_name_override_.empty()) {
497 final_name = placement_name_override_;
498 }
499
500 if (placement_configs_->mother == nullptr || !placement_configs_->mother->
502 // will lazay trigger mother build if needed
503 G4cout << "WARNING in MakePlacement of " + final_name +
504 ". No mother volume was set or constructable.\n"
505 "Defaulting to world volume" << G4endl;
506 }
507
508 if (placement_configs_->mother) {
509 effectiveMotherLogical = placement_configs_->mother->GetLogicalVolume();
510 } else {
511 effectiveMotherLogical = nullptr; // world placement
512 }
513
514 auto transform = G4Transform3D(placement_configs_->total_rotation,
515 placement_configs_->total_translation);
516 // 4. Create the G4PVPlacement
517 // placement is <G4VPhysicalVolume> and can construct itself:
518 placement_.LinkToRaw(new G4PVPlacement(
519 transform, // G4Transform3D transform
520 currentLogical, // pCurrentLogical
521 final_name, // pName
522 effectiveMotherLogical, // pMotherLogical
523 false, // many option, only false allowed (unless you remember Geant3 :))
524 placement_configs_->copy_no, // pCopyNo
525 placement_configs_->surface_check // pSurfChk
526 ));
527 if (!placement_) {
528 throw std::runtime_error(
529 "MakeLogicalPlacement failed "
530 "for builder named: \"" + builder_configs_->name + "\"\n"
531 " pointer is null after calling G4PVPlacement().");
532 }
533 return this->shared_from_this();
534 }
void LinkToRaw(T *ptr)
Link to an existing T object via pointer (non-owning)
Definition Linkable.hh:244
G4LogicalVolume * GetLogicalVolume() final
Gets a built LogicalVolume, builds default if not built yet.

◆ MakeSolid()

template<typename U >
VolumeBuilderBase< U >::DerivedPtr MakeSolid ( )

Make the G4VSolid.

Returns the builder. Use GetBaseSolid() to retrieve or make and retrieve it.

Returns
The builder

Definition at line 938 of file VolumeBuilderBase.hpp.

938 {
939 G4VSolid *solid;
940 G4String final_name = GetBuilderName();
941 if (builder_configs_->reflect_base_solid_z) {
942 solid = SolidConstructor(final_name + "_proto_solid");
943 solid = new G4ReflectedSolid(final_name, solid, G4ReflectZ3D(0));
944 } else {
945 solid = SolidConstructor(final_name);
946 }
947 solid_ptr_.LinkToRaw(solid);
948 return this->shared_from_this();
949 }
G4VSolid * SolidConstructor(const G4String &name) override=0
polymorphic access to solid construction

◆ set_shared_from_this_enabled()

template<typename T >
void set_shared_from_this_enabled ( bool  enabled)
inlineinherited

Definition at line 60 of file disableable_shared_from_this.hh.

60 {
61 is_enabled_ = enabled;
62 }

◆ SetName()

template<typename U >
VolumeBuilderBase< U >::DerivedPtr SetName ( const G4String &  name)

SetName Sets name used for solid and derived product names.

Parameters
nameThe name
Returns
The builder

Definition at line 403 of file VolumeBuilderBase.hpp.

403 {
404 this->builder_configs_->name = name;
405 return this->shared_from_this();
406 }

◆ shared_from_this() [1/2]

template<typename T >
std::shared_ptr< T > shared_from_this ( )
inlineinherited

Definition at line 44 of file disableable_shared_from_this.hh.

44 {
45 if (is_enabled_) {
46 return std::enable_shared_from_this<T>::shared_from_this();
47 } else {
48 return nullptr;
49 }
50 }

◆ shared_from_this() [2/2]

template<typename T >
std::shared_ptr< const T > shared_from_this ( ) const
inlineinherited

Definition at line 52 of file disableable_shared_from_this.hh.

52 {
53 if (is_enabled_) {
54 return std::enable_shared_from_this<T>::shared_from_this();
55 } else {
56 return nullptr;
57 }
58 }

◆ SolidConstructor()

template<typename U >
G4VSolid * SolidConstructor ( const G4String &  name)
overrideprotectedpure virtual

polymorphic access to solid construction

Implements IStructureBuilder.

Implemented in BoxBuilderCore, RZBuilderCore, and VolumeBuilderCore.

◆ StackPhysOffset()

template<typename U >
VolumeBuilderBase< U >::DerivedPtr StackPhysOffset ( const Unit3Vec offset = {CLHEP::mm, 0, 0, 0})

Like SetPhysOffset but stacks with previous transformations in order applied.

Unlike with G4Transform multiplication, stacking is done in a way that allows mutiple objects to move together. The first transformations can set individual objects, and a uniform transformation can be applied to them all to create a rigid motion of the collection.

Parameters
offset
Returns
The builder

Definition at line 570 of file VolumeBuilderBase.hpp.

570 {
571 const auto &offset = ProvisionUnits(stacked_offset);
572 placement_configs_->translation += offset; // add translation
573 PropagateTransform();
574 return this->shared_from_this();
575 }

◆ StackPhysRotation()

template<typename U >
VolumeBuilderBase< U >::DerivedPtr StackPhysRotation ( const G4RotationMatrix &  stacked_rot)

Like SetPhysRotation but stacks with previous transformations in order applied.

Unlike with G4Transform multiplication, stacking is done in a way that allows mutiple objects to move together. The first transformations can set individual objects, and a uniform transformation can be applied to them all to create a rigid motion of the collection.

Parameters
stacked_rot
Returns
The builder

Definition at line 546 of file VolumeBuilderBase.hpp.

546 {
547 // some aliases
548 const auto &old_rot = placement_configs_->rotation;
549 const auto &old_trans = placement_configs_->translation;
550 // sanitize input
551 auto new_rot = rot * old_rot; // order matters.
552 auto new_trans = rot * old_trans;
553 this->placement_configs_->rotation = new_rot;
554 this->placement_configs_->translation = new_trans;
555 PropagateTransform();
556 return this->shared_from_this();
557 }

◆ StackPhysTransform()

template<typename U >
VolumeBuilderBase< U >::DerivedPtr StackPhysTransform ( const UnitlessG4Transform3D new_transform)

Like SetPhysTransform but stacks with previous transformations in order applied.

Parameters
new_transform
Returns
The builder

Definition at line 586 of file VolumeBuilderBase.hpp.

586 {
587 //order matters when stacking. Not when "setting"
588 StackPhysRotation(new_transform.getRotation()); // gets a copy on stack
589 StackPhysOffset({GetEffectiveDefaultUnit(), new_transform.getTranslation()});
590 PropagateTransform();
591 return this->shared_from_this();
592 }
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.
G4double GetEffectiveDefaultUnit() const
Get the builder default unit or global if not set.

Friends And Related Symbol Documentation

◆ AssemblyCore

template<typename U >
friend class AssemblyCore
friend

Definition at line 72 of file VolumeBuilderBase.hh.

◆ BoxBuilderCore

template<typename U >
friend class BoxBuilderCore
friend

Definition at line 79 of file VolumeBuilderBase.hh.

◆ FromG4VSolidCore

template<typename U >
friend class FromG4VSolidCore
friend

Definition at line 80 of file VolumeBuilderBase.hh.

◆ RZBuilderCore

template<typename U >
friend class RZBuilderCore
friend

Definition at line 78 of file VolumeBuilderBase.hh.

◆ StructureBuilderBase

template<typename U >
template<typename >
friend class StructureBuilderBase
friend

Definition at line 74 of file VolumeBuilderBase.hh.

◆ StructureBuilderCore

template<typename U >
friend class StructureBuilderCore
friend

A type-erased (data shared view) view of a builder or assembly, ie a "structure.".

Can hold other structures, creating placement hierarchies. Behaves like a buidler, but only for logical volume properties and placement related commands. You can set VisAtt on a structure, and can set offsets and rotations of the whole structure for placement.

While this base view only supports limited methods, it is fully polymorphic and builder objects internally can trigger their full build chain.

This derived clas really exists for parallelism with VolumeBuilderCore.

See also
VolumeBuilder for inherited methods.

Definition at line 75 of file VolumeBuilderBase.hh.

◆ VB::CreateAssembly

template<typename U >
Assembly VB::CreateAssembly ( G4String  names)
friend

◆ VB::CreateFromG4VSolid

template<typename U >
FromG4VSolid VB::CreateFromG4VSolid ( G4VSolid *  solid)
friend

◆ VolumeBuilderBase

template<typename U >
template<typename >
friend class VolumeBuilderBase
friend

Definition at line 70 of file VolumeBuilderBase.hh.

◆ VolumeBuilderCore

template<typename U >
friend class VolumeBuilderCore
friend

A polymorphic, type-erased "view" of any specialized builder.

Use it to assign a specialized builder to a generic builder. But you don't use it directly. It has no direct public ctors or factories, but BuilderView(your_other_builder_object) constructs it as i_shared_ptr<VolumeBuilderCore> ( pointer-wrapped builder). Or pass your builder to something expecting a BuilderView, like ex: a std::vector<BuilderViewr>, aka a BuilderViewList.

See also
VolumeBuilder for inherited methods.

Definition at line 71 of file VolumeBuilderBase.hh.

Member Data Documentation

◆ is_enabled_

template<typename T >
std::atomic<bool> is_enabled_ {true}
protectedinherited

Definition at line 37 of file disableable_shared_from_this.hh.

37{true};

The documentation for this class was generated from the following files: