contrib/brl/bbas/bsol/bsol_intrinsic_curve_2d.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsol/bsol_intrinsic_curve_2d.h
00002 #ifndef bsol_intrinsic_curve_2d_h_
00003 #define bsol_intrinsic_curve_2d_h_
00004 //*****************************************************************************
00005 //:
00006 // \file
00007 // \brief Generic intrinsic curve in 2D that has intrinsic curvature, d_theta, d_s defined.
00008 //   Note that to make the definition intrinsic,
00009 //   I didn't inherent it from bdgl_polyline_2d; instead, I inherent it
00010 //   from vsol_curve_2d.
00011 //   For closed curve, refer to bdgl_intrinsic_closed_curve_2d.
00012 //
00013 // This class inherits from vsol_curve_2d.
00014 //
00015 // \author MingChing Chang
00016 // \date   2003-11-14
00017 //
00018 // \verbatim
00019 //  Modifications
00020 //   2003-11-14 MingChing Chang - Modified from the code of Thomas Sebastian
00021 //   2004-10-25 Ozge Can Ozcanli - Modified to add close curve support
00022 //   2004-11-01 Ozge Can Ozcanli - Added method upsample that interpolates new samples linearly according to arclength
00023 // \endverbatim
00024 //*****************************************************************************
00025 
00026 #include <vsol/vsol_curve_2d.h>
00027 #include <vsol/vsol_point_2d.h>
00028 #include <vsol/vsol_point_2d_sptr.h>
00029 #include <vsol/vsol_polyline_2d_sptr.h>
00030 #include <vcl_vector.h>
00031 #include <vcl_iostream.h>
00032 #include <vcl_cassert.h>
00033 
00034 #define ZERO_TOLERANCE 1E-1 // used in bsol_intrinsic_curve_2d.cxx
00035 
00036 //: General intrinsic curve class
00037 
00038 class bsol_intrinsic_curve_2d : public vsol_curve_2d
00039 {
00040  protected:
00041   //***************************************************************************
00042   // Data members
00043 
00044   //: Pointer to list of vsol_point_2d smart pointers
00045   vcl_vector<vsol_point_2d_sptr> *storage_;
00046   //: First point of the curve : just to conform to vsol_curve_2d standard
00047   vsol_point_2d_sptr p0_;
00048   //: Last point of the curve
00049   vsol_point_2d_sptr p1_;
00050   //: Arclength measured from the p0_
00051   vcl_vector<double> arcLength_;
00052   //: Arclength of the current segment i to i+1
00053   vcl_vector<double> s_;
00054   //: Normalized arclength measured from the p0_
00055   //  Normalized arclength of the whole curve is 1.
00056   vcl_vector<double> normArcLength_;
00057   //: Length of the intrinsic curve
00058   double length_;
00059   //: dx of the intrinsic curve
00060   vcl_vector<double> dx_;
00061   //: dy of the intrinsic curve
00062   vcl_vector<double> dy_;
00063   //: Curvature of the intrinsic curve
00064   vcl_vector<double> curvature_;
00065   //: Bending angle of the intrinsic curve
00066   vcl_vector<double> angle_;
00067   //: Total curvature of the intrinsic curve
00068   double totalCurvature_;
00069   //: Total angle change of the intrinsic curve
00070   double totalAngleChange_;
00071 
00072   bool isOpen_; // true - open, false - closed
00073 
00074  public:
00075   //***************************************************************************
00076   // Initialization
00077 
00078   //: Default Constructor
00079   bsol_intrinsic_curve_2d();
00080   //: Constructor from a vcl_vector of points
00081   bsol_intrinsic_curve_2d(const vcl_vector<vsol_point_2d_sptr> &new_vertices);
00082   //: Constructor from a vsol_polyline_2d_sptr
00083   bsol_intrinsic_curve_2d(const vsol_polyline_2d_sptr poly);
00084 
00085   //: Copy constructor
00086   bsol_intrinsic_curve_2d(const bsol_intrinsic_curve_2d &other);
00087   //: Destructor
00088   virtual ~bsol_intrinsic_curve_2d();
00089   //: Clone `this': creation of a new object and initialization
00090   // See Prototype pattern
00091   virtual vsol_spatial_object_2d* clone(void) const;
00092 
00093   //: Return a platform independent string identifying the class
00094   vcl_string is_a() const { return vcl_string("bsol_intrinsic_curve_2d"); }
00095 
00096   //***************************************************************************
00097   // Access
00098 
00099   bool isOpen(void) const { return isOpen_; }
00100   //: Return the first point of `this';  pure virtual of vsol_curve_2d
00101   virtual vsol_point_2d_sptr p0() const { return p0_; }
00102   //: Return the last point of `this';   pure virtual of vsol_curve_2d
00103   virtual vsol_point_2d_sptr p1() const { return p1_; }
00104   //: Is `i' a valid index for the list of vertices ?
00105   bool valid_index(unsigned int i) const { return i<storage_->size(); }
00106   //: Return vertex `i'
00107   vsol_point_2d_sptr vertex(const int i) const {
00108     assert(valid_index(i));
00109     return (*storage_)[i];
00110   }
00111   //: Return x coord of vertex `i'
00112   double x (const int i) const {
00113     assert(valid_index(i));
00114     return (*storage_)[i]->x();
00115   }
00116   //: Return y coord of vertex `i'
00117   double y (const int i) const {
00118     assert(valid_index(i));
00119     return (*storage_)[i]->y();
00120   }
00121   //: Return the number of vertices
00122   int size(void) const { return storage_->size(); }
00123   //: Return the total arclength from vertex `0' to vertex `i'
00124   double arcLength (const int i) const {
00125     assert (valid_index(i));
00126     return arcLength_[i];
00127   }
00128   //: Return the total arclength of the current vertex `i-1' to vertex `i'
00129   double s (const int i) const {
00130     assert (valid_index(i));
00131     return s_[i];
00132   }
00133   //: Return the normalized arclength from vertex `0' to vertex `i'
00134   double normArcLength (const int i) const;
00135   //: Return the length of the intrinsic curve
00136   virtual double length (void) const { return length_; }
00137   //: Return the dx vertex `i-1' to vertex `i'
00138   double dx (const int i) const {
00139     assert(valid_index(i));
00140     return (*storage_)[i]->x() - (*storage_)[i-1]->x();
00141   }
00142   //: Return the dy vertex `i-1' to vertex `i'
00143   double dy (const int i) const {
00144     assert(valid_index(i));
00145     return (*storage_)[i]->y() - (*storage_)[i-1]->y();
00146   }
00147   //: Return the curvature of vertex `i'
00148   double curvature (const int i) const;
00149   //: Return the angle of vertex `i'
00150   double angle (const int i) const;
00151 
00152   //: Return the total curvature of the intrinsic curve
00153   double totalCurvature (void) const { return totalCurvature_; }
00154   //: Return the total angle change of the intrinsic curve
00155   double totalAngleChange (void) const { return totalAngleChange_; }
00156 
00157   //***************************************************************************
00158   // Comparison
00159 
00160   //: Has `this' the same points than `other' in the same order ?
00161   virtual bool operator==(const bsol_intrinsic_curve_2d &other) const;
00162   virtual bool operator==(const vsol_spatial_object_2d& obj) const; // virtual of vsol_spatial_object_2d
00163   //: Has `this' the same points than `other' in the same order ?
00164   inline bool operator!=(const bsol_intrinsic_curve_2d &o) const { return !operator==(o); }
00165 
00166  protected:
00167 
00168   // Internal status setting functions
00169 
00170   //: Computing the properties of the curve.
00171   void computeDerivatives();
00172   void computeCurvatures();
00173   void computeArcLength();
00174   void computeAngles();
00175 
00176  public:
00177 
00178   //***************************************************************************
00179   // Status setting
00180 
00181   void setOpen(bool flag) { isOpen_ = flag; }
00182 
00183   //: Set the first point of the curve
00184   virtual void set_p0 (const vsol_point_2d_sptr &new_p0);
00185   //: Set the last point of the curve
00186   virtual void set_p1 (const vsol_point_2d_sptr &new_p1);
00187   //: Compute intrinsic properties.
00188   //  Note that if you even call the other modifying function with the
00189   //  flag bRecomputeProperties set to false, remember to call this function
00190   //  to recompute intrinsic properties of this curve.
00191   void computeProperties();
00192 
00193   //: Delete all points of the intrinsic curve
00194   void clear();
00195   //: Add another point to the end of the intrinsic curve
00196   void add_vertex (const vsol_point_2d_sptr &new_p, bool bRecomputeProterties=false);
00197   //: Add another point to the end of the intrinsic curve
00198   void add_vertex (double x, double y) {
00199     vsol_point_2d_sptr newpoint = new vsol_point_2d (x, y);
00200     add_vertex (newpoint, false);
00201   }
00202   //: Remove one vertex from the intrinsic curve
00203   void remove_vertex (const int i, bool bRecomputeProterties=false);
00204   //: Modify one vertex of the intrinsic curve
00205   void modify_vertex (const int i, double x, double y, bool bRecomputeProterties=false);
00206   //: Insert one vertex to position `i' of the intrinsic curve
00207   //  Note that it insert the vertex into `i-1'
00208   void insert_vertex (int i, double x, double y, bool bRecomputeProterties=false);
00209 
00210   void readCONFromFile(vcl_string fileName);
00211 
00212   //: Added by Ozge Can Ozcanli to upsample the curve
00213   bool upsample(int new_size);
00214 
00215   //***************************************************************************
00216   // Basic operations
00217 
00218   //: Compute the bounding box of `this'
00219   virtual void compute_bounding_box(void) const;
00220 
00221   //: output description to stream
00222   inline void describe(vcl_ostream &strm, int blanking=0) const {
00223     if (blanking < 0) blanking = 0; while (blanking--) strm << ' ';
00224     strm << "<bsol_intrinsic_curve_2d "
00225       // << static_cast<vsol_curve_2d>(*this) << ' '
00226          << "p0 = " << *p0_ << ", p1 = " << *p1_ << " >\n";
00227   }
00228 };
00229 
00230 #endif // bsol_intrinsic_curve_2d_h_