Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 09:03:16

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file orange/orangeinp/detail/ProtoBuilder.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <functional>
0010 
0011 #include "orange/OrangeInput.hh"
0012 #include "orange/OrangeTypes.hh"
0013 
0014 #include "ProtoMap.hh"
0015 
0016 namespace celeritas
0017 {
0018 struct JsonPimpl;
0019 namespace orangeinp
0020 {
0021 class ProtoInterface;
0022 
0023 namespace detail
0024 {
0025 //---------------------------------------------------------------------------//
0026 /*!
0027  * Manage data about the universe construction.
0028  *
0029  * On construction this builds a breadth-first ordered list of protos:
0030  * the input "global" universe will always be at the front of the list, and
0031  * universes may only depend on a universe with a larger ID.
0032  *
0033  * This is passed to \c ProtoInterface::build. It acts like a two-way map
0034  * between universe IDs and pointers to Proto interfaces. It \em must not
0035  * exceed the lifetime of any of the protos.
0036  *
0037  * The bounding box for a universe starts as "null" and is expanded by the
0038  * universes that use it: this allows, for example, different masked components
0039  * of an array to be used in multiple universes.
0040  */
0041 class ProtoBuilder
0042 {
0043   public:
0044     //!@{
0045     //! \name Type aliases
0046     using Tol = Tolerance<>;
0047     using SaveUnivJson = std::function<void(UniverseId, JsonPimpl&&)>;
0048     //!@}
0049 
0050     //! Input options for construction
0051     struct Options
0052     {
0053         //! Manually specify a tracking/construction tolerance
0054         Tolerance<> tol;
0055         //! Save metadata during construction for each universe
0056         SaveUnivJson save_json;
0057     };
0058 
0059   public:
0060     // Construct with output pointer, geometry construction options, and protos
0061     ProtoBuilder(OrangeInput* inp, ProtoMap const& protos, Options const& opts);
0062 
0063     //! Get the tolerance to use when constructing geometry
0064     Tol const& tol() const { return inp_->tol; }
0065 
0066     //! Whether output should be saved for each
0067     bool save_json() const { return static_cast<bool>(save_json_); }
0068 
0069     // Find a universe ID
0070     inline UniverseId find_universe_id(ProtoInterface const*) const;
0071 
0072     //! Get the next universe ID
0073     UniverseId next_id() const { return UniverseId(inp_->universes.size()); }
0074 
0075     // Get the bounding box of a universe
0076     inline BBox const& bbox(UniverseId) const;
0077 
0078     // Expand the bounding box of a universe
0079     void expand_bbox(UniverseId, BBox const& local_box);
0080 
0081     // Save debugging data for a universe
0082     void save_json(JsonPimpl&&) const;
0083 
0084     // Construct a universe (to be called *once* per proto)
0085     void insert(VariantUniverseInput&& unit);
0086 
0087   private:
0088     OrangeInput* inp_;
0089     ProtoMap const& protos_;
0090     SaveUnivJson save_json_;
0091     std::vector<BBox> bboxes_;
0092 };
0093 
0094 //---------------------------------------------------------------------------//
0095 // INLINE DEFINITIONS
0096 //---------------------------------------------------------------------------//
0097 /*!
0098  * Find a universe ID.
0099  */
0100 UniverseId ProtoBuilder::find_universe_id(ProtoInterface const* p) const
0101 {
0102     return protos_.find(p);
0103 }
0104 
0105 //---------------------------------------------------------------------------//
0106 /*!
0107  * Get the bounding box of a universe.
0108  */
0109 BBox const& ProtoBuilder::bbox(UniverseId univ_id) const
0110 {
0111     CELER_EXPECT(univ_id < bboxes_.size());
0112     return bboxes_[univ_id.get()];
0113 }
0114 
0115 //---------------------------------------------------------------------------//
0116 }  // namespace detail
0117 }  // namespace orangeinp
0118 }  // namespace celeritas