vgl_line_segment_2d.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_line_segment_2d.h
2 #ifndef vgl_line_segment_2d_h_
3 #define vgl_line_segment_2d_h_
4 //:
5 // \file
6 // \author mccane@cs.otago.ac.nz: but copied from vgl_line_segment_3d
7 //
8 // \verbatim
9 // Modifications
10 // Peter Vanroose - 9 July 2001 - Inlined constructors
11 // Peter Vanroose - 27 June 2001 - Added operator==
12 // J.L. Mundy - 13 April 2003 - Added angle and line coefficient functions
13 // \endverbatim
14 
15 #include <iosfwd>
16 #ifdef _MSC_VER
17 # include <vcl_msvc_warnings.h>
18 #endif
19 #include <vgl/vgl_point_2d.h> // data member of this class
20 
21 //: Represents a 2D line segment using two points.
22 template <class Type>
24 {
25  //: One end of line segment
27  //: The other end of the line segment
29 
30  public:
31  //: Default constructor - does not initialise!
32  inline vgl_line_segment_2d() = default;
33 
34  //: Copy constructor
37 
38  //: Construct from two end points
40  vgl_point_2d<Type> const& p2)
41  : point1_(p1), point2_(p2) {}
42 
43  //: Destructor
44  inline ~vgl_line_segment_2d() = default;
45 
46  //: One end-point of the line segment.
47  inline vgl_point_2d<Type> point1() const { return point1_; } // return a copy
48 
49  //: The other end-point of the line segment.
50  inline vgl_point_2d<Type> point2() const { return point2_; } // return a copy
51 
52  //: The equality comparison operator
53  inline bool operator==(vgl_line_segment_2d<Type> const& l) const {
54  return (this==&l) || (point1() == l.point1() && point2() == l.point2())
55  || (point1() == l.point2() && point2() == l.point1()); }
56 
57  //: The inequality comparison operator.
58  inline bool operator!=(vgl_line_segment_2d<Type>const& other)const{return !operator==(other);}
59 
60  // A consistent interface with vgl_line_2d:
61 
62  //: Parameter a of line a*x + b*y + c = 0
63  Type a() const;
64 
65  //: Parameter b of line a*x + b*y + c = 0
66  Type b() const;
67 
68  //: Parameter c of line a*x + b*y + c = 0
69  Type c() const;
70 
71  //: unit vector describing line direction
73 
74  //: unit vector orthogonal to line
76 
77  //: angle with the oriented horizontal line y=0, measured in radians.
78  // Returns values between -pi and pi.
79  double slope_radians() const;
80 
81  //: angle with the oriented horizontal line y=0, measured in 360-degrees.
82  // Returns values between -180 and 180.
83  double slope_degrees() const;
84 
85  //: Assignment
86  inline void set(vgl_point_2d<Type> const& p1, vgl_point_2d<Type> const& p2) {
87  point1_ = p1; point2_ = p2; }
88 
89  //: Return a point on the line defined by a scalar parameter \a t.
90  // \a t=0.0 corresponds to point1 and \a t=1.0 to point2.
91  // 0<t<1 for points on the segment between point1 and point2.
92  // t<0 for points on the (infinite) line, outside the segment, and closer to point1 than to point2.
93  // t>1 for points on the (infinite) line, outside the segment, and closer to point2 than to point1.
94  inline vgl_point_2d<Type> point_t(const double t) const { return point1() + t*(point2_-point1_); }
95 };
96 
97 //: Write to stream
98 // \relatesalso vgl_line_segment_2d
99 template <class Type>
100 std::ostream& operator<<(std::ostream& s, const vgl_line_segment_2d<Type>& p);
101 
102 //: Read from stream
103 // \relatesalso vgl_line_segment_2d
104 template <class Type>
105 std::istream& operator>>(std::istream& is, vgl_line_segment_2d<Type>& p);
106 
107 #define VGL_LINE_SEGMENT_2D_INSTANTIATE(T) extern "please include vgl/vgl_line_segment_2d.hxx first"
108 
109 #endif // vgl_line_segment_2d_h_
Represents a 2D line segment using two points.
Definition: vgl_fwd.h:18
Type b() const
Parameter b of line a*x + b*y + c = 0.
vgl_vector_2d< Type > normal() const
unit vector orthogonal to line.
bool operator==(vgl_line_segment_2d< Type > const &l) const
The equality comparison operator.
a point in 2D nonhomogeneous space
vgl_point_2d< Type > point1_
One end of line segment.
Type a() const
Parameter a of line a*x + b*y + c = 0.
double slope_degrees() const
angle with the oriented horizontal line y=0, measured in 360-degrees.
vgl_line_segment_2d(vgl_point_2d< Type > const &p1, vgl_point_2d< Type > const &p2)
Construct from two end points.
vgl_line_segment_2d(vgl_line_segment_2d< Type > const &l)
Copy constructor.
~vgl_line_segment_2d()=default
Destructor.
vgl_point_2d< Type > point2_
The other end of the line segment.
vgl_point_2d< Type > point_t(const double t) const
Return a point on the line defined by a scalar parameter t.
Type c() const
Parameter c of line a*x + b*y + c = 0.
vgl_point_2d< Type > point2() const
The other end-point of the line segment.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
bool operator!=(vgl_line_segment_2d< Type >const &other) const
The inequality comparison operator.
vgl_vector_2d< Type > direction() const
unit vector describing line direction.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
double slope_radians() const
angle with the oriented horizontal line y=0, measured in radians.
#define l
Represents a cartesian 2D point.
Definition: vgl_area.h:7
void set(vgl_point_2d< Type > const &p1, vgl_point_2d< Type > const &p2)
Assignment.
vgl_line_segment_2d()=default
Default constructor - does not initialise!.
vgl_point_2d< Type > point1() const
One end-point of the line segment.