File indexing completed on 2025-01-18 09:54:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef HEP_PLANE3D_H
0013 #define HEP_PLANE3D_H
0014
0015 #include <iosfwd>
0016 #include "CLHEP/Geometry/defs.h"
0017 #include "CLHEP/Geometry/Point3D.h"
0018 #include "CLHEP/Geometry/Normal3D.h"
0019 #include "CLHEP/Geometry/Transform3D.h"
0020
0021 namespace HepGeom {
0022
0023
0024
0025
0026
0027
0028
0029 template<class T>
0030 class Plane3D {
0031 protected:
0032 T a_, b_, c_, d_;
0033
0034 public:
0035
0036
0037 Plane3D() : a_(0.), b_(0.), c_(1.), d_(0.) {}
0038
0039
0040
0041 Plane3D(T a1, T b1, T c1, T d1) : a_(a1), b_(b1), c_(c1), d_(d1) {}
0042
0043
0044
0045 Plane3D(const Normal3D<T> & n, const Point3D<T> & p)
0046 : a_(n.x()), b_(n.y()), c_(n.z()), d_(-n*p) {}
0047
0048
0049
0050 Plane3D(const Point3D<T> & p1,
0051 const Point3D<T> & p2,
0052 const Point3D<T> & p3) {
0053 Normal3D<T> n = (p2-p1).cross(p3-p1);
0054 a_ = n.x(); b_ = n.y(); c_ = n.z(); d_ = -n*p1;
0055 }
0056
0057
0058
0059 Plane3D(const Plane3D<T> &) = default;
0060
0061
0062
0063 template<typename U = T,
0064 typename = typename std::enable_if<!std::is_same<U,float>::value >::type>
0065 Plane3D(const Plane3D<float> & p)
0066 : a_(p.a_), b_(p.b_), c_(p.c_), d_(p.d_) {}
0067
0068
0069
0070 Plane3D(Plane3D<T> &&) = default;
0071
0072
0073
0074 ~Plane3D() = default;
0075
0076
0077
0078 Plane3D<T> & operator=(const Plane3D<T> &) = default;
0079
0080
0081
0082 Plane3D<T> & operator=(Plane3D<T> &&) = default;
0083
0084
0085
0086 T a() const { return a_; }
0087
0088
0089 T b() const { return b_; }
0090
0091
0092 T c() const { return c_; }
0093
0094
0095 T d() const { return d_; }
0096
0097
0098
0099 Normal3D<T> normal() const { return Normal3D<T>(a_,b_,c_); }
0100
0101
0102
0103 Plane3D<T> & normalize() {
0104 double ll = std::sqrt(a_*a_ + b_*b_ + c_*c_);
0105 if (ll > 0.) { a_ /= ll; b_ /= ll; c_ /= ll, d_ /= ll; }
0106 return *this;
0107 }
0108
0109
0110
0111 T distance(const Point3D<T> & p) const {
0112 return a()*p.x() + b()*p.y() + c()*p.z() + d();
0113 }
0114
0115
0116
0117 Point3D<T> point(const Point3D<T> & p) const {
0118 T k = distance(p)/(a()*a()+b()*b()+c()*c());
0119 return Point3D<T>(p.x()-a()*k, p.y()-b()*k, p.z()-c()*k);
0120 }
0121
0122
0123
0124 Point3D<T> point() const {
0125 T k = -d()/(a()*a()+b()*b()+c()*c());
0126 return Point3D<T>(a()*k, b()*k, c()*k);
0127 }
0128
0129
0130
0131 bool operator == (const Plane3D<T> & p) const {
0132 return a() == p.a() && b() == p.b() && c() == p.c() && d() == p.d();
0133 }
0134
0135
0136
0137 bool operator != (const Plane3D<T> & p) const {
0138 return a() != p.a() || b() != p.b() || c() != p.c() || d() != p.d();
0139 }
0140
0141
0142
0143 Plane3D<T> & transform(const Transform3D & m) {
0144 Normal3D<T> n = normal();
0145 n.transform(m);
0146 d_ = -n*point().transform(m); a_ = n.x(); b_ = n.y(); c_ = n.z();
0147 return *this;
0148 }
0149 };
0150
0151
0152
0153
0154
0155 std::ostream & operator<<(std::ostream & os, const Plane3D<float> & p);
0156
0157
0158
0159
0160
0161 std::ostream & operator<<(std::ostream & os, const Plane3D<double> & p);
0162
0163 }
0164
0165 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
0166
0167 typedef HepGeom::Plane3D<double> HepPlane3D;
0168 #endif
0169
0170 #endif