Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-17 10:21:49

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/g4org/Transformer.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <G4AffineTransform.hh>
0010 #include <G4RotationMatrix.hh>
0011 #include <G4ThreeVector.hh>
0012 #include <G4Transform3D.hh>
0013 
0014 #include "corecel/Macros.hh"
0015 #include "geocel/Types.hh"
0016 #include "geocel/g4/Convert.hh"
0017 #include "orange/transform/NoTransformation.hh"
0018 #include "orange/transform/Transformation.hh"
0019 #include "orange/transform/Translation.hh"
0020 #include "orange/transform/VariantTransform.hh"
0021 
0022 #include "Scaler.hh"
0023 
0024 namespace celeritas
0025 {
0026 namespace g4org
0027 {
0028 //---------------------------------------------------------------------------//
0029 /*!
0030  * Return an ORANGE transformation from a Geant4 transformation.
0031  *
0032  * In Geant4, "object" or "direct" transform refers to daughter-to-parent, how
0033  * to place the daughter object in the parent. The "frame" transform (raw \c
0034  * GetTransform or the \c fPtrTransform object) is how to transform from parent
0035  * to daughter and is the inverse of that transform.
0036  *
0037  * Even though the affine transform's matrix has a \c operator() which does a
0038  * matrix-vector multiply (aka \c gemv), this is *not* the same as the affine
0039  * transform's rotation, which applies the *inverse* of the stored matrix.
0040  *
0041  * All ORANGE/Celeritas transforms are "daughter to parent". The transforms
0042  * returned from this function \em must be daughter-to-parent!
0043  */
0044 class Transformer
0045 {
0046   public:
0047     //!@{
0048     //! \name Type aliases
0049     using Real3 = Array<real_type, 3>;
0050     //!@}
0051 
0052   public:
0053     // Construct with a scale
0054     inline explicit Transformer(Scaler const& scale);
0055 
0056     // Convert a translation
0057     inline Translation operator()(G4ThreeVector const& t) const;
0058 
0059     // Convert a pure rotation
0060     inline Transformation operator()(G4RotationMatrix const& g4rm) const;
0061 
0062     // Convert a translation + rotation
0063     inline Transformation operator()(G4ThreeVector const& t,
0064                                      G4RotationMatrix const& g4rm) const;
0065 
0066     // Convert a more general transform (includes reflection)
0067     inline Transformation operator()(G4Transform3D const& g4tr) const;
0068 
0069     // Convert a general affine transform
0070     inline VariantTransform variant(G4AffineTransform const& at) const;
0071 
0072     // Construct dynamically
0073     inline VariantTransform variant(G4ThreeVector const& t,
0074                                     G4RotationMatrix const* rot) const;
0075 
0076   private:
0077     //// DATA ////
0078 
0079     Scaler const& scale_;
0080 };
0081 
0082 //---------------------------------------------------------------------------//
0083 // FREE FUNCTIONS
0084 //---------------------------------------------------------------------------//
0085 // Convert a ThreeVector
0086 inline Real3 convert_from_geant(G4ThreeVector const& vec);
0087 
0088 //---------------------------------------------------------------------------//
0089 // Convert three doubles to a Real3
0090 inline Real3 convert_from_geant(double x, double y, double z);
0091 
0092 //---------------------------------------------------------------------------//
0093 // Convert a rotation matrix
0094 inline SquareMatrixReal3 convert_from_geant(G4RotationMatrix const& rot);
0095 
0096 //---------------------------------------------------------------------------//
0097 // Get the transpose/inverse of a rotation matrix
0098 inline SquareMatrixReal3 transposed_from_geant(G4RotationMatrix const& rot);
0099 
0100 //---------------------------------------------------------------------------//
0101 // Whether a vector has zero magnitude
0102 inline bool is_zero(G4ThreeVector const& vec);
0103 
0104 //---------------------------------------------------------------------------//
0105 // INLINE DEFINITIONS
0106 //---------------------------------------------------------------------------//
0107 /*!
0108  * Construct with a scaling function.
0109  */
0110 Transformer::Transformer(Scaler const& scale) : scale_{scale} {}
0111 
0112 //---------------------------------------------------------------------------//
0113 /*!
0114  * Create a transform from a translation.
0115  */
0116 auto Transformer::operator()(G4ThreeVector const& t) const -> Translation
0117 {
0118     return Translation{scale_.to<Real3>(t[0], t[1], t[2])};
0119 }
0120 
0121 //---------------------------------------------------------------------------//
0122 /*!
0123  * Create a transform from a translation plus rotation.
0124  */
0125 auto Transformer::operator()(G4ThreeVector const& g4t,
0126                              G4RotationMatrix const& g4rm) const
0127     -> Transformation
0128 {
0129     SquareMatrixReal3 mat{Real3(g4rm.xx(), g4rm.xy(), g4rm.xz()),
0130                           Real3(g4rm.yx(), g4rm.yy(), g4rm.yz()),
0131                           Real3(g4rm.zx(), g4rm.zy(), g4rm.zz())};
0132 
0133     return Transformation{mat, scale_.to<Real3>(g4t)};
0134 }
0135 
0136 //---------------------------------------------------------------------------//
0137 /*!
0138  * Convert a more general transform (including possibly reflection).
0139  */
0140 Transformation Transformer::operator()(G4Transform3D const& g4tr) const
0141 {
0142     SquareMatrixReal3 mat{Real3(g4tr.xx(), g4tr.xy(), g4tr.xz()),
0143                           Real3(g4tr.yx(), g4tr.yy(), g4tr.yz()),
0144                           Real3(g4tr.zx(), g4tr.zy(), g4tr.zz())};
0145 
0146     return Transformation{mat,
0147                           scale_.to<Real3>(g4tr.dx(), g4tr.dy(), g4tr.dz())};
0148 }
0149 
0150 //---------------------------------------------------------------------------//
0151 /*!
0152  * Create a transform from an affine transform.
0153  *
0154  * The affine transform's stored rotation matrix is \em inverted! Also, this
0155  * is frequently used when the rotation is identity, so we make a special case.
0156  */
0157 auto Transformer::variant(G4AffineTransform const& affine) const
0158     -> VariantTransform
0159 {
0160     if (!affine.NetRotation().isIdentity())
0161     {
0162         return Transformation{transposed_from_geant(affine.NetRotation()),
0163                               scale_.to<Real3>(affine.NetTranslation())};
0164     }
0165     if (!is_zero(affine.NetTranslation()))
0166     {
0167         return Translation{scale_.to<Real3>(affine.NetTranslation())};
0168     }
0169     return NoTransformation{};
0170 }
0171 
0172 //---------------------------------------------------------------------------//
0173 /*!
0174  * Create a transform from a translation and optional rotation.
0175  */
0176 auto Transformer::variant(G4ThreeVector const& trans,
0177                           G4RotationMatrix const* rot) const -> VariantTransform
0178 {
0179     if (rot)
0180     {
0181         // Do another check for the identity matrix (parameterized volumes
0182         // often have one)
0183         if (!rot->isIdentity())
0184         {
0185             return (*this)(trans, *rot);
0186         }
0187     }
0188     if (!is_zero(trans))
0189     {
0190         return (*this)(trans);
0191     }
0192     return NoTransformation{};
0193 }
0194 
0195 //---------------------------------------------------------------------------//
0196 /*!
0197  * Convert a ThreeVector.
0198  */
0199 Real3 convert_from_geant(G4ThreeVector const& vec)
0200 {
0201     return convert_from_geant(vec[0], vec[1], vec[2]);
0202 }
0203 
0204 //---------------------------------------------------------------------------//
0205 /*!
0206  * Convert three doubles to a Real3.
0207  */
0208 Real3 convert_from_geant(double x, double y, double z)
0209 {
0210     return Real3{{static_cast<real_type>(x),
0211                   static_cast<real_type>(y),
0212                   static_cast<real_type>(z)}};
0213 }
0214 
0215 //---------------------------------------------------------------------------//
0216 /*!
0217  * Convert a rotation matrix.
0218  */
0219 SquareMatrixReal3 convert_from_geant(G4RotationMatrix const& rot)
0220 {
0221     return {convert_from_geant(rot.xx(), rot.xy(), rot.xz()),
0222             convert_from_geant(rot.yx(), rot.yy(), rot.yz()),
0223             convert_from_geant(rot.zx(), rot.zy(), rot.zz())};
0224 }
0225 
0226 //---------------------------------------------------------------------------//
0227 /*!
0228  * Get a transposed rotation matrix.
0229  */
0230 SquareMatrixReal3 transposed_from_geant(G4RotationMatrix const& rot)
0231 {
0232     return {convert_from_geant(rot.xx(), rot.yx(), rot.zx()),
0233             convert_from_geant(rot.xy(), rot.yy(), rot.zy()),
0234             convert_from_geant(rot.xz(), rot.yz(), rot.zz())};
0235 }
0236 
0237 //---------------------------------------------------------------------------//
0238 /*!
0239  * Whether a vector has zero magnitude.
0240  */
0241 CELER_FORCEINLINE bool is_zero(G4ThreeVector const& vec)
0242 {
0243     return vec[0] == 0 && vec[1] == 0 && vec[2] == 0;
0244 }
0245 
0246 //---------------------------------------------------------------------------//
0247 }  // namespace g4org
0248 }  // namespace celeritas