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