IMP logo
IMP Reference Guide  develop.d97d4ead1f,2024/11/21
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-2022 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 #include <cereal/access.hpp>
16 
17 IMPALGEBRA_BEGIN_NAMESPACE
18 
19 //! Represent a plane in 3D.
20 /** \geometry */
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), normal_(normal_to_plane) {
32  IMP_USAGE_CHECK_FLOAT_EQUAL(normal_.get_squared_magnitude(), 1,
33  "The normal vector must be normalized");
34  }
35  Vector3D get_point_on_plane() const { return normal_ * distance_; }
36  const Vector3D &get_normal() const { return normal_; }
37  //! Project the point onto the plane
38  Vector3D get_projected(const Vector3D &p) const {
39  return p - normal_ * (normal_ * p - distance_);
40  }
41  /** @name Orientation
42  Up is the direction of the normal. You really shouldn't use
43  these as they aren't very reliable.
44  @{
45  */
46  bool get_is_above(const Vector3D &p) const { return get_height(p) > 0; }
47  bool get_is_below(const Vector3D &p) const { return get_height(p) < 0; }
48  /** @} */
49  double get_height(const Vector3D &p) const { return normal_ * p - distance_; }
50  IMP_SHOWABLE_INLINE(Plane3D,
51  { out << "(" << distance_ << ": " << spaces_io(normal_) << ")"; });
52 
53  //! Return the plane with the opposite normal
54  Plane3D get_opposite() const { return Plane3D(-distance_, -normal_); }
55  double get_distance_from_origin() const { return distance_; }
56 
57 private:
58  double distance_;
59  Vector3D normal_; // normal to plane
60 
61  friend class cereal::access;
62 
63  template<class Archive> void serialize(Archive &ar) {
64  ar(distance_, normal_);
65  }
66 
67 };
68 
69 //! Return the distance between a plane and a point in 3D
70 /** \see Plane3D */
71 inline double get_distance(const Plane3D &pln, const Vector3D &p) {
72  return (pln.get_projected(p) - p).get_magnitude();
73 }
74 
75 //! Return the point reflected about the plane
76 inline Vector3D get_reflected(const Plane3D &pln, const Vector3D &p) {
77  Vector3D proj = pln.get_projected(p);
78  return p + 2 * (proj - p);
79 }
80 
81 IMP_AREA_GEOMETRY_METHODS(Plane3D, plane_3d, IMP_UNUSED(g);
82  return std::numeric_limits<double>::infinity(), {
83  IMP_UNUSED(g);
84  Vector3D ip = get_ones_vector_d<3>(std::numeric_limits<double>::infinity());
85  return BoundingBoxD<3>(ip) + BoundingBox3D(-ip);
86 });
87 IMPALGEBRA_END_NAMESPACE
88 
89 #endif /* IMPALGEBRA_PLANE_3D_H */
Base class for geometric types.
double get_height(const Surface &s, const XYZR &d)
Get height of sphere above surface.
Definition: Surface.h:129
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
BoundingBoxD< 3 > BoundingBox3D
Typedef for Python.
Definition: BoundingBoxD.h:183
#define IMP_USAGE_CHECK_FLOAT_EQUAL(expra, exprb, message)
Definition: check_macros.h:178
Vector3D get_reflected(const Plane3D &pln, const Vector3D &p)
Return the point reflected about the plane.
Definition: Plane3D.h:76
#define IMP_AREA_GEOMETRY_METHODS(Name, name, area, bounding_box)
Implement the needed namespace methods for a geometry type.
Base class for geometric types.
double get_distance(const Plane3D &pln, const Vector3D &p)
Return the distance between a plane and a point in 3D.
Definition: Plane3D.h:71
#define IMP_UNUSED(variable)
Plane3D get_opposite() const
Return the plane with the opposite normal.
Definition: Plane3D.h:54
A bounding box in D dimensions.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
Simple 3D vector class.
Vector3D get_projected(const Vector3D &p) const
Project the point onto the plane.
Definition: Plane3D.h:38
Represent a plane in 3D.
Definition: Plane3D.h:21