vgl_conic.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_conic.h
2 #ifndef vgl_conic_h_
3 #define vgl_conic_h_
4 //:
5 // \file
6 // \brief A quadratic plane curve
7 //
8 // This example tells you the type of the given conic equation,
9 // and prints the equation in readable form:
10 // \code
11 // vgl_conic<double> c(1, 0, 2, 0, 0, -3);
12 // std::cout << c.real_type() << '\n'; // prints "real ellipse"
13 // std::cout << c << '\n'; // prints the equation: X^2 + 2 Y^2 - 3 = 0
14 // \endcode
15 //
16 // \verbatim
17 // Modifications
18 // Peter Vanroose, 10 sep 1996 wrote description and example file.
19 // Peter Vanroose, 17 jun 1998 added PolarLine() and PolarPoint().
20 // Peter Vanroose, 18 jun 1998 added Hyperbola and Circle interface.
21 // Peter Vanroose, 19 jun 1998 added dual space functions.
22 // Peter Vanroose, 21 jun 1998 added Parabola interface.
23 // Peter Vanroose, 27 jun 1998 added ComputeParabolaParameters().
24 // M.Vergauwen & P.Vanroose, 4 jul 1998 added Intersect() & CommonTangents()
25 // Peter Vanroose, 29 aug 2001 ported from Geometry to vgl
26 // Peter Vanroose, 30 aug 2001 complete rewrite of most of the code
27 // Peter Vanroose, 31 aug 2001 added extensive testing + fixed some bugs
28 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
29 // Ricardo Fabbri, 08 nov 2008 added curvature_at() method.
30 // \endverbatim
31 //
32 //-----------------------------------------------------------------------------
33 
34 #include <list>
35 #include <string>
36 #include <iosfwd>
37 #ifdef _MSC_VER
38 # include <vcl_msvc_warnings.h>
39 #endif
40 
41 #include <vgl/vgl_homg_point_2d.h>
42 #include <vgl/vgl_homg_line_2d.h>
43 
44 //: A quadratic plane curve
45 //
46 // A conic is either an ellipse (or circle), a hyperbola, or a parabola.
47 // It is represented by a quadratic equation in two nonhomogeneous
48 // or three homogeneous coordinates. Conversely, every quadratic
49 // equation represents a conic, be it that it can be degenerate:
50 // either in two (intersecting or parallel) lines, or in two
51 // coincident lines. Also, it can have no "visible", real points,
52 // when it is an imaginary ellipse, or consist of two complementary
53 // imaginary lines in which case it only has one real point, which could
54 // still be at infinity.
55 //
56 // These 11 cases are the possible values of vgl_conic::real_type().
57 // The default constructor sets the type to "invalid conic";
58 // otherwise the correct type is automatically set when the equation
59 // of the conic is given to the constructor that takes 6 numeric values
60 // (a,b,c,d,e,f): the cartesian equation is then
61 // $ax^2 + bxy + cy^2 + dx + ey + f = 0$; the homogeneous equation is
62 // $ax^2 + bxy + cy^2 + dxw + eyw + fw^2 = 0$. (Sometimes with $z$ for $w$.)
63 // The numeric type (typically double or float) is the template argument
64 // of this class.
65 //
66 // When the conic is degenerate and consists of two lines, the method
67 // components() returns a list of two (possibly identical) lines.
68 // Otherwise, this method returns an empty list.
69 
70 template <class T>
71 class vgl_conic
72 {
73  public:
87  num_conic_types // is here to enable iterating through this list
88  };
89 
90  private:
91  // DATA MEMBERS
92 
94  T a_; //!< coefficient of \a x^2
95  T b_; //!< coefficient of \a xy
96  T c_; //!< coefficient of \a y^2
97  T d_; //!< coefficient of \a xw
98  T e_; //!< coefficient of \a yw
99  T f_; //!< coefficient of \a w^2
100 
101  public:
102  inline vgl_conic_type type() const { return type_; }
103 
104  //: Returns the type of the conic as a string.
105  // Possible returned strings are:
106  // "real ellipse", "real circle", "imaginary ellipse", "imaginary circle",
107  // "hyperbola", "parabola",
108  // "real intersecting lines", "complex intersecting lines",
109  // "real parallel lines", "complex parallel lines", "coincident lines".
110  // The default constructor sets the type to "invalid conic".
111  std::string real_type() const;
112 
113  //: Returns the internal enum value corresponding to the string argument.
114  // Useful for comparison purposes, or for use in "case" statements.
115  static vgl_conic_type type_by_name(std::string const& name);
116 
117  //: Converts the conic type from enum (internal representation) to string.
118  static std::string type_by_number(vgl_conic_type type);
119 
120  //: Returns the coefficient of \f$X^2\f$
121  inline T a() const { return a_; }
122 
123  //: Returns the coefficient of \f$XY\f$
124  inline T b() const { return b_; }
125 
126  //: Returns the coefficient of \f$Y^2\f$
127  inline T c() const { return c_; }
128 
129  //: Returns the coefficient of \f$XW\f$
130  inline T d() const { return d_; }
131 
132  //: Returns the coefficient of \f$YW\f$
133  inline T e() const { return e_; }
134 
135  //: Returns the coefficient of \f$W^2\f$
136  inline T f() const { return f_; }
137 
138  // CONSTRUCTORS AND RELATED STUFF
139 
140  // default constructor
141  vgl_conic() : type_(no_type) {}
142 #if 0 // The compiler defaults for these are all right
143  // copy constructor
144  vgl_conic(vgl_conic<T> const& c)
145  : type_(c.type()), a_(c.a()), b_(c.b()), c_(c.c()), d_(c.d()), e_(c.e()), f_(c.f()) {}
146  // assignment operator
147  vgl_conic& operator=(vgl_conic<T> const& c) {
148  type_=c.type(); a_=c.a(); b_=c.b(); c_=c.c(); d_=c.d(); e_=c.e(); f_=c.f();
149  return *this;
150  }
151  // destructor
152  ~vgl_conic() {}
153 #endif
154 
155  //: constructor using polynomial coefficients.
156  // The order of the coefficients is: $X^2$, $XY$, $Y^2$, $XW$, $YW$, $W^2$,
157  // where $W$ is the homogeneous coordinate (sometimes denoted by $Z$).
158  vgl_conic(T a, T b, T c, T d, T e, T f);
159 
160  //: constructor using polynomial coefficients, given as a C array.
161  // The order of the coefficients is: $X^2$, $XY$, $Y^2$, $XW$, $YW$, $W^2$,
162  // where $W$ is the homogeneous coordinate (sometimes denoted by $Z$).
163  vgl_conic(T const coeff[]);
164 
165  //: constructor using centre, signed radii, and angle.
166  // This constructor can only be used for non-degenerate, real
167  // conics: If the centre point c is a finite point and rx and ry
168  // have the same sign, an ellipse is defined (any ellipse can
169  // uniquely be specified this way); rx is the length of one main
170  // axis, ry of the other axis. Hyperbolas are obtained if rx and
171  // ry have opposite sign; the positive one determines the distance
172  // from bots tops to the centre, and the other one specified the
173  // 'minor' axis length. The rotation is about the centre of the
174  // ellipse or hyperbola, measured counterclockwise from the X axis.
175  // A parabola is obtained when the centre has w()=0,
176  // i.e., is a point at infinity. In that case (rx,ry) is the
177  // top, and theta is an eccentricity parameter (since the centre
178  // already specifies the direction of the symmetry axis).
179  vgl_conic(vgl_homg_point_2d<T> const& c, T rx, T ry, T theta);
180 
181  //: set or reset the conic using polynomial coefficients.
182  // The order of the coefficients is: $X^2$, $XY$, $Y^2$, $XW$, $YW$, $W^2$,
183  // where $W$ is the homogeneous coordinate (sometimes denoted by $Z$).
184  void set(T a, T b, T c, T d, T e, T f);
185 
186  //: comparison operator.
187  // Comparison is on the conic, not the equation coefficients. Hence two
188  // conics are identical if their coefficient vectors are multiples of
189  // each other.
190  bool operator==(vgl_conic<T> const& c) const;
191 
192  // UTILITY FUNCTIONS
193 
194  //: Returns true if this conic is degenerate, i.e., if it consists of 2 lines.
195  bool is_degenerate() const;
196 
197  //: Returns true if a central conic, i.e., an ellipse, circle, or hyperbola.
198  // Also the degenerate versions of these return true.
199  // Returns false if a parabola or two parallel or coinciding lines.
200  bool is_central() const;
201 
202  //: Returns true if the point pt belongs to the conic.
203  // I.e., if it \e exactly satisfies the conic equation.
204  // Beware of rounding for floating point type T! An "almost" returns false!
205  bool contains(vgl_homg_point_2d<T> const& pt) const;
206 
207  //: Returns the list of component lines, when degenerate and real components.
208  // Otherwise returns an empty list.
209  // If two coinciding lines, the list contains two identical elements.
210  // Hence this list always has length 0 or 2.
211  std::list<vgl_homg_line_2d<T> > components() const;
212 
213  // Elementary geometric functions ----------------------------------
214 
215  //: Returns the polar line of the given point, w.r.t. this conic.
216  // For a non-degenerate conic, the polar line of a point outside of the conic
217  // is the connection line of the two points on the conic that form the conic
218  // "contour" as seen from that point, i.e., the touch points of the two
219  // tangents to the conic going through the given point.
220  //
221  // For a point on the conic, it is just the tangent in that point.
222  //
223  // And for a point inside the conic, it is the set of all polar points of
224  // the lines through the given point. This set happens to be a straight line.
226 
227  //: Returns the polar point of the given line, w.r.t. this conic.
228  // For a non-degenerate conic, the polar point of a line that intersects the
229  // conic in two points is the intersection point of the two tangent lines
230  // though those two points. Hence it is the point of which this line is
231  // the polar line.
232  //
233  // For a tangent line to the conic, it is just the tangent point.
234  //
235  // And for a line not intersecting the conic, it is the common intersection
236  // point (inside the conic) of the polar lines of all points of that line.
238 
239  //: Returns the tangent to the conic in the point p, if p is on the conic.
240  // In general, returns the polar line of the point w.r.t. the conic.
241  vgl_homg_line_2d<T> tangent_at(vgl_homg_point_2d<T> const& p) const { return polar_line(p); }
242 
243  //: Returns the centre of the conic, or its point at infinity if a parabola.
244  // When two intersecting or parallel lines, returns their intersection point.
245  // In all cases this is the polar point of the line at infinity.
246  vgl_homg_point_2d<T> centre() const { return polar_point(vgl_homg_line_2d<T>(0,0,1)); }
247 
248  //: Returns the curvature of the conic at point p, assuming p is on the conic.
249  double curvature_at(vgl_point_2d<T> const& p) const;
250 
251  //: Converts the coefficients to a geometric description of an ellipse.
252  // Returns false if the conic is not an ellipse. Double is appropriate
253  // since integer coefficients can produce non-integer ellipse parameters.
254  bool ellipse_geometry(double& xc, double& yc, double& major_axis_length,
255  double& minor_axis_length, double& angle_in_radians) const;
256 
257  // Functions related to dual space ---------------------------------
258 
259  //: Returns the dual or tangential representation of this conic.
260  // The homogeneous coordinates of the points belonging to the dual conic
261  // are the coefficients of the equations of all tangents to the original
262  // conic.
263  vgl_conic dual_conic() const;
264 
265  //: Returns the dual or tangential representation of this conic.
266  vgl_conic tangential_form() const { return dual_conic(); }
267 
268  //: Modify this conic by translating it over distance \a x in the \a X direction and distance \a y in the \a Y direction.
269  void translate_by(T x, T y);
270 
271  private:
272  //--------------------------------------------------------------------------
273  //: set conic type from polynomial coefficients and store in member type_
274  // This method must be called by all constructors (except the default
275  // constructor) and all methods that change the coefficients.
276  void set_type_from_equation();
277 };
278 
279 //: Write "<vgl_conic aX^2+bXY+cY^2+dXW+eYW+fW^2>" to stream
280 // \relatesalso vgl_conic
281 template <class T>
282 std::ostream& operator<<(std::ostream& s, vgl_conic<T> const& c);
283 
284 //: Read a b c d e f from stream
285 // \relatesalso vgl_conic
286 template <class T>
287 std::istream& operator>>(std::istream& s, vgl_conic<T>& c);
289 #define VGL_CONIC_INSTANTIATE(T) extern "please include vgl/vgl_conic.hxx first"
290 
291 #endif // vgl_conic_h_
vgl_conic_type type() const
Definition: vgl_conic.h:101
T d_
coefficient of xw
Definition: vgl_conic.h:96
Represents a homogeneous 2D line.
Definition: vgl_fwd.h:14
double curvature_at(vgl_point_2d< T > const &p) const
Returns the curvature of the conic at point p, assuming p is on the conic.
Definition: vgl_conic.hxx:360
void translate_by(T x, T y)
Modify this conic by translating it over distance x in the X direction and distance y in the Y direct...
Definition: vgl_conic.hxx:315
vgl_conic tangential_form() const
Returns the dual or tangential representation of this conic.
Definition: vgl_conic.h:265
bool contains(vgl_homg_point_2d< T > const &pt) const
Returns true if the point pt belongs to the conic.
Definition: vgl_conic.hxx:185
T f() const
Returns the coefficient of .
Definition: vgl_conic.h:135
vgl_homg_line_2d< T > tangent_at(vgl_homg_point_2d< T > const &p) const
Returns the tangent to the conic in the point p, if p is on the conic.
Definition: vgl_conic.h:240
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
line in projective 2D space
void set_type_from_equation()
set conic type from polynomial coefficients and store in member type_.
Definition: vgl_conic.hxx:144
T c_
coefficient of y^2
Definition: vgl_conic.h:95
T a() const
Returns the coefficient of .
Definition: vgl_conic.h:120
T b_
coefficient of xy
Definition: vgl_conic.h:94
T c() const
Returns the coefficient of .
Definition: vgl_conic.h:126
vgl_homg_point_2d< T > centre() const
Returns the centre of the conic, or its point at infinity if a parabola.
Definition: vgl_conic.h:245
static std::string type_by_number(vgl_conic_type type)
Converts the conic type from enum (internal representation) to string.
Definition: vgl_conic.hxx:48
std::string real_type() const
Returns the type of the conic as a string.
Definition: vgl_conic.hxx:36
bool is_central() const
Returns true if a central conic, i.e., an ellipse, circle, or hyperbola.
Definition: vgl_conic.hxx:306
vgl_conic_type
Definition: vgl_conic.h:73
T e() const
Returns the coefficient of .
Definition: vgl_conic.h:132
vgl_conic dual_conic() const
Returns the dual or tangential representation of this conic.
Definition: vgl_conic.hxx:324
A quadratic plane curve.
Definition: vgl_conic.h:70
point in projective 2D space
static vgl_conic_type type_by_name(std::string const &name)
Returns the internal enum value corresponding to the string argument.
Definition: vgl_conic.hxx:39
T b() const
Returns the coefficient of .
Definition: vgl_conic.h:123
bool ellipse_geometry(double &xc, double &yc, double &major_axis_length, double &minor_axis_length, double &angle_in_radians) const
Converts the coefficients to a geometric description of an ellipse.
Definition: vgl_conic.hxx:202
vgl_homg_point_2d< T > polar_point(vgl_homg_line_2d< T > const &l) const
Returns the polar point of the given line, w.r.t. this conic.
Definition: vgl_conic.hxx:341
T f_
coefficient of w^2
Definition: vgl_conic.h:98
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
T d() const
Returns the coefficient of .
Definition: vgl_conic.h:129
vgl_homg_line_2d< T > polar_line(vgl_homg_point_2d< T > const &p) const
Returns the polar line of the given point, w.r.t. this conic.
Definition: vgl_conic.hxx:332
void set(T a, T b, T c, T d, T e, T f)
set or reset the conic using polynomial coefficients.
Definition: vgl_conic.hxx:80
#define l
vgl_conic_type type_
Definition: vgl_conic.h:92
std::list< vgl_homg_line_2d< T > > components() const
Returns the list of component lines, when degenerate and real components.
Definition: vgl_conic.hxx:238
bool operator==(vgl_conic< T > const &c) const
comparison operator.
Definition: vgl_conic.hxx:57
Represents a homogeneous 2D point.
Definition: vgl_fwd.h:8
bool is_degenerate() const
Returns true if this conic is degenerate, i.e., if it consists of 2 lines.
Definition: vgl_conic.hxx:191
T e_
coefficient of yw
Definition: vgl_conic.h:97
T a_
coefficient of x^2
Definition: vgl_conic.h:93