Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterTraits.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ParameterTraits.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <cstddef>
16 
17 namespace Acts {
18 namespace detail {
19 
23  static constexpr bool may_modify_value = false;
24 
26  template <typename value_t>
27  static constexpr const value_t& getValue(const value_t& value) {
28  return value;
29  }
31  template <typename value_t>
32  static constexpr value_t getDifference(const value_t& lhs,
33  const value_t& rhs) {
34  return lhs - rhs;
35  }
36 };
37 
44 template <typename limits_t>
47  static constexpr bool may_modify_value = true;
49  static constexpr double min = limits_t::lowest();
51  static constexpr double max = limits_t::max();
52 
54  template <typename value_t>
55  static constexpr value_t getValue(const value_t& value) {
56  return std::clamp(value, static_cast<value_t>(min),
57  static_cast<value_t>(max));
58  }
60  template <typename value_t>
61  static constexpr value_t getDifference(const value_t& lhs,
62  const value_t& rhs) {
63  return getValue(lhs) - getValue(rhs);
64  }
65 };
66 
72 template <typename limits_t>
75  static constexpr bool may_modify_value = true;
77  static constexpr double min = limits_t::lowest();
79  static constexpr double max = limits_t::max();
80 
82  template <typename value_t>
83  static constexpr value_t getValue(const value_t& value) {
84  if ((min <= value) and (value < max)) {
85  return value;
86  } else {
87  return value - (max - min) * std::floor((value - min) / (max - min));
88  }
89  }
91  template <typename value_t>
92  static constexpr value_t getDifference(const value_t& lhs,
93  const value_t& rhs) {
94  constexpr value_t range = (max - min);
95  value_t delta = getValue(lhs) - getValue(rhs);
96  if ((2 * delta) < -range) {
97  return delta + range;
98  } else if (range < (2 * delta)) {
99  return delta - range;
100  } else {
101  return delta;
102  }
103  }
104 };
105 
106 // Limit types for parameter traits.
107 //
108 // The functions names are chosen to be consistent w/ std::numeric_limits
110  static constexpr double lowest() { return -M_PI; }
111  static constexpr double max() { return M_PI; }
112 };
114  static constexpr double lowest() { return 0; }
115  static constexpr double max() { return M_PI; }
116 };
117 
118 // Traits implementation structs for single parameters.
119 //
120 // Separate implementation structs are needed to generate a compile-time error
121 // in case of an unsupported enum type/ index combination.
122 template <typename index_t, index_t kIndex>
124 template <>
127 };
128 template <>
131 };
132 template <BoundIndices kIndex>
134  // other bound parameters not explicitly specified above are unrestricted
136 };
137 template <FreeIndices kIndex>
139  // all free parameters components are unrestricted
141 };
142 
150 template <typename index_t, index_t kIndex>
152 
153 // Traits implementation structs for all parameters in an indices enum.
154 //
155 // Separate implementation structs are needed to generate a compile-time error
156 // in case of an unsupported indices enum. Also required since template
157 // variables can not have an undefined default case.
158 template <typename indices_t>
160 template <>
162  static constexpr size_t kSize = static_cast<size_t>(BoundIndices::eBoundSize);
163 };
164 template <>
166  static constexpr size_t kSize = static_cast<size_t>(FreeIndices::eFreeSize);
167 };
168 
170 template <typename indices_t>
172 
173 } // namespace detail
174 } // namespace Acts