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 namespace MBSim {
00032 
00036   class NurbsCurve {
00037     public:
00041       NurbsCurve();
00042 //        NurbsCurve(Matrix<T> inverse) {
00043 //          Inverse = inverse;
00044 //          Inverse_setted = 1;
00045 //        } //changed
00046 //        NurbsCurve(const NurbsCurve<T, N>& nurb);
00047 //        NurbsCurve(const Vector<fmatvec::HPoint<3>  >& P1, const std::vector<double> &U1, int deg = 3);
00048 //        NurbsCurve(const Vector<fmatvec::Point<3>  >& P1, const std::vector<double> &W, const std::vector<double> &U1, int deg = 3);
00049       virtual ~NurbsCurve(); // empty destructor
00050 
00051       // Reference to internal data
00052       int degree() const 
00053       {
00054         return deg;
00055       }
00056       const fmatvec::MatVx4 & ctrlPnts() const 
00057       {
00058         return P;
00059       }
00060       const fmatvec::MatVx4 ctrlPnts(int i) const 
00061       {
00062         return P.row(i);
00063       }
00064       const fmatvec::Vec& knot() const 
00065       {
00066         return U;
00067       }
00068       double knot(int i) const 
00069       {
00070         return U(i);
00071       }
00072 
00073       // basic functions
00074 
00075 //        virtual void reset(const Vector<fmatvec::HPoint<3>  >& P1, const std::vector<double> &U1, int deg);
00076 //        virtual NurbsCurve& operator=(const NurbsCurve<T, N>&);
00077 //
00078 //        // Evaluattion functions
00079       virtual fmatvec::HPoint<3> operator()(double u) const;
00080       fmatvec::HPoint<3> hpointAt(double u) const  
00081       {
00082         return operator()(u);
00083       }
00084       fmatvec::HPoint<3> hpointAt(double u, int span) const;
00085       fmatvec::Point<3> pointAt(double u) const;
00086 //        friend fmatvec::HPoint<3>  C(double u, const NurbsCurve<T, N>& nurb) {
00087 //          return nurb(u);
00088 //        } //!< a function interface to operator()
00089 //        friend fmatvec::Point<3>  Cp(double u, const NurbsCurve<T, N>& nurb) {
00090 //          return project(nurb(u));
00091 //        } //!< returns the curvePoint in 3D
00092 
00093       // derivative functions
00094       void deriveAtH(double u, int, fmatvec::MatVx4 & ders) const;
00095 //        void deriveAt(double u, int, Vector<fmatvec::Point<3>  >&) const;
00096 //        void deriveAtH(double u, int, int, Vector<fmatvec::HPoint<3>  >&) const;
00097 //        void deriveAt(double u, int, int, Vector<fmatvec::Point<3>  >&) const;
00098       fmatvec::Point<3> derive3D(double u, int d) const;
00099       fmatvec::HPoint<3> derive(double u, int d) const;
00100       fmatvec::Point<3> normal(double u, const fmatvec::Point<3> & v) const;
00101 
00102       fmatvec::HPoint<3> firstD(double u) const;
00103       fmatvec::HPoint<3> firstD(double u, int span) const;
00104       fmatvec::Point<3> firstDn(double u) const;
00105       fmatvec::Point<3> firstDn(double u, int span) const;
00106 
00107 //        // Basis functions
00108 //        T basisFun(T u, int i, int p = -1) const;
00109 
00110 //        void dersBasisFuns(int n, T u, int span, Matrix<T>& M) const;
00111 //
00112 //        // Knot functions
00113 //        T minKnot() const //! the minimal value for the knot vector
00114 //        {
00115 //          return U[0];
00116 //        }
00117 //        T maxKnot() const //!< the maximal value for the knot vector
00118 //        {
00119 //          return U[U.n() - 1];
00120 //        }
00121 //        void findMultSpan(T u, int& r, int& s) const;
00122 //        int findMult(int r) const;
00123 //        int findKnot(T u) const;
00124 //        T getRemovalBnd(int r, int s) const;
00125 //
00126 //        void removeKnot(int r, int s, int num);
00127 //        void removeKnotsBound(const std::vector<double>& ub, std::vector<double>& ek, T E);
00128 //        int knotInsertion(T u, int r, NurbsCurve<T, N>& nc);
00129 //        void refineKnotVector(const std::vector<double>& X);
00130 //        void refineKnotVectorClosed(const std::vector<double>& X);
00131 //        void mergeKnotVector(const std::vector<double> &Um);
00132 //
00133 //        void clamp();
00134 //        void unclamp();
00135 //
00136 //        // Curve fitting functions
00137 //        int leastSquares(const Vector<fmatvec::Point<3>  >& Q, int degC, int n);
00138 //        int leastSquares(const Vector<fmatvec::Point<3>  >& Q, int degC, int n, const std::vector<double>& ub);
00139 //        int leastSquaresH(const Vector<fmatvec::HPoint<3>  >& Q, int degC, int n, const std::vector<double>& ub);
00140 //        int leastSquares(const Vector<fmatvec::Point<3>  >& Q, int degC, int n, const std::vector<double>& ub, const std::vector<double>& knot);
00141 //        int leastSquaresH(const Vector<fmatvec::HPoint<3>  >& Q, int degC, int n, const std::vector<double>& ub, const std::vector<double>& knot);
00142 //
00143 //        int leastSquaresClosed(const Vector<fmatvec::Point<3>  >& Q, int degC, int n);
00144 //        int leastSquaresClosed(const Vector<fmatvec::Point<3>  >& Q, int degC, int n, const std::vector<double>& ub);
00145 //        int leastSquaresClosedH(const Vector<fmatvec::HPoint<3>  >& Q, int degC, int n, const std::vector<double>& ub);
00146 //        int leastSquaresClosed(const Vector<fmatvec::Point<3>  >& Q, int degC, int n, const std::vector<double>& ub, const std::vector<double>& knot);
00147 //        int leastSquaresClosedH(const Vector<fmatvec::HPoint<3>  >& Q, int degC, int n, const std::vector<double>& ub, const std::vector<double>& knot);
00148 //
00149 //        void globalApproxErrBnd(Vector<fmatvec::Point<3>  >& Q, int deg, T E);
00150 //        void globalApproxErrBnd(Vector<fmatvec::Point<3>  >& Q, std::vector<double>& ub, int deg, T E);
00151 //        void globalApproxErrBnd2(Vector<fmatvec::Point<3>  >& Q, int degC, T E);
00152 //        void globalApproxErrBnd3(Vector<fmatvec::Point<3>  >& Q, int degC, T E);
00153 //        void globalApproxErrBnd3(Vector<fmatvec::Point<3>  >& Q, const std::vector<double> &ub, int degC, T E);
00154 //
00155 //        void globalInterp(const Vector<fmatvec::Point<3>  >& Q, int d);
00159       void globalInterp(const fmatvec::MatVx3& Q, double uMin, double uMax, int d, bool updateLater = false);
00160 //        void globalInterpH(const Vector<fmatvec::HPoint<3>  >& Q, int d);
00161 //        void globalInterpH(const Vector<fmatvec::HPoint<3>  >& Q, const std::vector<double>& U, int d);
00162 //        void globalInterpH(const Vector<fmatvec::HPoint<3>  >& Q, const std::vector<double>& ub, const std::vector<double>& U, int d);
00163 //        void globalInterpClosed(const Vector<fmatvec::Point<3>  >& Qw, int d);
00167       void globalInterpClosed(const fmatvec::MatVx3 & Q, double uMin, double uMax, int d, bool updateLater = false);
00168 
00172       void update(const fmatvec::MatVx3& Q);
00173 
00174 //        void globalInterpClosedH(const Vector<fmatvec::HPoint<3>  >& Qw, int d);
00175 //        void globalInterpClosedH(const Vector<fmatvec::HPoint<3>  >& Qw, const std::vector<double>& U, int d);
00176 //        void globalInterpClosedH(const Vector<fmatvec::HPoint<3>  >& Qw, const std::vector<double>& ub, const std::vector<double>& U, int d);
00177 //        void globalInterpClosed(const Vector<fmatvec::Point<3>  >& Qw, const std::vector<double>& ub, const std::vector<double>& Uc, int d);
00178 //
00179 //        void globalInterpD(const Vector<fmatvec::Point<3>  >& Q, const Vector<fmatvec::Point<3>  >& D, int d, int unitD, T a = 1.0);
00180 //
00181 //        Matrix<T> computeInverse(const std::vector<double> &v, const std::vector<double> &V, const int p);
00182 //        Matrix<T> computeInverseClosed(const std::vector<double> &v, const std::vector<double> &V, const int p);
00183 //
00184 //        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;
00185 //
00186 //        T length(T eps = 0.001, int n = 100) const;
00187 //        T lengthIn(T us, T ue, T eps = 0.001, int n = 100) const;
00188 //        T lengthF(T) const;
00189 //        T lengthF(T, int) const;
00190 //
00191 //        // Generate type of curve
00192 //        void makeCircle(const fmatvec::Point<3> & O, const fmatvec::Point<3> & X, const fmatvec::Point<3> & Y, T r, double as, double ae);
00193 //        void makeCircle(const fmatvec::Point<3> & O, T r, double as, double ae);
00194 //        void makeCircle(const fmatvec::Point<3> & O, T r);
00195 //        void makeLine(const fmatvec::Point<3> & P0, const fmatvec::Point<3> & P1, int d);
00196 //        virtual void degreeElevate(int t);
00197 //
00198 //  #ifndef HAVE_ISO_FRIEND_DECL
00199 //        friend void generateCompatibleCurves(NurbsCurveArray<T, N> &ca);
00200 //  #else
00201 //        friend void generateCompatibleCurves <>(NurbsCurveArray<T,N> &ca);
00202 //  #endif
00203 //
00204 //        void decompose(NurbsCurveArray<T, N>& c) const;
00205 //        void decomposeClosed(NurbsCurveArray<T, N>& c) const;
00206 //
00207 //        int splitAt(T u, NurbsCurve<T, N>& cl, NurbsCurve<T, N>& cu) const;
00208 //        int mergeOf(const NurbsCurve<T, N>& cl, const NurbsCurve<T, N> &cu);
00209 //
00210 //        // Modifies the NURBS curve
00211 //        void transform(const MatrixRT<T>& A);
00212 //        void modCP(int i, const fmatvec::HPoint<3> & a) {
00213 //          P[i] = a;
00214 //        } // To manipulate the value of the control point $P[i]$
00215 //        void modCPby(int i, const fmatvec::HPoint<3> & a) {
00216 //          P[i] += a;
00217 //        } // To manipulate the value of the control point $P[i]$
00218 //        virtual void modKnot(const std::vector<double>& knotU) {
00219 //          if (knotU.n() - deg_ - 1 == P.n())
00220 //            U = knotU;
00221 //        }  // to change the values of the knot vector only if the size is compatible with P.n
00222 
00223 //        int movePoint(T u, const fmatvec::Point<3> & delta);
00224 //        int movePoint(T u, const BasicArray<fmatvec::Point<3>  >& delta);
00225 //        int movePoint(const BasicArray<T>& ur, const BasicArray<fmatvec::Point<3>  >& D);
00226 //        int movePoint(const BasicArray<T>& ur, const BasicArray<fmatvec::Point<3>  >& D, const BasicArray_INT& Dr, const BasicArray_INT& Dk);
00227 //        int movePoint(const BasicArray<T>& ur, const BasicArray<fmatvec::Point<3>  >& D, const BasicArray_INT& Dr, const BasicArray_INT& Dk, const BasicArray_INT& fixCP);
00228 //
00229 //        void setTangent(T u, const fmatvec::Point<3> & T0);
00230 //        void setTangentAtEnd(const fmatvec::Point<3> & T0, const fmatvec::Point<3> & T1);
00231 //
00232 //        // I/O functions
00233 //        int read(const char*);
00234 //        int write(const char*) const;
00235 //        virtual int read(ifstream &fin);
00236 //        int write(ofstream &fout) const;
00237 //        int writePS(const char*, int cp = 0, T magFact = T(-1), T dash = T(5), bool bOpen = true) const;
00238 //        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;
00239 //
00240 //        int writeVRML(ostream &fout, T radius, int K, const Color& color, int Nu, int Nv, T u_s, T u_e) const;
00241 //        int writeVRML(const char* filename, T radius, int K, const Color& color, int Nu, int Nv, T u_s, T u_e) const;
00242 //        int writeVRML(const char* filename, T radius = 1, int K = 5, const Color& color = whiteColor, int Nu = 20, int Nv = 20) const {
00243 //          return writeVRML(filename, radius, K, color, Nu, Nv, U[0], U[U.n() - 1]);
00244 //        } // writes the curve to a VRML file
00245 //        int writeVRML(ostream& fout, T radius = 1, int K = 5, const Color& color = whiteColor, int Nu = 20, int Nv = 20) const {
00246 //          return writeVRML(fout, radius, K, color, Nu, Nv, U[0], U[U.n() - 1]);
00247 //        } // writes the curve to a VRML file
00248 //
00249 //        int writeVRML97(const char* filename, T radius, int K, const Color& color, int Nu, int Nv, T u_s, T u_e) const;
00250 //        int writeVRML97(ostream &fout, T radius, int K, const Color& color, int Nu, int Nv, T u_s, T u_e) const;
00251 //        int writeVRML97(const char* filename, T radius = 1, int K = 5, const Color& color = whiteColor, int Nu = 20, int Nv = 20) const {
00252 //          return writeVRML97(filename, radius, K, color, Nu, Nv, U[0], U[U.n() - 1]);
00253 //        } // writes the curve to a VRML file
00254 //        int writeVRML97(ostream& fout, T radius = 1, int K = 5, const Color& color = whiteColor, int Nu = 20, int Nv = 20) const {
00255 //          return writeVRML97(fout, radius, K, color, Nu, Nv, U[0], U[U.n() - 1]);
00256 //        } // writes the curve to a VRML file
00257 //
00258 //        int writeDisplayLINE(const char* filename, int iNu, const Color& color = blueColor, T fA = 1) const;
00259 //        int writeDisplayLINE(const char* filename, const Color& color, int iNu, T u_s, T u_e) const;
00260 //        void drawImg(Image_UBYTE& Img, unsigned char color = 255, T step = 0.01);
00261 //        void drawImg(Image_Color& Img, const Color& color, T step = 0.01);
00262 //        void drawAaImg(Image_Color& Img, const Color& color, int precision = 3, int alpha = 1);
00263 //        void drawAaImg(Image_Color& Img, const Color& color, const NurbsCurve<T, 3>& profile, int precision = 3, int alpha = 1);
00264 //        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);
00265 //
00266 //        BasicList<fmatvec::Point<3>  > tesselate(T tolerance, BasicList<T> *uk) const;
00267 
00268     protected:
00269       fmatvec::MatVx4 P; // the vector of control points
00270       fmatvec::SqrMat inverse; //Inverse of Ansatz-functions in case of only update later (different points, same knot-Vecs and same degree)
00271       fmatvec::Vec u;  // the knot vector
00272       fmatvec::Vec U;  // the knot vector
00273       int deg;  // the degree of the NURBS curve
00274 
00275       void resize(int n, int Deg);
00276 
00277       void knotAveraging(const std::vector<double>& uk, int deg);
00278       void updateUVecs(double uMin, double uMax);
00279 
00280       void knotAveragingClosed(const std::vector<double>& uk, int deg);
00281       void updateUVecsClosed(double uMin, double uMax);
00282 
00283       int findSpan(double u) const;
00284 
00285 //      Matrix<T> Inverse; //changed
00286 //      int Inverse_setted; //changed
00287   };
00288 
00289   //TODO: put those functions into mother nurbs class (maybe) to make them "func(...) const"
00290   void basisFuns(double u, int span, int deg, const fmatvec::Vec & U, fmatvec::Vec& funs);
00291   void dersBasisFuns(int n, double u, int span, int deg, const fmatvec::Vec & U, fmatvec::Mat& ders);
00292 
00293 }
00294 #endif