File indexing completed on 2026-11-17 10:21:49
0001
0002
0003
0004
0005
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
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 class Transformer
0045 {
0046 public:
0047
0048
0049 using Real3 = Array<real_type, 3>;
0050
0051
0052 public:
0053
0054 inline explicit Transformer(Scaler const& scale);
0055
0056
0057 inline Translation operator()(G4ThreeVector const& t) const;
0058
0059
0060 inline Transformation operator()(G4RotationMatrix const& g4rm) const;
0061
0062
0063 inline Transformation operator()(G4ThreeVector const& t,
0064 G4RotationMatrix const& g4rm) const;
0065
0066
0067 inline Transformation operator()(G4Transform3D const& g4tr) const;
0068
0069
0070 inline VariantTransform variant(G4AffineTransform const& at) const;
0071
0072
0073 inline VariantTransform variant(G4ThreeVector const& t,
0074 G4RotationMatrix const* rot) const;
0075
0076 private:
0077
0078
0079 Scaler const& scale_;
0080 };
0081
0082
0083
0084
0085
0086 inline Real3 convert_from_geant(G4ThreeVector const& vec);
0087
0088
0089
0090 inline Real3 convert_from_geant(double x, double y, double z);
0091
0092
0093
0094 inline SquareMatrixReal3 convert_from_geant(G4RotationMatrix const& rot);
0095
0096
0097
0098 inline SquareMatrixReal3 transposed_from_geant(G4RotationMatrix const& rot);
0099
0100
0101
0102 inline bool is_zero(G4ThreeVector const& vec);
0103
0104
0105
0106
0107
0108
0109
0110 Transformer::Transformer(Scaler const& scale) : scale_{scale} {}
0111
0112
0113
0114
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
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
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
0153
0154
0155
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
0175
0176 auto Transformer::variant(G4ThreeVector const& trans,
0177 G4RotationMatrix const* rot) const -> VariantTransform
0178 {
0179 if (rot)
0180 {
0181
0182
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
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
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
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
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
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 }
0248 }