Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:24:52

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/transform/Translation.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <algorithm>
0010 
0011 #include "corecel/cont/Span.hh"
0012 #include "corecel/math/Algorithms.hh"
0013 #include "corecel/math/ArrayOperators.hh"
0014 #include "orange/OrangeTypes.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Apply a translation (no rotation).
0021  *
0022  * The input translation is specified by translating the daughter's coordinate
0023  * into the parent.
0024  */
0025 class Translation
0026 {
0027   public:
0028     //@{
0029     //! \name Type aliases
0030     using StorageSpan = Span<real_type const, 3>;
0031     //@}
0032 
0033     //! Transform type identifier
0034     static CELER_CONSTEXPR_FUNCTION TransformType transform_type()
0035     {
0036         return TransformType::translation;
0037     }
0038 
0039   public:
0040     //! Construct with an identity translation
0041     explicit CELER_FUNCTION Translation() : tra_{0, 0, 0} {}
0042 
0043     //! Construct with the translation vector
0044     explicit CELER_FUNCTION Translation(Real3 const& trans) : tra_(trans) {}
0045 
0046     // Construct inline from storage
0047     explicit inline CELER_FUNCTION Translation(StorageSpan s);
0048 
0049     //// ACCESSORS ////
0050 
0051     //! Translation vector
0052     CELER_FORCEINLINE_FUNCTION Real3 const& translation() const
0053     {
0054         return tra_;
0055     }
0056 
0057     //! Get a view to the data for type-deleted storage
0058     CELER_FUNCTION StorageSpan data() const { return {&tra_[0], 3}; }
0059 
0060     //// CALCULATION ////
0061 
0062     // Transform from daughter to parent
0063     inline CELER_FUNCTION Real3 transform_up(Real3 const& pos) const;
0064 
0065     // Transform from parent to daughter
0066     inline CELER_FUNCTION Real3 transform_down(Real3 const& parent_pos) const;
0067 
0068     // Rotate from daughter to parent (identity)
0069     inline CELER_FUNCTION Real3 const& rotate_up(Real3 const& d) const;
0070 
0071     // Rotate from parent to daughter (identity)
0072     inline CELER_FUNCTION Real3 const& rotate_down(Real3 const& d) const;
0073 
0074     //! Calculate the inverse during preprocessing
0075     Translation calc_inverse() const { return Translation{negate(tra_)}; }
0076 
0077   private:
0078     Real3 tra_;
0079 };
0080 
0081 //---------------------------------------------------------------------------//
0082 // FREE FUNCTIONS
0083 //---------------------------------------------------------------------------//
0084 //!@{
0085 //! Host-only comparators
0086 inline bool operator==(Translation const& a, Translation const& b)
0087 {
0088     auto a_data = a.data();
0089     return std::equal(a_data.begin(), a_data.end(), b.data().begin());
0090 }
0091 
0092 inline bool operator!=(Translation const& a, Translation const& b)
0093 {
0094     return !(a == b);
0095 }
0096 //!@}
0097 
0098 //---------------------------------------------------------------------------//
0099 // INLINE DEFINITIONS
0100 //---------------------------------------------------------------------------//
0101 /*!
0102  * Construct inline from storage.
0103  */
0104 CELER_FUNCTION Translation::Translation(StorageSpan s) : tra_{s[0], s[1], s[2]}
0105 {
0106 }
0107 
0108 //---------------------------------------------------------------------------//
0109 /*!
0110  * Transform from daughter to parent.
0111  */
0112 CELER_FORCEINLINE_FUNCTION Real3 Translation::transform_up(Real3 const& pos) const
0113 {
0114     return pos + tra_;
0115 }
0116 
0117 //---------------------------------------------------------------------------//
0118 /*!
0119  * Transform from parent to daughter.
0120  */
0121 CELER_FORCEINLINE_FUNCTION Real3
0122 Translation::transform_down(Real3 const& parent_pos) const
0123 {
0124     return parent_pos - tra_;
0125 }
0126 
0127 //---------------------------------------------------------------------------//
0128 /*!
0129  * Rotate from daughter to parent (identity).
0130  */
0131 CELER_FORCEINLINE_FUNCTION Real3 const&
0132 Translation::rotate_up(Real3 const& d) const
0133 {
0134     return d;
0135 }
0136 
0137 //---------------------------------------------------------------------------//
0138 /*!
0139  * Rotate from parent to daughter (identity).
0140  */
0141 CELER_FORCEINLINE_FUNCTION Real3 const&
0142 Translation::rotate_down(Real3 const& d) const
0143 {
0144     return d;
0145 }
0146 
0147 //---------------------------------------------------------------------------//
0148 }  // namespace celeritas