Thea
Hyperplane3.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2011
12 //
13 //============================================================================
14 
15 #ifndef __Thea_Hyperplane3_hpp__
16 #define __Thea_Hyperplane3_hpp__
17 
18 #include "Common.hpp"
19 #include "HyperplaneN.hpp"
20 #include "Math.hpp"
21 
22 namespace Thea {
23 
25 template <typename T>
26 class /* THEA_API */ HyperplaneN<3, T> : public Internal::HyperplaneNBase<3, T>
27 {
28  private:
30 
31  public:
32  typedef typename BaseT::VectorT VectorT;
33 
34  static HyperplaneN fromThreePoints(VectorT const & point1, VectorT const & point2, VectorT const & point3)
35  {
36  HyperplaneN hyperplane;
37 
38  hyperplane.normal = (point2 - point1).cross(point3 - point1).normalized();
39  hyperplane.dist = hyperplane.normal.dot(point1);
40  return hyperplane;
41  }
42 
44  static HyperplaneN fromEquation(T const & a, T const & b, T const & c, T const & d)
45  {
46  HyperplaneN hyperplane;
47 
48  hyperplane.normal = VectorT(a, b, c);
49  T sqlen = hyperplane.normal.squaredNorm();
50  if (Math::fuzzyEq(sqlen, static_cast<T>(0)))
51  {
52  hyperplane.normal = VectorT::Zero();
53  hyperplane.dist = 0;
54  }
55  else
56  {
57  T len = std::sqrt(sqlen);
58  hyperplane.normal /= len;
59  hyperplane.dist = -d / len;
60  }
61 
62  return hyperplane;
63  }
64 
65  using BaseT::getEquation;
66 
68  void getEquation(T & a, T & b, T & c, T & d) const
69  {
70  a = this->normal.x();
71  b = this->normal.y();
72  c = this->normal.z();
73  d = -this->dist;
74  }
75 
76 }; // class HyperplaneN<3, T>
77 
78 #ifdef THEA_EXPORT_INSTANTIATION
79  template class THEA_API HyperplaneN<3, Real>;
80 #endif
81 
83 typedef HyperplaneN<3, Real> Plane3;
84 
85 } // namespace Thea
86 
87 #endif
Root namespace for the Thea library.
VectorT normal
The unit normal vector of the hyperplane.
T dist
The signed distance of the hyperplane from the origin.
A hyperplane ((N - 1)-flat) in N-dimensional space, where N is any positive (non-zero) integer and T ...
Definition: HyperplaneN.hpp:27
void getEquation(T &a, T &b, T &c, T &d) const
Get the coefficients a, b, c, d of the plane equation a * x + b * y + c * z + d = 0...
Definition: Hyperplane3.hpp:68
bool fuzzyEq(T const &a, T const &b, T const &tol)
Check if two numbers are approximately equal, with a given tolerance.
Definition: Math.hpp:210
static HyperplaneN fromEquation(T const &a, T const &b, T const &c, T const &d)
Construct a hyperplane given coefficients a, b, c, d of the plane equation a * x + b * y + c * z + d ...
Definition: Hyperplane3.hpp:44
Vector< N, T > VectorT
N-dimensional vector.
Definition: HyperplaneN.hpp:42
[Internal] Base class for hyperplanes ((N - 1)-flats) in N-dimensional space, where N is any positive...
Definition: HyperplaneN.hpp:38
HyperplaneN< 3, Real > Plane3
The default plane class in 3-dimensional real space.
Definition: Hyperplane3.hpp:83