Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:17:12

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-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/surf/RecursiveSimplifier.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <utility>
0011 #include <variant>
0012 
0013 #include "orange/OrangeTypes.hh"
0014 
0015 #include "VariantSurface.hh"
0016 
0017 #include "detail/AllSurfaces.hh"
0018 #include "detail/RecursiveSimplifierImpl.hh"
0019 
0020 namespace celeritas
0021 {
0022 //---------------------------------------------------------------------------//
0023 /*!
0024  * Recursively simplify, then call the given function on the final surface.
0025  *
0026  * The tolerance for this class should be relative.
0027  *
0028  * Example:
0029  * \code
0030   RecursiveSimplifier print_simplified([](Sense s, auto&& surf) {
0031       cout << to_char(s) << surf << endl;
0032   }, 1e-10);
0033   // Invoke on a compile-time surface type
0034   print_simplified(Sense::inside, Plane{{1,0,0}, 4});
0035   // Invoke on a run-time surface type
0036   for (auto&& [sense, surf] : my_senses_and_variants)
0037   {
0038       print_simplified(sense, surf);
0039   }
0040   \endcode
0041  *
0042  */
0043 template<class F>
0044 class RecursiveSimplifier
0045 {
0046   public:
0047     // Construct with tolerance and function to apply
0048     inline RecursiveSimplifier(F&& func, real_type tol);
0049 
0050     //! Construct with tolerance and function to apply
0051     RecursiveSimplifier(F&& func, Tolerance<> tol)
0052         : RecursiveSimplifier(std::forward<F>(func), tol.rel)
0053     {
0054     }
0055 
0056     // Apply to a surface with a known type
0057     template<class S>
0058     void operator()(Sense s, S const& surf);
0059 
0060     // Apply to a surface with unknown type
0061     void operator()(Sense s, VariantSurface const& surf);
0062 
0063   private:
0064     F func_;
0065     real_type tol_;
0066 };
0067 
0068 //---------------------------------------------------------------------------//
0069 // DEDUCTION GUIDES
0070 //---------------------------------------------------------------------------//
0071 template<class F, class... Ts>
0072 RecursiveSimplifier(F&&, Ts...) -> RecursiveSimplifier<F>;
0073 
0074 //---------------------------------------------------------------------------//
0075 // INLINE DEFINITIONS
0076 //---------------------------------------------------------------------------//
0077 /*!
0078  * Construct with tolerance and function to apply.
0079  */
0080 template<class F>
0081 RecursiveSimplifier<F>::RecursiveSimplifier(F&& func, real_type tol)
0082     : func_{std::forward<F>(func)}, tol_{tol}
0083 {
0084     CELER_EXPECT(tol_ >= 0);
0085 }
0086 
0087 //---------------------------------------------------------------------------//
0088 /*!
0089  * Apply to a surface with a known type.
0090  */
0091 template<class F>
0092 template<class S>
0093 void RecursiveSimplifier<F>::operator()(Sense sense, S const& surf)
0094 {
0095     return detail::RecursiveSimplifierImpl<F>{func_, sense, tol_}(surf);
0096 }
0097 
0098 //---------------------------------------------------------------------------//
0099 /*!
0100  * Apply to a surface with an unknown type.
0101  */
0102 template<class F>
0103 void RecursiveSimplifier<F>::operator()(Sense sense, VariantSurface const& surf)
0104 {
0105     return std::visit(detail::RecursiveSimplifierImpl<F>{func_, sense, tol_},
0106                       surf);
0107 }
0108 
0109 //---------------------------------------------------------------------------//
0110 }  // namespace celeritas