File indexing completed on 2025-01-30 10:26:04
0001
0002
0003
0004 #ifndef VECGEOM_BASE_VECTOR2D_H_
0005 #define VECGEOM_BASE_VECTOR2D_H_
0006
0007 #include "VecGeom/base/Global.h"
0008
0009 #include "VecGeom/backend/scalar/Backend.h"
0010 #include "VecGeom/base/AlignedBase.h"
0011
0012 #include <algorithm>
0013 #include <ostream>
0014
0015 namespace vecgeom {
0016
0017 VECGEOM_DEVICE_FORWARD_DECLARE(template <typename Type> class Vector2D;);
0018 VECGEOM_DEVICE_DECLARE_CONV_TEMPLATE(class, Vector2D, typename);
0019
0020 inline namespace VECGEOM_IMPL_NAMESPACE {
0021
0022 template <typename Type>
0023 class Vector2D : public AlignedBase {
0024
0025 private:
0026 Type vec[2];
0027
0028 typedef Vector2D<Type> VecType;
0029
0030 public:
0031 VECCORE_ATT_HOST_DEVICE
0032 Vector2D();
0033
0034 VECCORE_ATT_HOST_DEVICE
0035 Vector2D(const Type x, const Type y);
0036
0037 VECCORE_ATT_HOST_DEVICE
0038 Vector2D(Vector2D const &other);
0039
0040 VECCORE_ATT_HOST_DEVICE
0041 VecType operator=(VecType const &other);
0042
0043 VECCORE_ATT_HOST_DEVICE
0044 VECGEOM_FORCE_INLINE
0045 Type &operator[](const int index);
0046
0047 VECCORE_ATT_HOST_DEVICE
0048 VECGEOM_FORCE_INLINE
0049 Type operator[](const int index) const;
0050
0051 VECCORE_ATT_HOST_DEVICE
0052 VECGEOM_FORCE_INLINE
0053 Type &x();
0054
0055 VECCORE_ATT_HOST_DEVICE
0056 VECGEOM_FORCE_INLINE
0057 Type x() const;
0058
0059 VECCORE_ATT_HOST_DEVICE
0060 VECGEOM_FORCE_INLINE
0061 Type &y();
0062
0063 VECCORE_ATT_HOST_DEVICE
0064 VECGEOM_FORCE_INLINE
0065 Type y() const;
0066
0067 VECCORE_ATT_HOST_DEVICE
0068 VECGEOM_FORCE_INLINE
0069 void Set(const Type x, const Type y);
0070
0071 VECCORE_ATT_HOST_DEVICE
0072 VECGEOM_FORCE_INLINE
0073 Type Cross(VecType const &other) const { return vec[0] * other.vec[1] - vec[1] * other.vec[0]; }
0074
0075 #define VECTOR2D_TEMPLATE_INPLACE_BINARY_OP(OPERATOR) \
0076 VECCORE_ATT_HOST_DEVICE \
0077 VECGEOM_FORCE_INLINE \
0078 VecType &operator OPERATOR(const VecType &other) \
0079 { \
0080 vec[0] OPERATOR other.vec[0]; \
0081 vec[1] OPERATOR other.vec[1]; \
0082 return *this; \
0083 } \
0084 VECCORE_ATT_HOST_DEVICE \
0085 VECGEOM_FORCE_INLINE \
0086 VecType &operator OPERATOR(const Type &scalar) \
0087 { \
0088 vec[0] OPERATOR scalar; \
0089 vec[1] OPERATOR scalar; \
0090 return *this; \
0091 }
0092 VECTOR2D_TEMPLATE_INPLACE_BINARY_OP(+=)
0093 VECTOR2D_TEMPLATE_INPLACE_BINARY_OP(-=)
0094 VECTOR2D_TEMPLATE_INPLACE_BINARY_OP(*=)
0095 VECTOR2D_TEMPLATE_INPLACE_BINARY_OP(/=)
0096 #undef VECTOR2D_TEMPLATE_INPLACE_BINARY_OP
0097 };
0098
0099 template <typename Type>
0100 VECCORE_ATT_HOST_DEVICE
0101 Vector2D<Type>::Vector2D()
0102 {
0103 vec[0] = 0;
0104 vec[1] = 0;
0105 }
0106
0107 template <typename Type>
0108 VECCORE_ATT_HOST_DEVICE
0109 Vector2D<Type>::Vector2D(const Type x, const Type y)
0110 {
0111 vec[0] = x;
0112 vec[1] = y;
0113 }
0114
0115 template <typename Type>
0116 VECCORE_ATT_HOST_DEVICE
0117 Vector2D<Type>::Vector2D(Vector2D const &other)
0118 {
0119 vec[0] = other.vec[0];
0120 vec[1] = other.vec[1];
0121 }
0122
0123 template <typename Type>
0124 VECCORE_ATT_HOST_DEVICE
0125 Vector2D<Type> Vector2D<Type>::operator=(Vector2D<Type> const &other)
0126 {
0127 vec[0] = other.vec[0];
0128 vec[1] = other.vec[1];
0129 return *this;
0130 }
0131
0132 template <typename Type>
0133 VECCORE_ATT_HOST_DEVICE
0134 Type &Vector2D<Type>::operator[](const int index)
0135 {
0136 return vec[index];
0137 }
0138
0139 template <typename Type>
0140 VECCORE_ATT_HOST_DEVICE
0141 Type Vector2D<Type>::operator[](const int index) const
0142 {
0143 return vec[index];
0144 }
0145
0146 template <typename Type>
0147 VECCORE_ATT_HOST_DEVICE
0148 Type &Vector2D<Type>::x()
0149 {
0150 return vec[0];
0151 }
0152
0153 template <typename Type>
0154 VECCORE_ATT_HOST_DEVICE
0155 Type Vector2D<Type>::x() const
0156 {
0157 return vec[0];
0158 }
0159
0160 template <typename Type>
0161 VECCORE_ATT_HOST_DEVICE
0162 Type &Vector2D<Type>::y()
0163 {
0164 return vec[1];
0165 }
0166
0167 template <typename Type>
0168 VECCORE_ATT_HOST_DEVICE
0169 Type Vector2D<Type>::y() const
0170 {
0171 return vec[1];
0172 }
0173
0174 template <typename Type>
0175 VECCORE_ATT_HOST_DEVICE
0176 void Vector2D<Type>::Set(const Type x, const Type y)
0177 {
0178 vec[0] = x;
0179 vec[1] = y;
0180 }
0181
0182 template <typename Type>
0183 std::ostream &operator<<(std::ostream &os, Vector2D<Type> const &vec)
0184 {
0185 os << "(" << vec[0] << ", " << vec[1] << ")";
0186 return os;
0187 }
0188
0189 #define VECTOR2D_BINARY_OP(OPERATOR, INPLACE) \
0190 template <typename Type> \
0191 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Vector2D<Type> operator OPERATOR(const Vector2D<Type> &lhs, \
0192 const Vector2D<Type> &rhs) \
0193 { \
0194 Vector2D<Type> result(lhs); \
0195 result INPLACE rhs; \
0196 return result; \
0197 } \
0198 template <typename Type, typename ScalarType> \
0199 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Vector2D<Type> operator OPERATOR(Vector2D<Type> const &lhs, \
0200 const ScalarType rhs) \
0201 { \
0202 Vector2D<Type> result(lhs); \
0203 result INPLACE rhs; \
0204 return result; \
0205 } \
0206 template <typename Type, typename ScalarType> \
0207 VECGEOM_FORCE_INLINE VECCORE_ATT_HOST_DEVICE Vector2D<Type> operator OPERATOR(const ScalarType rhs, \
0208 Vector2D<Type> const &lhs) \
0209 { \
0210 Vector2D<Type> result(rhs); \
0211 result INPLACE lhs; \
0212 return result; \
0213 }
0214 VECTOR2D_BINARY_OP(+, +=)
0215 VECTOR2D_BINARY_OP(-, -=)
0216 VECTOR2D_BINARY_OP(*, *=)
0217 VECTOR2D_BINARY_OP(/, /=)
0218 #undef VECTOR2D_BINARY_OP
0219
0220 }
0221 }
0222
0223 #endif