IMP  2.0.1
The Integrative Modeling Platform
Plane3D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/Plane3D.h \brief Simple 3D plane class.
3  *
4  * Copyright 2007-2013 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPALGEBRA_PLANE_3D_H
9 #define IMPALGEBRA_PLANE_3D_H
10 
11 #include <IMP/algebra/algebra_config.h>
12 #include "Vector3D.h"
13 #include "BoundingBoxD.h"
14 #include "GeometricPrimitiveD.h"
15 
16 IMPALGEBRA_BEGIN_NAMESPACE
17 
18 /** Represent a plane in 3D.
19  \geometry
20 */
21 class Plane3D: public GeometricPrimitiveD<3> {
22 public:
23  Plane3D(){}
24  Plane3D(const Vector3D& point_on_plane,const Vector3D &normal_to_plane):
25  normal_(normal_to_plane) {
26  distance_= normal_*point_on_plane;
27  IMP_USAGE_CHECK_FLOAT_EQUAL(normal_.get_squared_magnitude(),1,
28  "The normal vector must be normalized");
29  }
30  Plane3D(double distance_to_plane ,const Vector3D &normal_to_plane):
31  distance_(distance_to_plane),
32  normal_(normal_to_plane){
33  IMP_USAGE_CHECK_FLOAT_EQUAL(normal_.get_squared_magnitude(),1,
34  "The normal vector must be normalized");
35  }
36  Vector3D get_point_on_plane() const {return normal_*distance_;}
37  const Vector3D &get_normal() const {return normal_;}
38  //! Project the point onto the plane
39  Vector3D get_projected(const Vector3D &p) const {
40  return p-normal_*(normal_*p-distance_);
41  }
42 #ifndef IMP_DOXYGEN
43  Vector3D get_projection(const Vector3D &p) const {
44  return get_projected(p);
45  }
46 #endif
47  /** @name Orientation
48  Up is the direction of the normal. You really shouldn't use
49  these as they aren't very reliable.
50  @{
51  */
52  bool get_is_above(const Vector3D &p) const {
53  return get_height(p) > 0;
54  }
55  bool get_is_below(const Vector3D &p) const {
56  return get_height(p) < 0;
57  }
58  /** @} */
59  double get_height(const Vector3D &p) const {
60  return normal_*p-distance_;
61  }
62  IMP_SHOWABLE_INLINE(Plane3D, {
63  out << "(" << distance_ << ": " << spaces_io(normal_)
64  << ")";
65  });
66 
67  //! Return the plane with the opposite normal
69  return Plane3D(-distance_, -normal_);
70  }
71  double get_distance_from_origin() const {
72  return distance_;
73  }
74 private:
75  double distance_;
76  Vector3D normal_; //normal to plane
77 };
78 
79 
80 //! Return the distance between a plane and a point in 3D
81 /** \relatesalso Plane3D */
82 inline double get_distance(const Plane3D& pln, const Vector3D &p) {
83  return (pln.get_projection(p)-p).get_magnitude();
84 }
85 
86 
87 //! return the point reflected about the plane
88 inline Vector3D get_reflected(const Plane3D &pln, const Vector3D &p) {
89  Vector3D proj= pln.get_projected(p);
90  return p+2*(proj-p);
91 }
92 
93 
94 IMP_AREA_GEOMETRY_METHODS(Plane3D, plane_3d,
95  IMP_UNUSED(g);
96  return std::numeric_limits<double>::infinity(),
97  {
98  IMP_UNUSED(g);
99  Vector3D ip
100  = get_ones_vector_d<3>(
101  std::numeric_limits<double>::infinity());
102  return BoundingBoxD<3>(ip)+ BoundingBox3D(-ip);
103  });
104 IMPALGEBRA_END_NAMESPACE
105 
106 #endif /* IMPALGEBRA_PLANE_3D_H */