DLG4::VolumeBuilders
A fluent interface for Geant4 geometry definition.
Loading...
Searching...
No Matches
StructureBuilderBase.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 "DLG4Units.hh"
9#include <G4Transform3D.hh>
10
11/*
12
13 * VolumeMaker.hh
14 *
15 * Created on: Jun 19, 2024
16 * Author: D. S. Leonard
17 * Implicit or explicit misrepresentation authorship is not protected under any license.
18 * See RZBuilder.hh for main documentation.
19 *
20 *s
21
22 */
23//#define SHARED_WRAPPER DLG4::i_shared_ptr
24#include <vector>
25#include "i_shared_ptr.hh"
27
28
29// #define FIXME(msg) static_assert(false, "FIXME: " msg)
30
31//#include "DetectorConstruction_includes.hh"
32#include "G4Colour.hh"
33#include "G4VPhysicalVolume.hh"
34#include <G4ThreeVector.hh>
35#include <mutex>
36#include <memory>
37#include <stdexcept>
38//#include "StructureBuilder.hh"
39#include <BuilderFactories.hh>
40
41
61 template <typename U>
64
65 // we need access to the other templates for copy
66 template <typename>
67 friend class VolumeBuilderBase;
69 friend class AssemblyCore;
70 friend Assembly VB::CreateAssembly(G4String name);
71 template <typename>
73 //allow i_shared_ptr access to our converting ctors
74 //This way they get instantly wrapped!
75
76 friend class i_shared_ptr<StructureBuilderBase<U>>;
77
78 public:
79 //Methods specific to structures/assemblies....
80
81
106 std::optional<int> copy_no = std::nullopt
107 , const G4String &name_override = "");
108
121 DerivedPtr ForkForLogicalVolume(const G4String &new_name);
122
123
133
134
140 DerivedPtr SetName(const G4String &name);
141
151
157 DerivedPtr SetColor(double r = 0.5, double g = 0.5, double b = 0.5, double alpha = 1.0);
158
165 DerivedPtr SetColor(const G4Colour &color);
166
173 DerivedPtr SetAlpha(G4double alpha);
174
180 DerivedPtr ForceSolid(bool x = true); // default if uncalled is false.
181
188 DerivedPtr SetVisibility(bool is_visible = true);
189
190
191 // ... inside the StructureBuilder class declaration ...
192
207 DerivedPtr SetPhysRotation(const G4RotationMatrix &rot);
208
223 DerivedPtr StackPhysRotation(const G4RotationMatrix &stacked_rot);
224
242 DerivedPtr SetPhysOffset(const DLG4::VolumeBuilders::ThreeVecDimensioner &offset = {0, 0, 0, VB::Length::mm});
257 DerivedPtr StackPhysOffset(const DLG4::VolumeBuilders::ThreeVecDimensioner &offset = {0, 0, 0, VB::Length::mm});
258
283
294 [[nodiscard]] G4Transform3D GetPhysTransform() const {
295 return {placement_configs_->rotation, placement_configs_->translation};
296 }
297
298
305 DerivedPtr OverridePlacementName(const G4String &pName);
306
307
314 DerivedPtr SetCopyNo(G4int pCopyNo);
315
322 DerivedPtr SetSurfaceCheck(G4bool pSurfChk);
323
324
337
350
351
363
386
387
400
406
407
413
414 G4String GetBuilderName() const;
415
416 protected:
418 G4VSolid *SolidConstructor(const G4String &name) override = 0;
419
420 private:
421 //Methods with basically protected intent
422 // But for now, keep them private and require explicit friend access
423 // ctors only useable through explicitly granted (friendship) inheritance...
424
425 friend class RZBuilderCore;
426 friend class StructureBuilderCore;
427 friend class FromG4VSolidCore;
428
429
431
432 DerivedPtr Clone() const {
433 i_shared_ptr<IStructureBuilder> base_cloned_ptr = this->clone_impl();
434 // really is a static cast in principle, but just to catch programming bugs:
435 auto retval = i_dynamic_pointer_cast<U>(base_cloned_ptr);
436 return retval;
437 }
438
439 SharedPtr<IStructureBuilder> clone_impl() const override;
440
441 StructureBuilder ToStructureView() const override;
442
443 VolumeBuilder ToVolumeBuilder() const override;
444
445
446 template <typename T, typename std::enable_if_t<std::is_base_of_v<IStructureBuilder, T>,
447 int> = 0>
449
450
451 StructureBuilderBase &operator=(const StructureBuilderBase &other) = delete;
453
454 StructureBuilderBase(StructureBuilderBase &&) noexcept = default;
455
456 G4VSolid *GetSolidPtr() const {
457 return this->solid_ptr_.get_mutable();
458 };
459
460 //Configs (re-linked by copy-ctors)
461 ONCE_MUTABLE Linkable<BuilderConfigs> builder_configs_{BuilderConfigs{}};
462 ONCE_MUTABLE Linkable<BooleanConfigs> boolean_configs_{BooleanConfigs{}};
463 ONCE_MUTABLE Linkable<VolumeConfigs> lv_configs_{VolumeConfigs{}};
464 ONCE_MUTABLE Linkable<PlacementConfigs> placement_configs_{PlacementConfigs{}};
465
466 //Products (sometimes copied by copy methods)
467 ONCE_MUTABLE Linkable<G4VSolid> solid_ptr_{}; // child can and should create the solid
468 ONCE_MUTABLE Linkable<G4VSolid> final_solid_ptr_{};
471 bool was_placed_ = false; // set when assembly is placed.
472
473 // Interface pointer for type erased class access (Copied in linking ctor only)
474
475
476 //Unsaved data, left out of configs to be reset on all copy operations.
477 bool explicit_copyno_set_ = false;
478 bool explicit_physical_copy_name_set_ = false;
479 G4String placement_name_override_ = ""; // Will be derived from boolean_name_ by default
480
481
482 //Some hard baked settings
483 // let's pretend like we'd ever care about resource management:
484 static constexpr bool has_ownership_{false}; // for future resource control
485 static constexpr bool enable_full_lazy_builds{true};
486 // Allows full chain lazy building. Off for now.
487
488 //Methods with truly private intent.
489
490 [[nodiscard]] G4String GetLogicVolName() const; // should this be pulic?
491
492 void ValidateForPVBuild(std::string const &site = std::string());
493 void ValidatePlacementNotBuilt(const std::string &operation) const;
494
495
496 // Nothing below here but implementation for some private static helper
497 // methods used by the instance methods
498
499 static void NoNameCheck(const std::string &name, const std::string &site) {
500 if (name.empty()) {
501 throw std::runtime_error("Error in " + site + " ,"
502 "for builder named: " + name + "\n"
503 " Must provide a new name");
504 }
505 }
506 };
507} // namespace StructureBuilder
508
509
510//#include "StructureBuilder.hpp"
511
512//The implementation:
513#endif
Typesafe units, for VolumeBuilders, Geant, ETC !! No accidental mixing values and units.
#define SET_LINK_TYPE
Definition Linkable.hh:63
#define ONCE_MUTABLE
A 3D vector that manages unit policy for parameter passing If constructed with a unit,...
A type-erased (data shared view) view of a builder or assembly, ie a "structure.".
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.
A type-erased (data shared view) view of a builder or assembly, ie a "structure.".
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
DerivedPtr StackPhysOffset(const DLG4::VolumeBuilders::ThreeVecDimensioner &offset={0, 0, 0, VB::Length::mm})
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.
VB::Length GetEffectiveDefaultUnit() const
Get the structure default unit or global if not set.
~StructureBuilderBase() override
Destructor Normally does not delete volume objects.
DerivedPtr SetName(const G4String &name)
SetName Sets name used for solid and derived product names.
DerivedPtr MakePlacement()
Make a placed physical volume.
A type-erased (data shared view) view of a builder or assembly, ie a "structure.".
VolumeBuilder: Common functionality for volume builder classes.
DerivedPtr CopyPlacementConfigsFrom(const VolumeBuilder &other)
This may help if reusing placement configs for a different structure.
Assembly CreateAssembly(G4String name)
Assembly of strucures, ie builders and/or other assemblies.
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(double r=0.5, double g=0.5, double b=0.5, double alpha=1.0)
DerivedPtr SetMaterial(const VBHelpers::G4MaterialPtrOrString &material)
DerivedPtr SetMother(const VolumeBuilder &mother)
Set the mother volume with a builder, or G4VPhysicalVolume or G4VLogicalVolume through implicit conve...
G4Transform3D GetPhysTransform() const
A helper to get the Physical Volume transform with units applied This is built from rotation and tran...
DerivedPtr SetAutoCopyNo(bool set)
Enable (disable for false) auto Physical Volume numbering, on by default.
DerivedPtr SetAutoPlacementNaming(bool set)
Enable auto Physical Volume naming.
DerivedPtr SetCopyNo(G4int pCopyNo)
Set the copy number for the placement.
DerivedPtr SetPhysOffset(const DLG4::VolumeBuilders::ThreeVecDimensioner &offset={0, 0, 0, VB::Length::mm})
Set the translation vector for 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(VB::Length unit)
Set the per-Structure default unit for all later non-factory offsets.