CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
Plane3D.h
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: Plane3D.h,v 1.5 2010/06/16 16:21:27 garren Exp $
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>
16#include "CLHEP/Geometry/defs.h"
17#include "CLHEP/Geometry/Point3D.h"
18#include "CLHEP/Geometry/Normal3D.h"
19#include "CLHEP/Geometry/Transform3D.h"
20
21namespace HepGeom {
22
23 /**
24 * Template class for geometrical plane in 3D.
25 *
26 * @author Evgeni Chernyaev <[email protected]>
27 * @ingroup geometry
28 */
29 template<class T>
30 class Plane3D {
31 protected:
32 T a_, b_, c_, d_;
33
34 public:
35 /**
36 * Default constructor - creates plane z=0. */
37 Plane3D() : a_(0.), b_(0.), c_(1.), d_(0.) {}
38
39 /**
40 * Constructor from four numbers - creates plane a*x+b*y+c*z+d=0. */
41 Plane3D(T a1, T b1, T c1, T d1) : a_(a1), b_(b1), c_(c1), d_(d1) {}
42
43 /**
44 * Constructor from normal and point. */
45 Plane3D(const Normal3D<T> & n, const Point3D<T> & p)
46 : a_(n.x()), b_(n.y()), c_(n.z()), d_(-n*p) {}
47
48 /**
49 * Constructor from three points. */
50 Plane3D(const Point3D<T> & p1,
51 const Point3D<T> & p2,
52 const Point3D<T> & p3) {
53 Normal3D<T> n = (p2-p1).cross(p3-p1);
54 a_ = n.x(); b_ = n.y(); c_ = n.z(); d_ = -n*p1;
55 }
56
57 /**
58 * Copy constructor. */
59 Plane3D(const Plane3D<T> &) = default;
60
61 /**
62 * Constructor for Plane3D<double> from Plane3D<float>. */
63 template<typename U = T,
64 typename = typename std::enable_if<!std::is_same<U,float>::value >::type>
66 : a_(p.a_), b_(p.b_), c_(p.c_), d_(p.d_) {}
67
68 /**
69 * Move constructor. */
70 Plane3D(Plane3D<T> &&) = default;
71
72 /**
73 * Destructor. */
74 ~Plane3D() = default;
75
76 /**
77 * Assignment. */
78 Plane3D<T> & operator=(const Plane3D<T> &) = default;
79
80 /**
81 * Move assignment. */
83
84 /**
85 * Returns the a-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
86 T a() const { return a_; }
87 /**
88 * Returns the b-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
89 T b() const { return b_; }
90 /**
91 * Returns the c-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
92 T c() const { return c_; }
93 /**
94 * Returns the free member of the plane equation: a*x+b*y+c*z+d=0. */
95 T d() const { return d_; }
96
97 /**
98 * Returns normal. */
99 Normal3D<T> normal() const { return Normal3D<T>(a_,b_,c_); }
100
101 /**
102 * Normalization. */
104 double ll = std::sqrt(a_*a_ + b_*b_ + c_*c_);
105 if (ll > 0.) { a_ /= ll; b_ /= ll; c_ /= ll, d_ /= ll; }
106 return *this;
107 }
108
109 /**
110 * Returns distance to the point. */
111 T distance(const Point3D<T> & p) const {
112 return a()*p.x() + b()*p.y() + c()*p.z() + d();
113 }
114
115 /**
116 * Returns projection of the point to the plane. */
117 Point3D<T> point(const Point3D<T> & p) const {
118 T k = distance(p)/(a()*a()+b()*b()+c()*c());
119 return Point3D<T>(p.x()-a()*k, p.y()-b()*k, p.z()-c()*k);
120 }
121
122 /**
123 * Returns projection of the origin to the plane. */
125 T k = -d()/(a()*a()+b()*b()+c()*c());
126 return Point3D<T>(a()*k, b()*k, c()*k);
127 }
128
129 /**
130 * Test for equality. */
131 bool operator == (const Plane3D<T> & p) const {
132 return a() == p.a() && b() == p.b() && c() == p.c() && d() == p.d();
133 }
134
135 /**
136 * Test for inequality. */
137 bool operator != (const Plane3D<T> & p) const {
138 return a() != p.a() || b() != p.b() || c() != p.c() || d() != p.d();
139 }
140
141 /**
142 * Transformation by Transform3D. */
144 Normal3D<T> n = normal();
145 n.transform(m);
146 d_ = -n*point().transform(m); a_ = n.x(); b_ = n.y(); c_ = n.z();
147 return *this;
148 }
149 };
150
151 /**
152 * Output to the stream.
153 * @relates Plane3D
154 */
155 std::ostream & operator<<(std::ostream & os, const Plane3D<float> & p);
156
157 /**
158 * Output to the stream.
159 * @relates Plane3D
160 */
161 std::ostream & operator<<(std::ostream & os, const Plane3D<double> & p);
162
163} /* namespace HepGeom */
164
165#ifdef ENABLE_BACKWARDS_COMPATIBILITY
166// backwards compatibility will be enabled ONLY in CLHEP 1.9
167typedef HepGeom::Plane3D<double> HepPlane3D;
168#endif
169
170#endif /* HEP_PLANE3D_H */
Plane3D(T a1, T b1, T c1, T d1)
Definition: Plane3D.h:41
Plane3D< T > & normalize()
Definition: Plane3D.h:103
Plane3D(const Normal3D< T > &n, const Point3D< T > &p)
Definition: Plane3D.h:45
Plane3D(Plane3D< T > &&)=default
T d() const
Definition: Plane3D.h:95
T b() const
Definition: Plane3D.h:89
T c() const
Definition: Plane3D.h:92
T a() const
Definition: Plane3D.h:86
Plane3D(const Plane3D< T > &)=default
Point3D< T > point(const Point3D< T > &p) const
Definition: Plane3D.h:117
Plane3D(const Point3D< T > &p1, const Point3D< T > &p2, const Point3D< T > &p3)
Definition: Plane3D.h:50
Plane3D< T > & operator=(const Plane3D< T > &)=default
Plane3D< T > & operator=(Plane3D< T > &&)=default
Normal3D< T > normal() const
Definition: Plane3D.h:99
Point3D< T > point() const
Definition: Plane3D.h:124
Plane3D(const Plane3D< float > &p)
Definition: Plane3D.h:65
~Plane3D()=default
T distance(const Point3D< T > &p) const
Definition: Plane3D.h:111
bool operator!=(const Plane3D< T > &p) const
Definition: Plane3D.h:137
bool operator==(const Plane3D< T > &p) const
Definition: Plane3D.h:131
Plane3D< T > & transform(const Transform3D &m)
Definition: Plane3D.h:143
std::ostream & operator<<(std::ostream &os, const BasicVector3D< float > &a)