00001 /* LIBGUL - Geometry Utility Library 00002 * Copyright (C) 1998-1999 Norbert Irmer 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this library; if not, write to the 00016 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00017 * Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #ifndef GUNU_BASICS_H 00021 #define GUNU_BASICS_H 00022 00023 namespace gunu { 00024 00025 using gul::Ptr; 00026 using gul::point; 00027 using gul::curve; 00028 00029 /************************************************************************* 00030 00031 BASICAL NURBS FUNCTIONS 00032 00033 **************************************************************************/ 00034 00035 /*------------------------------------------------------------------------- 00036 Search for the knotspan containing 'u' in a knot vector 'U' (clamped,degree 00037 'p','n'+1 knots (see "The NURBS book")) 00038 -------------------------------------------------------------------------*/ 00039 template< class T > 00040 int FindSpan( const T u, const int n, const int p, const Ptr< T >& U ); 00041 00042 /*------------------------------------------------------------------------- 00043 Finds the knotspan containing 'u' in a knot vector 'U' (clamped,degree 00044 'p','n'+1 knots (see "The NURBS book")), and counts the multiplicity 00045 of 'u' 00046 -------------------------------------------------------------------------*/ 00047 template< class T > 00048 int FindSpanMultip( const T u, const int n, const int p, 00049 const Ptr< T >& U, int *s ); 00050 00051 /*------------------------------------------------------------------------- 00052 Counts the multiplicity of a knot 'u' 00053 -------------------------------------------------------------------------*/ 00054 template< class T > 00055 inline int GetKnotMultiplicity( const T u, const Ptr< T >& U, const int span ) 00056 { 00057 int l; 00058 00059 l = span; 00060 00061 while( l >= 0 ) 00062 { 00063 if( U[l] != u ) 00064 break; 00065 l--; 00066 } 00067 00068 return(span-l); 00069 } 00070 00071 /*--------------------------------------------------------------------- 00072 Normalizes a clamped knot vector 'U', so that it starts with 0.0 and 00073 ends with 1.0 00074 ---------------------------------------------------------------------*/ 00075 template< class T > 00076 inline void NormalizeKnotVector( const int n, const int p, Ptr< T >& U ) 00077 { 00078 T u0,su; 00079 int i; 00080 00081 u0 = U[0]; 00082 su = U[n+1] - u0; 00083 if( (u0 == 0.) && (su == 1.) ) return; 00084 00085 for( i = 0; i <= p; i++ ) 00086 U[i] = 0.; 00087 for( i = p+1; i <= n; i++ ) 00088 U[i] = (U[i] - u0) / su; 00089 for( i = n+1; i <= n+p+1; i++ ) 00090 U[i] = 1.; 00091 } 00092 00093 /*--------------------------------------------------------------------- 00094 Normalizes a clamped knot vector 'U', so that it starts with 0.0 and 00095 ends with 1.0 00096 ---------------------------------------------------------------------*/ 00097 template< class T > 00098 inline void NormalizeKnotVector( const int n, const int p, const Ptr< T >& U, 00099 T& offsetU, T& scaleU, Ptr< T >& retU ) 00100 { 00101 T u0,su; 00102 int i; 00103 00104 offsetU = u0 = U[0]; 00105 scaleU = su = U[n+1] - u0; 00106 if( (u0 == 0.) && (su == 1.) ) return; 00107 00108 for( i = 0; i <= p; i++ ) 00109 retU[i] = 0.; 00110 for( i = p+1; i <= n; i++ ) 00111 retU[i] = (U[i] - u0) / su; 00112 for( i = n+1; i <= n+p+1; i++ ) 00113 retU[i] = 1.; 00114 } 00115 00116 /* -------------------------------------------------------------------- 00117 Checks whether a knot vector is valid, clamped and normalized 00118 ---------------------------------------------------------------------*/ 00119 template< class T > 00120 GULAPI bool ValidateKnotVec( 00121 const int n, const int p, const Ptr<T>& knt, 00122 bool& is_clamped, bool& is_normalized ); 00123 00124 /*-------------------------------------------------------------------------- 00125 Constructs a uniform clamped knot vector ( u_{i+1}-u_{i}) is equal 00126 for all inner nodes 00127 --------------------------------------------------------------------------*/ 00128 template< class T > 00129 GULAPI void UniformKnotVector( const int n, const int p, Ptr< T >& U ); 00130 00131 /*-------------------------------------------------------------------------- 00132 Constructs a clamped knot vector usabel for a Bezier function 00133 --------------------------------------------------------------------------*/ 00134 00135 template< class T > 00136 void BezierKnotVector( const int p, Ptr< T >& U ) 00137 { 00138 int i; 00139 00140 for( i = 0; i <= p; i++ ) 00141 { 00142 U[i] = 0.; 00143 U[p+1+i] = 1.; 00144 } 00145 } 00146 00147 /*-------------------------------------------------------------------------- 00148 Mirrors a knot vector 00149 --------------------------------------------------------------------------*/ 00150 template< class T > 00151 void MirrorKnotVector( int nU, Ptr<T>& U ) 00152 { 00153 T uw,ui; 00154 int i, m = nU-1; 00155 00156 uw = U[0] + U[m]; 00157 00158 for( i = 1; i <= m/2; i++ ) 00159 { 00160 ui = U[i]; 00161 U[i] = uw - U[m-i]; 00162 U[m-i] = uw - ui; 00163 } 00164 } 00165 00166 /*-------------------------------------------------------------------------- 00167 Mirrors a row vector 00168 --------------------------------------------------------------------------*/ 00169 template< class EP > 00170 void MirrorVector( int nP, Ptr<EP>& P ) 00171 { 00172 int i, m = nP-1; 00173 00174 for( i = 0; i <= m/2; i++ ) 00175 Swap( P[i], P[m-i] ); 00176 } 00177 00178 /*-------------------------------------------------------------------------- 00179 Mirrors a column vector 00180 --------------------------------------------------------------------------*/ 00181 template< class EP > 00182 void MirrorColumns( int nRows, int nCols, Ptr< Ptr<EP> >& P ) 00183 { 00184 int i,j, m = nRows-1; 00185 00186 for( i = 0; i <= m/2; i++ ) 00187 for( j = 0; j < nCols; j++ ) 00188 Swap( P[i][j], P[m-i][j] ); 00189 } 00190 00191 /*------------------------------------------------------------------ 00192 Calculates a single basis function 00193 ------------------------------------------------------------------*/ 00194 template< class T > 00195 GULAPI T CalcBasisFunction( int p, int i, T u, int n, const Ptr<T>& U ); 00196 00197 /*------------------------------------------------------------------ 00198 Calculates the non vanishing NURBS basis functions (see "The NURBS book") 00199 -------------------------------------------------------------------*/ 00200 template< class T > 00201 void CalcBasisFunctions( const T u, const int i, const int p, const Ptr< T >& U, 00202 Ptr< T >& N ); 00203 00204 /*------------------------------------------------------------------------ 00205 Calculates a point on a NURBS curve (see "The NURBS Book") 00206 ----------------------------------------------------------------------- */ 00207 template< class T, class HP, class EP > 00208 GULAPI void CurvePoint( const T u, const int n, const int p, const Ptr< T >& U, 00209 const Ptr< HP >& Pw, EP *C ); 00210 00211 /*------------------------------------------------------------------------ 00212 Calculates a point on a NURBS surface (see "The NURBS Book") 00213 ----------------------------------------------------------------------- */ 00214 template< class T, class HP, class EP > 00215 GULAPI 00216 void SurfacePoint( 00217 const T u, const T v, 00218 const int nu, const int pu, const Ptr< T >& U, 00219 const int nv, const int pv, const Ptr< T >& V, 00220 const Ptr< Ptr< HP > >& Pw, EP *retS ); 00221 00222 /*------------------------------------------------------------------------ 00223 Calculates a point on a Bezier surface (for Bezier surfaces there is 00224 no need to search the knot span) 00225 ----------------------------------------------------------------------- */ 00226 template< class T, class HP, class EP > 00227 GULAPI 00228 void BezierSurfacePoint( 00229 const T u, const T v, 00230 const int pu, const Ptr< T >& U, 00231 const int pv, const Ptr< T >& V, 00232 const Ptr< Ptr< HP > >& Pw, EP *retS ); 00233 00234 /*------------------------------------------------------------------------ 00235 Calculates a point on a NURBS curve (the curve is passed as a structure) 00236 ----------------------------------------------------------------------- */ 00237 template< class T, class HP > 00238 void EvaluateCurve( const curve< T, HP > *Curv, 00239 const T u, point<T> *C ) 00240 { 00241 CurvePoint( u, Curv->cpt.n, Curv->p, Curv->knt.U, Curv->cpt.Pw, C ); 00242 } 00243 00244 00245 } 00246 00247 #endif