vgl_line_segment_3d.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_line_segment_3d.h
2 #ifndef vgl_line_segment_3d_h_
3 #define vgl_line_segment_3d_h_
4 //:
5 // \file
6 // \brief line segment in 3D nonhomogeneous space
7 // \author awf@robots.ox.ac.uk
8 //
9 // \verbatim
10 // Modifications
11 // Peter Vanroose - 9 July 2001 - Inlined constructors
12 // Peter Vanroose - 27 June 2001 - Added operator==
13 // Kieran O'Mahony - 13 Aug 2007 - Added contains()
14 // \endverbatim
15 
16 #include <iosfwd>
17 #ifdef _MSC_VER
18 # include <vcl_msvc_warnings.h>
19 #endif
20 #include <vgl/vgl_point_3d.h> // data member of this class
21 
22 //: Represents a 3D line segment using two points.
23 template <class Type>
25 {
28 
29  public:
30  //: Default constructor - does not initialise!
31  inline vgl_line_segment_3d() = default;
32 
33  //: Copy constructor
35  : point1_(l.point1()), point2_(l.point2()) {}
36 
37  //: Construct from two end points
39  vgl_point_3d<Type> const& p2)
40  : point1_(p1), point2_(p2) {}
41 
42  inline ~vgl_line_segment_3d() = default;
43 
44  inline vgl_point_3d<Type> point1() const { return point1_; } // return a copy
45  inline vgl_point_3d<Type> point2() const { return point2_; } // return a copy
46 
47  //: the comparison operator
48  inline bool operator==(vgl_line_segment_3d<Type> const& l) const {
49  return (this==&l) || (point1() == l.point1() && point2() == l.point2())
50  || (point1() == l.point2() && point2() == l.point1()); }
51 
52  inline bool operator!=(vgl_line_segment_3d<Type>const& other)const{return !operator==(other);}
53 
54  //: assignment
55  inline void set(vgl_point_3d<Type> const& p1, vgl_point_3d<Type> const& p2) { point1_ = p1; point2_ = p2; }
56 
57  //: Return the direction vector of this line (not normalised - but perhaps it should be, like other line classes?)
58  inline vgl_vector_3d<Type> direction() const { return point2()-point1(); }
59 
60  //: Return a point on the line defined by a scalar parameter \a t.
61  // \a t=0.0 corresponds to point1 and \a t=1.0 to point2.
62  // 0<t<1 for points on the segment between point1 and point2.
63  // t<0 for points on the (infinite) line, outside the segment, and closer to point1 than to point2.
64  // t>1 for points on the (infinite) line, outside the segment, and closer to point2 than to point1.
65  //\note Assumes that direction() is not normalized.
66  inline vgl_point_3d<Type> point_t(const double t) const { return point1() + t*direction(); }
67 
68  //: Check if point \a p is on the line segment
69  inline bool contains(const vgl_point_3d<Type>& p ) const
70  {
71  double r = (point1_ - point2_).length() - ( (point1_ - p).length() + (point2_ - p).length() );
72  return r < 1e-8 && r > -1e-8;
73  }
74 };
75 
76 //: Write to stream
77 // \relatesalso vgl_line_segment_3d
78 template <class Type>
79 std::ostream& operator<<(std::ostream& s, const vgl_line_segment_3d<Type>& p);
80 
81 //: Read from stream
82 // \relatesalso vgl_line_segment_3d
83 template <class Type>
84 std::istream& operator>>(std::istream& is, vgl_line_segment_3d<Type>& p);
85 #define VGL_LINE_SEGMENT_3D_INSTANTIATE(T) extern "please include vgl/vgl_line_segment_3d.hxx first"
86 
87 #endif // vgl_line_segment_3d_h_
~vgl_line_segment_3d()=default
vgl_line_segment_3d(vgl_line_segment_3d< Type > const &l)
Copy constructor.
vgl_point_3d< Type > point1() const
void set(vgl_point_3d< Type > const &p1, vgl_point_3d< Type > const &p2)
assignment.
vgl_point_3d< Type > point_t(const double t) const
Return a point on the line defined by a scalar parameter t.
vgl_point_3d< Type > point2() const
Represents a cartesian 3D point.
Definition: vgl_fwd.h:11
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
vgl_point_3d< Type > point1_
a point in 3D nonhomogeneous space
bool contains(const vgl_point_3d< Type > &p) const
Check if point p is on the line segment.
Represents a 3D line segment using two points.
Definition: vgl_fwd.h:19
vgl_line_segment_3d()=default
Default constructor - does not initialise!.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
bool operator!=(vgl_line_segment_3d< Type >const &other) const
#define l
bool operator==(vgl_line_segment_3d< Type > const &l) const
the comparison operator.
vgl_vector_3d< Type > direction() const
Return the direction vector of this line (not normalised - but perhaps it should be,...
double length(v const &a)
Return the length of a vector.
Definition: vgl_vector_2d.h:94
vgl_point_3d< Type > point2_
vgl_line_segment_3d(vgl_point_3d< Type > const &p1, vgl_point_3d< Type > const &p2)
Construct from two end points.