1 // This is gel/vsol/vsol_line_2d.h
2 #ifndef vsol_line_2d_h_
3 #define vsol_line_2d_h_
4 //*****************************************************************************
5 //:
6 // \file
7 // \brief Straight line segment in a 2D space.
8 //
9 // The direction gives the orientation and the length of the segment
10 //
11 // \author Francois BERTEL
12 // \date   2000-04-28
13 //
14 // \verbatim
15 //  Modifications
16 //   2000-04-28 Francois BERTEL Creation
17 //   2000-06-17 Peter Vanroose  Implemented all operator==()s and type info
18 //   2001-07-03 Peter Vanroose  Added constructor from vgl_line_segment_2d
19 //   2001-07-03 Peter Vanroose  Replaced vnl_double_2 by vgl_vector_2d
20 //   2004-05-14 Peter Vanroose  Added describe()
21 // \endverbatim
22 //*****************************************************************************
23 
24 #include <string>
25 #include <iostream>
26 #include <iosfwd>
27 #include <vsol/vsol_curve_2d.h>
28 #include <vsol/vsol_point_2d_sptr.h>
29 #include <vgl/vgl_fwd.h> // vgl_line_segment_2d, vgl_homg_line_2d, vgl_point_2d
30 #include <vgl/vgl_homg_line_2d.h>
31 #include <vgl/vgl_line_segment_2d.h>
32 #include <vsl/vsl_binary_io.h>
33 #ifdef _MSC_VER
34 #  include <vcl_msvc_warnings.h>
35 #endif
36 
37 class vsol_line_2d : public vsol_curve_2d
38 {
39   //***************************************************************************
40   // Data members
41   //***************************************************************************
42 
43   //---------------------------------------------------------------------------
44   // Description: First point of the straight line segment
45   //---------------------------------------------------------------------------
46   vsol_point_2d_sptr p0_;
47 
48   //---------------------------------------------------------------------------
49   // Description: Last point of the straight line segment
50   //---------------------------------------------------------------------------
51   vsol_point_2d_sptr p1_;
52 
53  public:
54   //***************************************************************************
55   // Initialization
56   //***************************************************************************
57 
58   //---------------------------------------------------------------------------
59   //: Default Constructor
60   //---------------------------------------------------------------------------
61   vsol_line_2d();
62 
63   //---------------------------------------------------------------------------
64   //: Constructor from the direction and the middle point
65   //---------------------------------------------------------------------------
66   vsol_line_2d(vgl_vector_2d<double> const& new_direction,
67                const vsol_point_2d_sptr &new_middle);
68 
69   //---------------------------------------------------------------------------
70   //: Constructor from the direction and the middle point
71   //---------------------------------------------------------------------------
72   vsol_line_2d(vgl_vector_2d<double> const& new_direction,
73                const vgl_point_2d<double> &new_middle);
74 
75   //---------------------------------------------------------------------------
76   //: Constructor from the first and the last point of the straight line
77   //---------------------------------------------------------------------------
vsol_line_2d(vsol_point_2d_sptr const & new_p0,vsol_point_2d_sptr const & new_p1)78   vsol_line_2d(vsol_point_2d_sptr const& new_p0,
79                vsol_point_2d_sptr const& new_p1)
80     : vsol_curve_2d(), p0_(new_p0), p1_(new_p1) {}
81 
82   //---------------------------------------------------------------------------
83   //: Constructor from two vgl_point_2d (end points)
84   //---------------------------------------------------------------------------
85   vsol_line_2d(vgl_point_2d<double> const& p0, vgl_point_2d<double> const& p1);
86 
87   //---------------------------------------------------------------------------
88   //: Constructor from a vgl_line_segment_2d
89   //---------------------------------------------------------------------------
90   vsol_line_2d(vgl_line_segment_2d<double> const& l);
91 
92   //---------------------------------------------------------------------------
93   //: Copy constructor
94   //  no duplication of the points
95   //---------------------------------------------------------------------------
96   vsol_line_2d(vsol_line_2d const& other) = default;
97 
98   //---------------------------------------------------------------------------
99   //: Destructor
100   //---------------------------------------------------------------------------
101   ~vsol_line_2d() override = default;
102 
103   //---------------------------------------------------------------------------
104   //: Return `this' if `this' is a line_2d, 0 otherwise
105   //---------------------------------------------------------------------------
cast_to_line()106   vsol_line_2d const*cast_to_line()const override{return this;}
cast_to_line()107   vsol_line_2d *cast_to_line() override {return this;}
108 
109  private: // has been superseded by is_a()
110   //: Return the curve type
curve_type()111   vsol_curve_2d_type curve_type() const override { return vsol_curve_2d::LINE; }
112 
113  public:
114   //---------------------------------------------------------------------------
115   //: Clone `this': creation of a new object and initialization
116   //  See Prototype pattern
117   //---------------------------------------------------------------------------
118   vsol_spatial_object_2d* clone() const override;
119 
120   //***************************************************************************
121   // Access
122   //***************************************************************************
123 
124   //---------------------------------------------------------------------------
125   //: Middle point of the straight line segment
126   //---------------------------------------------------------------------------
127   vsol_point_2d_sptr middle() const;
128 
129   //---------------------------------------------------------------------------
130   //: direction of the straight line segment.
131   //---------------------------------------------------------------------------
132   vgl_vector_2d<double> direction() const;
133 
134   //---------------------------------------------------------------------------
135   //: First point of the straight line segment
136   //---------------------------------------------------------------------------
p0()137   vsol_point_2d_sptr p0() const override { return p0_; }
138 
139   //---------------------------------------------------------------------------
140   //: Last point of the straight line segment
141   //---------------------------------------------------------------------------
p1()142   vsol_point_2d_sptr p1() const override { return p1_; }
143 
144   //---------------------------------------------------------------------------
145   //: Get an unbounded vgl_homg_line_2d
146   //---------------------------------------------------------------------------
147   vgl_homg_line_2d<double> vgl_hline_2d() const;
148 
149   //---------------------------------------------------------------------------
150   //: Get a vgl_line_segment_2d
151   //---------------------------------------------------------------------------
152   vgl_line_segment_2d<double> vgl_seg_2d() const;
153 
154   //***************************************************************************
155   // Comparison
156   //***************************************************************************
157 
158   //---------------------------------------------------------------------------
159   //: Has `this' the same points than `other' ?
160   //---------------------------------------------------------------------------
161   virtual bool operator==(vsol_line_2d const& other) const;
162   bool operator==(vsol_spatial_object_2d const& obj) const override; // virtual of vsol_spatial_object_2d
163 
164   //---------------------------------------------------------------------------
165   //: Has `this' not the same points than `other' ?
166   //---------------------------------------------------------------------------
167   inline bool operator!=(vsol_line_2d const& o) const {return !operator==(o);}
168 
169   //***************************************************************************
170   // Status report
171   //***************************************************************************
172 
173   //---------------------------------------------------------------------------
174   //: Compute the bounding box of `this'
175   //---------------------------------------------------------------------------
176   void compute_bounding_box() const override;
177 
178   //---------------------------------------------------------------------------
179   //: Return the length of `this'
180   //---------------------------------------------------------------------------
181   double length() const override;
182 
183   //---------------------------------------------------------------------------
184   //: Return the tangent angle (in degrees) of `this'. 0<angle<360
185   //---------------------------------------------------------------------------
186   double tangent_angle() const;
187 
188   //***************************************************************************
189   // Status setting
190   //***************************************************************************
191 
192   //---------------------------------------------------------------------------
193   //: Set the first point of the straight line segment
194   //---------------------------------------------------------------------------
195   void set_p0(vsol_point_2d_sptr const& new_p0) override;
196 
197   //---------------------------------------------------------------------------
198   //: Set the last point of the straight line segment
199   //---------------------------------------------------------------------------
200   void set_p1(vsol_point_2d_sptr const& new_p1) override;
201 
202   //---------------------------------------------------------------------------
203   //: Set the length of `this'. Doesn't change middle point and orientation.
204   //  If p0 and p1 are equal then the direction is set to (1,0)
205   //  REQUIRE: new_length>=0
206   //---------------------------------------------------------------------------
207   void set_length(const double new_length);
208 
209   //***************************************************************************
210   // Basic operations
211   //***************************************************************************
212 
213   //---------------------------------------------------------------------------
214   //: Is `p' in `this' ?
215   //---------------------------------------------------------------------------
216   virtual bool in(vsol_point_2d_sptr const& p) const;
217 
218   //---------------------------------------------------------------------------
219   //: Return the tangent to `this' at `p'. Has to be deleted manually
220   //  REQUIRE: in(p)
221   //---------------------------------------------------------------------------
222   virtual vgl_homg_line_2d<double>* tangent_at_point(vsol_point_2d_sptr const& p) const;
223 
224   // ==== Binary IO methods ======
225 
226   //: Binary save self to stream.
227   void b_write(vsl_b_ostream &os) const override;
228 
229   //: Binary load self from stream.
230   void b_read(vsl_b_istream &is) override;
231 
232   //: Return IO version number;
233   short version() const;
234 
235   //: Print an ascii summary to the stream
236   void print_summary(std::ostream &os) const;
237 
238   //: Return a platform independent string identifying the class
is_a()239   std::string is_a() const override { return std::string("vsol_line_2d"); }
240 
241   //: Return true if the argument matches the string identifying the class or any parent class
is_class(std::string const & cls)242   virtual bool is_class(std::string const& cls) const { return cls==is_a(); }
243 
244   //---------------------------------------------------------------------------
245   //: output description to stream
246   //---------------------------------------------------------------------------
247   void describe(std::ostream &strm, int blanking=0) const override;
248 };
249 
250 //: Binary save vsol_line_2d* to stream.
251 void vsl_b_write(vsl_b_ostream &os, const vsol_line_2d* p);
252 
253 //: Binary load vsol_line_2d* from stream.
254 void vsl_b_read(vsl_b_istream &is, vsol_line_2d* &p);
255 
256 #endif // vsol_line_2d_h_
257