DLG4::VolumeBuilders
A fluent interface for Geant4 geometry definition.
Loading...
Searching...
No Matches
StructureBuilder.hh
Go to the documentation of this file.
1#pragma once
2#ifndef STRUCTUREBUILDER_HH
3#define STRUCTUREBUILDER_HH
4#include <optional>
5#include "Linkable.hh"
6#include "ISolidBuilder.hh"
8#include <G4Transform3D.hh>
9
10/*
11
12 * VolumeMaker.hh
13 *
14 * Created on: Jun 19, 2024
15 * Author: D. S. Leonard
16 * Implicit or explicit misrepresentation authorship is not protected under any license.
17 * See RZBuilder.hh for main documentation.
18 *
19 *s
20
21 */
22//#define SHARED_WRAPPER DLG4::i_shared_ptr
23#include <vector>
24#include "i_shared_ptr.hh"
25#include "VolumeBuilderTypes.hh"
26
27
28// #define FIXME(msg) static_assert(false, "FIXME: " msg)
29
30//#include "DetectorConstruction_includes.hh"
31#include "G4Colour.hh"
32#include "G4VPhysicalVolume.hh"
33#include <G4ThreeVector.hh>
34#include <mutex>
35#include <memory>
36#include <stdexcept>
37//#include "StructureBuilder.hh"
38
39namespace DLG4::VolumeBuilders {
58 template <typename U>
61
62 // we need access to the other templates for copy
63 template <typename>
64 friend class VolumeBuilder;
66 friend class Assembly;
67 friend AssemblyPtr CreateAssembly(G4String name);
68 template <typename>
69 friend class StructureBuilder;
70 //allow i_shared_ptr access to our converting ctors
71 //This way they get instantly wrapped!
72
73 friend class i_shared_ptr<StructureBuilder<U>>;
74
75 public:
76 //Methods specific to structures/assemblies....
77
78
103 std::optional<int> copy_no = std::nullopt
104 , const G4String &name_override = "");
105
118 DerivedPtr ForkForLogicalVolume(const G4String &new_name);
119
120
130
131
137 DerivedPtr SetName(const G4String &name);
138
147 DerivedPtr SetMaterial(G4Material *material);
148
154 DerivedPtr SetColor(double r = 0.5, double g = 0.5, double b = 0.5, double alpha = 1.0);
155
162 DerivedPtr SetColor(const G4Colour &color);
163
170 DerivedPtr SetAlpha(G4double alpha);
171
177 DerivedPtr ForceSolid(bool x = true); // default if uncalled is false.
178
185 DerivedPtr SetVisibility(bool is_visible = true);
186
187
188 // ... inside the StructureBuilder class declaration ...
189
204 DerivedPtr SetPhysRotation(const G4RotationMatrix &rot);
205
220 DerivedPtr StackPhysRotation(const G4RotationMatrix &stacked_rot);
221
238 DerivedPtr SetPhysOffset(const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0});
253 DerivedPtr StackPhysOffset(const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0});
254
279
290 [[nodiscard]] G4Transform3D GetPhysTransform() const {
291 return {placement_configs_->rotation, placement_configs_->translation};
292 }
293
294
301 DerivedPtr OverridePlacementName(const G4String &pName);
302
303
310 DerivedPtr SetCopyNo(G4int pCopyNo);
311
318 DerivedPtr SetSurfaceCheck(G4bool pSurfChk);
319
320
333
346
347
359
382
383
396
401 G4double GetEffectiveDefaultUnit() const;
402
403
409
410 G4String GetBuilderName() const;
411
412 protected:
414 G4VSolid *SolidConstructor(const G4String &name) override = 0;
415
416 private:
417 //Methods with basically protected intent
418 // But for now, keep them private and require explicit friend access
419 // ctors only useable through explicitly granted (friendship) inheritance...
420
421 friend class RZBuilder;
422 friend class StructureBuilderReference;
423 friend class FromG4VSolid;
424
425
427
428 DerivedPtr Clone() const {
429 i_shared_ptr<IStructureBuilder> base_cloned_ptr = this->clone_impl();
430 // really is a static cast in principle, but just to catch programming bugs:
431 auto retval = i_dynamic_pointer_cast<U>(base_cloned_ptr);
432 return retval;
433 }
434
435 SharedPtr<IStructureBuilder> clone_impl() const override;
436
437 StructureView ToStructureView() const override;
438
439 BuilderView ToBuilderView() const override;
440
441
442 template <typename T, typename std::enable_if_t<std::is_base_of_v<IStructureBuilder, T>,
443 int> = 0>
445
446
447 StructureBuilder &operator=(const StructureBuilder &other) = delete;
449
450 StructureBuilder(StructureBuilder &&) noexcept = default;
451
452 G4VSolid *GetSolidPtr() const {
453 return this->solid_ptr_.get_mutable();
454 };
455
456 //Configs (re-linked by copy-ctors)
457 ONCE_MUTABLE Linkable<BuilderConfigs> builder_configs_{BuilderConfigs{}};
458 ONCE_MUTABLE Linkable<BooleanConfigs> boolean_configs_{BooleanConfigs{}};
459 ONCE_MUTABLE Linkable<VolumeConfigs> lv_configs_{VolumeConfigs{}};
460 ONCE_MUTABLE Linkable<PlacementConfigs> placement_configs_{PlacementConfigs{}};
461
462 //Products (sometimes copied by copy methods)
463 ONCE_MUTABLE Linkable<G4VSolid> solid_ptr_{}; // child can and should create the solid
464 ONCE_MUTABLE Linkable<G4VSolid> final_solid_ptr_{};
465 ONCE_MUTABLE Linkable<G4LogicalVolume> logicvol_ptr_{};
466 ONCE_MUTABLE Linkable<G4VPhysicalVolume> placement_{};
467 bool was_placed_ = false; // set when assembly is placed.
468
469 // Interface pointer for type erased class access (Copied in linking ctor only)
470
471
472 //Unsaved data, left out of configs to be reset on all copy operations.
473 bool explicit_copyno_set_ = false;
474 bool explicit_physical_copy_name_set_ = false;
475 G4String placement_name_override_ = ""; // Will be derived from boolean_name_ by default
476
477
478 //Some hard baked settings
479 // let's pretend like we'd ever care about resource management:
480 static constexpr bool has_ownership_{false}; // for future resource control
481 static constexpr bool enable_full_lazy_builds{true};
482 // Allows full chain lazy building. Off for now.
483
484 //Methods with truly private intent.
485
486 [[nodiscard]] G4String GetLogicVolName() const; // should this be pulic?
487
488 void ValidateForPVBuild(std::string const &site = std::string());
489 void ValidatePlacementNotBuilt(const std::string &operation) const;
490
491
492 // Nothing below here but implementation for some private static helper
493 // methods used by the instance methods
494
495 static void NoNameCheck(const std::string &name, const std::string &site) {
496 if (name.empty()) {
497 throw std::runtime_error("Error in " + site + " ,"
498 "for builder named: " + name + "\n"
499 " Must provide a new name");
500 }
501 }
502 };
503} // namespace StructureBuilder
504
505
506//#include "StructureBuilder.hpp"
507
508//The implementation:
509#endif
#define SET_LINK_TYPE
Definition Linkable.hh:62
#define ONCE_MUTABLE
T * get_mutable() const
Definition Linkable.hh:287
A type-erased (data shared view) view of a builder or assembly, ie a "structure.".
Definition Assembly.hh:46
A solid buider class that just wrap an existing G4VSolid.
a non-fluent polymorphic class to access the derived solid-builder from a base reference: This gets u...
Builder class for RZ mult-plane defined solids.
Definition RZBuilder.hh:132
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.".
G4double GetEffectiveDefaultUnit() const
Get the structure default unit or global if not set.
DerivedPtr StackPhysTransform(const UnitlessG4Transform3D &stacked_transform)
Like SetPhysTransform but stacks with previous transformations in order applied.
G4VSolid * SolidConstructor(const G4String &name) override=0
polymorphic access to solid construction
~StructureBuilder() override
Destructor Normally does not delete volume objects.
friend AssemblyPtr CreateAssembly(G4String name)
Assembly of strucures, ie builders and/or other assemblies.
Definition Assembly.cc:25
DerivedPtr StackPhysRotation(const G4RotationMatrix &stacked_rot)
Like SetPhysRotation but stacks with previous transformations in order applied.
DerivedPtr StackPhysOffset(const Unit3Vec &offset={CLHEP::mm, 0, 0, 0})
Like SetPhysOffset but stacks with previous transformations in order applied.
DerivedPtr SetName(const G4String &name)
SetName Sets name used for solid and derived product names.
DerivedPtr MakePlacement()
Make a placed physical volume.
A 3D vector that carries its own unit information.
VolumeBuilder: Common functionality for volume builder classes.
A wrapper for std::shared_ptr that allows and facilitates many implicit(i) type conversions.
DerivedPtr CopyPlacementConfigsFrom(const BuilderView &other)
This may help if reusing placement configs for a different structure.
DerivedPtr ForkForPlacement(std::optional< int > copy_no=std::nullopt, const G4String &name_override="")
Calls MakeLogicalVolume() and creates a new builder instance with a copy of all placement-related con...
DerivedPtr ForkForLogicalVolume(const G4String &new_name)
Makes final solid (including booleans) if unbuilt and copies builder with products cleared for furthe...
DerivedPtr SetColor(const G4Colour &color)
DerivedPtr SetColor(double r=0.5, double g=0.5, double b=0.5, double alpha=1.0)
DerivedPtr SetVisibility(bool is_visible=true)
DerivedPtr SetMaterial(G4Material *material)
DerivedPtr SetAlpha(G4double alpha)
DerivedPtr ForceSolid(bool x=true)
G4Transform3D GetPhysTransform() const
A helper to get the Physical Volume transform with units applied This is built from rotation and tran...
DerivedPtr SetMother(const BuilderView &mother)
Set the mother volume with a builder, or G4VPhysicalVolume or G4VLogicalVolume through implicit conve...
DerivedPtr SetAutoCopyNo(bool set)
Enable (disable for false) auto Physical Volume numbering, on by default.
DerivedPtr SetPhysOffset(const Unit3Vec &offset={CLHEP::mm, 0, 0, 0})
Set the translation vector for placement.
DerivedPtr SetAutoPlacementNaming(bool set)
Enable auto Physical Volume naming.
DerivedPtr SetCopyNo(G4int pCopyNo)
Set the copy number for the placement.
DerivedPtr SetPhysRotation(const G4RotationMatrix &rot)
Set the rotation matrix 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 OverridePlacementName(const G4String &pName)
Set the placement name for the volume.
DerivedPtr PlaceAndFork()
An alias for MakePlacement and ClonePlacement Used for one or many unaltered placments in sequence,...
DerivedPtr SetSurfaceCheck(G4bool pSurfChk)
Set the surface check flag for the placement.
DerivedPtr SetDefaultUnit(G4double unit)
Set the per-Structure default unit for all later non-factory offsets.
G4Transform3D UnitlessG4Transform3D