Logo ROOT   6.10/09
Reference Guide
Plane3D.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: L. Moneta 12/2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 , LCG ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class LorentzVector
12 //
13 // Created by: moneta at Fri Dec 02 2005
14 //
15 // Last update: $Id$
16 //
17 #ifndef ROOT_Math_GenVector_Plane3D
18 #define ROOT_Math_GenVector_Plane3D 1
19 
20 #include <type_traits>
21 
24 
25 
26 
27 namespace ROOT {
28 
29 namespace Math {
30 
31 namespace Impl {
32 
33 //_______________________________________________________________________________
34 /**
35  Class describing a geometrical plane in 3 dimensions.
36  A Plane3D is a 2 dimensional surface spanned by two linearly independent vectors.
37  The plane is described by the equation
38  \f$ a*x + b*y + c*z + d = 0 \f$ where (a,b,c) are the components of the
39  normal vector to the plane \f$ n = (a,b,c) \f$ and \f$ d = - n \dot x \f$, where x is any point
40  belonging to plane.
41  More information on the mathematics describing a plane in 3D is available on
42  <A HREF=http://mathworld.wolfram.com/Plane.html>MathWord</A>.
43  The Plane3D class contains the 4 scalar values in T which represent the
44  four coefficients, fA, fB, fC, fD. fA, fB, fC are the normal components normalized to 1,
45  i.e. fA**2 + fB**2 + fC**2 = 1
46 
47  @ingroup GenVector
48 */
49 
50 template <typename T = double>
51 class Plane3D {
52 
53 public:
54  // ------ ctors ------
55 
56  typedef T Scalar;
57 
60 
61  /**
62  default constructor create plane z = 0
63  */
64  Plane3D() : fA(0), fB(0), fC(1), fD(0) {}
65 
66  /**
67  generic constructors from the four scalar values describing the plane
68  according to the equation ax + by + cz + d = 0
69  \param a scalar value
70  \param b scalar value
71  \param c scalar value
72  \param d sxcalar value
73  */
74  Plane3D(const Scalar &a, const Scalar &b, const Scalar &c, const Scalar &d) : fA(a), fB(b), fC(c), fD(d)
75  {
76  // renormalize a,b,c to unit
77  Normalize();
78  }
79 
80  /**
81  constructor a Plane3D from a normal vector and a point coplanar to the plane
82  \param n normal expressed as a ROOT::Math::DisplacementVector3D<Cartesian3D<T> >
83  \param p point expressed as a ROOT::Math::PositionVector3D<Cartesian3D<T> >
84  */
85  Plane3D(const Vector &n, const Point &p) { BuildFromVecAndPoint(n, p); }
86 
87  /**
88  Construct from a generic DisplacementVector3D (normal vector) and PositionVector3D (point coplanar to
89  the plane)
90  \param n normal expressed as a generic ROOT::Math::DisplacementVector3D
91  \param p point expressed as a generic ROOT::Math::PositionVector3D
92  */
93  template <class T1, class T2, class U>
95  {
97  }
98 
99  /**
100  constructor from three Cartesian point belonging to the plane
101  \param p1 point1 expressed as a generic ROOT::Math::PositionVector3D
102  \param p2 point2 expressed as a generic ROOT::Math::PositionVector3D
103  \param p3 point3 expressed as a generic ROOT::Math::PositionVector3D
104  */
105  Plane3D(const Point &p1, const Point &p2, const Point &p3) { BuildFrom3Points(p1, p2, p3); }
106 
107  /**
108  constructor from three generic point belonging to the plane
109  \param p1 point1 expressed as ROOT::Math::DisplacementVector3D<Cartesian3D<T> >
110  \param p2 point2 expressed as ROOT::Math::DisplacementVector3D<Cartesian3D<T> >
111  \param p3 point3 expressed as ROOT::Math::DisplacementVector3D<Cartesian3D<T> >
112  */
113  template <class T1, class T2, class T3, class U>
115  {
116  BuildFrom3Points(Point(p1.X(), p1.Y(), p1.Z()), Point(p2.X(), p2.Y(), p2.Z()), Point(p3.X(), p3.Y(), p3.Z()));
117  }
118 
119  // compiler-generated copy ctor and dtor are fine.
120 
121  // ------ assignment ------
122 
123  /**
124  Assignment operator from other Plane3D class
125  */
126  Plane3D &operator=(const Plane3D &plane)
127  {
128  fA = plane.fA;
129  fB = plane.fB;
130  fC = plane.fC;
131  fD = plane.fD;
132  return *this;
133  }
134 
135  /**
136  Return the a coefficient of the plane equation \f$ a*x + b*y + c*z + d = 0 \f$. It is also the
137  x-component of the vector perpendicular to the plane.
138  */
139  Scalar A() const { return fA; }
140 
141  /**
142  Return the b coefficient of the plane equation \f$ a*x + b*y + c*z + d = 0 \f$. It is also the
143  y-component of the vector perpendicular to the plane
144  */
145  Scalar B() const { return fB; }
146 
147  /**
148  Return the c coefficient of the plane equation \f$ a*x + b*y + c*z + d = 0 \f$. It is also the
149  z-component of the vector perpendicular to the plane
150  */
151  Scalar C() const { return fC; }
152 
153  /**
154  Return the d coefficient of the plane equation \f$ a*x + b*y + c*z + d = 0 \f$. It is also
155  the distance from the origin (HesseDistance)
156  */
157  Scalar D() const { return fD; }
158 
159  /**
160  Return normal vector to the plane as Cartesian DisplacementVector
161  */
162  Vector Normal() const { return Vector(fA, fB, fC); }
163 
164  /**
165  Return the Hesse Distance (distance from the origin) of the plane or
166  the d coefficient expressed in normalize form
167  */
168  Scalar HesseDistance() const { return fD; }
169 
170  /**
171  Return the signed distance to a Point.
172  The distance is signed positive if the Point is in the same side of the
173  normal vector to the plane.
174  \param p Point expressed in Cartesian Coordinates
175  */
176  Scalar Distance(const Point &p) const { return fA * p.X() + fB * p.Y() + fC * p.Z() + fD; }
177 
178  /**
179  Return the distance to a Point described with generic coordinates
180  \param p Point expressed as generic ROOT::Math::PositionVector3D
181  */
182  template <class T1, class U>
183  Scalar Distance(const PositionVector3D<T1, U> &p) const
184  {
185  return Distance(Point(p.X(), p.Y(), p.Z()));
186  }
187 
188  /**
189  Return the projection of a Cartesian point to a plane
190  \param p Point expressed as PositionVector3D<Cartesian3D<T> >
191  */
192  Point ProjectOntoPlane(const Point &p) const
193  {
194  const Scalar d = Distance(p);
195  return XYZPoint(p.X() - fA * d, p.Y() - fB * d, p.Z() - fC * d);
196  }
197 
198  /**
199  Return the projection of a point to a plane
200  \param p Point expressed as generic ROOT::Math::PositionVector3D
201  */
202  template <class T1, class U>
204  {
205  const Point pxyz = ProjectOntoPlane(Point(p.X(), p.Y(), p.Z()));
206  return PositionVector3D<T, U>(pxyz.X(), pxyz.Y(), pxyz.Z());
207  }
208 
209  // ------------------- Equality -----------------
210 
211  /**
212  Exact equality
213  */
214  bool operator==(const Plane3D &rhs) const { return (fA == rhs.fA && fB == rhs.fB && fC == rhs.fC && fD == rhs.fD); }
215  bool operator!=(const Plane3D &rhs) const { return !(operator==(rhs)); }
216 
217 protected:
218  /**
219  Normalize the normal (a,b,c) plane components
220  */
221  template <typename SCALAR = T, typename std::enable_if<std::is_arithmetic<SCALAR>::value>::type * = nullptr>
222  void Normalize()
223  {
224  // normalize the plane
225  const SCALAR s = sqrt(fA * fA + fB * fB + fC * fC);
226  // what to do if s = 0 ?
227  if (s == SCALAR(0)) {
228  fD = SCALAR(0);
229  } else {
230  const SCALAR w = Scalar(1) / s;
231  fA *= w;
232  fB *= w;
233  fC *= w;
234  fD *= w;
235  }
236  }
237 
238  /**
239  Normalize the normal (a,b,c) plane components
240  */
241  template <typename SCALAR = T, typename std::enable_if<!std::is_arithmetic<SCALAR>::value>::type * = nullptr>
242  void Normalize()
243  {
244  // normalize the plane
245  SCALAR s = sqrt(fA * fA + fB * fB + fC * fC);
246  // what to do if s = 0 ?
247  const auto m = (s == SCALAR(0));
248  // set zero entries to 1 in the vector to avoid /0 later on
249  s(m) = SCALAR(1);
250  fD(m) = SCALAR(0);
251  const SCALAR w = SCALAR(1) / s;
252  fA *= w;
253  fB *= w;
254  fC *= w;
255  fD *= w;
256  }
257 
258 private:
259  // internal method to construct class from a vector and a point
260  void BuildFromVecAndPoint(const Vector &n, const Point &p)
261  {
262  // build from a normal vector and a point
263  fA = n.X();
264  fB = n.Y();
265  fC = n.Z();
266  fD = -n.Dot(p);
267  Normalize();
268  }
269 
270  // internal method to construct class from 3 points
271  void BuildFrom3Points(const Point &p1, const Point &p2, const Point &p3)
272  {
273  // plane from thre points
274  // normal is (x3-x1) cross (x2 -x1)
275  const Vector n = (p2 - p1).Cross(p3 - p1);
276  fA = n.X();
277  fB = n.Y();
278  fC = n.Z();
279  fD = -n.Dot(p1);
280  Normalize();
281  }
282 
283  // plane data members the four scalar which satisfies fA*x + fB*y + fC*z + fD = 0
284  // for every point (x,y,z) belonging to the plane.
285  // fA**2 + fB**2 + fC** =1 plane is stored in normalized form
286  Scalar fA;
287  Scalar fB;
288  Scalar fC;
289  Scalar fD;
290 
291  }; // Plane3D<>
292 
293  /**
294  Stream Output and Input
295  */
296  // TODO - I/O should be put in the manipulator form
297  template <typename T>
298  std::ostream &operator<<(std::ostream &os, const Plane3D<T> &p)
299  {
300  os << "\n"
301  << p.Normal().X() << " " << p.Normal().Y() << " " << p.Normal().Z() << " " << p.HesseDistance() << "\n";
302  return os;
303  }
304 
305  } // end namespace Impl
306 
307  // typedefs for double and float versions
310 
311 } // end namespace Math
312 
313 } // end namespace ROOT
314 
315 
316 #endif
317 
318 
319 
320 
Plane3D(const DisplacementVector3D< T1, U > &n, const PositionVector3D< T2, U > &p)
Construct from a generic DisplacementVector3D (normal vector) and PositionVector3D (point coplanar to...
Definition: Plane3D.h:94
Scalar HesseDistance() const
Return the Hesse Distance (distance from the origin) of the plane or the d coefficient expressed in n...
Definition: Plane3D.h:168
PositionVector3D< Cartesian3D< T >, DefaultCoordinateSystemTag > Point
Definition: Plane3D.h:59
static double p3(double t, double a, double b, double c, double d)
Scalar Distance(const PositionVector3D< T1, U > &p) const
Return the distance to a Point described with generic coordinates.
Definition: Plane3D.h:183
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
Vector Normal() const
Return normal vector to the plane as Cartesian DisplacementVector.
Definition: Plane3D.h:162
double T(double x)
Definition: ChebyshevPol.h:34
void Normalize()
Normalize the normal (a,b,c) plane components.
Definition: Plane3D.h:222
Class describing a generic position vector (point) in 3 dimensions.
Scalar D() const
Return the d coefficient of the plane equation .
Definition: Plane3D.h:157
Scalar B() const
Return the b coefficient of the plane equation .
Definition: Plane3D.h:145
TArc * a
Definition: textangle.C:12
bool operator!=(const Plane3D &rhs) const
Definition: Plane3D.h:215
void BuildFromVecAndPoint(const Vector &n, const Point &p)
Definition: Plane3D.h:260
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
bool operator==(const Plane3D &rhs) const
Exact equality.
Definition: Plane3D.h:214
Plane3D()
default constructor create plane z = 0
Definition: Plane3D.h:64
Scalar Z() const
Cartesian Z, converting if necessary from internal coordinate system.
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
static double p2(double t, double a, double b, double c)
Scalar Z() const
Cartesian Z, converting if necessary from internal coordinate system.
Class describing a generic displacement vector in 3 dimensions.
Plane3D(const PositionVector3D< T1, U > &p1, const PositionVector3D< T2, U > &p2, const PositionVector3D< T3, U > &p3)
constructor from three generic point belonging to the plane
Definition: Plane3D.h:114
Plane3D(const Point &p1, const Point &p2, const Point &p3)
constructor from three Cartesian point belonging to the plane
Definition: Plane3D.h:105
DisplacementVector3D< Cartesian3D< T >, DefaultCoordinateSystemTag > Vector
Definition: Plane3D.h:58
Scalar X() const
Cartesian X, converting if necessary from internal coordinate system.
TMarker * m
Definition: textangle.C:8
static double p1(double t, double a, double b)
Impl::Plane3D< float > Plane3DF
Definition: Plane3D.h:309
SVector< T, 3 > Cross(const SVector< T, 3 > &lhs, const SVector< T, 3 > &rhs)
Vector Cross Product (only for 3-dim vectors) .
Definition: Functions.h:322
Plane3D & operator=(const Plane3D &plane)
Assignment operator from other Plane3D class.
Definition: Plane3D.h:126
int type
Definition: TGX11.cxx:120
Namespace for new Math classes and functions.
Scalar Y() const
Cartesian Y, converting if necessary from internal coordinate system.
Plane3D(const Scalar &a, const Scalar &b, const Scalar &c, const Scalar &d)
generic constructors from the four scalar values describing the plane according to the equation ax + ...
Definition: Plane3D.h:74
Scalar C() const
Return the c coefficient of the plane equation .
Definition: Plane3D.h:151
Scalar Distance(const Point &p) const
Return the signed distance to a Point.
Definition: Plane3D.h:176
Plane3D(const Vector &n, const Point &p)
constructor a Plane3D from a normal vector and a point coplanar to the plane
Definition: Plane3D.h:85
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
void BuildFrom3Points(const Point &p1, const Point &p2, const Point &p3)
Definition: Plane3D.h:271
Scalar A() const
Return the a coefficient of the plane equation .
Definition: Plane3D.h:139
Class describing a geometrical plane in 3 dimensions.
Definition: Plane3D.h:51
DefaultCoordinateSystemTag Default tag for identifying any coordinate system.
Point ProjectOntoPlane(const Point &p) const
Return the projection of a Cartesian point to a plane.
Definition: Plane3D.h:192
const Int_t n
Definition: legend1.C:16
PositionVector3D< T1, U > ProjectOntoPlane(const PositionVector3D< T1, U > &p) const
Return the projection of a point to a plane.
Definition: Plane3D.h:203
Scalar Dot(const DisplacementVector3D< OtherCoords, Tag > &v) const
Return the scalar (dot) product of two displacement vectors.
PositionVector3D< Cartesian3D< double >, DefaultCoordinateSystemTag > XYZPoint
3D Point based on the cartesian coordinates x,y,z in double precision
Definition: Point3Dfwd.h:33