MBSim

Project Page
nurbs_curve.h
00001 /* Copyright (C) 2004-2014 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU Lesser General Public
00005  * License as published by the Free Software Foundation; either
00006  * version 2.1 of the License, or (at your option) any later version.
00007  *
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Lesser General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public
00014  * License along with this library; if not, write to the Free Software
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: martin.o.foerg@googlemail.com
00018  */
00019 
00020 #ifndef NURBS_CURVE_FMATVEC_H_
00021 #define NURBS_CURVE_FMATVEC_H_
00022 
00023 #include <mbsim/numerics/nurbs/nurbs_defs.h>
00024 
00025 namespace fmatvec {
00026   typedef Matrix<General, Fixed<4>, Var, double> Mat4xV;
00027 
00028   typedef Matrix<General, Var, Fixed<4>, double> MatVx4;
00029 }
00030 
00031 using namespace std;
00032 using namespace fmatvec;
00033 
00034 namespace MBSim {
00035 
00041   class NurbsCurve {
00042     public:
00046       NurbsCurve();
00047 //        NurbsCurve(Matrix<T> inverse) {
00048 //          Inverse = inverse;
00049 //          Inverse_setted = 1;
00050 //        } //changed
00051 //        NurbsCurve(const NurbsCurve<T, N>& nurb);
00052 //        NurbsCurve(const Vector<fmatvec::HPoint<3>  >& P1, const std::vector<double> &U1, int deg = 3);
00053 //        NurbsCurve(const Vector<fmatvec::Point<3>  >& P1, const std::vector<double> &W, const std::vector<double> &U1, int deg = 3);
00054       virtual ~NurbsCurve(); // empty destructor
00055 
00056       // Reference to internal data
00057       int degree() const 
00058       {
00059         return deg;
00060       }
00061       const fmatvec::MatVx4 & ctrlPnts() const 
00062       {
00063         return P;
00064       }
00065       const fmatvec::Vec4 ctrlPnts(int i) const 
00066       {
00067         return trans(P.row(i));
00068       }
00069       const fmatvec::Vec& knot() const 
00070       {
00071         return U;
00072       }
00073       double knot(int i) const 
00074       {
00075         return U(i);
00076       }
00077 
00078       const fmatvec::Vec getuVec() const {
00079         return u;
00080       }
00081 
00082       // basic functions
00083 
00084 //        virtual void reset(const Vector<fmatvec::HPoint<3>  >& P1, const std::vector<double> &U1, int deg);
00085 //        virtual NurbsCurve& operator=(const NurbsCurve<T, N>&);
00086 //
00087 //        // Evaluattion functions
00088       virtual fmatvec::HPoint<3> operator()(double u) const;
00089       fmatvec::HPoint<3> hpointAt(double u) const  
00090       {
00091         return operator()(u);
00092       }
00093       fmatvec::HPoint<3> hpointAt(double u, int span) const;
00094       fmatvec::Point<3> pointAt(double u) const;
00095 //        friend fmatvec::HPoint<3>  C(double u, const NurbsCurve<T, N>& nurb) {
00096 //          return nurb(u);
00097 //        } //!< a function interface to operator()
00098 //        friend fmatvec::Point<3>  Cp(double u, const NurbsCurve<T, N>& nurb) {
00099 //          return project(nurb(u));
00100 //        } //!< returns the curvePoint in 3D
00101 
00102       // derivative functions
00103       void deriveAtH(double u, int d, fmatvec::MatVx4 & ders) const;
00104 //        void deriveAt(double u, int, Vector<fmatvec::Point<3>  >&) const;
00105 //        void deriveAtH(double u, int, int, Vector<fmatvec::HPoint<3>  >&) const;
00106 //        void deriveAt(double u, int, int, Vector<fmatvec::Point<3>  >&) const;
00107       fmatvec::Point<3> derive3D(double u, int d) const;
00108       fmatvec::HPoint<3> derive(double u, int d) const;
00109       fmatvec::Point<3> normal(double u, const fmatvec::Point<3> & v) const;
00110 
00111       fmatvec::HPoint<3> firstD(double u) const;
00112       fmatvec::HPoint<3> firstD(double u, int span) const;
00113       fmatvec::Point<3> firstDn(double u) const;
00114       fmatvec::Point<3> firstDn(double u, int span) const;
00115 
00116 //        // Basis functions
00117 //        T basisFun(T u, int i, int p = -1) const;
00118 
00119 //        void dersBasisFuns(int n, T u, int span, Matrix<T>& M) const;
00120 //
00121 //        // Knot functions
00122 //        T minKnot() const //! the minimal value for the knot vector
00123 //        {
00124 //          return U[0];
00125 //        }
00126 //        T maxKnot() const //!< the maximal value for the knot vector
00127 //        {
00128 //          return U[U.n() - 1];
00129 //        }
00130 //        void findMultSpan(T u, int& r, int& s) const;
00131 //        int findMult(int r) const;
00132 //        int findKnot(T u) const;
00133 //        T getRemovalBnd(int r, int s) const;
00134 //
00135 //        void removeKnot(int r, int s, int num);
00136 //        void removeKnotsBound(const std::vector<double>& ub, std::vector<double>& ek, T E);
00137 //        int knotInsertion(T u, int r, NurbsCurve<T, N>& nc);
00138 //        void refineKnotVector(const std::vector<double>& X);
00139 //        void refineKnotVectorClosed(const std::vector<double>& X);
00140 //        void mergeKnotVector(const std::vector<double> &Um);
00141 //
00142 //        void clamp();
00143 //        void unclamp();
00144 //
00145 //        // Curve fitting functions
00146 //        int leastSquares(const Vector<fmatvec::Point<3>  >& Q, int degC, int n);
00147 //        int leastSquares(const Vector<fmatvec::Point<3>  >& Q, int degC, int n, const std::vector<double>& ub);
00148 //        int leastSquaresH(const Vector<fmatvec::HPoint<3>  >& Q, int degC, int n, const std::vector<double>& ub);
00149 //        int leastSquares(const Vector<fmatvec::Point<3>  >& Q, int degC, int n, const std::vector<double>& ub, const std::vector<double>& knot);
00150 //        int leastSquaresH(const Vector<fmatvec::HPoint<3>  >& Q, int degC, int n, const std::vector<double>& ub, const std::vector<double>& knot);
00151 //
00152 //        int leastSquaresClosed(const Vector<fmatvec::Point<3>  >& Q, int degC, int n);
00153 //        int leastSquaresClosed(const Vector<fmatvec::Point<3>  >& Q, int degC, int n, const std::vector<double>& ub);
00154 //        int leastSquaresClosedH(const Vector<fmatvec::HPoint<3>  >& Q, int degC, int n, const std::vector<double>& ub);
00155 //        int leastSquaresClosed(const Vector<fmatvec::Point<3>  >& Q, int degC, int n, const std::vector<double>& ub, const std::vector<double>& knot);
00156 //        int leastSquaresClosedH(const Vector<fmatvec::HPoint<3>  >& Q, int degC, int n, const std::vector<double>& ub, const std::vector<double>& knot);
00157 //
00158 //        void globalApproxErrBnd(Vector<fmatvec::Point<3>  >& Q, int deg, T E);
00159 //        void globalApproxErrBnd(Vector<fmatvec::Point<3>  >& Q, std::vector<double>& ub, int deg, T E);
00160 //        void globalApproxErrBnd2(Vector<fmatvec::Point<3>  >& Q, int degC, T E);
00161 //        void globalApproxErrBnd3(Vector<fmatvec::Point<3>  >& Q, int degC, T E);
00162 //        void globalApproxErrBnd3(Vector<fmatvec::Point<3>  >& Q, const std::vector<double> &ub, int degC, T E);
00163 //
00164 //        void globalInterp(const Vector<fmatvec::Point<3>  >& Q, int d);
00168       void globalInterp(const std::vector<fmatvec::Point<3> >& Q, const std::vector<double>& uk, int d, bool updateLater = false);
00169       void globalInterp(const std::vector<fmatvec::Point<3> >& Q, double uMin, double uMax, int d, bool updateLater = false);
00170       void globalInterp(const fmatvec::MatVx3& Q, double uMin, double uMax, int d, bool updateLater = false);
00171 
00172 //        void globalInterpH(const Vector<fmatvec::HPoint<3>  >& Q, int d);
00173 //        void globalInterpH(const Vector<fmatvec::HPoint<3>  >& Q, const std::vector<double>& U, int d);
00174       void globalInterpH(const MatVx4& Q, const Vec& ub, const Vec& Uc, int d, bool updateLater = false);
00175 //        void globalInterpClosed(const Vector<fmatvec::Point<3>  >& Qw, int d);
00179       void globalInterpClosed(const fmatvec::MatVx3 & Q, double uMin, double uMax, int d, bool updateLater = false);
00180 
00184       void update(const fmatvec::MatVx3& Q);
00185       void update(const fmatvec::MatVx4& Q);
00186 
00187 //        void globalInterpClosedH(const Vector<fmatvec::HPoint<3>  >& Qw, int d);
00188 //        void globalInterpClosedH(const Vector<fmatvec::HPoint<3>  >& Qw, const std::vector<double>& U, int d);
00189       void globalInterpClosedH(const MatVx4& Qw, const Vec& ub, const Vec& Uc, int d, bool updateLater = false);
00190 //        void globalInterpClosed(const Vector<fmatvec::Point<3>  >& Qw, const std::vector<double>& ub, const std::vector<double>& Uc, int d);
00191 //
00192 //        void globalInterpD(const Vector<fmatvec::Point<3>  >& Q, const Vector<fmatvec::Point<3>  >& D, int d, int unitD, T a = 1.0);
00193 //
00194 //        Matrix<T> computeInverse(const std::vector<double> &v, const std::vector<double> &V, const int p);
00195 //        Matrix<T> computeInverseClosed(const std::vector<double> &v, const std::vector<double> &V, const int p);
00196 //
00197 //        void projectTo(const fmatvec::Point<3> & p, T guess, T& u, fmatvec::Point<3> & r, T e1 = 0.001, T e2 = 0.001, int maxTry = 100) const;
00198 //
00199 //        T length(T eps = 0.001, int n = 100) const;
00200 //        T lengthIn(T us, T ue, T eps = 0.001, int n = 100) const;
00201 //        T lengthF(T) const;
00202 //        T lengthF(T, int) const;
00203 //
00204 //        // Generate type of curve
00205 //        void makeCircle(const fmatvec::Point<3> & O, const fmatvec::Point<3> & X, const fmatvec::Point<3> & Y, T r, double as, double ae);
00206 //        void makeCircle(const fmatvec::Point<3> & O, T r, double as, double ae);
00207 //        void makeCircle(const fmatvec::Point<3> & O, T r);
00208 //        void makeLine(const fmatvec::Point<3> & P0, const fmatvec::Point<3> & P1, int d);
00209 //        virtual void degreeElevate(int t);
00210 //
00211 //  #ifndef HAVE_ISO_FRIEND_DECL
00212 //        friend void generateCompatibleCurves(NurbsCurveArray<T, N> &ca);
00213 //  #else
00214 //        friend void generateCompatibleCurves <>(NurbsCurveArray<T,N> &ca);
00215 //  #endif
00216 //
00217 //        void decompose(NurbsCurveArray<T, N>& c) const;
00218 //        void decomposeClosed(NurbsCurveArray<T, N>& c) const;
00219 //
00220 //        int splitAt(T u, NurbsCurve<T, N>& cl, NurbsCurve<T, N>& cu) const;
00221 //        int mergeOf(const NurbsCurve<T, N>& cl, const NurbsCurve<T, N> &cu);
00222 //
00223 //        // Modifies the NURBS curve
00224 //        void transform(const MatrixRT<T>& A);
00225       void modCP(int i, const fmatvec::HPoint<3> & a) {
00226         P.set(i, a.T());
00227       } // To manipulate the value of the control point $P[i]$
00228 //        void modCPby(int i, const fmatvec::HPoint<3> & a) {
00229 //          P[i] += a;
00230 //        } // To manipulate the value of the control point $P[i]$
00231 //        virtual void modKnot(const std::vector<double>& knotU) {
00232 //          if (knotU.n() - deg_ - 1 == P.n())
00233 //            U = knotU;
00234 //        }  // to change the values of the knot vector only if the size is compatible with P.n
00235 
00236 //        int movePoint(T u, const fmatvec::Point<3> & delta);
00237 //        int movePoint(T u, const BasicArray<fmatvec::Point<3>  >& delta);
00238 //        int movePoint(const BasicArray<T>& ur, const BasicArray<fmatvec::Point<3>  >& D);
00239 //        int movePoint(const BasicArray<T>& ur, const BasicArray<fmatvec::Point<3>  >& D, const BasicArray_INT& Dr, const BasicArray_INT& Dk);
00240 //        int movePoint(const BasicArray<T>& ur, const BasicArray<fmatvec::Point<3>  >& D, const BasicArray_INT& Dr, const BasicArray_INT& Dk, const BasicArray_INT& fixCP);
00241 //
00242 //        void setTangent(T u, const fmatvec::Point<3> & T0);
00243 //        void setTangentAtEnd(const fmatvec::Point<3> & T0, const fmatvec::Point<3> & T1);
00244 //
00245 //        // I/O functions
00246 //        int read(const char*);
00247 //        int write(const char*) const;
00248 //        virtual int read(ifstream &fin);
00249 //        int write(ofstream &fout) const;
00250 //        int writePS(const char*, int cp = 0, T magFact = T(-1), T dash = T(5), bool bOpen = true) const;
00251 //        int writePSp(const char*, const Vector<fmatvec::Point<3>  >&, const Vector<fmatvec::Point<3>  >&, int cp = 0, T magFact = 0.0, T dash = 5.0, bool bOpen = true) const;
00252 //
00253 //        int writeVRML(ostream &fout, T radius, int K, const Color& color, int Nu, int Nv, T u_s, T u_e) const;
00254 //        int writeVRML(const char* filename, T radius, int K, const Color& color, int Nu, int Nv, T u_s, T u_e) const;
00255 //        int writeVRML(const char* filename, T radius = 1, int K = 5, const Color& color = whiteColor, int Nu = 20, int Nv = 20) const {
00256 //          return writeVRML(filename, radius, K, color, Nu, Nv, U[0], U[U.n() - 1]);
00257 //        } // writes the curve to a VRML file
00258 //        int writeVRML(ostream& fout, T radius = 1, int K = 5, const Color& color = whiteColor, int Nu = 20, int Nv = 20) const {
00259 //          return writeVRML(fout, radius, K, color, Nu, Nv, U[0], U[U.n() - 1]);
00260 //        } // writes the curve to a VRML file
00261 //
00262 //        int writeVRML97(const char* filename, T radius, int K, const Color& color, int Nu, int Nv, T u_s, T u_e) const;
00263 //        int writeVRML97(ostream &fout, T radius, int K, const Color& color, int Nu, int Nv, T u_s, T u_e) const;
00264 //        int writeVRML97(const char* filename, T radius = 1, int K = 5, const Color& color = whiteColor, int Nu = 20, int Nv = 20) const {
00265 //          return writeVRML97(filename, radius, K, color, Nu, Nv, U[0], U[U.n() - 1]);
00266 //        } // writes the curve to a VRML file
00267 //        int writeVRML97(ostream& fout, T radius = 1, int K = 5, const Color& color = whiteColor, int Nu = 20, int Nv = 20) const {
00268 //          return writeVRML97(fout, radius, K, color, Nu, Nv, U[0], U[U.n() - 1]);
00269 //        } // writes the curve to a VRML file
00270 //
00271 //        int writeDisplayLINE(const char* filename, int iNu, const Color& color = blueColor, T fA = 1) const;
00272 //        int writeDisplayLINE(const char* filename, const Color& color, int iNu, T u_s, T u_e) const;
00273 //        void drawImg(Image_UBYTE& Img, unsigned char color = 255, T step = 0.01);
00274 //        void drawImg(Image_Color& Img, const Color& color, T step = 0.01);
00275 //        void drawAaImg(Image_Color& Img, const Color& color, int precision = 3, int alpha = 1);
00276 //        void drawAaImg(Image_Color& Img, const Color& color, const NurbsCurve<T, 3>& profile, int precision = 3, int alpha = 1);
00277 //        NurbsSurface<T, 3> drawAaImg(Image_Color& Img, const Color& color, const NurbsCurve<T, 3>& profile, const NurbsCurve<T, 3> &scaling, int precision = 3, int alpha = 1);
00278 //
00279 //        BasicList<fmatvec::Point<3>  > tesselate(T tolerance, BasicList<T> *uk) const;
00280 
00281       int findSpan(double u) const;
00282 
00283     protected:
00284       fmatvec::MatVx4 P; // the vector of control points
00285       fmatvec::SqrMat inverse; //Inverse of Ansatz-functions in case of only update later (different points, same knot-Vecs and same degree)
00286       fmatvec::Vec u;  // the knot vector
00287       fmatvec::Vec U;  // the knot vector
00288       int deg;  // the degree of the NURBS curve
00289 
00290       void resize(int n, int Deg);
00291 
00292       void knotAveraging(const std::vector<double>& uk, int deg);
00293       double chordLengthParam(const MatVx3& Q, Vec& ub);
00294       void updateUVecs(double uMin, double uMax);
00295 
00296       void knotAveragingClosed(const std::vector<double>& uk, int deg);
00297       void updateUVecsClosed(double uMin, double uMax);
00298 
00299 //      Matrix<T> Inverse; //changed
00300 //      int Inverse_setted; //changed
00301   };
00302 
00303   //TODO: put those functions into mother nurbs class (maybe) to make them "func(...) const"
00304   void knotAveraging(const Vec& uk, int deg, Vec& U);
00305   void knotAveragingClosed(const Vec& uk, int deg, Vec& U);
00306   void basisFuns(double u, int span, int deg, const fmatvec::Vec & U, fmatvec::Vec& funs);
00307   void dersBasisFuns(int n, double u, int span, int deg, const fmatvec::Vec & U, fmatvec::Mat& ders);
00308   void binomialCoef(Mat& Bin);
00309 
00310 }
00311 #endif