Geant4-11
G4FieldUtils.icc
Go to the documentation of this file.
1// ********************************************************************
2// * License and Disclaimer *
3// * *
4// * The Geant4 software is copyright of the Copyright Holders of *
5// * the Geant4 Collaboration. It is provided under the terms and *
6// * conditions of the Geant4 Software License, included in the file *
7// * LICENSE and available at http://cern.ch/geant4/license . These *
8// * include a list of copyright holders. *
9// * *
10// * Neither the authors of this software system, nor their employing *
11// * institutes,nor the agencies providing financial support for this *
12// * work make any representation or warranty, express or implied, *
13// * regarding this software system or assume any liability for its *
14// * use. Please see the license in the file LICENSE and URL above *
15// * for the full disclaimer and the limitation of liability. *
16// * *
17// * This code implementation is the result of the scientific and *
18// * technical work of the GEANT4 collaboration. *
19// * By using, copying, modifying or distributing the software (or *
20// * any work based on the software) you agree to acknowledge its *
21// * use in resulting scientific publications, and indicate your *
22// * acceptance of all terms of the Geant4 Software license. *
23// ********************************************************************
24//
25// Helper namespace field_utils inline implementation
26
27// Author: Dmitry Sorokin, Google Summer of Code 2017
28// Supervision: John Apostolakis, CERN
29// --------------------------------------------------------------------
30
31namespace field_utils {
32
33namespace internal
34{
35 template<class T>
36 size_t getFirstIndex(const T& value)
37 {
38 return static_cast<size_t>(value);
39 }
40}
41
42template <typename ArrayType>
43inline G4double getValue(const ArrayType& array, Value1D value)
44{
45 const auto begin = internal::getFirstIndex(value);
46 return array[begin];
47}
48
49template <typename ArrayType>
50G4double getValue2(const ArrayType& array, Value1D value)
51{
52 return sqr(getValue(array, value));
53}
54
55template <typename ArrayType>
56G4double getValue(const ArrayType& array, Value3D value)
57{
58 return std::sqrt(getValue2(array, value));
59}
60
61template <typename ArrayType>
62G4double getValue2(const ArrayType& array, const Value3D value)
63{
64 const auto begin = internal::getFirstIndex(value);
65 return sqr(array[begin]) + sqr(array[begin+1]) + sqr(array[begin+2]);
66}
67
68template <typename ArrayType>
69G4ThreeVector makeVector(const ArrayType& array, Value3D value)
70{
71 const auto begin = internal::getFirstIndex(value);
72 return G4ThreeVector(array[begin], array[begin + 1], array[begin + 2]);
73}
74
75template <typename SourceArray, typename TargetArray>
76void setValue(const SourceArray& src, Value1D value, TargetArray& trg)
77{
78 const auto begin = internal::getFirstIndex(value);
79 trg[begin] = src[begin];
80}
81
82template <typename SourceArray, typename TargetArray, typename ...TargetArrays>
83void setValue(const SourceArray& src, Value1D value,
84 TargetArray& trg, TargetArrays&... trgs)
85{
86 const auto begin = internal::getFirstIndex(value);
87 trg[begin] = src[begin];
88 setValue(src, value, trgs...);
89}
90
91template <typename T>
92T clamp(T value, T lo, T hi)
93{
94 return std::min(std::max(lo, value), hi);
95}
96
97} // field_utils