Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

gunu_linearize.cpp

Go to the documentation of this file.
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 #include "stdafx.h"
00021 
00022 #include <iostream>
00023 
00024 #include "gul_types.h"
00025 #include "gust_new.h"
00026 #include "gul_vector.h"
00027 #include "guge_linear.h"
00028 #include "guge_normalize.h"
00029 #include "gunu_basics.h"
00030 #include "gunu_derivatives.h"
00031 #include "gunu_divide.h"
00032 #include "gunu_intersect.h"
00033 #include "gunu_linearize.h"
00034 
00035 namespace gunu {
00036 
00037 using gul::List;
00038 using gul::List2;
00039 using gul::ListNode;
00040 using gul::point;
00041 using gul::hpoint;
00042 using gul::point2;
00043 using gul::hpoint2;
00044 using gul::curve;
00045 using gul::Min;
00046 using gul::set;
00047 // using gul::is_zero;
00048 using gul::normalize;
00049 using gul::euclid;
00050 
00051 /************************************************************************
00052   Callback fuer Dreiecksausgabe
00053   *************************************************************************/
00054 template< class T, class HP >
00055 void LinearizeCallback( TessInfo<T,HP> *A, void *usrdata )
00056 {
00057   point<T> P00,P01,P10,P11,T00,T01,T10,T11;
00058   int nu,nv,pu,pv;
00059   LinearizeCallbackData<T,HP> *cb = (LinearizeCallbackData<T,HP> *)usrdata;
00060   Ptr< Ptr< HP > > Pw;
00061 
00062   P00 = A->org->P00;
00063   P01 = A->org->P01;
00064   P10 = A->org->P10;
00065   P11 = A->org->P11;
00066 
00067   if( cb->much_info == 0 )
00068   {
00069     cb->usrfunc( cb->usrdata, 
00070                  &P00, &P01, &P10, &P11, NULL, NULL, NULL, NULL,
00071                  NULL, NULL, NULL, NULL );
00072   }
00073   else                             /* Calc Normals */
00074   {
00075     nu = A->org->nu;
00076     pu = A->org->pu;
00077     nv = A->org->nv;
00078     pv = A->org->pv;
00079     Pw = A->org->Pw;
00080   
00081     CalcNormal_U0V0<T>( nu, pu, nv, pv, Pw, &T00 );
00082     CalcNormal_U1V0<T>( nu, pu, nv, pv, Pw, &T01 );
00083     CalcNormal_U0V1<T>( nu, pu, nv, pv, Pw, &T10 );
00084     CalcNormal_U1V1<T>( nu, pu, nv, pv, Pw, &T11 );
00085 
00086     cb->usrfunc( cb->usrdata, &P00, &P01, &P10, &P11,
00087                  &T00, &T01, &T10, &T11, &A->u1, &A->u2, &A->v1, &A->v2 );
00088   }
00089 }
00090 /************************************************************************
00091   Callback fuer Dreiecksausgabe
00092   *************************************************************************/
00093 template void LinearizeCallback( 
00094                    TessInfo<float,hpoint<float> > *A, void *usrdata );
00095 template void LinearizeCallback( 
00096                    TessInfo<double,hpoint<double> > *A, void *usrdata );
00097 
00098 template void LinearizeCallback( 
00099                    TessInfo<float,point<float> > *A, void *usrdata );
00100 template void LinearizeCallback( 
00101                    TessInfo<double,point<double> > *A, void *usrdata );
00102 
00103 /*******************************************************************
00104   NURBS Flaeche durch Dreiecke approximieren
00105 ********************************************************************/
00106 
00107 
00108 template< class T, class HP, class EP >
00109 GULAPI void LinearizeSurface( 
00110             int max_iter,
00111             const int nu, const int pu, Ptr< T > U,
00112             const int nv, const int pv, Ptr< T > V,
00113             Ptr< Ptr < HP > > Pw,
00114             const T tol,
00115             void (*usrfunc)( void *, 
00116                    EP *, EP *, EP *, EP *,
00117                    EP *, EP *, EP *, EP *,
00118                    T *, T *, T *, T * ),
00119             void *usrdata )
00120 {
00121   TessInfo<T,HP> A,B;
00122   LinearizeCallbackData<T,HP> Cb(0,0,0,0);
00123   
00124 /* Flaechen Info initialisieren */
00125 
00126   A.org->nu = nu;
00127   A.org->pu = pu;  
00128   A.org->U =  U;
00129   A.org->nv = nv;
00130   A.org->pv = pv;  
00131   A.org->V =  V;
00132   A.org->Pw = Pw;
00133 /*
00134   guge::CalcBoundingBox( (A.org->nu+1)*(A.org->nv+1), 
00135                A.org->Pw[0], A.x1, A.x2, A.y1, A.y2, A.z1, A.z2 ); 
00136 */
00137   A.org->P00 = euclid( A.org->Pw[0][0] );
00138   A.org->P01 = euclid( A.org->Pw[0][A.org->nu] );
00139   A.org->P10 = euclid( A.org->Pw[A.org->nv][0] );
00140   A.org->P11 = euclid( A.org->Pw[A.org->nv][A.org->nu] );
00141 
00142   A.u1 = 0.0;
00143   A.u2 = 1.0; 
00144   A.v1 = 0.0;
00145   A.v2 = 1.0; 
00146 
00147 /* Daten fuer Callback initialisieren */
00148 
00149   B.org->nu = nu;
00150   B.org->pu = pu;  
00151   B.org->U =  U;
00152   B.org->nv = nv;
00153   B.org->pv = pv;  
00154   B.org->V =  V;
00155   B.org->Pw = Pw;
00156 /*
00157   guge::CalcBoundingBox( (B.org->nu+1)*(B.org->nv+1), 
00158                B.org->Pw[0], B.x1, B.x2, B.y1, B.y2, B.z1, B.z2 ); 
00159 */
00160   B.org->P00 = euclid( B.org->Pw[0][0] );
00161   B.org->P01 = euclid( B.org->Pw[0][B.org->nu] );
00162   B.org->P10 = euclid( B.org->Pw[B.org->nv][0] );
00163   B.org->P11 = euclid( B.org->Pw[B.org->nv][B.org->nu] );
00164 
00165   B.u1 = 0.0;
00166   B.u2 = 1.0; 
00167   B.v1 = 0.0;
00168   B.v2 = 1.0;   
00169   
00170   Cb.much_info = 1;
00171   Cb.Org = &B;
00172   Cb.usrfunc = usrfunc;
00173   Cb.usrdata = usrdata;
00174 
00175   DoLinearizeSurface<T,HP>( 1, max_iter, &A, tol, LinearizeCallback<T,HP>, 
00176                             &Cb );
00177 }
00178 
00179 
00180 // template instantiation
00181 template GULAPI void LinearizeSurface( 
00182             int max_iter,
00183             const int nu, const int pu, Ptr< float > U,
00184             const int nv, const int pv, Ptr< float > V,
00185             Ptr< Ptr< hpoint<float> > > Pw,
00186             const float tol,
00187             void (*usrfunc)( void *, 
00188                  point<float> *, point<float> *, point<float> *, point<float> *,
00189                  point<float> *, point<float> *, point<float> *, point<float> *,
00190                  float *, float *, float *, float * ),
00191             void *usrdata );
00192 template GULAPI void LinearizeSurface( 
00193           int max_iter,
00194           const int nu, const int pu, Ptr< double > U,
00195           const int nv, const int pv, Ptr< double > V,
00196           Ptr< Ptr< hpoint<double> > > Pw,
00197           const double tol,
00198           void (*usrfunc)( void *, 
00199              point<double> *, point<double> *, point<double> *, point<double> *,
00200              point<double> *, point<double> *, point<double> *, point<double> *,
00201              double *, double *, double *, double * ),
00202           void *usrdata );
00203 
00204 
00205 template GULAPI void LinearizeSurface( 
00206             int max_iter,
00207             const int nu, const int pu, Ptr< float > U,
00208             const int nv, const int pv, Ptr< float > V,
00209             Ptr< Ptr< point<float> > > Pw,
00210             const float tol,
00211             void (*usrfunc)( void *, 
00212                  point<float> *, point<float> *, point<float> *, point<float> *,
00213                  point<float> *, point<float> *, point<float> *, point<float> *,
00214                  float *, float *, float *, float * ),
00215             void *usrdata );
00216 template GULAPI void LinearizeSurface( 
00217           int max_iter,
00218           const int nu, const int pu, Ptr< double > U,
00219           const int nv, const int pv, Ptr< double > V,
00220           Ptr< Ptr< point<double> > > Pw,
00221           const double tol,
00222           void (*usrfunc)( void *, 
00223              point<double> *, point<double> *, point<double> *, point<double> *,
00224              point<double> *, point<double> *, point<double> *, point<double> *,
00225              double *, double *, double *, double * ),
00226           void *usrdata );
00227 
00228 
00229 
00230 /**********************************************************************
00231   linearize curve, internal recurrent function
00232 **********************************************************************/
00233 template< class T, class HP, class EP >
00234 void DoLinearizeCurve( int current_iter, int max_iter,
00235                        curve<T,HP> *C, const T tol,
00236                        void (*usrfunc)( void *, EP *, EP * ), void *usrdata )   
00237 {
00238   if( (current_iter < max_iter) &&
00239       (!guge::IsLinear<T,HP,EP>( C->cpt.n, C->cpt.Pw, tol )) )
00240   {   
00241     curve<T,HP> C1,C2;
00242 
00243     SplitCurve<T,HP>( C, 0.5, &C1, &C2 );
00244     C->cpt.Pw.reset();
00245     C->knt.U.reset();
00246     
00247     DoLinearizeCurve( current_iter+1, max_iter, &C1, tol, usrfunc, usrdata );
00248     DoLinearizeCurve( current_iter+1, max_iter, &C2, tol, usrfunc, usrdata );
00249   }
00250   else
00251   {
00252     EP P1,P2;
00253 
00254     P1 = euclid( C->cpt.Pw[0] );
00255     P2 = euclid( C->cpt.Pw[C->cpt.n] );
00256     C->cpt.Pw.reset();
00257     C->knt.U.reset();
00258 
00259     usrfunc( usrdata, &P1, &P2 );
00260   }
00261 }
00262 /**********************************************************************
00263   linearize curve, internal recurrent function
00264 **********************************************************************/
00265 template void DoLinearizeCurve( int current_iter, int max_iter,
00266                   curve< float,hpoint<float> > *C,
00267                   const float tol,
00268                   void (*usrfunc)( void *, point<float> *, point<float> * ),
00269                   void *usrdata );
00270 template void DoLinearizeCurve( int current_iter, int max_iter,
00271                   curve< double,hpoint<double> > *C,
00272                   const double tol,
00273                   void (*usrfunc)( void *, point<double> *, point<double> * ),
00274                   void *usrdata );
00275 template void DoLinearizeCurve( int current_iter, int max_iter,
00276                   curve< float,hpoint2<float> > *C,
00277                   const float tol,
00278                   void (*usrfunc)( void *, point2<float> *, point2<float> * ),
00279                   void *usrdata );
00280 template void DoLinearizeCurve( int current_iter, int max_iter,
00281                   curve< double,hpoint2<double> > *C,
00282                   const double tol,
00283                   void (*usrfunc)( void *, point2<double> *, point2<double> * ),
00284                   void *usrdata );
00285 
00286 template void DoLinearizeCurve( int current_iter, int max_iter,
00287                   curve< float,point<float> > *C,
00288                   const float tol,
00289                   void (*usrfunc)( void *, point<float> *, point<float> * ),
00290                   void *usrdata );
00291 template void DoLinearizeCurve( int current_iter, int max_iter,
00292                   curve< double,point<double> > *C,
00293                   const double tol,
00294                   void (*usrfunc)( void *, point<double> *, point<double> * ),
00295                   void *usrdata );
00296 template void DoLinearizeCurve( int current_iter, int max_iter,
00297                   curve< float,point2<float> > *C,
00298                   const float tol,
00299                   void (*usrfunc)( void *, point2<float> *, point2<float> * ),
00300                   void *usrdata );
00301 template void DoLinearizeCurve( int current_iter, int max_iter,
00302                   curve< double,point2<double> > *C,
00303                   const double tol,
00304                   void (*usrfunc)( void *, point2<double> *, point2<double> * ),
00305                   void *usrdata );
00306 
00307 
00308 
00309 
00310 /**********************************************************************
00311   linearize curve
00312 **********************************************************************/
00313 template< class T, class HP, class EP >
00314 GULAPI
00315 void LinearizeCurve( int max_iter,
00316              const int n, const int p, Ptr< T > U, Ptr< HP > Pw,
00317              const T tol,
00318              void (*usrfunc)( void *, EP *, EP * ), void *usrdata )
00319 {
00320   curve<T,HP> C;
00321 
00322   C.p = p;
00323   C.knt.m = n+p+1;
00324   C.knt.U = U;
00325 
00326   C.cpt.n = n;
00327   C.cpt.Pw = Pw;
00328 
00329   DoLinearizeCurve( 1, max_iter, &C, tol, usrfunc, usrdata );
00330 }
00331 /**********************************************************************
00332   linearize curve
00333 **********************************************************************/
00334 template GULAPI void LinearizeCurve( int max_iter,
00335            const int n, const int p, Ptr< double > U, Ptr< hpoint<double> > Pw,
00336            const double tol,
00337            void (*usrfunc)( void *, point<double> *, point<double> * ),
00338            void *usrdata );
00339 template GULAPI void LinearizeCurve( int max_iter,
00340            const int n, const int p, Ptr< float > U, Ptr< hpoint<float> > Pw,
00341            const float tol,
00342            void (*usrfunc)( void *, point<float> *, point<float> * ),
00343            void *usrdata );
00344 template GULAPI void LinearizeCurve( int max_iter,
00345            const int n, const int p, Ptr< double > U, Ptr< hpoint2<double> > Pw,
00346            const double tol,
00347            void (*usrfunc)( void *, point2<double> *, point2<double> * ),
00348            void *usrdata );
00349 template GULAPI void LinearizeCurve( int max_iter,
00350            const int n, const int p, Ptr< float > U, Ptr< hpoint2<float> > Pw,
00351            const float tol,
00352            void (*usrfunc)( void *, point2<float> *, point2<float> * ),
00353            void *usrdata );
00354 
00355 template GULAPI void LinearizeCurve( int max_iter,
00356            const int n, const int p, Ptr< double > U, Ptr< point<double> > Pw,
00357            const double tol,
00358            void (*usrfunc)( void *, point<double> *, point<double> * ),
00359            void *usrdata );
00360 template GULAPI void LinearizeCurve( int max_iter,
00361            const int n, const int p, Ptr< float > U, Ptr< point<float> > Pw,
00362            const float tol,
00363            void (*usrfunc)( void *, point<float> *, point<float> * ),
00364            void *usrdata );
00365 template GULAPI void LinearizeCurve( int max_iter,
00366            const int n, const int p, Ptr< double > U, Ptr< point2<double> > Pw,
00367            const double tol,
00368            void (*usrfunc)( void *, point2<double> *, point2<double> * ),
00369            void *usrdata );
00370 template GULAPI void LinearizeCurve( int max_iter,
00371            const int n, const int p, Ptr< float > U, Ptr< point2<float> > Pw,
00372            const float tol,
00373            void (*usrfunc)( void *, point2<float> *, point2<float> * ),
00374            void *usrdata );
00375 
00376 
00377 
00378 /**********************************************************************
00379   linearize curve, returns a polyline
00380 **********************************************************************/
00381 
00382 template< class EP >
00383 void LinearizeLineCallback( void *usrdata, EP *p1, EP *p2 )
00384 {
00385   List< ListNode<EP> > *L = (List< ListNode<EP> > *)usrdata;
00386   ListNode<EP> *node = new ListNode<EP>(*p2);
00387   L->Append(node);
00388 }
00389 template void LinearizeLineCallback( void *usrdata, point2<float> *p1, point2<float> *p2 );
00390 template void LinearizeLineCallback( void *usrdata, point2<double> *p1, point2<double> *p2 );
00391 template void LinearizeLineCallback( void *usrdata, point<float> *p1, point<float> *p2 );
00392 template void LinearizeLineCallback( void *usrdata, point<double> *p1, point<double> *p2 );
00393 
00394 
00395 
00396 template< class T, class HP, class EP >
00397 GULAPI
00398 void LinearizeCurve( int max_iter,
00399              const int n, const int p, Ptr< T > U, Ptr< HP > Pw,
00400              const T tol,
00401              int *ret_nP, Ptr< EP > *retP )
00402 {
00403   List< ListNode<EP> > L;
00404   ListNode<EP> *node = new ListNode<EP>(euclid(Pw[0]));
00405   int i,nEl;
00406   Ptr< EP > P;
00407 
00408   L.Append( node );
00409 
00410   LinearizeCurve( max_iter, n, p, U, Pw, tol,
00411                   LinearizeLineCallback<EP>, (void *)&L );
00412 
00413   nEl = L.nElems;
00414   node = L.head;
00415   P.reserve_pool( nEl );
00416   for( i = 0; i < nEl; i++ )
00417   {
00418     P[nEl-1-i] = node->el;
00419     node = node->next;
00420   }
00421   *ret_nP = nEl;
00422   *retP = P;
00423   
00424   L.DeleteElems();
00425 }
00426 
00427 template GULAPI void LinearizeCurve( int max_iter,
00428           const int n, const int p, Ptr< double > U, Ptr< hpoint<double> > Pw,
00429           const double tol,
00430           int *nP, Ptr< point<double> > *P );
00431 template GULAPI void LinearizeCurve( int max_iter,
00432           const int n, const int p, Ptr< float > U, Ptr< hpoint<float> > Pw,
00433           const float tol,
00434           int *nP, Ptr< point<float> > *P );
00435 template GULAPI void LinearizeCurve( int max_iter,
00436           const int n, const int p, Ptr< double > U, Ptr< hpoint2<double> > Pw,
00437           const double tol,
00438           int *nP, Ptr< point2<double> > *P );
00439 template GULAPI void LinearizeCurve( int max_iter,
00440           const int n, const int p, Ptr< float > U, Ptr< hpoint2<float> > Pw,
00441           const float tol,
00442           int *nP, Ptr< point2<float> > *P );
00443 
00444 template GULAPI void LinearizeCurve( int max_iter,
00445           const int n, const int p, Ptr< double > U, Ptr< point<double> > Pw,
00446           const double tol,
00447           int *nP, Ptr< point<double> > *P );
00448 template GULAPI void LinearizeCurve( int max_iter,
00449           const int n, const int p, Ptr< float > U, Ptr< point<float> > Pw,
00450           const float tol,
00451           int *nP, Ptr< point<float> > *P );
00452 template GULAPI void LinearizeCurve( int max_iter,
00453           const int n, const int p, Ptr< double > U, Ptr< point2<double> > Pw,
00454           const double tol,
00455           int *nP, Ptr< point2<double> > *P );
00456 template GULAPI void LinearizeCurve( int max_iter,
00457           const int n, const int p, Ptr< float > U, Ptr< point2<float> > Pw,
00458           const float tol,
00459           int *nP, Ptr< point2<float> > *P );
00460 
00461 }
00462 
00463 
00464 

Generated on Mon Jan 21 04:17:39 2002 for GUL 0.6 - Geometry Utility Library by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001