g4tools  5.4.0
plane
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_plane
5 #define tools_plane
6 
7 #include "line"
8 
9 namespace tools {
10 
11 template <class VEC3>
12 class plane {
13 protected:
14  typedef typename VEC3::elem_t T;
15 public:
16  plane(){}
17 
18  plane(const VEC3& a_p0,const VEC3& a_p1,const VEC3& a_p2) {
19  // Construct a plane given 3 points.
20  // Orientation is computed by taking (p1 - p0) x (p2 - p0) and
21  // pointing the normal in that direction.
22 
23  VEC3 P = a_p1;
24  P.subtract(a_p0);
25  VEC3 P2 = a_p2;
26  P2.subtract(a_p0);
27  P.cross(P2,m_normal);
28  if(!m_normal.normalize()) {} //throw
29  m_distance =
30  m_normal.v0() * a_p0.v0() +
31  m_normal.v1() * a_p0.v1() +
32  m_normal.v2() * a_p0.v2();
33  }
34 
35  plane(const VEC3& a_normal,const T& a_distance){
36  set(a_normal,a_distance);
37  }
38 
39  plane(const VEC3& a_normal,const VEC3& a_point){
40  set(a_normal,a_point);
41  }
42 
43  virtual ~plane() {}
44 public:
45  plane(const plane& a_from)
46  :m_normal(a_from.m_normal)
47  ,m_distance(a_from.m_distance)
48  {}
49  plane& operator=(const plane& a_from) {
50  m_normal = a_from.m_normal;
51  m_distance = a_from.m_distance;
52  return *this;
53  }
54 
55 public:
56  bool is_valid() const {return m_normal.length()?true:false;}
57 
58  void offset(const T& a_distance){
59  // Offset a plane by a given distance.
60  m_distance += a_distance;
61  }
62 
63  bool intersect(const line<VEC3>& a_line,VEC3& a_intersection) const {
64  // Intersect line and plane, returning true if there is an intersection
65  // false if line is parallel to plane
66  const VEC3& pos = a_line.position();
67  const VEC3& dir = a_line.direction();
68  T d = m_normal.dot(dir);
69  if(d==T()) return false;
70  T t = (m_distance - m_normal.dot(pos))/d;
71  a_intersection = dir;
72  a_intersection.multiply(t);
73  a_intersection.add(pos);
74  //a_intersection = pos + t * dir;
75  return true;
76  }
77 
78  bool is_in_half_space(const VEC3& a_point) const {
79  // Returns true if the given point is within the half-space
80  // defined by the plane
81  //vec pos = m_normal * m_distance;
82  VEC3 pos = m_normal;
83  pos.multiply(-m_distance);
84  pos.add(a_point);
85  return (m_normal.dot(pos) >= T() ? true : false);
86  }
87 
88  const VEC3& normal() const {return m_normal;}
89 
90  T distance_from_origin() const {return m_distance;}
91 
92  T distance(const VEC3& a_point) const {
93  // Return the distance from point to plane. Positive distance means
94  // the point is in the plane's half space.
95  return a_point.dot(m_normal) - m_distance;
96  }
97 
98  void set(const VEC3& a_normal,const T& a_distance){
99  m_normal = a_normal;
100  if(!m_normal.normalize()) {} //throw
101  m_distance = a_distance;
102  }
103 
104  void set(const VEC3& a_normal,const VEC3& a_point){
105  // Construct a plane given normal and a point to pass through
106  // Orientation is given by the normal vector n.
107  m_normal = a_normal;
108  if(!m_normal.normalize()) {} //throw
109  m_distance =
110  m_normal.v0() * a_point.v0() +
111  m_normal.v1() * a_point.v1() +
112  m_normal.v2() * a_point.v2();
113  }
114 
115 public: //iv2sg
116  const VEC3& getNormal() const {return m_normal;}
117 protected:
118  // equation of the plane is :
119  // norm[0]*x+norm[1]*y+norm[2]*z = dist
120 
121  VEC3 m_normal; //normalized.
123 };
124 
125 }
126 
127 #endif
tools::plane::operator=
plane & operator=(const plane &a_from)
Definition: plane:49
tools::plane::is_in_half_space
bool is_in_half_space(const VEC3 &a_point) const
Definition: plane:78
tools::plane::distance
T distance(const VEC3 &a_point) const
Definition: plane:92
tools::plane
Definition: plane:12
line
tools::plane::intersect
bool intersect(const line< VEC3 > &a_line, VEC3 &a_intersection) const
Definition: plane:63
tools::plane::set
void set(const VEC3 &a_normal, const T &a_distance)
Definition: plane:98
tools::plane::is_valid
bool is_valid() const
Definition: plane:56
tools::plane::normal
const VEC3 & normal() const
Definition: plane:88
tools::plane::plane
plane(const VEC3 &a_normal, const T &a_distance)
Definition: plane:35
tools::plane::distance_from_origin
T distance_from_origin() const
Definition: plane:90
tools::line::direction
const VEC3 & direction() const
Definition: line:63
tools::plane::plane
plane()
Definition: plane:16
tools::plane::offset
void offset(const T &a_distance)
Definition: plane:58
tools::plane::T
VEC3::elem_t T
Definition: plane:14
tools::plane::plane
plane(const VEC3 &a_normal, const VEC3 &a_point)
Definition: plane:39
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::plane::plane
plane(const plane &a_from)
Definition: plane:45
tools::plane::m_distance
T m_distance
Definition: plane:122
tools::plane::plane
plane(const VEC3 &a_p0, const VEC3 &a_p1, const VEC3 &a_p2)
Definition: plane:18
tools::line::position
const VEC3 & position() const
Definition: line:62
tools::plane::m_normal
VEC3 m_normal
Definition: plane:121
tools::plane::getNormal
const VEC3 & getNormal() const
Definition: plane:116
tools::plane::set
void set(const VEC3 &a_normal, const VEC3 &a_point)
Definition: plane:104
tools::line
Definition: line:13
tools::plane::~plane
virtual ~plane()
Definition: plane:43