contrib/gel/vsol/vsol_conic_2d.h

Go to the documentation of this file.
00001 // This is gel/vsol/vsol_conic_2d.h
00002 #ifndef vsol_conic_2d_h_
00003 #define vsol_conic_2d_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Euclidean general conic class in 2D space
00008 //
00009 // The conic is defined by its cartesian equation
00010 // a x^2 +b xy +c y^2 +d x +e y +f
00011 // This class inherits both from vsol_curve_2d and vgl_conic<double>.
00012 // From the latter it inherits most of its geometric functionality, but
00013 // robustified (with "eps" values to catch rounding errors), non-templated,
00014 // and with non-homogeneous Euclidean 2D geometry terminology instead of
00015 // homogeneous 3-tuples.
00016 //
00017 // \author François BERTEL
00018 // \date   2000/04/28
00019 //
00020 // \verbatim
00021 //  Modifications
00022 //   2000/04/28 François BERTEL Creation
00023 //   2000/06/17 Peter Vanroose  Implemented all operator==()s and type info
00024 //   2001/08/29 Peter Vanroose  midpoint() added
00025 //   2001/08/29 Peter Vanroose  conic intersection added (implemented in vgl)
00026 //   2001/08/29 Peter Vanroose  closest_point and distance to point added
00027 //   2001/08/30 Peter Vanroose  now inheriting from vgl_conic
00028 //   2001/08/31 Peter Vanroose  constructor added from centre, size, orientation
00029 //   2002/04/05 Peter Vanroose  axis() added
00030 //   2003/01/08 Peter Vanroose  moved static private methods to vsol_conic_2d.cxx
00031 //   2004/05/11 Joseph Mundy    added binary I/O methods
00032 //   2004/09/23 Ming-CHing Chang  Change cast_to_conic_2d() to cast_to_conic()
00033 // \endverbatim
00034 //*****************************************************************************
00035 
00036 #include <vgl/vgl_fwd.h>
00037 #include <vgl/vgl_conic.h> // parent class
00038 #include <vgl/vgl_conic_segment_2d.h>
00039 #include <vsl/vsl_binary_io.h>
00040 #include <vsol/vsol_curve_2d.h>
00041 #include <vsol/vsol_point_2d.h>
00042 #include <vsol/vsol_line_2d.h>
00043 #include <vsol/vsol_line_2d_sptr.h>
00044 #include <vnl/vnl_double_3x3.h>
00045 #include <vcl_list.h>
00046 #include <vcl_iostream.h>
00047 
00048 //: Euclidean general conic class, part of the vsol_curve_2d hierarchy
00049 
00050 class vsol_conic_2d : public vsol_curve_2d, public vgl_conic<double>
00051 {
00052   //***************************************************************************
00053   // Data members
00054   //***************************************************************************
00055 
00056   //---------------------------------------------------------------------------
00057   //: First point of the curve
00058   //---------------------------------------------------------------------------
00059   vsol_point_2d_sptr p0_;
00060 
00061   //---------------------------------------------------------------------------
00062   //: Last point of the curve
00063   //---------------------------------------------------------------------------
00064   vsol_point_2d_sptr p1_;
00065 
00066  public:
00067   //---------------------------------------------------------------------------
00068   //: the different kinds of conic
00069   //---------------------------------------------------------------------------
00070   enum vsol_conic_type
00071   {
00072     invalid=0, // not 'degenerate' since numbers 7 to 11 are degenerate - PVr
00073     real_ellipse,
00074     real_circle,
00075     complex_ellipse,
00076     complex_circle,
00077     hyperbola,
00078     parabola,
00079     real_intersecting_lines,
00080     complex_intersecting_lines,
00081     real_parallel_lines,
00082     complex_parallel_lines,
00083     coincident_lines
00084   };
00085 
00086  private: // has been superceeded by is_a()
00087   //: Return the curve type
00088   virtual vsol_curve_2d_type curve_type() const { return vsol_curve_2d::CONIC; }
00089 
00090  public:
00091   //***************************************************************************
00092   // Initialization
00093   //***************************************************************************
00094 
00095   //---------------------------------------------------------------------------
00096   //: Default Constructor
00097   //  produces and invalid conic (needed for binary I/O)
00098   //---------------------------------------------------------------------------
00099   vsol_conic_2d() : vsol_curve_2d(), vgl_conic<double>() {}
00100 
00101   //---------------------------------------------------------------------------
00102   //: Constructor from coefficients of the cartesian equation
00103   //  `a'x^2+`b'xy+`c'y^2+`d'x+`e'y+`f'
00104   //---------------------------------------------------------------------------
00105   vsol_conic_2d(double a, double b, double c, double d, double e, double f)
00106     : vsol_curve_2d(), vgl_conic<double>(a, b, c, d, e, f) {}
00107 
00108   //---------------------------------------------------------------------------
00109   //: Ellipse/hyperbola constructor from centre, size and orientation.
00110   //  This constructor can only be used for non-degenerate, real ellipses and
00111   //  hyperbolas: if rx and ry have the same sign, an ellipse is defined
00112   //  (and any ellipse can uniquely be specified this way);
00113   //  rx is the length of one main axis, ry of the other axis.
00114   //  Hyperbolas are obtained if rx and ry have opposite sign; the positive
00115   //  one determines the distance from bots tops to the centre, and the other
00116   //  one specified the 'minor' axis length.
00117   //---------------------------------------------------------------------------
00118   vsol_conic_2d(vsol_point_2d const& c, double rx, double ry, double theta);
00119 
00120   //---------------------------------------------------------------------------
00121   //: Parabola constructor from direction, top and excentricity parameter.
00122   //  This constructor can only be used for non-degenerate parabolas:
00123   //  specify the direction of the symmetry axis, the top, and an excentricity
00124   //  parameter theta.
00125   //---------------------------------------------------------------------------
00126   vsol_conic_2d(vgl_vector_2d<double> const& dir, vsol_point_2d const& top, double theta);
00127 
00128   //---------------------------------------------------------------------------
00129   //: Set ellipse/hyperbola from centre, size and orientation.
00130   //  Can only be used for non-degenerate, real ellipses and
00131   //  hyperbolas: if rx and ry have the same sign, an ellipse is defined
00132   //  (and any ellipse can uniquely be specified this way);
00133   //  rx is the length of one main axis, ry of the other axis.
00134   //  Hyperbolas are obtained if rx and ry have opposite sign; the positive
00135   //  one determines the distance from bots tops to the centre, and the other
00136   //  one specified the 'minor' axis length.
00137   //---------------------------------------------------------------------------
00138   void set_central_parameters(vsol_point_2d const& c, double rx, double ry, double theta);
00139 
00140   //---------------------------------------------------------------------------
00141   //: Set parabola from direction, top and excentricity parameter.
00142   //  This can only be used for non-degenerate parabolas:
00143   //  specify the direction of the symmetry axis, the top, and an excentricity
00144   //  parameter theta.
00145   //---------------------------------------------------------------------------
00146   void set_parabola_parameters(vgl_vector_2d<double> const& dir,
00147                                vsol_point_2d const& top, double theta);
00148 
00149   //---------------------------------------------------------------------------
00150   //: Constructor from vgl_conic_segment_2d
00151   //---------------------------------------------------------------------------
00152   vsol_conic_2d(vgl_conic_segment_2d<double> & cs) : vsol_curve_2d(), vgl_conic<double>(cs.conic()),
00153      p0_(new vsol_point_2d(cs.point1())), p1_(new vsol_point_2d(cs.point2())){}
00154 
00155   //---------------------------------------------------------------------------
00156   //: Copy constructor
00157   //---------------------------------------------------------------------------
00158   vsol_conic_2d(vsol_conic_2d const& c):vsol_curve_2d(c),vgl_conic<double>(c) {}
00159 
00160   //---------------------------------------------------------------------------
00161   //: Destructor
00162   //---------------------------------------------------------------------------
00163   virtual ~vsol_conic_2d() {}
00164 
00165   //---------------------------------------------------------------------------
00166   //: Clone `this': creation of a new object and initialization
00167   // See Prototype pattern
00168   //---------------------------------------------------------------------------
00169   virtual vsol_spatial_object_2d* clone() const;
00170 
00171   //***************************************************************************
00172   // Access
00173   //***************************************************************************
00174 
00175   //---------------------------------------------------------------------------
00176   //: Return the first point of `this';  pure virtual of vsol_curve_2d
00177   //---------------------------------------------------------------------------
00178   virtual vsol_point_2d_sptr p0() const { return p0_; }
00179 
00180   //---------------------------------------------------------------------------
00181   //: Return the last point of `this';  pure virtual of vsol_curve_2d
00182   //---------------------------------------------------------------------------
00183   virtual vsol_point_2d_sptr p1() const { return p1_; }
00184 
00185   //***************************************************************************
00186   // Comparison
00187   //***************************************************************************
00188 
00189   //---------------------------------------------------------------------------
00190   //: Has `this' the same coefficients and the same end points than `other' ?
00191   //---------------------------------------------------------------------------
00192   virtual bool operator==(vsol_conic_2d const& other) const;
00193   virtual bool operator==(vsol_spatial_object_2d const&) const; // virtual of vsol_spatial_object_2d
00194 
00195   //---------------------------------------------------------------------------
00196   //: Has `this' not the same coeffs than `other', or different end points ?
00197   //---------------------------------------------------------------------------
00198   inline bool operator!=(vsol_conic_2d const& o) const {return !operator==(o);}
00199 
00200   //***************************************************************************
00201   // Status report
00202   //***************************************************************************
00203 
00204   //---------------------------------------------------------------------------
00205   //: Return the real type of the conic from its coefficients
00206   //---------------------------------------------------------------------------
00207   vsol_conic_type real_type() const;
00208 
00209   //---------------------------------------------------------------------------
00210   //: Is `this' an real ellipse ?
00211   //---------------------------------------------------------------------------
00212   bool is_real_ellipse() const;
00213 
00214   //---------------------------------------------------------------------------
00215   //: Is `this' a real circle ?
00216   //---------------------------------------------------------------------------
00217   bool is_real_circle() const;
00218 
00219   //---------------------------------------------------------------------------
00220   //: Is `this' a complex ellipse ?
00221   //---------------------------------------------------------------------------
00222   bool is_complex_ellipse() const;
00223 
00224   //---------------------------------------------------------------------------
00225   //: Is `this' a complex circle ?
00226   //---------------------------------------------------------------------------
00227   bool is_complex_circle() const;
00228 
00229   //---------------------------------------------------------------------------
00230   //: Is `this' a parabola ?
00231   //---------------------------------------------------------------------------
00232   bool is_parabola() const;
00233 
00234   //---------------------------------------------------------------------------
00235   //: Is `this' a hyperbola ?
00236   //---------------------------------------------------------------------------
00237   bool is_hyperbola() const;
00238 
00239   //---------------------------------------------------------------------------
00240   //: Is `this' an real intersecting lines ?
00241   //---------------------------------------------------------------------------
00242   bool is_real_intersecting_lines() const;
00243 
00244   //---------------------------------------------------------------------------
00245   //: Is `this' an complex intersecting lines ?
00246   //---------------------------------------------------------------------------
00247   bool is_complex_intersecting_lines() const;
00248 
00249   //---------------------------------------------------------------------------
00250   //: Is `this' an coincident lines ?
00251   //---------------------------------------------------------------------------
00252   bool is_coincident_lines() const;
00253 
00254   //---------------------------------------------------------------------------
00255   //: Return 3 ellipse parameters:
00256   //  -                            centre (`cx',`cy'),
00257   //  -                            orientation `phi',
00258   //  -                            size (`width',`height')
00259   //  REQUIRE: is_real_ellipse()
00260   //---------------------------------------------------------------------------
00261   void ellipse_parameters(double &cx,
00262                           double &cy,
00263                           double &phi,
00264                           double &width,
00265                           double &height) const;
00266 
00267   //---------------------------------------------------------------------------
00268   //: Return ellipse angular position
00269   //  -   input pt  , a point on the ellipse
00270   //  -   output angle
00271   //  -  
00272   //  REQUIRE: is_real_ellipse()
00273   //---------------------------------------------------------------------------
00274   double ellipse_angular_position(vsol_point_2d_sptr const& pt) const;
00275 
00276   //---------------------------------------------------------------------------
00277   //: Return 3 hyperbola parameters:
00278   //  -                            centre (`cx',`cy'),
00279   //  -                            orientation `phi',
00280   //  -                            size (`half-axis',-`half-secondary-axis')
00281   //  REQUIRE: is_hyperbola()
00282   //---------------------------------------------------------------------------
00283   void hyperbola_parameters(double &cx,
00284                             double &cy,
00285                             double &phi,
00286                             double &main_axis,
00287                             double &secondary_axis) const;
00288 
00289   //---------------------------------------------------------------------------
00290   //: Return 2 parabola parameters:
00291   //  -                             top (`cx',`cy'),
00292   //  -                             orientation (`cosphi',`sinphi')
00293   //  REQUIRE: is_parabola()
00294   //---------------------------------------------------------------------------
00295   void parabola_parameters(double &cx,
00296                            double &cy,
00297                            double &cosphi,
00298                            double &sinphi) const;
00299 
00300   //---------------------------------------------------------------------------
00301   //: Return the length of `this'
00302   //---------------------------------------------------------------------------
00303   virtual double length() const; // pure virtual of vsol_curve_2d
00304 
00305   //---------------------------------------------------------------------------
00306   //: Return the matrix associated with the coefficients.
00307   //---------------------------------------------------------------------------
00308   vnl_double_3x3 matrix() const;
00309 
00310   //***************************************************************************
00311   // Status setting
00312   //***************************************************************************
00313 
00314   //---------------------------------------------------------------------------
00315   //: Set the first point of the curve
00316   //  REQUIRE: in(new_p0)
00317   //---------------------------------------------------------------------------
00318   virtual void set_p0(vsol_point_2d_sptr const& new_p0);
00319 
00320   //---------------------------------------------------------------------------
00321   //: Set the last point of the curve
00322   //  REQUIRE: in(new_p1)
00323   //---------------------------------------------------------------------------
00324   virtual void set_p1(vsol_point_2d_sptr const& new_p1);
00325 
00326   //***************************************************************************
00327   // Basic operations
00328   //***************************************************************************
00329 
00330   //---------------------------------------------------------------------------
00331   //: Return the centre or symmetry point of a central conic.
00332   //---------------------------------------------------------------------------
00333   vsol_point_2d_sptr midpoint() const;
00334 
00335   //---------------------------------------------------------------------------
00336   //: Return the main symmetry axis, if not degenerate.
00337   //---------------------------------------------------------------------------
00338   vsol_line_2d_sptr axis() const;
00339 
00340   //---------------------------------------------------------------------------
00341   //: Is `p' in `this' ? (ie `p' verifies the equation, within some margin)
00342   //---------------------------------------------------------------------------
00343   virtual bool in(vsol_point_2d_sptr const& p) const;
00344 
00345   //---------------------------------------------------------------------------
00346   //: Returns the tangent to the conic in the point p, if p is on the conic.
00347   //  In general, returns the polar line of the point w.r.t. the conic.
00348   //---------------------------------------------------------------------------
00349   virtual vgl_homg_line_2d<double>* tangent_at_point(vsol_point_2d_sptr const& p) const;
00350 
00351   //---------------------------------------------------------------------------
00352   //: Return the set of (real) intersection points of this conic with a line
00353   //---------------------------------------------------------------------------
00354   vcl_list<vsol_point_2d_sptr> intersection(vsol_line_2d const& line) const;
00355 
00356   //---------------------------------------------------------------------------
00357   //: Return the set of (real) intersection points of two conics
00358   //---------------------------------------------------------------------------
00359   vcl_list<vsol_point_2d_sptr> intersection(vsol_conic_2d const& c) const;
00360 
00361   //---------------------------------------------------------------------------
00362   //: Return the point on the conic boundary which is closest to the given point
00363   //---------------------------------------------------------------------------
00364   vsol_point_2d_sptr closest_point_on_curve(vsol_point_2d_sptr const& pt) const;
00365 
00366   //---------------------------------------------------------------------------
00367   //: Return the shortest distance of the point to the conic boundary
00368   //---------------------------------------------------------------------------
00369   double distance(vsol_point_2d_sptr const& pt) const;
00370 
00371   //---------------------------------------------------------------------------
00372   //: output description to stream
00373   //---------------------------------------------------------------------------
00374   inline void describe(vcl_ostream &strm, int blanking=0) const
00375   {
00376     if (blanking < 0) blanking = 0; while (blanking--) strm << ' ';
00377     strm << "vsol_conic_2d<" << static_cast<vgl_conic<double> >(*this)
00378          << '>' << vcl_endl;
00379   }
00380 
00381   //---------------------------------------------------------------------------
00382   //: Return `this' if `this' is an conic, 0 otherwise
00383   //---------------------------------------------------------------------------
00384   virtual vsol_conic_2d const*cast_to_conic()const{return this;}
00385   virtual vsol_conic_2d *cast_to_conic() {return this;}
00386 
00387   // ==== Binary IO methods ======
00388 
00389   //: Binary save self to stream.
00390   void b_write(vsl_b_ostream &os) const;
00391 
00392   //: Binary load self from stream.
00393   void b_read(vsl_b_istream &is);
00394 
00395   //: Return IO version number;
00396   short version() const;
00397 
00398   //: Print an ascii summary to the stream
00399   void print_summary(vcl_ostream &os) const;
00400 
00401   //: Return a platform independent string identifying the class
00402   virtual vcl_string is_a() const { return "vsol_conic_2d"; }
00403 
00404   //: Return true if the argument matches the string identifying the class or any parent class
00405   virtual bool is_class(vcl_string const& cls) const { return cls==is_a(); }
00406 };
00407 
00408 //: Binary save vsol_conic_2d* to stream.
00409 void vsl_b_write(vsl_b_ostream &os, const vsol_conic_2d* p);
00410 
00411 //: Binary load vsol_conic_2d* from stream.
00412 void vsl_b_read(vsl_b_istream &is, vsol_conic_2d* &p);
00413 
00414 #endif // vsol_conic_2d_h_

Generated on Sun Sep 7 05:15:19 2008 for contrib/gel/vsol by  doxygen 1.5.1