Curve.h

00001 #ifndef __CURVE_H__
00002 #define __CURVE_H__
00003 
00004 #include "Biarc.h"
00005 #include "PKFmanip.h"
00006 // #include "algo_helpers.h"
00007 #include <fstream>
00008 #include <vector>
00009 // For changeDirection reverse
00010 #include <algorithm>
00011 
00012 #define StraightSegTol 1e-7
00013 
00014 /*
00015  *  Ideas :
00016  *
00017  *  Make this for more than a single component.
00018  *  Make the pkf for other curves, not only BIARC_KNOT
00019  *
00020  *  -- more for fun : add a operator+ function that
00021  *  can add a whole curve to another (check if there
00022  *  are the same number of nodes in each curve, if not
00023  *  ask user to resapample or so. Than try to make
00024  *  a video that morphs from one knot to another by
00025  *  using some time step and the operator+ with lin
00026  *  interpolated moves or so.
00027  *  
00028  */
00029 
00030 #define make_default() makeMidpointRule()
00031 // #define make_default() make(.5)
00032 
00033 #define biarc_it  typename vector< Biarc<Vector> >::iterator
00034 #define biarc_constit  typename vector< Biarc<Vector> >::const_iterator
00035 #define biarc_ref typename vector< Biarc<Vector> >::reference
00036 #define biarc_constref typename vector< Biarc<Vector> >::const_reference
00037 
00038 template<class Vector>
00039 class Curve : public PKFmanip {
00040 
00041   void init();
00042   int _hint_i, _hint_j;
00043 
00044  protected:
00045 
00046   vector<Biarc<Vector> > _Biarcs;
00047   biarc_constit accessBiarc(int i) const;
00048   biarc_it accessBiarc(int i);
00049 
00050 //  int _NoNodes;
00051   int _Closed;
00052 
00053   int readSinglePKF(istream &in);
00054   ostream& writeSinglePKF(ostream &out);
00055   int readSingleData(istream &in, const char* delimiter);
00056   ostream& writeSingleData(ostream &out, const char* delimiter,
00057                       int tangents_flag);
00058   int readSingleXYZ(istream &in);
00059 
00060  public:
00061 
00062   Curve();
00063   Curve(const char* filename);
00064   Curve(istream &in);
00065   Curve(const Curve<Vector> &curve);
00066   Curve &operator= (const Curve<Vector> &c);
00067   ~Curve();
00068 
00069   biarc_ref operator[](int c);
00070   biarc_constref operator[](int c) const;
00071 
00072   void push(const Biarc<Vector> &b);
00073   void push(const Vector &p, const Vector &t);
00074   void append(const Biarc<Vector> &b);
00075   void append(const Vector &p,const Vector &t);
00076   void insert(int loc, const Biarc<Vector> &b);
00077   void insert(int loc, const Vector &p, const Vector &t);
00078   void remove(int loc);
00079   void remove(biarc_it b);
00080   void flush_all();
00081 
00082   biarc_constref getNext(int i) const;
00083   biarc_constref getPrevious(int i);
00084   void setNext(int i,const Biarc<Vector>& b);
00085   void setPrevious(int i,const Biarc<Vector>& b);
00086 
00087   biarc_it begin();
00088   biarc_it end();
00089 
00090   int isClosed() const ;
00091   void link() ;
00092   void unlink() ;
00093 
00094   void changeDirection() ;
00095 
00096   void make(float f);
00097   void makeMidpointRule();
00098   void make(int from_N, int to_N, float f);
00099   void make(Biarc<Vector>* from, Biarc<Vector>* to, float f);
00100   void makeMidpointRule(int from_N, int to_N);
00101   void makeMidpointRule(Biarc<Vector>* from, Biarc<Vector>* to);
00102 
00103   void resample(int NewNoNodes);
00104   void refine(int from_N, int to_N, int NewNoNodes);
00105   void refine(biarc_it from, biarc_it to, int NewNoNodes);
00106 
00107   // caution it's actually radius_tp !!!
00108   float radius_pt(int from, int to);
00109   float radius_pt(biarc_it from, biarc_it to);
00110   float radius_pt(const Biarc<Vector> &from, const Biarc<Vector> &to);
00111   float radius_pt(const Vector &p0, const Vector &t0, const Vector &p1) const;
00112   float radius_pt(const float s, const float t) const;
00113  
00114   float pp(int from, int to) const;
00115   float pp(float s, float t) const;
00116   
00117   float radius_global(Biarc<Vector>& at);
00118 
00119   float thickness_fast();
00120   float thickness(Vector *from = NULL, Vector *to = NULL);
00121 
00122   void get_hint(int *i, int *j) const;
00123   void set_hint(const int i, const int j);
00124 
00125   float minSegDistance();
00126   float maxSegDistance();
00127   float span() const;
00128   float distEnergy();
00129 
00130   float curvature(int n);
00131   float curvature(biarc_it b);
00132   Vector normalVector(int n);
00133   Vector normalVector(biarc_it b);
00134   float torsion(int n, int a);
00135   float torsion2(int n);
00136 
00137   // Return Frenet-Frame
00138   // void frenet(Vector3& t, Vector3& n, Vector3& b);
00139 
00140   void inertiaTensor(Matrix3& mat);
00141   void principalAxis(Matrix3& mat);
00142 
00143   void computeTangents();
00144   void polygonalToArcs();
00145   void arcsToPolygonal();
00146 
00147   int nodes() const;
00148   float length() const;
00149   Vector pointAt(float s) const;
00150   Vector tangentAt(float s) const;
00151   biarc_it biarcAt(float s);
00152   int biarcPos(float s);
00153 
00154   Curve& rotAroundAxis(float angle,Vector axis);
00155   Curve& operator+=(const Vector &v);
00156   Curve& operator-=(const Vector &v);
00157 
00158   Curve operator+(const Curve &c) const;
00159   Curve operator-(const Curve &c) const;
00160   Curve operator*(const float s) const;
00161 
00162   Curve& apply(Matrix3 &m);
00163   Vector getCenter() ;
00164   void center();
00165   void normalize();
00166   void scale(float s);
00167 
00168   void check_tangents();
00169 
00170   int readPKF(const char* filename);
00171   int readPKF(istream &in);
00172   int writePKF(const char* filename, int Header = 1);
00173   int writePKF(ostream &out, int Header = 1);
00174   int readXYZ(const char* filename);
00175   int readXYZ(istream &in);
00176   int readData(const char* filename, const char* delimiter);
00177   int readData(istream &in, const char* delimiter);
00178   int writeData(const char* filename, const char* delimiter,
00179                       int tangents_flag);
00180   int writeData(ostream &out, const char* delimiter,
00181                       int tangents_flag);
00182 
00183   //  friend ostream & operator<<(ostream &out, const Curve<Vector> &c);
00184 };
00185 
00186 template<class Vector>
00187 inline ostream &operator<<(ostream &out, Curve<Vector> &c) {
00188     
00189   if (c.nodes()==0) {
00190     out << "-- Curve : No points. --";
00191     return out;
00192   }
00193   
00194   for (biarc_it it=c.begin();it!=c.end();it++) {
00195     out << "Biarc "<< it->id() <<" : " << (*it);
00196     if (it!=(c.end()-1)) out << '\n';
00197   }
00198   return out;
00199 }
00200 
00201 // Templates need all code in the same file
00202 // that's why we include here the source file!!!
00203 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00204 #include "../lib/Curve.cpp"
00205 #endif
00206 
00207 #endif // __CURVE_H__

Generated on Mon Feb 8 17:22:35 2010 for libbiarc by  doxygen 1.5.6