Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Direction.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Direction.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2016-2018 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 <cassert>
14 #include <cstddef>
15 #include <iosfwd>
16 #include <string>
17 
18 namespace Acts {
19 
22 class Direction final {
23  private:
24  enum class Value : int {
25  Negative = -1,
26  Positive = 1,
27  };
28 
29  public:
30  static constexpr auto Negative = Value::Negative;
31  static constexpr auto Positive = Value::Positive;
32 
33  static constexpr auto Backward = Value::Negative;
34  static constexpr auto Forward = Value::Positive;
35 
41  static inline constexpr Direction fromScalar(ActsScalar scalar) {
42  assert(scalar != 0);
43  return scalar >= 0 ? Value::Positive : Value::Negative;
44  }
45 
53  static inline constexpr Direction fromScalarZeroAsPositive(
54  ActsScalar scalar) {
55  return scalar >= 0 ? Value::Positive : Value::Negative;
56  }
57 
62  static inline constexpr Direction fromIndex(std::size_t index) {
63  if (index == 0u) {
64  return Value::Negative;
65  }
66  return Value::Positive;
67  }
68 
73  inline constexpr std::size_t index() const {
74  if (m_value == Value::Negative) {
75  return 0u;
76  }
77  return 1u;
78  }
79 
83  inline constexpr int sign() const { return static_cast<int>(m_value); }
84 
88  inline constexpr Direction invert() const {
89  return (m_value == Value::Positive) ? Value::Negative : Value::Positive;
90  }
91 
92  std::string toString() const;
93 
94  inline constexpr Direction() = default;
95  inline constexpr Direction(Value value) : m_value(value) {}
96 
97  inline constexpr bool operator==(Direction other) const {
98  return m_value == other.m_value;
99  }
100 
101  inline constexpr bool operator!=(Direction other) const {
102  return m_value != other.m_value;
103  }
104 
105  private:
106  Value m_value = Value::Positive;
107 };
108 
109 std::ostream& operator<<(std::ostream& os, Direction dir);
110 
111 // Direction * T
112 
113 inline constexpr int operator*(Direction dir, int value) {
114  return dir.sign() * value;
115 }
116 
117 inline constexpr float operator*(Direction dir, float value) {
118  return dir.sign() * value;
119 }
120 
121 inline constexpr double operator*(Direction dir, double value) {
122  return dir.sign() * value;
123 }
124 
126  return dir.sign() * value;
127 }
128 
129 // T * Direction
130 
131 inline constexpr int operator*(int value, Direction dir) {
132  return value * dir.sign();
133 }
134 
135 inline constexpr float operator*(float value, Direction dir) {
136  return value * dir.sign();
137 }
138 
139 inline constexpr double operator*(double value, Direction dir) {
140  return value * dir.sign();
141 }
142 
144  return value * dir.sign();
145 }
146 
147 // T *= Direction
148 
149 inline constexpr int operator*=(int& value, Direction dir) {
150  value *= dir.sign();
151  return value;
152 }
153 
154 inline constexpr float operator*=(float& value, Direction dir) {
155  value *= dir.sign();
156  return value;
157 }
158 
159 inline constexpr double operator*=(double& value, Direction dir) {
160  value *= dir.sign();
161  return value;
162 }
163 
165  value *= dir.sign();
166  return value;
167 }
168 
169 } // namespace Acts