Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
Translation3D.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: W. Brown, M. Fischler, L. Moneta 2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 , LCG ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class Translation3D
12 //
13 // Created by: Lorenzo Moneta October 21 2005
14 //
15 //
16 #ifndef ROOT_Math_GenVector_Translation3D
17 #define ROOT_Math_GenVector_Translation3D 1
18 
19 
21 
22 #include "Math/GenVector/Plane3D.h"
23 
25 
27 
28 #include <iostream>
29 #include <type_traits>
30 
31 namespace ROOT {
32 
33 namespace Math {
34 
35 namespace Impl {
36 
37 //____________________________________________________________________________________________________
38 /**
39  Class describing a 3 dimensional translation. It can be combined (using the operator *)
40  with the ROOT::Math::Rotation3D classes and ROOT::Math::Transform3D to obtained combined
41  transformations and to operate on points and vectors.
42  Note that a the translation applied to a Vector object (DisplacementVector3D and LorentzVector classes)
43  performes a noop, i.e. it returns the same vector. A translation can be applied only to the Point objects
44  (PositionVector3D classes).
45 
46  @ingroup GenVector
47 
48 */
49 
50 template <typename T = double>
51 class Translation3D {
52 
53 public:
54  typedef T Scalar;
55 
56  typedef DisplacementVector3D<Cartesian3D<T>, DefaultCoordinateSystemTag> Vector;
57 
58  /**
59  Default constructor ( zero translation )
60  */
61  Translation3D() {}
62 
63  /**
64  Construct given a pair of pointers or iterators defining the
65  beginning and end of an array of 3 Scalars representing the z,y,z of the translation vector
66  */
67  template<class IT>
68  Translation3D(IT begin, IT end)
69  {
70  fVect.SetCoordinates(begin,end);
71  }
72 
73  /**
74  Construct from x,y,z values representing the translation
75  */
76  Translation3D(T dx, T dy, T dz) : fVect(Vector(dx, dy, dz)) {}
77 
78  /**
79  Construct from any Displacement vector in ant tag and coordinate system
80  */
81  template<class CoordSystem, class Tag>
82  explicit Translation3D( const DisplacementVector3D<CoordSystem,Tag> & v) :
83  fVect(Vector(v.X(),v.Y(),v.Z()))
84  { }
85 
86 
87  /**
88  Construct transformation from one coordinate system defined one point (the origin)
89  to a new coordinate system defined by other point (origin )
90  @param p1 point defining origin of original reference system
91  @param p2 point defining origin of transformed reference system
92 
93  */
94  template <class CoordSystem, class Tag>
95  Translation3D(const PositionVector3D<CoordSystem, Tag> &p1, const PositionVector3D<CoordSystem, Tag> &p2)
96  : fVect(p2 - p1)
97  { }
98 
99 
100  // use compiler generated copy ctor, copy assignmet and dtor
101 
102 
103  // ======== Components ==============
104 
105  /**
106  return a const reference to the underline vector representing the translation
107  */
108  const Vector & Vect() const { return fVect; }
109 
110  /**
111  Set the 3 components given an iterator to the start of
112  the desired data, and another to the end (3 past start).
113  */
114  template<class IT>
115  void SetComponents(IT begin, IT end) {
116  fVect.SetCoordinates(begin,end);
117  }
118 
119  /**
120  Get the 3 components into data specified by an iterator begin
121  and another to the end of the desired data (12 past start).
122  */
123  template<class IT>
124  void GetComponents(IT begin, IT end) const {
125  fVect.GetCoordinates(begin,end);
126  }
127 
128  /**
129  Get the 3 matrix components into data specified by an iterator begin
130  */
131  template<class IT>
132  void GetComponents(IT begin) const {
133  fVect.GetCoordinates(begin);
134  }
135 
136 
137  /**
138  Set the components from 3 scalars
139  */
140  void SetComponents(T dx, T dy, T dz) { fVect.SetCoordinates(dx, dy, dz); }
141 
142  /**
143  Get the components into 3 scalars
144  */
145  void GetComponents(T &dx, T &dy, T &dz) const { fVect.GetCoordinates(dx, dy, dz); }
146 
147  /**
148  Set the XYZ vector components from 3 scalars
149  */
150  void SetXYZ(T dx, T dy, T dz) { fVect.SetXYZ(dx, dy, dz); }
151 
152  // operations on points and vectors
153 
154 
155  /**
156  Transformation operation for Position Vector in any coordinate system and default tag
157  */
158  template<class CoordSystem, class Tag >
159  PositionVector3D<CoordSystem,Tag> operator() (const PositionVector3D <CoordSystem,Tag> & p) const {
160  return PositionVector3D<CoordSystem, Tag>(p.X() + fVect.X(), p.Y() + fVect.Y(), p.Z() + fVect.Z());
161  }
162  /**
163  Transformation operation
164  */
165  template <class CoordSystem, class Tag>
166  PositionVector3D<CoordSystem, Tag> operator*(const PositionVector3D<CoordSystem, Tag> &v) const
167  {
168  return operator()(v);
169  }
170 
171  /**
172  Transformation operation for Displacement Vector in any coordinate system and default tag
173  For the Displacement Vectors no translation apply so return the vector itself
174  */
175  template<class CoordSystem, class Tag >
176  DisplacementVector3D<CoordSystem,Tag> operator() (const DisplacementVector3D <CoordSystem,Tag> & v) const {
177  return v;
178  }
179  /**
180  Transformation operation
181  */
182  template <class CoordSystem, class Tag>
183  DisplacementVector3D<CoordSystem, Tag> operator*(const DisplacementVector3D<CoordSystem, Tag> &v) const
184  {
185  return operator()(v);
186  }
187 
188  /**
189  Transformation operation for points between different coordinate system tags
190  */
191  template<class CoordSystem, class Tag1, class Tag2 >
192  void Transform (const PositionVector3D <CoordSystem,Tag1> & p1, PositionVector3D <CoordSystem,Tag2> & p2 ) const {
193  PositionVector3D <CoordSystem,Tag2> tmp;
194  tmp.SetXYZ( p1.X(), p1.Y(), p1.Z() );
195  p2 = operator()(tmp);
196  }
197 
198  /**
199  Transformation operation for Displacement Vector of different coordinate systems
200  */
201  template <class CoordSystem, class Tag1, class Tag2>
202  void Transform(const DisplacementVector3D<CoordSystem, Tag1> &v1, DisplacementVector3D<CoordSystem, Tag2> &v2) const
203  {
204  // just copy v1 in v2
205  v2.SetXYZ(v1.X(), v1.Y(), v1.Z());
206  }
207 
208  /**
209  Transformation operation for a Lorentz Vector in any coordinate system
210  A LorentzVector contains a displacement vector so no translation applies as well
211  */
212  template <class CoordSystem>
213  LorentzVector<CoordSystem> operator()(const LorentzVector<CoordSystem> &q) const
214  {
215  return q;
216  }
217  /**
218  Transformation operation
219  */
220  template <class CoordSystem>
221  LorentzVector<CoordSystem> operator*(const LorentzVector<CoordSystem> &q) const
222  {
223  return operator()(q);
224  }
225 
226  /**
227  Transformation on a 3D plane
228  */
229  Plane3D<T> operator()(const Plane3D<T> &plane) const
230  {
231  // transformations on a 3D plane
232  const Vector n = plane.Normal();
233  // take a point on the plane. Use origin projection on the plane
234  // ( -ad, -bd, -cd) if (a**2 + b**2 + c**2 ) = 1
235  const T d = plane.HesseDistance();
236  PositionVector3D<Cartesian3D<T>> p(-d * n.X(), -d * n.Y(), -d * n.Z());
237  return PLANE(operator()(n), operator()(p));
238  }
239 
240  /**
241  multiply (combine) with another transformation in place
242  */
243  Translation3D<T> &operator*=(const Translation3D<T> &t)
244  {
245  fVect+= t.Vect();
246  return *this;
247  }
248 
249  /**
250  multiply (combine) two transformations
251  */
252  Translation3D<T> operator*(const Translation3D<T> &t) const { return Translation3D<T>(fVect + t.Vect()); }
253 
254  /**
255  Invert the transformation in place
256  */
257  void Invert() {
258  SetComponents( -fVect.X(), -fVect.Y(),-fVect.Z() );
259  }
260 
261  /**
262  Return the inverse of the transformation.
263  */
264  Translation3D<T> Inverse() const { return Translation3D<T>(-fVect.X(), -fVect.Y(), -fVect.Z()); }
265 
266  /**
267  Equality/inequality operators
268  */
269  bool operator==(const Translation3D<T> &rhs) const
270  {
271  if( fVect != rhs.fVect ) return false;
272  return true;
273  }
274 
275  bool operator!=(const Translation3D<T> &rhs) const { return !operator==(rhs); }
276 
277 private:
278 
279  Vector fVect; // internal 3D vector representing the translation
280 
281 };
282 
283 
284 
285 
286 
287 // global functions
288 
289 // TODO - I/O should be put in the manipulator form
290 
291 template <class T>
292 std::ostream &operator<<(std::ostream &os, const Translation3D<T> &t)
293 {
294  // TODO - this will need changing for machine-readable issues
295  // and even the human readable form needs formatiing improvements
296 
297  T m[3];
298  t.GetComponents(m, m + 3);
299  return os << "\n" << m[0] << " " << m[1] << " " << m[2] << "\n";
300 }
301 
302 // need a function Transform = Translation * Rotation ???
303 
304 } // end namespace Impl
305 
306 // typedefs for double and float versions
307 typedef Impl::Translation3D<double> Translation3D;
308 typedef Impl::Translation3D<float> Translation3DF;
309 
310 } // end namespace Math
311 
312 } // end namespace ROOT
313 
314 
315 #endif /* ROOT_Math_GenVector_Translation3D */