BasicVector.h
Go to the documentation of this file.
1 #ifndef _utl_BasicVector_h_
2 #define _utl_BasicVector_h_
3 
12 #include <CLHEP/Geometry/Normal3D.h>
13 #include <CLHEP/Geometry/Point3D.h>
14 #include <CLHEP/Geometry/Vector3D.h>
15 #include <boost/tuple/tuple.hpp>
16 #include <utl/CoordinateSystem.h>
17 #include <utl/unconfig.h>
18 #include <utl/config.h>
19 #include <utl/GeometryException.h>
20 #include <utl/ConsecutiveEnumFactory.h>
21 #include <utl/ThrowPolicy.h>
22 
23 
24 namespace utl {
25 
41  template<class HepVector>
42  class BasicVector {
43 
44  public:
45  typedef HepVector DataType;
46 
49  public:
50  virtual ~CoordinateType() = default;
51  virtual double X(double p1, double p2, double p3) const = 0;
52  virtual double Y(double p1, double p2, double p3) const = 0;
53  virtual double Z(double p1, double p2, double p3) const = 0;
55 
62  enum Kind {
66  };
71  static const char* const fgKindTags[3];
73  typedef
75  Kind,
76  eSpherical,
77  fgKindTags,
81  static
82  const CoordinateType&
83  Create(const Kind k)
84  {
85  switch (k) {
89  }
90  // This shouldn't happen if the switch is kept in sync with the enum;
91  // despite that is put here in order to avoid a compiler warning due
92  // to the (supposedly) missing return at the end of the function
93  // (which is unreacheable under the former condition).
94  std::ostringstream e;
95  e << "No conversion from enum value " << k << " to utl::BasicVector<HepVector>::CoordinateType";
96  throw CoordinateSystemException(e.str());
97  }
98  };
99 
100  protected:
102 
116  class CCartesian : public CoordinateType {
117  public:
118  virtual double X(double x, double /*y*/, double /*z*/) const override { return x; }
119  virtual double Y(double /*x*/, double y, double /*z*/) const override { return y; }
120  virtual double Z(double /*x*/, double /*y*/, double z) const override { return z; }
121  };
122 
124 
127  class CCylindrical : public CoordinateType {
128  public:
129  virtual double X(double rho, double phi, double z) const override;
130  virtual double Y(double rho, double phi, double z) const override;
131  virtual double Z(double rho, double phi, double z) const override;
132  };
133 
135 
138  class CSpherical : public CoordinateType {
139  public:
140  virtual double X(double r, double theta, double phi) const override;
141  virtual double Y(double r, double theta, double phi) const override;
142  virtual double Z(double r, double theta, double phi) const override;
143  };
144 
145  public:
147  typedef typename boost::tuple<double, double, double> Triple;
148 
149  protected:
151 
162  BasicVector(const double x, const double y, const double z,
163  const CoordinateSystemPtr& coordinateSystem) :
164  fCoordinateSystem(coordinateSystem),
165  fVector(x, y, z)
166  {
167  CoordinateSystemValid(coordinateSystem);
168  }
169 
171 
190  BasicVector(double p1, double p2, double p3,
191  const CoordinateSystemPtr& coordinateSystem,
192  const CoordinateType& t) :
193  fCoordinateSystem(coordinateSystem),
194  fVector(t.X(p1, p2, p3), t.Y(p1, p2, p3), t.Z(p1, p2, p3))
195  {
196  CoordinateSystemValid(coordinateSystem);
197  }
198 
199  public:
200  bool operator==(const BasicVector& v) const
201  { v.TransformTo(fCoordinateSystem); return this->fVector == v.fVector; }
202 
203  bool operator!=(const BasicVector& v) const
204  { return !(*this == v); }
205 
206  double GetX(const CoordinateSystemPtr& coordinateSystem) const
207  { TransformTo(coordinateSystem); return fVector.x(); }
208 
209  double GetY(const CoordinateSystemPtr& coordinateSystem) const
210  { TransformTo(coordinateSystem); return fVector.y(); }
211 
212  double GetZ(const CoordinateSystemPtr& coordinateSystem) const
213  { TransformTo(coordinateSystem); return fVector.z(); }
214 
216  Triple
217  GetCoordinates(const CoordinateSystemPtr& coordinateSystem)
218  const
219  {
220  TransformTo(coordinateSystem);
221  return boost::make_tuple(fVector.x(), fVector.y(), fVector.z());
222  }
223 
235  { return fCoordinateSystem; }
236 
237  // universally applicable operations
238 
239  // Assignment: rely on compiler to construct the correct operator
240 
242  BasicVector& operator*=(const double a) { fVector *= a; return *this; }
243 
245  BasicVector& operator/=(const double a) { fVector /= a; return *this; }
246 
248  double GetTheta(const CoordinateSystemPtr& coordinateSystem) const
249  { TransformTo(coordinateSystem); return fVector.theta(); }
251  double GetCosTheta(const CoordinateSystemPtr& coordinateSystem) const
252  { TransformTo(coordinateSystem); return fVector.cosTheta(); }
254  double GetPhi(const CoordinateSystemPtr& coordinateSystem) const
255  { TransformTo(coordinateSystem); return fVector.phi(); }
257  double GetR(const CoordinateSystemPtr& coordinateSystem) const
258  { TransformTo(coordinateSystem); return fVector.mag(); }
260  double GetR2(const CoordinateSystemPtr& coordinateSystem) const
261  { TransformTo(coordinateSystem); return fVector.mag2(); }
263  double GetRho(const CoordinateSystemPtr& coordinateSystem) const
264  { TransformTo(coordinateSystem); return fVector.perp(); }
266  double GetRho2(const CoordinateSystemPtr& coordinateSystem) const
267  { TransformTo(coordinateSystem); return fVector.perp2(); }
268 
270  Triple
271  GetSphericalCoordinates(const CoordinateSystemPtr& coordinateSystem)
272  const
273  {
274  TransformTo(coordinateSystem);
275  return boost::make_tuple(fVector.r(), fVector.theta(), fVector.phi());
276  }
277 
279  Triple
280  GetCylindricalCoordinates(const CoordinateSystemPtr& coordinateSystem)
281  const
282  {
283  TransformTo(coordinateSystem);
284  return boost::make_tuple(fVector.perp(), fVector.phi(), fVector.z());
285  }
286 
288 
298  bool
299  IsCloseTo(const BasicVector& v, const double eps = fgEpsilon)
300  const
301  {
302  v.TransformTo(fCoordinateSystem);
303  const double d = (fVector - v.fVector).mag2();
304  if (d < eps*eps)
305  return true;
306  else
307  return d / (fVector.mag2() + v.fVector.mag2()) < eps*eps;
308  }
309 
316  void
317  TransformTo(const CoordinateSystemPtr& newCoordinateSystem)
318  const
319  {
320  if (!SameCoordinateSystem(newCoordinateSystem)) {
321  fVector = TransformedVector(newCoordinateSystem);
322  fCoordinateSystem = newCoordinateSystem;
323  }
324  }
325 
326  template<class V>
327  bool SameCoordinateSystem(const BasicVector<V>& v) const
328  { return fCoordinateSystem == v.fCoordinateSystem; }
329 
330  bool SameCoordinateSystem(const CoordinateSystemPtr& coordinateSystem) const
331  { return fCoordinateSystem == coordinateSystem; }
332 
333  static const CCartesian kCartesian;
335  static const CSpherical kSpherical;
336 
337  protected:
339  ~BasicVector() = default;
340 
342  fCoordinateSystem(CoordinateSystem::GetRootCoordinateSystem()),
343  fVector(0, 0, 0)
344  { }
345 
348  const CoordinateSystemPtr& coordinateSystem) :
349  fCoordinateSystem(coordinateSystem),
350  fVector(v)
351  { }
352 
354  void Normalize() { fVector = fVector.unit(); }
355 
356  DataType
357  TransformedVector(const CoordinateSystemPtr& newCoordinateSystem) const;
358 
360  mutable DataType fVector;
361 
362  static const double fgEpsilon;
363 
364  };
365 
366 
370 
371  extern template class BasicVector<HepGeom::Point3D<double>>;
372  extern template class BasicVector<HepGeom::Vector3D<double>>;
373  extern template class BasicVector<HepGeom::Normal3D<double>>;
374 
375 }
376 
377 
378 #endif
Simple factory to create an enumerator for a given enumeration.
virtual double X(double rho, double phi, double z) const override
Definition: BasicVector.cc:34
virtual double Z(double p1, double p2, double p3) const =0
virtual ~CoordinateType()=default
BasicVector< HepGeom::Point3D< double > > PointBase
Definition: BasicVector.h:368
HepVector DataType
Definition: BasicVector.h:45
BasicVector(const double x, const double y, const double z, const CoordinateSystemPtr &coordinateSystem)
Construct a point from cartesian coordinates.
Definition: BasicVector.h:162
BasicVector< HepGeom::Vector3D< double > > VectorBase
Definition: BasicVector.h:369
double GetPhi(const CoordinateSystemPtr &coordinateSystem) const
azimuth (phi) angle in spherical and cylindrical coordinates
Definition: BasicVector.h:254
boost::tuple< double, double, double > Triple
Coordinate triple for easy getting or setting of coordinates.
Definition: BasicVector.h:147
double GetR(const CoordinateSystemPtr &coordinateSystem) const
radius r in spherical coordinates coordinates (distance to origin)
Definition: BasicVector.h:257
double GetTheta(const CoordinateSystemPtr &coordinateSystem) const
zenith (theta) angle in spherical coordinates
Definition: BasicVector.h:248
void Normalize()
normalize the vector
Definition: BasicVector.h:354
CoordinateSystemPtr GetCoordinateSystem() const
Get the coordinate system of the current internal representation.
Definition: BasicVector.h:234
BasicVector & operator*=(const double a)
Multiply by number.
Definition: BasicVector.h:242
double GetCosTheta(const CoordinateSystemPtr &coordinateSystem) const
cos of zenith (theta) angle
Definition: BasicVector.h:251
static const double fgEpsilon
Definition: BasicVector.h:362
~BasicVector()=default
Destructor - NOT virtual since class is NEVER used polymorphically.
Constructors for Transformer classes.
virtual double Z(double rho, double phi, double z) const override
Definition: BasicVector.cc:56
virtual double Z(double, double, double z) const override
Definition: BasicVector.h:120
bool operator!=(const BasicVector &v) const
Definition: BasicVector.h:203
boost::shared_ptr< const CoordinateTransformer > CoordinateSystemPtr
Shared pointer for coordinate systems.
boost::tuple< double, double, double > Triple
Coordinate triple for easy getting or setting of coordinates.
Definition: Triple.h:15
Kind
Coordinate type integral typification.
Definition: BasicVector.h:62
Basic vector class template for geometry.
Definition: BasicVector.h:42
DataType TransformedVector(const CoordinateSystemPtr &newCoordinateSystem) const
Definition: BasicVector.cc:24
double GetX(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:206
DataType fVector
Definition: BasicVector.h:360
static const CCylindrical kCylindrical
Definition: BasicVector.h:334
virtual double Y(double, double y, double) const override
Definition: BasicVector.h:119
static const CoordinateType & Create(const Kind k)
Factory method via enumeration.
Definition: BasicVector.h:83
static const CSpherical kSpherical
Definition: BasicVector.h:335
double eps
double GetR2(const CoordinateSystemPtr &coordinateSystem) const
radius r^2 in spherical coordinates coordinates (distance to origin)^2
Definition: BasicVector.h:260
double GetRho(const CoordinateSystemPtr &coordinateSystem) const
radius r in cylindrical coordinates (distance to z axis)
Definition: BasicVector.h:263
BasicVector(const DataType &v, const CoordinateSystemPtr &coordinateSystem)
Constructor from internal components for use by operators.
Definition: BasicVector.h:347
virtual double Z(double r, double theta, double phi) const override
Definition: BasicVector.cc:89
Simply throws.
Definition: ThrowPolicy.h:27
utl::ConsecutiveEnumFactory< Kind, eSpherical, fgKindTags, utl::ThrowPolicy< Kind, CoordinateSystemException > > KindCreator
Convenience typedef for creation of Kind enumerators.
Definition: BasicVector.h:79
double GetY(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:209
Class to have a type for indicating cylindrical coordinate components.
Definition: BasicVector.h:127
return boost::make_tuple(fVector.x(), fVector.y(), fVector.z())
CoordinateSystemPtr fCoordinateSystem
Definition: BasicVector.h:359
BasicVector< HepGeom::Normal3D< double > > AxialVectorBase
Definition: BasicVector.h:367
static const char *const fgKindTags[3]
Definition: BasicVector.h:71
Base class for classes indicating coordinate types.
Definition: BasicVector.h:48
double GetRho2(const CoordinateSystemPtr &coordinateSystem) const
radius r^2 in cylindrical coordinates (distance to z axis)^2
Definition: BasicVector.h:266
virtual double X(double p1, double p2, double p3) const =0
bool operator==(const BasicVector &v) const
Definition: BasicVector.h:200
Exception dealing with coordinate systems.
virtual double X(double x, double, double) const override
Definition: BasicVector.h:118
Class to have a type for indicating spherical coordinate components.
Definition: BasicVector.h:138
void CoordinateSystemValid(const CoordinateSystemPtr &theCoordinateSystem)
double GetZ(const CoordinateSystemPtr &coordinateSystem) const
Definition: BasicVector.h:212
BasicVector & operator/=(const double a)
Divide by number.
Definition: BasicVector.h:245
virtual double Y(double rho, double phi, double z) const override
Definition: BasicVector.cc:45
virtual double X(double r, double theta, double phi) const override
Definition: BasicVector.cc:67
virtual double Y(double r, double theta, double phi) const override
Definition: BasicVector.cc:78
virtual double Y(double p1, double p2, double p3) const =0
BasicVector(double p1, double p2, double p3, const CoordinateSystemPtr &coordinateSystem, const CoordinateType &t)
Construct a point from coordinates in arbitrary representation.
Definition: BasicVector.h:190
const double d
Definition: BasicVector.h:303
Class to have a type for indicating cartesian coordinate components.
Definition: BasicVector.h:116

, generated on Tue Sep 26 2023.