Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/lina/plane is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_plane
0005 #define tools_plane
0006 
0007 #include "line"
0008 
0009 namespace tools {
0010 
0011 template <class VEC3>
0012 class plane {
0013 protected:
0014   typedef typename VEC3::elem_t T;
0015 public:
0016   plane(){}
0017 
0018   plane(const VEC3& a_p0,const VEC3& a_p1,const VEC3& a_p2) {
0019     // Construct a plane given 3 points.
0020     // Orientation is computed by taking (p1 - p0) x (p2 - p0) and
0021     // pointing the normal in that direction.
0022 
0023     VEC3 P = a_p1;
0024     P.subtract(a_p0);
0025     VEC3 P2 = a_p2;
0026     P2.subtract(a_p0);
0027     P.cross(P2,m_normal);
0028     if(!m_normal.normalize()) {} //throw
0029     m_distance =
0030       m_normal.v0() * a_p0.v0() +
0031       m_normal.v1() * a_p0.v1() +
0032       m_normal.v2() * a_p0.v2();
0033   }
0034 
0035   plane(const VEC3& a_normal,const T& a_distance){
0036     set(a_normal,a_distance);
0037   }
0038 
0039   plane(const VEC3& a_normal,const VEC3& a_point){
0040     set(a_normal,a_point);
0041   }
0042 
0043   virtual ~plane() {}
0044 public:
0045   plane(const plane& a_from)
0046   :m_normal(a_from.m_normal)
0047   ,m_distance(a_from.m_distance)
0048   {}
0049   plane& operator=(const plane& a_from) {
0050     m_normal = a_from.m_normal;
0051     m_distance = a_from.m_distance;
0052     return *this;
0053   }
0054 
0055 public:
0056   bool is_valid() const {return m_normal.length()?true:false;}
0057 
0058   void offset(const T& a_distance){
0059     // Offset a plane by a given distance.
0060     m_distance += a_distance;
0061   }
0062 
0063   bool intersect(const line<VEC3>& a_line,VEC3& a_intersection) const {
0064     // Intersect line and plane, returning true if there is an intersection
0065     // false if line is parallel to plane
0066     const VEC3& pos = a_line.position();
0067     const VEC3& dir = a_line.direction();
0068     T d = m_normal.dot(dir);
0069     if(d==T()) return false;
0070     T t = (m_distance - m_normal.dot(pos))/d;
0071     a_intersection = dir;
0072     a_intersection.multiply(t);
0073     a_intersection.add(pos);
0074     //a_intersection = pos + t * dir;
0075     return true;
0076   }
0077 
0078   bool is_in_half_space(const VEC3& a_point) const {
0079     // Returns true if the given point is within the half-space
0080     // defined by the plane
0081     //vec pos = m_normal * m_distance;
0082     VEC3 pos = m_normal;
0083     pos.multiply(-m_distance);
0084     pos.add(a_point);
0085     return (m_normal.dot(pos) >= T() ? true : false);
0086   }
0087 
0088   const VEC3& normal() const {return m_normal;}
0089 
0090   T distance_from_origin() const {return m_distance;}
0091 
0092   T distance(const VEC3& a_point) const {
0093     // Return the distance from point to plane. Positive distance means
0094     // the point is in the plane's half space.
0095     return a_point.dot(m_normal) - m_distance;
0096   }
0097 
0098   void set(const VEC3& a_normal,const T& a_distance){
0099     m_normal = a_normal;
0100     if(!m_normal.normalize()) {} //throw
0101     m_distance = a_distance;
0102   }
0103 
0104   void set(const VEC3& a_normal,const VEC3& a_point){
0105     // Construct a plane given normal and a point to pass through
0106     // Orientation is given by the normal vector n.
0107     m_normal = a_normal;
0108     if(!m_normal.normalize()) {} //throw
0109     m_distance =
0110       m_normal.v0() * a_point.v0() +
0111       m_normal.v1() * a_point.v1() +
0112       m_normal.v2() * a_point.v2();
0113   }
0114 
0115 public: //iv2sg
0116   const VEC3& getNormal() const {return m_normal;}
0117 protected:
0118   // equation of the plane is :
0119   //  norm[0]*x+norm[1]*y+norm[2]*z = dist
0120 
0121   VEC3 m_normal; //normalized.
0122   T m_distance;
0123 };
0124 
0125 }
0126 
0127 #endif