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
VolumeBuilder< U > Class Template Referenceabstract

#include <VolumeBuilder.hh>

Collaboration diagram for VolumeBuilder< 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 BuilderView &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
 
DerivedPtr AddSubtraction (const BuilderView &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
 
DerivedPtr AddIntersection (const BuilderView &other, const Unit3Vec &offset={CLHEP::mm, 0, 0, 0}, G4RotationMatrix *rotation=nullptr)
 
DerivedPtr AddBoolean (const BuilderView &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 BuilderView &mother)
 
DerivedPtr SetAutoPlacementNaming (bool set)
 
DerivedPtr SetAutoCopyNo (bool set)
 
DerivedPtr ReflectZFinalSolid ()
 
DerivedPtr ReflectZBaseSolid ()
 
virtual 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 BuilderView &other)
 
DerivedPtr CopyVolumeConfigsFrom (const BuilderView &other)
 
DerivedPtr SetDefaultUnit (G4double unit)
 
G4double GetEffectiveDefaultUnit () const
 
DerivedPtr AddTo (BuilderViewList &list) const
 
DerivedPtr AddTo (StructureViewList &list) const
 
DerivedPtr AddTo (AssemblyPtr &assembly) const
 
 ~VolumeBuilder () override
 
BuilderView ToBuilderView () const override
 
StructureView ToStructureView () const override
 
G4String GetBuilderName () const
 
template<typename U >
 VolumeBuilder (const VolumeBuilder< U > &other)
 
template<typename T , std::enable_if_t< std::is_base_of_v< IStructureBuilder, T >, int > >
 VolumeBuilder (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 VolumeBuilder
 
class VolumeBuilderReference
 
class Assembly
 
template<typename >
class StructureBuilder
 
class StructureBuilderReference
 
class RZBuilder
 
class BoxBuilder
 
class FromG4VSolid
 
FromG4VSolidPtr CreateFromG4VSolid (G4VSolid *solid)
 
AssemblyPtr CreateAssembly (G4String names)
 

Detailed Description

template<typename U>
class DLG4::VolumeBuilders::VolumeBuilder< 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 58 of file VolumeBuilder.hh.

Constructor & Destructor Documentation

◆ ~VolumeBuilder()

template<typename U >
~VolumeBuilder ( )
override

Destructor Normally does not delete volume objects.

Geant takes care of that.

Definition at line 114 of file VolumeBuilder.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 }
void make_persistent()
Definition Linkable.hh:295

◆ VolumeBuilder() [1/2]

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

Definition at line 83 of file VolumeBuilder.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 }

◆ VolumeBuilder() [2/2]

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

Definition at line 100 of file VolumeBuilder.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 >
VolumeBuilder< U >::DerivedPtr AddTo ( BuilderViewList list) const

Definition at line 1005 of file VolumeBuilder.hpp.

1005 {
1006 list.emplace_back(this->ToBuilderView());
1007 auto retval = shared_mutable_this(this);
1008 return retval;
1009 }
BuilderView ToBuilderView() const override
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 VolumeBuilderReference.

Definition at line 997 of file VolumeBuilder.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 }

◆ GetBuilderName()

template<typename U >
G4String GetBuilderName ( ) const

Definition at line 973 of file VolumeBuilder.hpp.

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

◆ MakeLogicalVolume()

template<typename U >
VolumeBuilder< 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 VolumeBuilder.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:278

◆ MakePlacement()

template<typename U >
VolumeBuilder< 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 VolumeBuilder.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:243
G4LogicalVolume * GetLogicalVolume() final
Gets a built LogicalVolume, builds default if not built yet.

◆ MakeSolid()

template<typename U >
VolumeBuilder< 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 VolumeBuilder.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

◆ ReflectZBaseSolid()

template<typename U >
VolumeBuilder< U >::DerivedPtr ReflectZBaseSolid ( )

Configure base solid to be flipped BEFORE applying booleans.

See also
ReflectZFinalSolid() to flip entire solid with or without Booleans.
Returns
The same builder (allows chaining)

Definition at line 928 of file VolumeBuilder.hpp.

928 {
929 if (final_solid_ptr_) {
930 throw std::runtime_error("Error VolumeBuilder::ReflectZBaseSolid, \n"
931 "The base solid is already built. \n");
932 }
933 builder_configs_->reflect_base_solid_z = true;
934 return this->shared_from_this();
935 }

◆ ReflectZFinalSolid()

template<typename U >
VolumeBuilder< U >::DerivedPtr ReflectZFinalSolid ( )

Configure final solid to be flipped in z.

Definition at line 918 of file VolumeBuilder.hpp.

