Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:10:45

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://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 Direction Negative() { return Direction{Value::Negative}; }
0031   static constexpr Direction Positive() { return Direction{Value::Positive}; }
0032 
0033   static constexpr Direction Backward() { return Direction{Value::Negative}; }
0034   static constexpr Direction Forward() { return Direction{Value::Positive}; }
0035 
0036   static constexpr Direction OppositeNormal() {
0037     return Direction{Value::Negative};
0038   }
0039   static constexpr Direction AlongNormal() {
0040     return Direction{Value::Positive};
0041   }
0042 
0043   /// This turns a signed value into a direction. Will assert on zero.
0044   ///
0045   /// @param scalar is the signed value
0046   ///
0047   /// @return a direction enum
0048   static constexpr Direction fromScalar(double scalar) {
0049     assert(scalar != 0);
0050     return scalar >= 0 ? Positive() : Negative();
0051   }
0052 
0053   /// This turns a signed value into a direction and 0 will be handled as a
0054   /// positive direction. Only use this when you are convinced that the 0 case
0055   /// is properly handled downstream.
0056   ///
0057   /// @param scalar is the signed value
0058   ///
0059   /// @return a direction enum
0060   static constexpr Direction fromScalarZeroAsPositive(double scalar) {
0061     return scalar >= 0 ? Positive() : Negative();
0062   }
0063 
0064   /// Convert and index [0,1] to a direction e.g. for sorting in
0065   /// std::array<T, 2u>
0066   ///
0067   /// @param index is the direction at input
0068   static constexpr Direction fromIndex(std::size_t index) {
0069     return index == 0u ? Negative() : 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   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   constexpr int sign() const { return static_cast<int>(m_value); }
0087 
0088   /// Reverse the direction
0089   ///
0090   /// @return an opposite direction
0091   constexpr Direction invert() const {
0092     return *this == Positive() ? Negative() : Positive();
0093   }
0094 
0095   std::string toString() const;
0096 
0097   friend constexpr bool operator==(Direction lhs, Direction rhs) {
0098     return lhs.m_value == rhs.m_value;
0099   }
0100 
0101  private:
0102   explicit constexpr Direction(Value value) : m_value(value) {}
0103 
0104   Value m_value = Value::Positive;
0105 };
0106 
0107 std::ostream& operator<<(std::ostream& os, Direction dir);
0108 
0109 // Direction * T
0110 
0111 constexpr int operator*(Direction dir, int value) {
0112   return dir.sign() * value;
0113 }
0114 
0115 constexpr float operator*(Direction dir, float value) {
0116   return dir.sign() * value;
0117 }
0118 
0119 constexpr double operator*(Direction dir, double value) {
0120   return dir.sign() * value;
0121 }
0122 
0123 inline Acts::Vector3 operator*(Direction dir, const Acts::Vector3& value) {
0124   return dir.sign() * value;
0125 }
0126 
0127 // T * Direction
0128 
0129 constexpr int operator*(int value, Direction dir) {
0130   return value * dir.sign();
0131 }
0132 
0133 constexpr float operator*(float value, Direction dir) {
0134   return value * dir.sign();
0135 }
0136 
0137 constexpr double operator*(double value, Direction dir) {
0138   return value * dir.sign();
0139 }
0140 
0141 inline Acts::Vector3 operator*(const Acts::Vector3& value, Direction dir) {
0142   return value * dir.sign();
0143 }
0144 
0145 // T *= Direction
0146 
0147 constexpr int operator*=(int& value, Direction dir) {
0148   value *= dir.sign();
0149   return value;
0150 }
0151 
0152 constexpr float operator*=(float& value, Direction dir) {
0153   value *= dir.sign();
0154   return value;
0155 }
0156 
0157 constexpr double operator*=(double& value, Direction dir) {
0158   value *= dir.sign();
0159   return value;
0160 }
0161 
0162 inline Acts::Vector3& operator*=(Acts::Vector3& value, Direction dir) {
0163   value *= dir.sign();
0164   return value;
0165 }
0166 
0167 }  // namespace Acts