Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Plane3D.h
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id:$
3// ---------------------------------------------------------------------------
4//
5// This file is a part of the CLHEP - a Class Library for High Energy Physics.
6//
7// History:
8// 22.09.96 E.Chernyaev - initial version
9// 19.10.96 J.Allison - added == and <<.
10// 15.04.03 E.Chernyaev - CLHEP-1.9: template version
11
12#ifndef HEP_PLANE3D_H
13#define HEP_PLANE3D_H
14
15#include <iosfwd>
19
20namespace HepGeom {
21
22 /**
23 * Template class for geometrical plane in 3D.
24 *
25 * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch>
26 * @ingroup geometry
27 */
28 template<class T>
29 class Plane3D {
30 protected:
31 T a_, b_, c_, d_;
32
33 public:
34 /**
35 * Default constructor - creates plane z=0. */
36 Plane3D() : a_(0.), b_(0.), c_(1.), d_(0.) {}
37
38 /**
39 * Constructor from four numbers - creates plane a*x+b*y+c*z+d=0. */
40 Plane3D(T a1, T b1, T c1, T d1) : a_(a1), b_(b1), c_(c1), d_(d1) {}
41
42 /**
43 * Constructor from normal and point. */
44 Plane3D(const Normal3D<T> & n, const Point3D<T> & p)
45 : a_(n.x()), b_(n.y()), c_(n.z()), d_(-n*p) {}
46
47 /**
48 * Constructor from three points. */
49 Plane3D(const Point3D<T> & p1,
50 const Point3D<T> & p2,
51 const Point3D<T> & p3) {
52 Normal3D<T> n = (p2-p1).cross(p3-p1);
53 a_ = n.x(); b_ = n.y(); c_ = n.z(); d_ = -n*p1;
54 }
55
56 /** Copy constructor.
57 * Plane3D<double> has two constructors:
58 * from Plane3D<double> (provided by compiler) and
59 * from Plane3D<float> (defined in this file).
60 * Plane3D<float> has only the last one.
61 */
63 : a_(p.a_), b_(p.b_), c_(p.c_), d_(p.d_) {}
64
65 /**
66 * Destructor. */
68
69 /**
70 * Assignment. */
72 a_ = p.a_; b_ = p.b_; c_ = p.c_; d_ = p.d_; return *this;
73 }
74
75 /**
76 * Returns the a-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
77 T a() const { return a_; }
78 /**
79 * Returns the b-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
80 T b() const { return b_; }
81 /**
82 * Returns the c-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
83 T c() const { return c_; }
84 /**
85 * Returns the free member of the plane equation: a*x+b*y+c*z+d=0. */
86 T d() const { return d_; }
87
88 /**
89 * Returns normal. */
90 Normal3D<T> normal() const { return Normal3D<T>(a_,b_,c_); }
91
92 /**
93 * Normalization. */
95 double ll = std::sqrt(a_*a_ + b_*b_ + c_*c_);
96 if (ll > 0.) { a_ /= ll; b_ /= ll; c_ /= ll, d_ /= ll; }
97 return *this;
98 }
99
100 /**
101 * Returns distance to the point. */
102 T distance(const Point3D<T> & p) const {
103 return a()*p.x() + b()*p.y() + c()*p.z() + d();
104 }
105
106 /**
107 * Returns projection of the point to the plane. */
108 Point3D<T> point(const Point3D<T> & p) const {
109 T k = distance(p)/(a()*a()+b()*b()+c()*c());
110 return Point3D<T>(p.x()-a()*k, p.y()-b()*k, p.z()-c()*k);
111 }
112
113 /**
114 * Returns projection of the origin to the plane. */
116 T k = -d()/(a()*a()+b()*b()+c()*c());
117 return Point3D<T>(a()*k, b()*k, c()*k);
118 }
119
120 /**
121 * Test for equality. */
122 bool operator == (const Plane3D<T> & p) const {
123 return a() == p.a() && b() == p.b() && c() == p.c() && d() == p.d();
124 }
125
126 /**
127 * Test for inequality. */
128 bool operator != (const Plane3D<T> & p) const {
129 return a() != p.a() || b() != p.b() || c() != p.c() || d() != p.d();
130 }
131
132 /**
133 * Transformation by Transform3D. */
135 Normal3D<T> n = normal();
136 n.transform(m);
137 d_ = -n*point().transform(m); a_ = n.x(); b_ = n.y(); c_ = n.z();
138 return *this;
139 }
140 };
141
142 /**
143 * Output to the stream.
144 * @relates Plane3D
145 */
146 std::ostream & operator<<(std::ostream & os, const Plane3D<float> & p);
147
148 /**
149 * Output to the stream.
150 * @relates Plane3D
151 */
152 std::ostream & operator<<(std::ostream & os, const Plane3D<double> & p);
153
154} /* namespace HepGeom */
155
156#endif /* HEP_PLANE3D_H */
Plane3D(T a1, T b1, T c1, T d1)
Definition: Plane3D.h:40
Plane3D< T > & normalize()
Definition: Plane3D.h:94
Plane3D(const Normal3D< T > &n, const Point3D< T > &p)
Definition: Plane3D.h:44
Plane3D(const Plane3D< float > &p)
Definition: Plane3D.h:62
T d() const
Definition: Plane3D.h:86
T b() const
Definition: Plane3D.h:80
T c() const
Definition: Plane3D.h:83
T a() const
Definition: Plane3D.h:77
Point3D< T > point(const Point3D< T > &p) const
Definition: Plane3D.h:108
Plane3D(const Point3D< T > &p1, const Point3D< T > &p2, const Point3D< T > &p3)
Definition: Plane3D.h:49
Normal3D< T > normal() const
Definition: Plane3D.h:90
Point3D< T > point() const
Definition: Plane3D.h:115
T distance(const Point3D< T > &p) const
Definition: Plane3D.h:102
Plane3D< T > & operator=(const Plane3D< T > &p)
Definition: Plane3D.h:71
bool operator!=(const Plane3D< T > &p) const
Definition: Plane3D.h:128
bool operator==(const Plane3D< T > &p) const
Definition: Plane3D.h:122
Plane3D< T > & transform(const Transform3D &m)
Definition: Plane3D.h:134
std::ostream & operator<<(std::ostream &os, const BasicVector3D< float > &a)