Plane.hpp
Go to the documentation of this file.
1 
7 #include <etk/types.hpp>
8 
9 #pragma once
10 
11 #include <etk/debug.hpp>
12 #include <vector>
13 
14 namespace etk {
19  template <typename T> class Plane {
20  private:
21  etk::Vector3D<T> m_normal;
22  T m_intercept;
23  public:
27  Plane() :
28  m_normal(0, 0, 0),
29  m_intercept(0) {
30 
31  }
37  Plane(etk::Vector3D<T> _normal, T _intercept=0) :
38  m_normal(_normal),
39  m_intercept(_intercept) {
40 
41  }
46  Plane(const Plane& _obj) :
47  m_normal(_obj.m_normal),
48  m_intercept(_obj.m_intercept) {
49 
50  }
55  void setNormal(const etk::Vector3D<T>& _obj) {
56  m_normal = _obj;
57  }
62  void setIntercept(float _intercept) {
63  m_intercept=_intercept;
64  }
72  const etk::Vector3D<T>& _p1,
73  const etk::Vector3D<T>& _p2) {
74  m_normal = (_p1 - _p0).cross(_p2 - _p0);
75  m_normal.normalize();
76  calculateIntercept(_p0);
77  }
82  void calculateIntercept(const etk::Vector3D<T>& _pointOnPlane) {
83  m_intercept=-m_normal.dot(_pointOnPlane);
84  }
88  void normalize() {
89  float normalLength = m_normal.getLength();
90  if (normalLength == 0) {
91  return;
92  }
93  m_normal /= normalLength;
94  m_intercept /= normalLength;
95  }
100  const etk::Vector3D<T>& getNormal() const {
101  return m_normal;
102  }
107  T getIntercept() const {
108  return m_intercept;
109  }
117  const Plane<T>& _p3) {
118  float denominator = m_normal.dot((_p2.m_normal).cross(_p3.m_normal));
119  //scalar triple product of normals
120  if(denominator==0.0f) {
121  //no intersection
122  return etk::Vector3D<T>(0,0,0);
123  }
124  etk::Vector3D<T> temp1, temp2, temp3;
125  temp1 = (_p2.m_normal.cross(_p3.m_normal))*m_intercept;
126  temp2 = (_p3.m_normal.cross(m_normal)) * _p2.m_intercept;
127  temp3 = (m_normal.cross(_p2.m_normal)) * _p3.m_intercept;
128  return (temp1+temp2+temp3) / (-denominator);
129  }
135  float getDistance(const etk::Vector3D<T>& _point) const {
136  return _point.x() * m_normal.x()
137  + _point.y() * m_normal.y()
138  + _point.z() * m_normal.z()
139  + m_intercept;
140  }
147  Plane<T> linearInterpolate(const Plane<T>& _p2, float _factor) {
148  Plane<T> result;
149  result.m_normal=m_normal*(1.0f-_factor) + _p2.m_normal*_factor;
150  result.m_normal.normalize();
151  result.m_intercept=m_intercept*(1.0f-_factor) + _p2.m_intercept*_factor;
152  return result;
153  }
160  bool operator==(const Plane<T>& _obj) const {
161  if( m_normal == _obj.m_normal
162  && m_intercept == _obj.m_intercept) {
163  return true;
164  }
165  return false;
166  }
173  bool operator!=(const Plane<T>& _obj) const {
174  return!((*this) == _obj);
175  }
176  };
177 }
178 
179 
void setNormal(const etk::Vector3D< T > &_obj)
Set the normal of the plane.
Definition: Plane.hpp:55
T getIntercept() const
Get intercept Value of the plane.
Definition: Plane.hpp:107
void setFromPoints(const etk::Vector3D< T > &_p0, const etk::Vector3D< T > &_p1, const etk::Vector3D< T > &_p2)
Set the plane with 3 points in the space.
Definition: Plane.hpp:71
Plane(const Plane &_obj)
Copy constructor of a Plane.
Definition: Plane.hpp:46
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
const T & y() const
Get Y value.
Definition: Vector3D.hpp:415
Plane< T > linearInterpolate(const Plane< T > &_p2, float _factor)
Create a linear interpolation of the plane with an other.
Definition: Plane.hpp:147
Plane()
Constructor of a Plane.
Definition: Plane.hpp:27
const T & z() const
Get Z value.
Definition: Vector3D.hpp:422
etk::Vector3D< T > intersect3(const Plane< T > &_p2, const Plane< T > &_p3)
Get the intersection between 3 planes.
Definition: Plane.hpp:116
bool operator!=(const Plane< T > &_obj) const
In-Equality compare operator with an other object.
Definition: Plane.hpp:173
Plane equation template: define a plane equation.
Definition: Plane.hpp:19
float dot(const Vector3D< T > &_obj) const
Return the dot product.
Definition: Vector3D.hpp:157
void normalize()
Normalize tha plane properties.
Definition: Plane.hpp:88
Plane(etk::Vector3D< T > _normal, T _intercept=0)
Constructor of a Plane.
Definition: Plane.hpp:37
float getDistance(const etk::Vector3D< T > &_point) const
Get distance from a point to the plane.
Definition: Plane.hpp:135
Vectorial 3-dimention vector (x/y/z)
Definition: Vector3D.hpp:20
void setIntercept(float _intercept)
Set interception value of the plane.
Definition: Plane.hpp:62
void calculateIntercept(const etk::Vector3D< T > &_pointOnPlane)
Calculate interception value with a point in the plane.
Definition: Plane.hpp:82
const T & x() const
Get X value.
Definition: Vector3D.hpp:408
const etk::Vector3D< T > & getNormal() const
Get the normal of the plane.
Definition: Plane.hpp:100
bool operator==(const Plane< T > &_obj) const
Equality compare operator with an other object.
Definition: Plane.hpp:160
Vector3D< T > cross(const Vector3D< T > &_obj) const
Return the cross product between this and another vector.
Definition: Vector3D.hpp:267
Vector3D< T > & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: Vector3D.hpp:216