IMP logo
IMP Reference Guide  2.22.0
The Integrative Modeling Platform
Segment3D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/Segment3D.h
3  * \brief Simple implementation of segments in 3D
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPALGEBRA_SEGMENT_3D_H
9 #define IMPALGEBRA_SEGMENT_3D_H
10 
11 #include <IMP/algebra/algebra_config.h>
12 #include "Vector3D.h"
13 #include "BoundingBoxD.h"
14 #include "algebra_macros.h"
15 #include "GeometricPrimitiveD.h"
16 #include <iostream>
17 #include <cereal/access.hpp>
18 #include "constants.h"
19 
20 IMPALGEBRA_BEGIN_NAMESPACE
21 //! Simple implementation of segments in 3D
22 /** A line segment is defined by a start and end point (as Vector3D).
23  \geometry
24  */
25 class IMPALGEBRAEXPORT Segment3D : public GeometricPrimitiveD<3> {
26  public:
27  Segment3D() {}
28  Segment3D(const Vector3D &start, const Vector3D &end);
29  //! Get the start (i==0) or end (i==1) point of the segment
30  const Vector3D &get_point(unsigned int i) const {
31  IMP_INTERNAL_CHECK(i < 2, "Invalid point index");
32  return p_[i];
33  }
34  Vector3D get_middle_point() const { return .5 * p_[0] + .5 * p_[1]; }
35  //! Get a normalized direction vector from get_point(0) to get_point(1).
36  Vector3D get_direction() const { return (p_[1] - p_[0]).get_unit_vector(); }
37  double get_length() const;
39  { out << spaces_io(p_[0]) << ": " << spaces_io(p_[1]); });
40 
41  private:
42  Vector3D p_[2];
43 
44  friend class cereal::access;
45 
46  template<class Archive> void serialize(Archive &ar) {
47  ar(p_[0], p_[1]);
48  }
49 };
50 
51 IMP_LINEAR_GEOMETRY_METHODS(Segment3D, segment_3d,
52  return BoundingBoxD<3>(g.get_point(0)) +
53  BoundingBoxD<3>(g.get_point(1)));
54 
55 //! Return the 'relative' projection of a point p onto the line that contains s.
56 /** Formally, the projection of p onto the line through s is s[0]+f*(s[1]-s[0])
57  f is in the range [0..1] if the projection of p is inside s.
58 
59  @param s segment in 3D
60  @param p point in 3D
61 
62  @return the 'relative' projection of p onto the line containing s
63 */
64 IMPALGEBRAEXPORT double get_relative_projection_on_segment(
65  const Segment3D &s, const algebra::Vector3D &p);
66 
67 //! Get the distance between a segment and a point
68 /** \see Segment3D */
69 IMPALGEBRAEXPORT double get_distance(const Segment3D &s, const Vector3D &p);
70 
71 //! Get the distance between two segments
72 /** \see Segment3D */
73 IMPALGEBRAEXPORT double get_distance(const Segment3D &a, const Segment3D &b);
74 
75 IMPALGEBRA_END_NAMESPACE
76 
77 #endif /* IMPALGEBRA_SEGMENT_3D_H */
Base class for geometric types.
double get_distance(const Segment3D &a, const Segment3D &b)
Get the distance between two segments.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
#define IMP_LINEAR_GEOMETRY_METHODS(Name, name, bounding_box)
Implement the needed namespace methods for a geometry type.
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:139
Base class for geometric types.
Vector3D get_direction() const
Get a normalized direction vector from get_point(0) to get_point(1).
Definition: Segment3D.h:36
A bounding box in D dimensions.
Various useful constants.
Simple implementation of segments in 3D.
Definition: Segment3D.h:25
double get_relative_projection_on_segment(const Segment3D &s, const algebra::Vector3D &p)
Return the 'relative' projection of a point p onto the line that contains s.
VectorD< 3 > Vector3D
Definition: VectorD.h:408
Simple 3D vector class.
Various helper macros.
const Vector3D & get_point(unsigned int i) const
Get the start (i==0) or end (i==1) point of the segment.
Definition: Segment3D.h:30