Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:56

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file orange/orangeinp/detail/VolumeBuilder.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/io/Label.hh"
0011 #include "orange/transform/VariantTransform.hh"
0012 
0013 #include "../CsgTypes.hh"
0014 
0015 namespace celeritas
0016 {
0017 namespace orangeinp
0018 {
0019 namespace detail
0020 {
0021 //---------------------------------------------------------------------------//
0022 class CsgUnitBuilder;
0023 struct BoundingZone;
0024 class PopVBTransformOnDestruct;
0025 
0026 //---------------------------------------------------------------------------//
0027 /*!
0028  * Construct volumes out of objects.
0029  *
0030  * This class maintains a stack of transforms used by nested objects. It
0031  * ultimately returns a node ID corresponding to the CSG node (and bounding box
0032  * etc.) of the constructed object.
0033  *
0034  * To add a transform, store the result of \c make_scoped_transform within a
0035  * scoping block (generally the calling function). The resulting RAII class
0036  * prevents an imbalance from manual calls to "push" and "pop". For an example
0037  * usage, see \c celeritas::orangeinp::Transformed::build .
0038  */
0039 class VolumeBuilder
0040 {
0041   public:
0042     //!@{
0043     //! \name Type aliases
0044     using Metadata = Label;
0045     using Tol = Tolerance<>;
0046     //!@}
0047 
0048   public:
0049     // Construct with unit builder (and volume name??)
0050     explicit VolumeBuilder(CsgUnitBuilder* ub);
0051 
0052     //// ACCESSORS ////
0053 
0054     // Get the construction tolerance
0055     Tol const& tol() const;
0056 
0057     //!@{
0058     //! Access the unit builder for construction
0059     CsgUnitBuilder const& unit_builder() const { return *ub_; }
0060     CsgUnitBuilder& unit_builder() { return *ub_; }
0061     //!@}
0062 
0063     // Access the local-to-global transform during construction
0064     VariantTransform const& local_transform() const;
0065 
0066     //// MUTATORS ////
0067 
0068     // Add a region to the CSG tree, automatically calculating bounding zone
0069     NodeId insert_region(Metadata&& md, Joined&& j);
0070 
0071     // Add a region to the CSG tree, including a better bounding zone
0072     NodeId insert_region(Metadata&& md, Joined&& j, BoundingZone&& bz);
0073 
0074     // Add a negated region to the CSG tree
0075     NodeId insert_region(Metadata&& md, Negated&& n);
0076 
0077     // Apply a transform within this scope
0078     [[nodiscard]] PopVBTransformOnDestruct
0079     make_scoped_transform(VariantTransform const& t);
0080 
0081   private:
0082     //// DATA ////
0083 
0084     CsgUnitBuilder* ub_;
0085     std::vector<TransformId> transforms_;
0086 
0087     //// PRIVATE METHODS ////
0088 
0089     // Add a new variant transform
0090     void push_transform(VariantTransform&& vt);
0091 
0092     // Pop the last transform, used only by PopVBTransformOnDestruct
0093     void pop_transform();
0094 
0095     //// FRIENDS ////
0096 
0097     friend class PopVBTransformOnDestruct;
0098 };
0099 
0100 //---------------------------------------------------------------------------//
0101 //! Implementation-only RAII helper class for VolumeBuilder (detail detail)
0102 class PopVBTransformOnDestruct
0103 {
0104   private:
0105     friend class VolumeBuilder;
0106 
0107     // Construct with a volume builder pointer
0108     explicit PopVBTransformOnDestruct(VolumeBuilder* vb);
0109 
0110   public:
0111     //! Capture the pointer when move constructed
0112     PopVBTransformOnDestruct(PopVBTransformOnDestruct&& other)
0113         : vb_(std::exchange(other.vb_, nullptr))
0114     {
0115     }
0116 
0117     //! Capture the pointer when move assigned
0118     PopVBTransformOnDestruct& operator=(PopVBTransformOnDestruct&& other)
0119     {
0120         vb_ = std::exchange(other.vb_, nullptr);
0121         return *this;
0122     }
0123 
0124     //! Call pop when we own the pointer and go out of scope
0125     ~PopVBTransformOnDestruct()
0126     {
0127         if (vb_)
0128         {
0129             vb_->pop_transform();
0130         }
0131     }
0132 
0133   private:
0134     VolumeBuilder* vb_{nullptr};
0135 };
0136 
0137 //---------------------------------------------------------------------------//
0138 }  // namespace detail
0139 }  // namespace orangeinp
0140 }  // namespace celeritas