Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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_MathX_GenVectorX_Plane3D
18#define ROOT_MathX_GenVectorX_Plane3D 1
19
20#include <type_traits>
21
24
25namespace ROOT {
26
27namespace ROOT_MATH_ARCH {
28
29namespace Impl {
30
31//_______________________________________________________________________________
32/**
33 Class describing a geometrical plane in 3 dimensions.
34 A Plane3D is a 2 dimensional surface spanned by two linearly independent vectors.
35 The plane is described by the equation
36 \f$ a*x + b*y + c*z + d = 0 \f$ where (a,b,c) are the components of the
37 normal vector to the plane \f$ n = (a,b,c) \f$ and \f$ d = - n \dot x \f$, where x is any point
38 belonging to plane.
39 More information on the mathematics describing a plane in 3D is available on
40 <A HREF=http://mathworld.wolfram.com/Plane.html>MathWord</A>.
41 The Plane3D class contains the 4 scalar values in T which represent the
42 four coefficients, fA, fB, fC, fD. fA, fB, fC are the normal components normalized to 1,
43 i.e. fA**2 + fB**2 + fC**2 = 1
44
45 @ingroup GenVectorX
46
47 @see GenVectorX
48*/
49
50template <typename T = double>
51class Plane3D {
52
53public:
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>
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 Plane3D(const Plane3D &) = default;
121
122 // ------ assignment ------
123
124 /**
125 Assignment operator from other Plane3D class
126 */
127 Plane3D &operator=(const Plane3D &) = default;
128
129 /**
130 Return the a coefficient of the plane equation \f$ a*x + b*y + c*z + d = 0 \f$. It is also the
131 x-component of the vector perpendicular to the plane.
132 */
133 Scalar A() const { return fA; }
134
135 /**
136 Return the b coefficient of the plane equation \f$ a*x + b*y + c*z + d = 0 \f$. It is also the
137 y-component of the vector perpendicular to the plane
138 */
139 Scalar B() const { return fB; }
140
141 /**
142 Return the c coefficient of the plane equation \f$ a*x + b*y + c*z + d = 0 \f$. It is also the
143 z-component of the vector perpendicular to the plane
144 */
145 Scalar C() const { return fC; }
146
147 /**
148 Return the d coefficient of the plane equation \f$ a*x + b*y + c*z + d = 0 \f$. It is also
149 the distance from the origin (HesseDistance)
150 */
151 Scalar D() const { return fD; }
152
153 /**
154 Return normal vector to the plane as Cartesian DisplacementVector
155 */
156 Vector Normal() const { return Vector(fA, fB, fC); }
157
158 /**
159 Return the Hesse Distance (distance from the origin) of the plane or
160 the d coefficient expressed in normalize form
161 */
162 Scalar HesseDistance() const { return fD; }
163
164 /**
165 Return the signed distance to a Point.
166 The distance is signed positive if the Point is in the same side of the
167 normal vector to the plane.
168 \param p Point expressed in Cartesian Coordinates
169 */
170 Scalar Distance(const Point &p) const { return fA * p.X() + fB * p.Y() + fC * p.Z() + fD; }
171
172 /**
173 Return the distance to a Point described with generic coordinates
174 \param p Point expressed as generic ROOT::Math::PositionVector3D
175 */
176 template <class T1, class U>
178 {
179 return Distance(Point(p.X(), p.Y(), p.Z()));
180 }
181
182 /**
183 Return the projection of a Cartesian point to a plane
184 \param p Point expressed as PositionVector3D<Cartesian3D<T> >
185 */
187 {
188 const Scalar d = Distance(p);
189 return XYZPoint(p.X() - fA * d, p.Y() - fB * d, p.Z() - fC * d);
190 }
191
192 /**
193 Return the projection of a point to a plane
194 \param p Point expressed as generic ROOT::Math::PositionVector3D
195 */
196 template <class T1, class U>
198 {
199 const Point pxyz = ProjectOntoPlane(Point(p.X(), p.Y(), p.Z()));
200 return PositionVector3D<T, U>(pxyz.X(), pxyz.Y(), pxyz.Z());
201 }
202
203 // ------------------- Equality -----------------
204
205 /**
206 Exact equality
207 */
208 bool operator==(const Plane3D &rhs) const { return (fA == rhs.fA && fB == rhs.fB && fC == rhs.fC && fD == rhs.fD); }
209 bool operator!=(const Plane3D &rhs) const { return !(operator==(rhs)); }
210
211protected:
212 /**
213 Normalize the normal (a,b,c) plane components
214 */
217 {
218 // normalize the plane
219 using std::sqrt;
220 const SCALAR s = sqrt(fA * fA + fB * fB + fC * fC);
221 // what to do if s = 0 ?
222 if (s == SCALAR(0)) {
223 fD = SCALAR(0);
224 } else {
225 const SCALAR w = Scalar(1) / s;
226 fA *= w;
227 fB *= w;
228 fC *= w;
229 fD *= w;
230 }
231 }
232
233 /**
234 Normalize the normal (a,b,c) plane components
235 */
238 {
239 // normalize the plane
240 using std::sqrt;
241 SCALAR s = sqrt(fA * fA + fB * fB + fC * fC);
242 // what to do if s = 0 ?
243 const auto m = (s == SCALAR(0));
244 // set zero entries to 1 in the vector to avoid /0 later on
245 s(m) = SCALAR(1);
246 fD(m) = SCALAR(0);
247 const SCALAR w = SCALAR(1) / s;
248 fA *= w;
249 fB *= w;
250 fC *= w;
251 fD *= w;
252 }
253
254private:
255 // internal method to construct class from a vector and a point
256 void BuildFromVecAndPoint(const Vector &n, const Point &p)
257 {
258 // build from a normal vector and a point
259 fA = n.X();
260 fB = n.Y();
261 fC = n.Z();
262 fD = -n.Dot(p);
263 Normalize();
264 }
265
266 // internal method to construct class from 3 points
267 void BuildFrom3Points(const Point &p1, const Point &p2, const Point &p3)
268 {
269 // plane from three points
270 // normal is (x3-x1) cross (x2 -x1)
271 const Vector n = (p2 - p1).Cross(p3 - p1);
272 fA = n.X();
273 fB = n.Y();
274 fC = n.Z();
275 fD = -n.Dot(p1);
276 Normalize();
277 }
278
279 // plane data members the four scalar which satisfies fA*x + fB*y + fC*z + fD = 0
280 // for every point (x,y,z) belonging to the plane.
281 // fA**2 + fB**2 + fC** =1 plane is stored in normalized form
286
287}; // Plane3D<>
288
289/**
290 Stream Output and Input
291*/
292// TODO - I/O should be put in the manipulator form
293template <typename T>
294std::ostream &operator<<(std::ostream &os, const Plane3D<T> &p)
295{
296 os << "\n"
297 << p.Normal().X() << " " << p.Normal().Y() << " " << p.Normal().Z() << " " << p.HesseDistance() << "\n";
298 return os;
299}
300
301} // end namespace Impl
302
303// typedefs for double and float versions
306
307} // end namespace ROOT_MATH_ARCH
308
309} // end namespace ROOT
310
311#endif
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
TGLVector3 Cross(const TGLVector3 &v1, const TGLVector3 &v2)
Definition TGLUtil.h:323
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
DefaultCoordinateSystemTag Default tag for identifying any coordinate system.
Class describing a geometrical plane in 3 dimensions.
Definition Plane3D.h:51
Scalar Distance(const Point &p) const
Return the signed distance to a Point.
Definition Plane3D.h:170
Plane3D(const Plane3D &)=default
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:162
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
void BuildFromVecAndPoint(const Vector &n, const Point &p)
Definition Plane3D.h:256
DisplacementVector3D< Cartesian3D< T >, DefaultCoordinateSystemTag > Vector
Definition Plane3D.h:58
Point ProjectOntoPlane(const Point &p) const
Return the projection of a Cartesian point to a plane.
Definition Plane3D.h:186
void Normalize()
Normalize the normal (a,b,c) plane components.
Definition Plane3D.h:216
Plane3D & operator=(const Plane3D &)=default
Assignment operator from other Plane3D class.
void BuildFrom3Points(const Point &p1, const Point &p2, const Point &p3)
Definition Plane3D.h:267
bool operator==(const Plane3D &rhs) const
Exact equality.
Definition Plane3D.h:208
PositionVector3D< T1, U > ProjectOntoPlane(const PositionVector3D< T1, U > &p) const
Return the projection of a point to a plane.
Definition Plane3D.h:197
PositionVector3D< Cartesian3D< T >, DefaultCoordinateSystemTag > Point
Definition Plane3D.h:59
Plane3D(const Point &p1, const Point &p2, const Point &p3)
constructor from three Cartesian point belonging to the plane
Definition Plane3D.h:105
Scalar C() const
Return the c coefficient of the plane equation .
Definition Plane3D.h:145
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
Scalar Distance(const PositionVector3D< T1, U > &p) const
Return the distance to a Point described with generic coordinates.
Definition Plane3D.h:177
Scalar B() const
Return the b coefficient of the plane equation .
Definition Plane3D.h:139
Scalar A() const
Return the a coefficient of the plane equation .
Definition Plane3D.h:133
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
bool operator!=(const Plane3D &rhs) const
Definition Plane3D.h:209
Plane3D()
default constructor create plane z = 0
Definition Plane3D.h:64
Vector Normal() const
Return normal vector to the plane as Cartesian DisplacementVector.
Definition Plane3D.h:156
Scalar D() const
Return the d coefficient of the plane equation .
Definition Plane3D.h:151
const Int_t n
Definition legend1.C:16
std::ostream & operator<<(std::ostream &os, const Plane3D< T > &p)
Stream Output and Input.
Definition Plane3D.h:294
Impl::Plane3D< float > Plane3DF
Definition Plane3D.h:305
PositionVector3D< Cartesian3D< double >, DefaultCoordinateSystemTag > XYZPoint
3D Point based on the cartesian coordinates x,y,z in double precision
Definition Point3Dfwd.h:45
Impl::Plane3D< double > Plane3D
Definition Plane3D.h:304
TMarker m
Definition textangle.C:8