Virtual Testbed
Ship dynamics simulator for extreme conditions
plane.hh
1 #ifndef VTESTBED_GEOMETRY_PLANE_HH
2 #define VTESTBED_GEOMETRY_PLANE_HH
3 
4 #include <iosfwd>
5 
6 #include <vtestbed/geometry/math.hh>
7 #include <vtestbed/geometry/plane_position.hh>
8 #include <vtestbed/geometry/ray.hh>
9 #include <vtestbed/geometry/types.hh>
10 
11 namespace vtb {
12 
13  namespace geometry {
14 
27  template <class T, int N>
28  class Plane {
29 
30  public:
31  static constexpr const int dimensions = N;
32  using scalar_type = T;
33  using value_type = Vertex<T,N>;
34  using reference = value_type&;
35  using const_reference = const value_type&;
36 
37  private:
38  value_type _normal{T{}};
39  value_type _origin{T{}};
40 
41  public:
42 
43  Plane() = default;
44 
45  inline explicit
46  Plane(const_reference normal, const_reference origin):
47  _normal{normal}, _origin{origin} {}
48 
49  inline explicit
50  Plane(const_reference normal):
51  _normal{normal} {}
52 
63  explicit
64  Plane(const_reference a, const_reference b, const_reference c);
65 
67  inline const_reference normal() const { return this->_normal; }
68 
70  inline const_reference origin() const { return this->_origin; }
71 
73  inline void invert() { this->_normal = -this->_normal; }
74 
86  inline T
87  operator()(const_reference point) const {
88  return dot(normal(), point-origin());
89  }
90 
97  inline T operator()() const { return -dot(normal(), origin()); }
98 
104  inline value_type
105  project(const_reference point) const {
106  return point - dot(normal(), point-origin())*normal();
107  }
108 
113  inline value_type
114  project_vector(const_reference vector) const {
115  return vector - dot(normal(), vector)*normal();
116  }
117 
126  int redundant_dimension() const;
127 
134  Basis<T,N> basis() const;
135 
151 
152  inline void
153  clear() {
154  this->_normal = T{};
155  this->_origin = T{};
156  }
157 
158  void gnuplot(std::ostream& out) const;
159 
160  };
161 
162  template <class T, int N>
163  std::ostream&
164  operator<<(std::ostream& out, const Plane<T,N>& rhs);
165 
172  template <class T, int N>
173  inline Vertex<T,N>
174  intersection(const Ray<T,N>& ray, const Plane<T,N>& plane) {
175  T num = dot(plane.origin() - ray.origin(), plane.normal());
176  T den = dot(ray.direction(), plane.normal());
177  return ray.origin() + ray.direction() * num / den;
178  }
179 
180  template <class T, int N>
181  inline Plane_position
182  compare(const Plane<T,N> plane, const Vertex<T,N>& point, T eps) {
183  T t = plane(point);
184  if (t < -eps) { return Plane_position::Back; }
185  else if (t > eps) { return Plane_position::Front; }
186  else { return Plane_position::Same; }
187  }
188 
189  template <class T, int N>
190  inline Plane_position
191  compare(const Plane<T,N> lhs, const Plane<T,N>& rhs, T eps) {
192  T t = dot(lhs.normal(), rhs.normal());
193  if (t < -eps) { return Plane_position::Back; }
194  else if (t > eps) { return Plane_position::Front; }
195  else { return Plane_position::Same; }
196  }
197 
198  }
199 
200 }
201 
202 #endif // vim:filetype=cpp
const_reference normal() const
A vector that is orthogonal to the plane.
Definition: plane.hh:67
Coordinate_system< T, N > coordinate_system() const
Coordinate system defined by the and the origin.
Basis< T, N > basis() const
Vector basis for the coordinate system defined by the and the origin.
void invert()
Invert plane orientation (the direction of the normal).
Definition: plane.hh:73
const_reference origin() const
A point on the plane.
Definition: plane.hh:70
int redundant_dimension() const
The index of dimension that has nought basis vector.
value_type project_vector(const_reference vector) const
Project vector on the plane.
Definition: plane.hh:114
T operator()() const
Substitute nought into the plane equation to get .
Definition: plane.hh:97
Main namespace.
Definition: convert.hh:9
T operator()(const_reference point) const
Substitute point p to the plane equation.
Definition: plane.hh:87
Three- and two-dimensional plane.
Definition: plane.hh:28
value_type project(const_reference point) const
Project point on the plane.
Definition: plane.hh:105
Vertex< T, N > intersection(const Ray< T, N > &ray, const Plane< T, N > &plane)
Returns intersection point of ray and plane. Edge cases are ignored.
Definition: plane.hh:174