File indexing completed on 2023-10-25 09:39:05
0001 #ifndef GeometryVector_newBasic3DVector_h
0002 #define GeometryVector_newBasic3DVector_h
0003
0004 #include "DataFormats/GeometryVector/interface/Basic2DVector.h"
0005 #include "DataFormats/GeometryVector/interface/Theta.h"
0006 #include "DataFormats/GeometryVector/interface/Phi.h"
0007 #include "DataFormats/GeometryVector/interface/PreciseFloatType.h"
0008 #include "DataFormats/GeometryVector/interface/CoordinateSets.h"
0009 #include "DataFormats/Math/interface/SSEVec.h"
0010 #include <iosfwd>
0011 #include <cmath>
0012
0013 namespace detailsBasic3DVector {
0014 inline float __attribute__((always_inline)) __attribute__((pure)) eta(float x, float y, float z) {
0015 float t(z / std::sqrt(x * x + y * y));
0016 return ::asinhf(t);
0017 }
0018 inline double __attribute__((always_inline)) __attribute__((pure)) eta(double x, double y, double z) {
0019 double t(z / std::sqrt(x * x + y * y));
0020 return ::asinh(t);
0021 }
0022 inline long double __attribute__((always_inline)) __attribute__((pure))
0023 eta(long double x, long double y, long double z) {
0024 long double t(z / std::sqrt(x * x + y * y));
0025 return ::asinhl(t);
0026 }
0027 }
0028
0029 template <typename T>
0030 class Basic3DVector {
0031 public:
0032 typedef T ScalarType;
0033 typedef mathSSE::Vec4<T> VectorType;
0034 typedef mathSSE::Vec4<T> MathVector;
0035 typedef Geom::Cylindrical2Cartesian<T> Cylindrical;
0036 typedef Geom::Spherical2Cartesian<T> Spherical;
0037 typedef Spherical Polar;
0038
0039
0040
0041
0042
0043 Basic3DVector() {}
0044
0045
0046 Basic3DVector(const Basic3DVector& p) : v(p.v) {}
0047
0048
0049 template <class U>
0050 Basic3DVector(const Basic3DVector<U>& p) : v(p.v) {}
0051
0052
0053 Basic3DVector(const Basic2DVector<T>& p) : v(p.x(), p.y(), 0) {}
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 template <class OtherPoint>
0064 explicit Basic3DVector(const OtherPoint& p) : v(p.x(), p.y(), p.z()) {}
0065
0066
0067 template <class U>
0068 Basic3DVector(mathSSE::Vec4<U> const& iv) : v(iv) {}
0069
0070
0071 Basic3DVector(const T& x, const T& y, const T& z, const T& w = 0) : v(x, y, z, w) {}
0072
0073
0074
0075
0076
0077 template <typename U>
0078 Basic3DVector(const Geom::Theta<U>& theta, const Geom::Phi<U>& phi, const T& r) {
0079 Polar p(theta.value(), phi.value(), r);
0080 v.o.theX = p.x();
0081 v.o.theY = p.y();
0082 v.o.theZ = p.z();
0083 }
0084
0085 MathVector const& mathVector() const { return v; }
0086 MathVector& mathVector() { return v; }
0087
0088 T operator[](int i) const { return v[i]; }
0089 T& operator[](int i) { return v[i]; }
0090
0091
0092 T x() const { return v.o.theX; }
0093
0094
0095 T y() const { return v.o.theY; }
0096
0097
0098 T z() const { return v.o.theZ; }
0099
0100 T w() const { return v.o.theW; }
0101
0102 Basic2DVector<T> xy() const { return v.xy(); }
0103
0104
0105 bool operator==(const Basic3DVector& rh) const { return v == rh.v; }
0106
0107
0108 T mag2() const { return ::dot(v, v); }
0109
0110
0111 T mag() const { return std::sqrt(mag2()); }
0112
0113
0114 T perp2() const { return ::dotxy(v, v); }
0115
0116
0117 T perp() const { return std::sqrt(perp2()); }
0118
0119
0120 T transverse() const { return perp(); }
0121
0122
0123
0124
0125
0126 T barePhi() const { return std::atan2(y(), x()); }
0127 Geom::Phi<T> phi() const { return Geom::Phi<T>(barePhi()); }
0128
0129
0130
0131
0132
0133 T bareTheta() const { return std::atan2(perp(), z()); }
0134 Geom::Theta<T> theta() const { return Geom::Theta<T>(std::atan2(perp(), z())); }
0135
0136
0137
0138
0139
0140
0141 T eta() const { return detailsBasic3DVector::eta(x(), y(), z()); }
0142
0143
0144
0145
0146 Basic3DVector unit() const {
0147 T my_mag = mag2();
0148 return (0 != my_mag) ? (*this) * (T(1) / std::sqrt(my_mag)) : *this;
0149 }
0150
0151
0152
0153 template <class U>
0154 Basic3DVector& operator+=(const Basic3DVector<U>& p) {
0155 v = v + p.v;
0156 return *this;
0157 }
0158
0159
0160
0161 template <class U>
0162 Basic3DVector& operator-=(const Basic3DVector<U>& p) {
0163 v = v - p.v;
0164 return *this;
0165 }
0166
0167
0168 Basic3DVector operator-() const { return Basic3DVector(-v); }
0169
0170
0171 Basic3DVector& operator*=(T t) {
0172 v = t * v;
0173 return *this;
0174 }
0175
0176
0177 Basic3DVector& operator/=(T t) {
0178
0179 v = v / t;
0180 return *this;
0181 }
0182
0183
0184 T dot(const Basic3DVector& rh) const { return ::dot(v, rh.v); }
0185
0186
0187
0188
0189
0190
0191 template <class U>
0192 typename PreciseFloatType<T, U>::Type dot(const Basic3DVector<U>& lh) const {
0193 return Basic3DVector<typename PreciseFloatType<T, U>::Type>(*this).dot(
0194 Basic3DVector<typename PreciseFloatType<T, U>::Type>(lh));
0195 }
0196
0197
0198 Basic3DVector cross(const Basic3DVector& lh) const { return ::cross(v, lh.v); }
0199
0200
0201
0202
0203
0204
0205 template <class U>
0206 Basic3DVector<typename PreciseFloatType<T, U>::Type> cross(const Basic3DVector<U>& lh) const {
0207 return Basic3DVector<typename PreciseFloatType<T, U>::Type>(*this).cross(
0208 Basic3DVector<typename PreciseFloatType<T, U>::Type>(lh));
0209 }
0210
0211 public:
0212 mathSSE::Vec4<T> v;
0213 } __attribute__((aligned(16)));
0214
0215 namespace geometryDetails {
0216 std::ostream& print3D(std::ostream& s, double x, double y, double z);
0217 }
0218
0219
0220 template <class T>
0221 inline std::ostream& operator<<(std::ostream& s, const Basic3DVector<T>& v) {
0222 return geometryDetails::print3D(s, v.x(), v.y(), v.z());
0223 }
0224
0225
0226 template <class T>
0227 inline Basic3DVector<T> operator+(const Basic3DVector<T>& a, const Basic3DVector<T>& b) {
0228 return a.v + b.v;
0229 }
0230 template <class T>
0231 inline Basic3DVector<T> operator-(const Basic3DVector<T>& a, const Basic3DVector<T>& b) {
0232 return a.v - b.v;
0233 }
0234
0235 template <class T, class U>
0236 inline Basic3DVector<typename PreciseFloatType<T, U>::Type> operator+(const Basic3DVector<T>& a,
0237 const Basic3DVector<U>& b) {
0238 typedef Basic3DVector<typename PreciseFloatType<T, U>::Type> RT;
0239 return RT(a).v + RT(b).v;
0240 }
0241
0242 template <class T, class U>
0243 inline Basic3DVector<typename PreciseFloatType<T, U>::Type> operator-(const Basic3DVector<T>& a,
0244 const Basic3DVector<U>& b) {
0245 typedef Basic3DVector<typename PreciseFloatType<T, U>::Type> RT;
0246 return RT(a).v - RT(b).v;
0247 }
0248
0249
0250 template <class T>
0251 inline T operator*(const Basic3DVector<T>& v1, const Basic3DVector<T>& v2) {
0252 return v1.dot(v2);
0253 }
0254
0255
0256 template <class T, class U>
0257 inline typename PreciseFloatType<T, U>::Type operator*(const Basic3DVector<T>& v1, const Basic3DVector<U>& v2) {
0258 return v1.dot(v2);
0259 }
0260
0261
0262
0263
0264 template <class T>
0265 inline Basic3DVector<T> operator*(const Basic3DVector<T>& v, T t) {
0266 return v.v * t;
0267 }
0268
0269
0270 template <class T>
0271 inline Basic3DVector<T> operator*(T t, const Basic3DVector<T>& v) {
0272 return v.v * t;
0273 }
0274
0275 template <class T, typename S>
0276 inline Basic3DVector<T> operator*(S t, const Basic3DVector<T>& v) {
0277 return static_cast<T>(t) * v;
0278 }
0279
0280 template <class T, typename S>
0281 inline Basic3DVector<T> operator*(const Basic3DVector<T>& v, S t) {
0282 return static_cast<T>(t) * v;
0283 }
0284
0285
0286
0287
0288 template <class T>
0289 inline Basic3DVector<T> operator/(const Basic3DVector<T>& v, T t) {
0290 return v.v / t;
0291 }
0292
0293 template <class T, typename S>
0294 inline Basic3DVector<T> operator/(const Basic3DVector<T>& v, S s) {
0295
0296 T t = s;
0297 return v / t;
0298 }
0299
0300 typedef Basic3DVector<float> Basic3DVectorF;
0301 typedef Basic3DVector<double> Basic3DVectorD;
0302
0303
0304 #include "Basic3DVectorLD.h"
0305
0306 #endif