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"
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#include <BuilderFactories.hh>
39
40
60 template <typename U>
63
64 // we need access to the other templates for copy
65 template <typename>
66 friend class VolumeBuilder;
68 friend class Assembly;
69 friend AssemblyPtr VB::CreateAssembly(G4String name);
70 template <typename>
71 friend class StructureBuilder;
72 //allow i_shared_ptr access to our converting ctors
73 //This way they get instantly wrapped!
74
75 friend class i_shared_ptr<StructureBuilder<U>>;
76
77 public:
78 //Methods specific to structures/assemblies....
79
80
105 std::optional<int> copy_no = std::nullopt
106 , const G4String &name_override = "");
107
120 DerivedPtr ForkForLogicalVolume(const G4String &new_name);
121
122
132
133
139 DerivedPtr SetName(const G4String &name);
140
149 DerivedPtr SetMaterial(G4Material *material);
150
156 DerivedPtr SetColor(double r = 0.5, double g = 0.5, double b = 0.5, double alpha = 1.0);
157
164 DerivedPtr SetColor(const G4Colour &color);
165
172 DerivedPtr SetAlpha(G4double alpha);
173
179 DerivedPtr ForceSolid(bool x = true); // default if uncalled is false.
180
187 DerivedPtr SetVisibility(bool is_visible = true);
188
189
190 // ... inside the StructureBuilder class declaration ...
191
206 DerivedPtr SetPhysRotation(const G4RotationMatrix &rot);
207
222 DerivedPtr StackPhysRotation(const G4RotationMatrix &stacked_rot);
223
240 DerivedPtr SetPhysOffset(const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0});
255 DerivedPtr StackPhysOffset(const Unit3Vec &offset = {CLHEP::mm, 0, 0, 0});
256
281
292 [[nodiscard]] G4Transform3D GetPhysTransform() const {
293 return {placement_configs_->rotation, placement_configs_->translation};
294 }
295
296
303 DerivedPtr OverridePlacementName(const G4String &pName);
304
305
312 DerivedPtr SetCopyNo(G4int pCopyNo);
313
320 DerivedPtr SetSurfaceCheck(G4bool pSurfChk);
321
322
335
348
349
361
384
385
398
403 G4double GetEffectiveDefaultUnit() const;
404
405
411
412 G4String GetBuilderName() const;
413
414 protected:
416 G4VSolid *SolidConstructor(const G4String &name) override = 0;
417
418 private:
419 //Methods with basically protected intent
420 // But for now, keep them private and require explicit friend access
421 // ctors only useable through explicitly granted (friendship) inheritance...
422
423 friend class RZBuilder;
424 friend class StructureBuilderReference;
425 friend class FromG4VSolid;
426
427
429
430 DerivedPtr Clone() const {
431 i_shared_ptr<IStructureBuilder> base_cloned_ptr = this->clone_impl();
432 // really is a static cast in principle, but just to catch programming bugs:
433 auto retval = i_dynamic_pointer_cast<U>(base_cloned_ptr);
434 return retval;
435 }
436
437 SharedPtr<IStructureBuilder> clone_impl() const override;
438
439 StructureView ToStructureView() const override;
440
441 BuilderView ToBuilderView() const override;
442
443
444 template <typename T, typename std::enable_if_t<std::is_base_of_v<IStructureBuilder, T>,
445 int> = 0>
447
448
449 StructureBuilder &operator=(const StructureBuilder &other) = delete;
451
452 StructureBuilder(StructureBuilder &&) noexcept = default;
453
454 G4VSolid *GetSolidPtr() const {
455 return this->solid_ptr_.get_mutable();
456 };
457
458 //Configs (re-linked by copy-ctors)
459 ONCE_MUTABLE Linkable<BuilderConfigs> builder_configs_{BuilderConfigs{}};
460 ONCE_MUTABLE Linkable<BooleanConfigs> boolean_configs_{BooleanConfigs{}};
461 ONCE_MUTABLE Linkable<VolumeConfigs> lv_configs_{VolumeConfigs{}};
462 ONCE_MUTABLE Linkable<PlacementConfigs> placement_configs_{PlacementConfigs{}};
463
464 //Products (sometimes copied by copy methods)
465 ONCE_MUTABLE Linkable<G4VSolid> solid_ptr_{}; // child can and should create the solid
466 ONCE_MUTABLE Linkable<G4VSolid> final_solid_ptr_{};
469 bool was_placed_ = false; // set when assembly is placed.
470
471 // Interface pointer for type erased class access (Copied in linking ctor only)
472
473
474 //Unsaved data, left out of configs to be reset on all copy operations.
475 bool explicit_copyno_set_ = false;
476 bool explicit_physical_copy_name_set_ = false;
477 G4String placement_name_override_ = ""; // Will be derived from boolean_name_ by default
478
479
480 //Some hard baked settings
481 // let's pretend like we'd ever care about resource management:
482 static constexpr bool has_ownership_{false}; // for future resource control
483 static constexpr bool enable_full_lazy_builds{true};
484 // Allows full chain lazy building. Off for now.
485
486 //Methods with truly private intent.
487
488 [[nodiscard]] G4String GetLogicVolName() const; // should this be pulic?
489
490 void ValidateForPVBuild(std::string const &site = std::string());
491 void ValidatePlacementNotBuilt(const std::string &operation) const;
492
493
494 // Nothing below here but implementation for some private static helper
495 // methods used by the instance methods
496
497 static void NoNameCheck(const std::string &name, const std::string &site) {
498 if (name.empty()) {
499 throw std::runtime_error("Error in " + site + " ,"
500 "for builder named: " + name + "\n"
501 " Must provide a new name");
502 }
503 }
504 };
505} // namespace StructureBuilder
506
507
508//#include "StructureBuilder.hpp"
509
510//The implementation:
511#endif
#define SET_LINK_TYPE
Definition Linkable.hh:62
#define ONCE_MUTABLE
A type-erased (data shared view) view of a builder or assembly, ie a "structure.".
Definition Assembly.hh:38
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:131
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.
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.
DerivedPtr CopyPlacementConfigsFrom(const BuilderView &other)
This may help if reusing placement configs for a different structure.
AssemblyPtr CreateAssembly(G4String name)
Assembly of strucures, ie builders and/or other assemblies.
Definition Assembly.cc:18
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)
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.