Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Acts/Definitions/Direction.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2016-2018 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 
0013 #include <cassert>
0014 #include <cstddef>
0015 #include <iosfwd>
0016 #include <string>
0017 
0018 namespace Acts {
0019 
0020 /// The direction is always with respect to a given momentum, surface normal or
0021 /// other general axes
0022 class Direction final {
0023  private:
0024   enum class Value : int {
0025     Negative = -1,
0026     Positive = 1,
0027   };
0028 
0029  public:
0030   static constexpr auto Negative = Value::Negative;
0031   static constexpr auto Positive = Value::Positive;
0032 
0033   static constexpr auto Backward = Value::Negative;
0034   static constexpr auto Forward = Value::Positive;
0035 
0036   static constexpr auto OppositeNormal = Value::Negative;
0037   static constexpr auto AlongNormal = Value::Positive;
0038 
0039   /// This turns a signed value into a direction. Will assert on zero.
0040   ///
0041   /// @param scalar is the signed value
0042   ///
0043   /// @return a direction enum
0044   static inline constexpr Direction fromScalar(ActsScalar scalar) {
0045     assert(scalar != 0);
0046     return scalar >= 0 ? Value::Positive : Value::Negative;
0047   }
0048 
0049   /// This turns a signed value into a direction and 0 will be handled as a
0050   /// positive direction. Only use this when you are convinced that the 0 case
0051   /// is properly handled downstream.
0052   ///
0053   /// @param scalar is the signed value
0054   ///
0055   /// @return a direction enum
0056   static inline constexpr Direction fromScalarZeroAsPositive(
0057       ActsScalar scalar) {
0058     return scalar >= 0 ? Value::Positive : Value::Negative;
0059   }
0060 
0061   /// Convert and index [0,1] to a direction e.g. for sorting in
0062   /// std::array<T, 2u>
0063   ///
0064   /// @param index is the direction at input
0065   static inline constexpr Direction fromIndex(std::size_t index) {
0066     if (index == 0u) {
0067       return Value::Negative;
0068     }
0069     return Value::Positive;
0070   }
0071 
0072   /// Convert dir to index [0,1] which allows to store direction dependent
0073   /// objects in std::array<T, 2u>
0074   ///
0075   /// @return either 0 or 1
0076   inline constexpr std::size_t index() const {
0077     if (m_value == Value::Negative) {
0078       return 0u;
0079     }
0080     return 1u;
0081   }
0082 
0083   /// Turns the direction into a signed value
0084   ///
0085   /// @return a signed value
0086   inline constexpr int sign() const { return static_cast<int>(m_value); }
0087 
0088   /// Reverse the direction
0089   ///
0090   /// @return an opposite direction
0091   inline constexpr Direction invert() const {
0092     return (m_value == Value::Positive) ? Value::Negative : Value::Positive;
0093   }
0094 
0095   std::string toString() const;
0096 
0097   inline constexpr Direction() = default;
0098   inline constexpr Direction(Value value) : m_value(value) {}
0099 
0100   inline constexpr bool operator==(Direction other) const {
0101     return m_value == other.m_value;
0102   }
0103 
0104   inline constexpr bool operator!=(Direction other) const {
0105     return m_value != other.m_value;
0106   }
0107 
0108  private:
0109   Value m_value = Value::Positive;
0110 };
0111 
0112 std::ostream& operator<<(std::ostream& os, Direction dir);
0113 
0114 // Direction * T
0115 
0116 inline constexpr int operator*(Direction dir, int value) {
0117   return dir.sign() * value;
0118 }
0119 
0120 inline constexpr float operator*(Direction dir, float value) {
0121   return dir.sign() * value;
0122 }
0123 
0124 inline constexpr double operator*(Direction dir, double value) {
0125   return dir.sign() * value;
0126 }
0127 
0128 inline Acts::Vector3 operator*(Direction dir, const Acts::Vector3& value) {
0129   return dir.sign() * value;
0130 }
0131 
0132 // T * Direction
0133 
0134 inline constexpr int operator*(int value, Direction dir) {
0135   return value * dir.sign();
0136 }
0137 
0138 inline constexpr float operator*(float value, Direction dir) {
0139   return value * dir.sign();
0140 }
0141 
0142 inline constexpr double operator*(double value, Direction dir) {
0143   return value * dir.sign();
0144 }
0145 
0146 inline Acts::Vector3 operator*(const Acts::Vector3& value, Direction dir) {
0147   return value * dir.sign();
0148 }
0149 
0150 // T *= Direction
0151 
0152 inline constexpr int operator*=(int& value, Direction dir) {
0153   value *= dir.sign();
0154   return value;
0155 }
0156 
0157 inline constexpr float operator*=(float& value, Direction dir) {
0158   value *= dir.sign();
0159   return value;
0160 }
0161 
0162 inline constexpr double operator*=(double& value, Direction dir) {
0163   value *= dir.sign();
0164   return value;
0165 }
0166 
0167 inline Acts::Vector3& operator*=(Acts::Vector3& value, Direction dir) {
0168   value *= dir.sign();
0169   return value;
0170 }
0171 
0172 }  // namespace Acts