Skip to content

Commit

Permalink
refactor!: Generalize NavigationDirection to Direction (#2030)
Browse files Browse the repository at this point in the history
I realized that `NavigationDirection` was used in various different parts of our code in terms of a general direction. I saw two options to resolve this: create another enum to disambiguate them or generalize the `NavigationDirection` to `Direction`. Opted for the later because it seems to be the same concept and we do not use the same thing with two different meanings in a single place.

On top I added two new aliases `Positive` and `Negative` which made more sense to me in the context of surfaces and their normal. Also I added a few more utility functions and used them across the codebase.
  • Loading branch information
andiwand committed Apr 23, 2023
1 parent 2ffdeee commit b442bb9
Show file tree
Hide file tree
Showing 77 changed files with 601 additions and 564 deletions.
134 changes: 0 additions & 134 deletions Core/include/Acts/Definitions/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,144 +8,10 @@

#pragma once

#include "Acts/Definitions/Algebra.hpp"

#include <iosfwd>
#include <limits>

namespace Acts {

/// Tolerance for being numerical equal for geometry building
static constexpr ActsScalar s_epsilon =
3 * std::numeric_limits<ActsScalar>::epsilon();

/// Tolerance for being on Surface
///
/// @note This is intentionally given w/o an explicit unit to avoid having
/// to include the units header unneccessarily. With the native length
/// unit of mm this corresponds to 0.1um.
static constexpr ActsScalar s_onSurfaceTolerance = 1e-4;

/// Tolerance for not being within curvilinear projection
/// this allows using the same curvilinear frame to eta = 6,
/// validity tested with IntegrationTests/PropagationTest
static constexpr ActsScalar s_curvilinearProjTolerance = 0.999995;

/// @enum NavigationDirection
/// The navigation direction is always with
/// respect to a given momentum or direction
enum class NavigationDirection : int { Backward = -1, Forward = 1 };

/// Convert navigation dir to index [0,1] which allows to
/// store direction dependent objects in std::array<T,2u>
///
/// @param nDir is the navigation direction at input
///
/// returns either 0 or 1
inline constexpr size_t indexFromDirection(NavigationDirection nDir) {
if (nDir == NavigationDirection::Backward) {
return 0u;
}
return 1u;
}

/// Convert and ndex [0,1] to a navigation direction
/// for sorting in std::array<T,2u>
///
/// @param index is the navigation direction at input
///
/// returns either 0 or 1
inline constexpr NavigationDirection directionFromIndex(size_t index) {
if (index == 0u) {
return NavigationDirection::Backward;
}
return NavigationDirection::Forward;
}

/// This turns a signed value into a navigation direction
///
/// @param value is the signed value
///
/// @return a navigation direciton enum
inline constexpr NavigationDirection directionFromStepSize(ActsScalar value) {
assert(value != 0);
return value > 0 ? NavigationDirection::Forward
: NavigationDirection::Backward;
}

/// Invert a navigation direction enum
///
/// @param nDir is the navigation direction at input
///
/// return an opposite navigation direction
inline constexpr NavigationDirection invertDirection(NavigationDirection nDir) {
return (nDir == NavigationDirection::Forward) ? NavigationDirection::Backward
: NavigationDirection::Forward;
}

std::ostream& operator<<(std::ostream& os, NavigationDirection navDir);

// NavigationDirection * T

inline constexpr auto operator*(NavigationDirection dir, int value) {
return static_cast<std::underlying_type_t<NavigationDirection>>(dir) * value;
}

inline constexpr auto operator*(NavigationDirection dir, float value) {
return static_cast<std::underlying_type_t<NavigationDirection>>(dir) * value;
}

inline constexpr auto operator*(NavigationDirection dir, double value) {
return static_cast<std::underlying_type_t<NavigationDirection>>(dir) * value;
}

inline Acts::Vector3 operator*(NavigationDirection dir,
const Acts::Vector3& value) {
return static_cast<std::underlying_type_t<NavigationDirection>>(dir) * value;
}

// T * NavigationDirection

inline constexpr auto operator*(int value, NavigationDirection dir) {
return value * static_cast<std::underlying_type_t<NavigationDirection>>(dir);
}

inline constexpr auto operator*(float value, NavigationDirection dir) {
return value * static_cast<std::underlying_type_t<NavigationDirection>>(dir);
}

inline constexpr auto operator*(double value, NavigationDirection dir) {
return value * static_cast<std::underlying_type_t<NavigationDirection>>(dir);
}

inline Acts::Vector3 operator*(const Acts::Vector3& value,
NavigationDirection dir) {
return value * static_cast<std::underlying_type_t<NavigationDirection>>(dir);
}

// T *= NavigationDirection

inline constexpr auto operator*=(int& value, NavigationDirection dir) {
value *= static_cast<std::underlying_type_t<NavigationDirection>>(dir);
return value;
}

inline constexpr auto operator*=(float& value, NavigationDirection dir) {
value *= static_cast<std::underlying_type_t<NavigationDirection>>(dir);
return value;
}

inline constexpr auto operator*=(double& value, NavigationDirection dir) {
value *= static_cast<std::underlying_type_t<NavigationDirection>>(dir);
return value;
}

inline Acts::Vector3& operator*=(Acts::Vector3& value,
NavigationDirection dir) {
value *= static_cast<std::underlying_type_t<NavigationDirection>>(dir);
return value;
}

/// This is a steering enum to tell which material update stage:
/// - PreUpdate : update on approach of a surface
/// - FullUpdate : update when passing a surface
Expand Down
169 changes: 169 additions & 0 deletions Core/include/Acts/Definitions/Direction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// This file is part of the Acts project.
//
// Copyright (C) 2016-2018 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#pragma once

#include "Acts/Definitions/Algebra.hpp"

#include <iosfwd>
#include <string>

namespace Acts {

/// The direction is always with respect to a given momentum, surface normal or
/// other general axes
class Direction final {
private:
enum class Value : int {
Negative = -1,
Positive = 1,
};

public:
static constexpr auto Negative = Value::Negative;
static constexpr auto Positive = Value::Positive;

static constexpr auto Backward = Value::Negative;
static constexpr auto Forward = Value::Positive;

/// This turns a signed value into a direction. Will assert on zero.
///
/// @param scalar is the signed value
///
/// @return a direciton enum
static inline constexpr Direction fromScalar(ActsScalar scalar) {
assert(scalar != 0);
return scalar >= 0 ? Value::Positive : Value::Negative;
}

/// This turns a signed value into a direction and 0 will be handled as a
/// positive direction. Only use this when you are convinced that the 0 case
/// is properly handled downstream.
///
/// @param scalar is the signed value
///
/// @return a direciton enum
static inline constexpr Direction fromScalarZeroAsPositive(
ActsScalar scalar) {
return scalar >= 0 ? Value::Positive : Value::Negative;
}

/// Convert and index [0,1] to a direction e.g. for sorting in
/// std::array<T, 2u>
///
/// @param index is the direction at input
static inline constexpr Direction fromIndex(std::size_t index) {
if (index == 0u) {
return Value::Negative;
}
return Value::Positive;
}

/// Convert dir to index [0,1] which allows to store direction dependent
/// objects in std::array<T, 2u>
///
/// @return either 0 or 1
inline constexpr std::size_t index() const {
if (m_value == Value::Negative) {
return 0u;
}
return 1u;
}

/// Turns the direction into a signed value
///
/// @return a signed value
inline constexpr int sign() const { return static_cast<int>(m_value); }

/// Reverse the direction
///
/// @param dir is the direction at input
///
/// @return an opposite direction
inline constexpr Direction invert() const {
return (m_value == Value::Positive) ? Value::Negative : Value::Positive;
}

std::string toString() const;

inline constexpr Direction() = default;
inline constexpr Direction(Value value) : m_value(value) {}

inline constexpr bool operator==(Direction other) const {
return m_value == other.m_value;
}

inline constexpr bool operator!=(Direction other) const {
return m_value != other.m_value;
}

private:
Value m_value = Value::Positive;
};

std::ostream& operator<<(std::ostream& os, Direction dir);

// Direction * T

inline constexpr int operator*(Direction dir, int value) {
return dir.sign() * value;
}

inline constexpr float operator*(Direction dir, float value) {
return dir.sign() * value;
}

inline constexpr double operator*(Direction dir, double value) {
return dir.sign() * value;
}

inline Acts::Vector3 operator*(Direction dir, const Acts::Vector3& value) {
return dir.sign() * value;
}

// T * Direction

inline constexpr int operator*(int value, Direction dir) {
return value * dir.sign();
}

inline constexpr float operator*(float value, Direction dir) {
return value * dir.sign();
}

inline constexpr double operator*(double value, Direction dir) {
return value * dir.sign();
}

inline Acts::Vector3 operator*(const Acts::Vector3& value, Direction dir) {
return value * dir.sign();
}

// T *= Direction

inline constexpr int operator*=(int& value, Direction dir) {
value *= dir.sign();
return value;
}

inline constexpr float operator*=(float& value, Direction dir) {
value *= dir.sign();
return value;
}

inline constexpr double operator*=(double& value, Direction dir) {
value *= dir.sign();
return value;
}

inline Acts::Vector3& operator*=(Acts::Vector3& value, Direction dir) {
value *= dir.sign();
return value;
}

} // namespace Acts
33 changes: 33 additions & 0 deletions Core/include/Acts/Definitions/Tolerance.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file is part of the Acts project.
//
// Copyright (C) 2023 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#pragma once

#include "Acts/Definitions/Algebra.hpp"

#include <limits>

namespace Acts {

/// Tolerance for being numerical equal for geometry building
static constexpr ActsScalar s_epsilon =
3 * std::numeric_limits<ActsScalar>::epsilon();

/// Tolerance for being on Surface
///
/// @note This is intentionally given w/o an explicit unit to avoid having
/// to include the units header unneccessarily. With the native length
/// unit of mm this corresponds to 0.1um.
static constexpr ActsScalar s_onSurfaceTolerance = 1e-4;

/// Tolerance for not being within curvilinear projection
/// this allows using the same curvilinear frame to eta = 6,
/// validity tested with IntegrationTests/PropagationTest
static constexpr ActsScalar s_curvilinearProjTolerance = 0.999995;

} // namespace Acts
4 changes: 2 additions & 2 deletions Core/include/Acts/Detector/Portal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#pragma once

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Definitions/Common.hpp"
#include "Acts/Definitions/Direction.hpp"
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/Geometry/GeometryIdentifier.hpp"
#include "Acts/Navigation/NavigationDelegates.hpp"
Expand Down Expand Up @@ -120,7 +120,7 @@ class Portal : public std::enable_shared_from_this<Portal> {
///
/// @note this overwrites the existing link
void assignDetectorVolumeUpdator(
NavigationDirection dir, DetectorVolumeUpdator&& dVolumeUpdator,
Direction dir, DetectorVolumeUpdator&& dVolumeUpdator,
const std::vector<std::shared_ptr<DetectorVolume>>& attachedVolumes);

/// Update the volume link, w/o directive, i.e. it relies that there's only
Expand Down
4 changes: 2 additions & 2 deletions Core/include/Acts/Detector/PortalHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class DetectorVolume;
/// It consists of the new portal, the index, the direction, the parameters
/// gathered from the sub volumes, the binning description
using PortalReplacement =
std::tuple<std::shared_ptr<Experimental::Portal>, unsigned int,
NavigationDirection, std::vector<ActsScalar>, BinningValue>;
std::tuple<std::shared_ptr<Experimental::Portal>, unsigned int, Direction,
std::vector<ActsScalar>, BinningValue>;

/// @brief Create and attach the multi link updator
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#pragma once

#include "Acts/Definitions/Common.hpp"
#include "Acts/Definitions/Direction.hpp"
#include "Acts/EventData/detail/TransformationFreeToBound.hpp"
#include "Acts/Utilities/Logger.hpp"

Expand Down Expand Up @@ -90,7 +90,7 @@ struct CorrectedFreeToBoundTransformer {
std::optional<std::tuple<BoundVector, BoundSymMatrix>> operator()(
const FreeVector& freeParams, const FreeSymMatrix& freeCovariance,
const Surface& surface, const GeometryContext& geoContext,
NavigationDirection navDir = NavigationDirection::Forward,
Direction navDir = Direction::Forward,
const Logger& logger = getDummyLogger()) const;

private:
Expand Down

0 comments on commit b442bb9

Please sign in to comment.