918 {
919 if (final_solid_ptr_) {
920 throw std::runtime_error("Error VolumeBuilder::ReflectZFinalSolid, \n"
921 "The final solid is already built. \n");
922 }
923 boolean_configs_->reflect_z = true;
924 return this->shared_from_this();
925 }

◆ 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 >
VolumeBuilder< 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 VolumeBuilder.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 BoxBuilder, RZBuilder, and VolumeBuilderReference.

◆ StackPhysOffset()

template<typename U >
VolumeBuilder< 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 VolumeBuilder.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 >
VolumeBuilder< 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 VolumeBuilder.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 >
VolumeBuilder< 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 VolumeBuilder.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.

◆ ToBuilderView()

template<typename U >
BuilderView ToBuilderView ( ) const
overridevirtual

Implements IStructureBuilder.

Reimplemented in VolumeBuilderReference.

Definition at line 978 of file VolumeBuilder.hpp.

978 {
979 // calls the BuilderView copy/convert ctor::
980 // presently the i_shared converter only works with l-value.
981 std::shared_ptr<U> builder_std_ptr =
982 std::const_pointer_cast<U>(this->shared_from_this());
983 auto x = DerivedPtr(builder_std_ptr);
984 return BuilderView(x);
985 }
SharedPtr< VolumeBuilderReference > BuilderView
Common interface for all volume builder types.

◆ ToStructureView()

template<typename U >
StructureView ToStructureView ( ) const
overridevirtual

Implements IStructureBuilder.

Definition at line 988 of file VolumeBuilder.hpp.

988 {
989 // calls the structure view copy/convert ctor:
990 std::shared_ptr<U> builder_std_ptr =
991 std::const_pointer_cast<U>(this->shared_from_this());
992 auto x = DerivedPtr(builder_std_ptr);
993 return StructureView(x);
994 }
SharedPtr< StructureBuilderReference > StructureView
Common interface for all structure types.

Friends And Related Symbol Documentation

◆ Assembly

template<typename U >
friend class Assembly
friend

Definition at line 70 of file VolumeBuilder.hh.

◆ BoxBuilder

template<typename U >
friend class BoxBuilder
friend

Definition at line 77 of file VolumeBuilder.hh.

◆ CreateAssembly

template<typename U >
AssemblyPtr CreateAssembly ( G4String  names)
friend

Assembly of strucures, ie builders and/or other assemblies.

Definition at line 25 of file Assembly.cc.

25 {
26 // Haha... assemblies actually are builders!!!
27 auto object = AssemblyPtr(new Assembly());
28 // ... with is_builder set to false.
29 object->placement_configs_->is_builder = false;
30 //We're storing a pointer to the view in the builder by writing to the builder through that same view!
31 BuilderView builder_view = object->ToBuilderView();
32 builder_view->StoreBuilderView(builder_view);
33 // Then store itself in its new builder (that links to its data).
34 //object->builder_configs_->builder_view->StoreIStructurePtr(IStructurePtr(object.get())); // this is the owning copy.
35 // ... and viewed through a reduced StructureView interface:
36 object->SetName(name);
37 auto return_obj = AssemblyPtr(object);
38 return return_obj;
39
40 // This let's us reuse builder methods!
41 }
SharedPtr< Assembly > AssemblyPtr

◆ CreateFromG4VSolid

template<typename U >
FromG4VSolidPtr CreateFromG4VSolid ( G4VSolid *  solid)
friend

Constructor to make a builder from an Existing Geant solid.

Parameters
solidG4VSolid pointer
Returns

Definition at line 25 of file FromG4VSolid.cc.

25 {
26 auto object = FromG4VSolidPtr();
27 object.get()->SetSolid(solid);
28 object.get()->SetName(solid->GetName());
29 return FromG4VSolidPtr(object);
30 }
SharedPtr< FromG4VSolid > FromG4VSolidPtr

◆ FromG4VSolid

template<typename U >
friend class FromG4VSolid
friend

Definition at line 78 of file VolumeBuilder.hh.

◆ RZBuilder

template<typename U >
friend class RZBuilder
friend

Definition at line 76 of file VolumeBuilder.hh.

◆ StructureBuilder

template<typename U >
template<typename >
friend class StructureBuilder
friend

Definition at line 72 of file VolumeBuilder.hh.

◆ StructureBuilderReference

template<typename U >
friend class StructureBuilderReference
friend

Definition at line 73 of file VolumeBuilder.hh.

◆ VolumeBuilder

template<typename U >
template<typename >
friend class VolumeBuilder
friend

Definition at line 68 of file VolumeBuilder.hh.

◆ VolumeBuilderReference

template<typename U >
friend class VolumeBuilderReference
friend

Definition at line 69 of file VolumeBuilder.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: