From 33d9b6db9b82b6cdd770aba1e1339e5ff6be216e Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 21 Dec 2023 09:56:06 -0500 Subject: [PATCH] update c++17 vector util operators --- .../edm4hep/utils/vector_utils_legacy.h | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/utils/include/edm4hep/utils/vector_utils_legacy.h b/utils/include/edm4hep/utils/vector_utils_legacy.h index 8f6225035..3bb68fa1a 100644 --- a/utils/include/edm4hep/utils/vector_utils_legacy.h +++ b/utils/include/edm4hep/utils/vector_utils_legacy.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -44,6 +45,11 @@ namespace utils { return v.z; } + template + constexpr auto vector_t(const V& v) { + return v.t; + } + // 2D vector uses a,b instead of x,y template <> inline constexpr auto vector_x(const edm4hep::Vector2i& v) { @@ -98,6 +104,7 @@ namespace utils { using ValueType = typename detail::ValueTypeHelper::type; using EDM4hepVectorTypes = std::tuple; + using EDM4hepVector4DTypes = std::tuple; using EDM4hepVector3DTypes = std::tuple; using EDM4hepVector2DTypes = std::tuple; using EDM4hepFloatVectorTypes = std::tuple; @@ -111,6 +118,8 @@ namespace utils { template using EnableIfEDM4hepVector3DType = std::enable_if_t, bool>; + template + using EnableIfEdm4hepVector4DType = std::enable_if_t, bool>; template using EnableIfEDM4hepFloatVectorType = std::enable_if_t, bool>; @@ -218,6 +227,15 @@ inline constexpr V operator+(const V& v1, const V& v2) { return {x, y, z}; } +template = false> +inline constexpr V operator+(const V& v1, const V& v2) { + const auto x = edm4hep::utils::vector_x(v1) + edm4hep::utils::vector_x(v2); + const auto y = edm4hep::utils::vector_y(v1) + edm4hep::utils::vector_y(v2); + const auto z = edm4hep::utils::vector_z(v1) + edm4hep::utils::vector_z(v2); + const auto t = edm4hep::utils::vector_t(v1) + edm4hep::utils::vector_t(v2); + return {x, y, z, t}; +} + template = false> inline constexpr double operator*(const V& v1, const V& v2) { return edm4hep::utils::vector_x(v1) * edm4hep::utils::vector_x(v2) + @@ -231,6 +249,14 @@ inline constexpr double operator*(const V& v1, const V& v2) { edm4hep::utils::vector_z(v1) * edm4hep::utils::vector_z(v2); } +template = false> +inline constexpr double operator*(const V& v1, const V& v2) { + return (edm4hep::utils::vector_x(v1) * edm4hep::utils::vector_x(v2) + + edm4hep::utils::vector_y(v1) * edm4hep::utils::vector_y(v2) + + edm4hep::utils::vector_z(v1) * edm4hep::utils::vector_z(v2)) - + edm4hep::utils::vector_t(v1) * edm4hep::utils::vector_t(v2); +} + template = false> inline constexpr V operator*(const double d, const V& v) { using VT = edm4hep::utils::ValueType; @@ -248,6 +274,16 @@ inline constexpr V operator*(const double d, const V& v) { return {x, y, z}; } +template = false> +inline constexpr V operator*(const double d, const V& v) { + using VT = edm4hep::utils::ValueType; + const VT x = d * edm4hep::utils::vector_x(v); + const VT y = d * edm4hep::utils::vector_y(v); + const VT z = d * edm4hep::utils::vector_z(v); + const VT t = d * edm4hep::utils::vector_t(v); + return {x, y, z, t}; +} + template inline constexpr V operator*(const V& v, const double d) { return d * v;