contrib/gel/vsol/vsol_digital_curve_2d.h
Go to the documentation of this file.
00001 // This is gel/vsol/vsol_digital_curve_2d.h
00002 #ifndef vsol_digital_curve_2d_h_
00003 #define vsol_digital_curve_2d_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Digital curve in 2D with interpolation
00008 //
00009 // This class inherits from vsol_curve_2d.
00010 //
00011 // \author Matt Leotta
00012 // \date   2004-07-13
00013 //
00014 // \verbatim
00015 //  Modifications
00016 // \endverbatim
00017 //*****************************************************************************
00018 
00019 #include <vgl/vgl_fwd.h>
00020 #include <vsl/vsl_binary_io.h>
00021 #include <vsol/vsol_curve_2d.h>
00022 #include <vsol/vsol_point_2d_sptr.h>
00023 #include <vsol/vsol_digital_curve_2d_sptr.h>
00024 #include <vcl_vector.h>
00025 #include <vcl_string.h>
00026 #include <vcl_iosfwd.h>
00027 
00028 //: Digital curve class, part of the vsol_curve_2d hierarchy
00029 // This class is more basic and "pure" than the vdgl counterpart.
00030 // The curve is made up of vsol point and has no addition data members
00031 
00032 class vsol_digital_curve_2d : public vsol_curve_2d
00033 {
00034  protected:
00035   //***************************************************************************
00036   // Data members
00037   //***************************************************************************
00038 
00039   //---------------------------------------------------------------------------
00040   // Description: List of vsol_point_2d
00041   //---------------------------------------------------------------------------
00042   vcl_vector<vsol_point_2d_sptr> samples_;
00043 
00044  public:
00045 
00046   //***************************************************************************
00047   // Initialization
00048   //***************************************************************************
00049 
00050   //---------------------------------------------------------------------------
00051   //: Default Constructor
00052   //---------------------------------------------------------------------------
00053   vsol_digital_curve_2d();
00054 
00055   //---------------------------------------------------------------------------
00056   //: Constructor from a vcl_vector of points
00057   //---------------------------------------------------------------------------
00058   vsol_digital_curve_2d(const vcl_vector<vsol_point_2d_sptr> &samples);
00059 
00060   //---------------------------------------------------------------------------
00061   //: Copy constructor
00062   //---------------------------------------------------------------------------
00063   vsol_digital_curve_2d(const vsol_digital_curve_2d &other);
00064 
00065   //---------------------------------------------------------------------------
00066   //: Destructor
00067   //---------------------------------------------------------------------------
00068   virtual ~vsol_digital_curve_2d();
00069 
00070   //---------------------------------------------------------------------------
00071   //: Clone `this': creation of a new object and initialization
00072   // See Prototype pattern
00073   //---------------------------------------------------------------------------
00074   virtual vsol_spatial_object_2d* clone() const;
00075 
00076   //***************************************************************************
00077   // Access
00078   //***************************************************************************
00079 
00080   //---------------------------------------------------------------------------
00081   //: Return the first point of `this'
00082   //---------------------------------------------------------------------------
00083   virtual vsol_point_2d_sptr p0() const; // pure virtual of vsol_curve_2d
00084 
00085   //---------------------------------------------------------------------------
00086   //: Return the last point of `this'
00087   //---------------------------------------------------------------------------
00088   virtual vsol_point_2d_sptr p1() const; // pure virtual of vsol_curve_2d
00089 
00090   //---------------------------------------------------------------------------
00091   //: Return point `i'
00092   //  REQUIRE: valid_index(i)
00093   //---------------------------------------------------------------------------
00094   vsol_point_2d_sptr point(const int i) const;
00095 
00096   //---------------------------------------------------------------------------
00097   //: Interpolate a point on the curve given a floating point index
00098   //  Linear interpolation is used for now
00099   //  \note index is NOT arc length.  For example, if size()==10
00100   //        then interp(5.5) is interpolated half way between points
00101   //        at indices 5 and 6.  In general this is not at 5.5 units along
00102   //        the curve or even at 55% through the curve.
00103   //---------------------------------------------------------------------------
00104   vgl_point_2d<double> interp(double index) const;
00105 
00106   //***************************************************************************
00107   // Comparison
00108   //***************************************************************************
00109 
00110   //---------------------------------------------------------------------------
00111   //: Has `this' the same points than `other' in the same order ?
00112   //---------------------------------------------------------------------------
00113   virtual bool operator==(const vsol_digital_curve_2d &other) const;
00114   virtual bool operator==(const vsol_spatial_object_2d& obj) const; // virtual of vsol_spatial_object_2d
00115 
00116   //---------------------------------------------------------------------------
00117   //: Has `this' the same points than `other' in the same order ?
00118   //---------------------------------------------------------------------------
00119   inline bool operator!=(const vsol_digital_curve_2d &o) const {return !operator==(o);}
00120 
00121 
00122   //***************************************************************************
00123   // Status setting
00124   //***************************************************************************
00125 
00126   //---------------------------------------------------------------------------
00127   //: Set the first point of the curve
00128   //  REQUIRE: in(new_p0)
00129   //---------------------------------------------------------------------------
00130   virtual void set_p0(const vsol_point_2d_sptr &new_p0);
00131 
00132   //---------------------------------------------------------------------------
00133   //: Set the last point of the curve
00134   //  REQUIRE: in(new_p1)
00135   //---------------------------------------------------------------------------
00136   virtual void set_p1(const vsol_point_2d_sptr &new_p1);
00137 
00138   //---------------------------------------------------------------------------
00139   //: Add another point to the curve
00140   //---------------------------------------------------------------------------
00141   void add_vertex(const vsol_point_2d_sptr &new_p);
00142 
00143   //***************************************************************************
00144   // Status report
00145   //***************************************************************************
00146 
00147   //---------------------------------------------------------------------------
00148   //: Return `this' if `this' is a digital_curve, 0 otherwise
00149   //---------------------------------------------------------------------------
00150   virtual vsol_digital_curve_2d const*cast_to_digital_curve()const{return this;}
00151   virtual vsol_digital_curve_2d *cast_to_digital_curve() {return this;}
00152 
00153  private: // has been superseded by is_a()
00154   //: Return the curve type
00155   virtual vsol_curve_2d_type curve_type() const { return vsol_curve_2d::DIGITAL_CURVE; }
00156 
00157  public:
00158   //---------------------------------------------------------------------------
00159   //: Return the length of `this'
00160   //---------------------------------------------------------------------------
00161   virtual double length() const; // pure virtual of vsol_curve_2d
00162 
00163   //---------------------------------------------------------------------------
00164   //: Compute the bounding box of `this'
00165   //---------------------------------------------------------------------------
00166   virtual void compute_bounding_box() const;
00167 
00168   //---------------------------------------------------------------------------
00169   //: Return the number of vertices
00170   //---------------------------------------------------------------------------
00171   unsigned int size() const { return samples_.size(); }
00172 
00173   //---------------------------------------------------------------------------
00174   //: Is `i' a valid index for the list of vertices ?
00175   //---------------------------------------------------------------------------
00176   bool valid_index(unsigned int i) const { return i<samples_.size(); }
00177 
00178   //***************************************************************************
00179   // Basic operations
00180   //***************************************************************************
00181 
00182   //---------------------------------------------------------------------------
00183   //: output description to stream
00184   //---------------------------------------------------------------------------
00185   void describe(vcl_ostream &strm, int blanking=0) const;
00186 
00187   // ==== Binary IO methods ======
00188 
00189   //: Binary save self to stream.
00190   void b_write(vsl_b_ostream &os) const;
00191 
00192   //: Binary load self from stream.
00193   void b_read(vsl_b_istream &is);
00194 
00195   //: Return IO version number;
00196   short version() const;
00197 
00198   //: Print an ascii summary to the stream
00199   void print_summary(vcl_ostream &os) const;
00200 
00201   //: Return a platform independent string identifying the class
00202   virtual vcl_string is_a() const { return vcl_string("vsol_digital_curve_2d"); }
00203 
00204   //: Return true if the argument matches the string identifying the class or any parent class
00205   virtual bool is_class(vcl_string const& cls) const { return cls==is_a(); }
00206 };
00207 
00208 //: Binary save vsol_digital_curve_2d* to stream.
00209 void vsl_b_write(vsl_b_ostream &os, const vsol_digital_curve_2d* p);
00210 
00211 //: Binary load vsol_digital_curve_2d* from stream.
00212 void vsl_b_read(vsl_b_istream &is, vsol_digital_curve_2d* &p);
00213 
00214 //: Return the floating point index of the point on the curve nearest to \p point
00215 double closest_index(const vgl_point_2d<double>& point,
00216                      const vsol_digital_curve_2d_sptr& curve);
00217 
00218 //: Split the input curve into two pieces at the floating point index
00219 bool split(const vsol_digital_curve_2d_sptr& input,
00220            double index,
00221            vsol_digital_curve_2d_sptr& output1,
00222            vsol_digital_curve_2d_sptr& output2);
00223 
00224 #endif // vsol_digital_curve_2d_h_