EIC 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 
16 namespace Acts {
17 namespace detail {
18 
22  static constexpr bool may_modify_value = false;
23 
25  template <typename value_t>
26  static constexpr const value_t& getValue(const value_t& value) {
27  return value;
28  }
30  template <typename value_t>
31  static constexpr value_t getDifference(const value_t& lhs,
32  const value_t& rhs) {
33  return lhs - rhs;
34  }
35 };
36 
43 template <typename limits_t>
46  static constexpr bool may_modify_value = true;
48  static constexpr double min = limits_t::lowest();
50  static constexpr double max = limits_t::max();
51 
53  template <typename value_t>
54  static constexpr value_t getValue(const value_t& value) {
55  return std::clamp(value, static_cast<value_t>(min),
56  static_cast<value_t>(max));
57  }
59  template <typename value_t>
60  static constexpr value_t getDifference(const value_t& lhs,
61  const value_t& rhs) {
62  return getValue(lhs) - getValue(rhs);
63  }
64 };
65 
71 template <typename limits_t>
74  static constexpr bool may_modify_value = true;
76  static constexpr double min = limits_t::lowest();
78  static constexpr double max = limits_t::max();
79 
81  template <typename value_t>
82  static constexpr value_t getValue(const value_t& value) {
83  if ((min <= value) and (value < max)) {
84  return value;
85  } else {
86  return value - (max - min) * std::floor((value - min) / (max - min));
87  }
88  }
90  template <typename value_t>
91  static constexpr value_t getDifference(const value_t& lhs,
92  const value_t& rhs) {
93  constexpr value_t range = (max - min);
94  value_t delta = getValue(lhs) - getValue(rhs);
95  if ((2 * delta) < -range) {
96  return delta + range;
97  } else if (range < (2 * delta)) {
98  return delta - range;
99  } else {
100  return delta;
101  }
102  }
103 };
104 
105 // Limit types for parameter traits.
106 //
107 // The functions names are chosen to be consisten w/ std::numeric_limits
109  static constexpr double lowest() { return -M_PI; }
110  static constexpr double max() { return M_PI; }
111 };
113  static constexpr double lowest() { return 0; }
114  static constexpr double max() { return M_PI; }
115 };
116 
117 // Traits implementation structs for single parameters.
118 //
119 // Separate implementation structs are needed to generate a compile-time error
120 // in case of an unsupported enum type/ index combination.
121 template <typename index_t, index_t kIndex>
123 template <>
126 };
127 template <>
130 };
131 template <BoundIndices kIndex>
133  // other bound parameters not explicitely specified above are unrestricted
135 };
136 template <FreeIndices kIndex>
138  // all free parameters components are unrestricted
140 };
141 
149 template <typename index_t, index_t kIndex>
151 
152 // Traits implementation structs for all parameters in an indices enum.
153 //
154 // Separate implementation structs are needed to generate a compile-time error
155 // in case of an unsupported indices enum. Also required since template
156 // variables can not have an undefined default case.
157 template <typename indices_t>
159 template <>
162  static constexpr unsigned int kSize =
163  static_cast<unsigned int>(BoundIndices::eBoundSize);
164 };
165 template <>
168  static constexpr unsigned int kSize =
169  static_cast<unsigned int>(FreeIndices::eFreeSize);
170 };
171 
173 template <typename indices_t>
175 
177 template <typename indices_t>
179 
180 } // namespace detail
181 } // namespace Acts