Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:36

0001 // -*- C++ -*-
0002 // $Id: Plane3D.h,v 1.5 2010/06/16 16:21:27 garren Exp $
0003 // ---------------------------------------------------------------------------
0004 //
0005 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
0006 //
0007 // History:
0008 // 22.09.96 E.Chernyaev - initial version
0009 // 19.10.96 J.Allison - added == and <<.
0010 // 15.04.03 E.Chernyaev - CLHEP-1.9: template version
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    * Template class for geometrical plane in 3D.
0025    *
0026    * @author Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch>
0027    * @ingroup geometry
0028    */
0029   template<class T>
0030   class Plane3D {
0031   protected:
0032     T a_, b_, c_, d_;
0033 
0034   public:
0035     /**
0036      *  Default constructor - creates plane z=0. */
0037     Plane3D() : a_(0.), b_(0.), c_(1.), d_(0.) {}
0038 
0039     /**
0040      * Constructor from four numbers - creates plane a*x+b*y+c*z+d=0. */
0041     Plane3D(T a1, T b1, T c1, T d1) : a_(a1), b_(b1), c_(c1), d_(d1) {}
0042 
0043     /**
0044      * Constructor from normal and point. */
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      * Constructor from three points. */
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      * Copy constructor. */
0059     Plane3D(const Plane3D<T> &) = default;
0060 
0061     /**
0062      * Constructor for Plane3D<double> from Plane3D<float>. */
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      * Move constructor. */
0070     Plane3D(Plane3D<T> &&) = default;
0071 
0072     /**
0073      * Destructor. */
0074     ~Plane3D() = default;
0075 
0076     /**
0077      * Assignment. */
0078     Plane3D<T> & operator=(const Plane3D<T> &) = default;
0079 
0080     /**
0081      * Move assignment. */
0082     Plane3D<T> & operator=(Plane3D<T> &&) = default;
0083 
0084     /**
0085      * Returns the a-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
0086     T a() const { return a_; }
0087     /**
0088      * Returns the b-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
0089     T b() const { return b_; }
0090     /**
0091      * Returns the c-coefficient in the plane equation: a*x+b*y+c*z+d=0. */
0092     T c() const { return c_; }
0093     /**
0094      * Returns the free member of the plane equation: a*x+b*y+c*z+d=0. */
0095     T d() const { return d_; }
0096 
0097     /**
0098      * Returns normal. */
0099     Normal3D<T> normal() const { return Normal3D<T>(a_,b_,c_); }
0100 
0101     /**
0102      * Normalization. */
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      * Returns distance to the point. */
0111     T distance(const Point3D<T> & p) const {
0112       return a()*p.x() + b()*p.y() + c()*p.z() + d();
0113     }
0114 
0115     /**
0116      * Returns projection of the point to the plane. */
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      * Returns projection of the origin to the plane. */
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      * Test for equality. */
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      * Test for inequality. */
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      * Transformation by Transform3D. */
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    * Output to the stream.
0153    * @relates Plane3D
0154    */
0155   std::ostream & operator<<(std::ostream & os, const Plane3D<float> & p);
0156 
0157   /**
0158    * Output to the stream.
0159    * @relates Plane3D
0160    */
0161   std::ostream & operator<<(std::ostream & os, const Plane3D<double> & p);
0162 
0163 } /* namespace HepGeom */
0164 
0165 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
0166 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
0167 typedef HepGeom::Plane3D<double> HepPlane3D;
0168 #endif
0169 
0170 #endif /* HEP_PLANE3D_H */