fourier_syn.h

00001 #ifndef __FOURIER_SYN__
00002 #define __FOURIER_SYN__
00003 
00004 #include <stdlib.h>
00005 #include <assert.h>
00006 #include <math.h>
00007 
00008 #include <iostream>
00009 #include <iomanip>
00010 #include <fstream>
00011 #include <vector>
00012 
00013 #include <Vector3.h>
00014 #include <Vector4.h>
00015 #include <Matrix3.h>
00016 #include <Curve.h>
00017 
00018 typedef Vector3 Coeff;
00019 typedef vector<Coeff> Coeffs;
00020 typedef unsigned int uint;
00021 
00022 class FourierKnot {
00023 
00024 public:
00025 
00026   // Members
00027   Coeff c0;
00028   Coeffs csin, ccos;
00029   
00030 
00031   // Constructor / Destructor
00032   FourierKnot();
00033   virtual ~FourierKnot();
00034   FourierKnot(const char* file);
00035   FourierKnot(Coeffs lsin, Coeffs lcos);
00036   FourierKnot(Coeff constant, Coeffs lsin, Coeffs lcos);
00037 
00038   void clear();
00039   void scale(float s);
00040 
00041   void set(Coeff c, Coeffs lsin, Coeffs lcos);
00042   void setConst(Coeff constant);
00043   void setSin(Coeffs lsin);
00044   void setCos(Coeffs lcos);
00045 
00046   virtual Vector3 operator()(float t) const;
00047   virtual Vector3 prime(float t) const;
00048   virtual Vector3 primeprime(float t) const;
00049   virtual float curvature(float t) const;
00050 
00051   virtual void toCurve(const int sampling, Curve<Vector3> *curve);
00052   virtual void toCurve(float(*pt2func)(float), const int sampling, Curve<Vector3> *curve);
00053   virtual void toCurveOnS3(const int sampling, Curve<Vector4> *curve);
00054   void rotate(const Vector3 v,float alpha);
00055   void apply(Matrix3 &m);
00056   void mirror(const Vector3 v);
00057   void flip_dir(float sh = 0);
00058   void shift(float sh);
00059 
00060   virtual void mul(const float d);
00061   virtual void div(const float d);
00062 
00063   FourierKnot operator*(const float d) const;
00064   FourierKnot operator/(const float d) const;
00065   FourierKnot operator+(const FourierKnot &fk) const;
00066 
00067   // Friend classes
00068   friend istream& operator>>(istream &in, FourierKnot &fk);
00069   friend ostream& operator<<(ostream &out, const FourierKnot &fk);
00070 
00071 };
00072 
00073 
00074 inline Vector3 cut(Vector3 v) {
00075   if (fabs(v[0])<1e-15) v[0] = 0;
00076   if (fabs(v[1])<1e-15) v[1] = 0;
00077   if (fabs(v[2])<1e-15) v[2] = 0;
00078   return v;
00079 }
00080 
00081 // FourierKnot Classes I/O Friends
00082 inline ostream& operator<<(ostream &out, const FourierKnot &fk) {
00083   assert(fk.ccos.size()==fk.csin.size());
00084   out << fk.c0 << endl;
00085   for (uint i=0;i<fk.ccos.size();++i)
00086     out << cut(fk.ccos[i]) << " " << cut(fk.csin[i]) << endl;
00087   return out;
00088 }
00089 
00090 // Read in a fourier coeff file
00091 // structure :
00092 // a00 a01 a02             | (constant)
00093 // a10 a11 a12 b10 b11 b12 | (cos sin)
00094 // ...
00095 inline istream& operator>>(istream &in, FourierKnot &fk) {
00096   Coeff c,c0,c1; Coeffs csin, ccos;
00097   in >> c;
00098   while ((in >> c0 >> c1)) {
00099     ccos.push_back(c0); csin.push_back(c1);
00100   }
00101   if (in.fail() && !in.eof()) {
00102     cerr << "istream (FourierKnot) : Bad input!\n";
00103     exit(1);
00104   };
00105   fk.set(c,csin,ccos);
00106   return in;
00107 }
00108 
00109 #endif // __FOURIER_SYN__

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