Files
geant4/source/geometry/magneticfield/include/G4FieldUtils.icc
2025-12-05 08:54:02 +01:00

98 lines
3.5 KiB
Plaintext

// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// Helper namespace field_utils inline implementation
// Author: Dmitry Sorokin (CERN, Google Summer of Code 2017), 13.10.2017
// Supervision: John Apostolakis (CERN)
// --------------------------------------------------------------------
namespace field_utils {
namespace internal
{
template<class T>
std::size_t getFirstIndex(const T& value)
{
return static_cast<std::size_t>(value);
}
}
template <typename ArrayType>
inline G4double getValue(const ArrayType& array, Value1D value)
{
const auto begin = internal::getFirstIndex(value);
return array[begin];
}
template <typename ArrayType>
G4double getValue2(const ArrayType& array, Value1D value)
{
return sqr(getValue(array, value));
}
template <typename ArrayType>
G4double getValue(const ArrayType& array, Value3D value)
{
return std::sqrt(getValue2(array, value));
}
template <typename ArrayType>
G4double getValue2(const ArrayType& array, const Value3D value)
{
const auto begin = internal::getFirstIndex(value);
return sqr(array[begin]) + sqr(array[begin+1]) + sqr(array[begin+2]);
}
template <typename ArrayType>
G4ThreeVector makeVector(const ArrayType& array, Value3D value)
{
const auto begin = internal::getFirstIndex(value);
return G4ThreeVector(array[begin], array[begin + 1], array[begin + 2]);
}
template <typename SourceArray, typename TargetArray>
void setValue(const SourceArray& src, Value1D value, TargetArray& trg)
{
const auto begin = internal::getFirstIndex(value);
trg[begin] = src[begin];
}
template <typename SourceArray, typename TargetArray, typename ...TargetArrays>
void setValue(const SourceArray& src, Value1D value,
TargetArray& trg, TargetArrays&... trgs)
{
const auto begin = internal::getFirstIndex(value);
trg[begin] = src[begin];
setValue(src, value, trgs...);
}
template <typename T>
T clamp(T value, T lo, T hi)
{
return std::min(std::max(lo, value), hi);
}
} // field_utils