Back to home page

EIC code displayed by LXR

 
 

    


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

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/detail/TransformRecordInserter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Macros.hh"
0010 #include "corecel/data/Collection.hh"
0011 #include "corecel/data/CollectionBuilder.hh"
0012 #include "corecel/data/DedupeCollectionBuilder.hh"
0013 
0014 #include "../OrangeData.hh"
0015 #include "../transform/VariantTransform.hh"
0016 
0017 namespace celeritas
0018 {
0019 namespace detail
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Construct a compressed transform from a variant.
0024  *
0025  * TODO: deduplicate transforms via hashes? Define special compressed transform
0026  * for 90-degree rotations?
0027  */
0028 class TransformRecordInserter
0029 {
0030   public:
0031     //!@{
0032     //! \name Type aliases
0033     template<class T>
0034     using Items = Collection<T, Ownership::value, MemSpace::host>;
0035     //!@}
0036 
0037   public:
0038     // Construct with pointers to target data
0039     inline TransformRecordInserter(Items<TransformRecord>* transforms,
0040                                    Items<real_type>* reals);
0041 
0042     // Return a transform ID from a transform variant
0043     inline TransformId operator()(VariantTransform const& tr);
0044 
0045     // Construct a transform using known type
0046     template<class T>
0047     inline TransformId operator()(T const& tr);
0048 
0049   private:
0050     TransformId null_transform_;
0051     CollectionBuilder<TransformRecord> transforms_;
0052     DedupeCollectionBuilder<real_type> reals_;
0053 };
0054 
0055 //---------------------------------------------------------------------------//
0056 // INLINE DEFINITIONS
0057 //---------------------------------------------------------------------------//
0058 /*!
0059  * Construct with pointers to target data.
0060  */
0061 TransformRecordInserter::TransformRecordInserter(
0062     Items<TransformRecord>* transforms, Items<real_type>* reals)
0063     : transforms_{transforms}, reals_{reals}
0064 {
0065     CELER_EXPECT(transforms && reals);
0066 }
0067 
0068 //---------------------------------------------------------------------------//
0069 /*!
0070  * Construct from a transform variant.
0071  */
0072 TransformId TransformRecordInserter::operator()(VariantTransform const& tr)
0073 {
0074     CELER_ASSUME(!tr.valueless_by_exception());
0075     return std::visit(*this, tr);
0076 }
0077 
0078 //---------------------------------------------------------------------------//
0079 /*!
0080  * Construct from a transform with a known type.
0081  */
0082 template<class T>
0083 TransformId TransformRecordInserter::operator()(T const& tr)
0084 {
0085     /*!
0086      * \todo Add equality and hash for TransformRecord and replace this with
0087      * just a dedupe collection builder.
0088      */
0089     if constexpr (std::is_same_v<T, NoTransformation>)
0090     {
0091         // Reuse the same null transform ID everywhere
0092         if (null_transform_)
0093         {
0094             return null_transform_;
0095         }
0096         // Save the transform ID for later
0097         null_transform_ = transforms_.size_id();
0098     }
0099 
0100     TransformRecord record;
0101     record.type = tr.transform_type();
0102     auto data = tr.data();
0103     record.data_offset = *reals_.insert_back(data.begin(), data.end()).begin();
0104 
0105     CELER_ASSERT(record);
0106     return transforms_.push_back(record);
0107 }
0108 
0109 //---------------------------------------------------------------------------//
0110 }  // namespace detail
0111 }  // namespace celeritas