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

guge_normalize.h

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 #ifndef GUGE_NORMALIZE_H
00021 #define GUGE_NORMALIZE_H
00022 
00023 namespace guge {
00024 
00025 using gul::Ptr;
00026 using gul::point;
00027 using gul::point2;
00028 using gul::point1;
00029 using gul::hpoint;
00030 using gul::hpoint2;
00031 using gul::hpoint1;
00032 
00033 /*-----------------------------------------------------------------------
00034   Update an existing bounding box with additional points
00035 -----------------------------------------------------------------------*/
00036 template< class T >
00037 inline void UpdateBoundingBoxH( const int nP, const Ptr< hpoint<T> > &Pw,
00038                                 T& minx, T& maxx,
00039                                 T& miny, T& maxy,
00040                                 T& minz, T& maxz )
00041 {
00042   T x,y,z,w;  
00043   int i;
00044 
00045   for( i = 0; i < nP; i++ )
00046   {
00047     w = Pw[i].w;
00048     
00049     x = Pw[i].x / w;
00050     if( x < minx ) minx = x;
00051     else if( x > maxx ) maxx = x;
00052     
00053     y = Pw[i].y / w;
00054     if( y < miny ) miny = y;
00055     else if( y > maxy ) maxy = y;
00056     
00057     z = Pw[i].z / w;
00058     if( z < minz ) minz = z;
00059     else if( z > maxz ) maxz = z;
00060   }  
00061 }
00062 
00063 template< class T >
00064 inline void UpdateBoundingBoxH( const int nP, const Ptr< hpoint2<T> > &Pw,
00065                                 T& minx, T& maxx,
00066                                 T& miny, T& maxy )
00067 {
00068   T x,y,w;  
00069   int i;
00070 
00071   for( i = 0; i < nP; i++ )
00072   {
00073     w = Pw[i].w;
00074     
00075     x = Pw[i].x / w;
00076     if( x < minx ) minx = x;
00077     else if( x > maxx ) maxx = x;
00078     
00079     y = Pw[i].y / w;
00080     if( y < miny ) miny = y;
00081     else if( y > maxy ) maxy = y;
00082   }  
00083 }
00084 
00085 template< class T >
00086 inline void UpdateBoundingBoxH( const int nP, const Ptr< hpoint1<T> > &Pw,
00087                                 T& minx, T& maxx )
00088 {
00089   T x,w;  
00090   int i;
00091 
00092   for( i = 0; i < nP; i++ )
00093   {
00094     w = Pw[i].w;
00095     
00096     x = Pw[i].x / w;
00097     if( x < minx ) minx = x;
00098     else if( x > maxx ) maxx = x;
00099   }  
00100 }
00101 
00102 /**********************************************************************/
00103 
00104 template< class T >
00105 inline void UpdateBoundingBoxE( const int nP, const Ptr< point<T> > &P,
00106                                 T& minx, T& maxx,
00107                                 T& miny, T& maxy,
00108                                 T& minz, T& maxz )
00109 {                    
00110   for( int i = 0; i < nP; i++ )
00111   {
00112     if( P[i].x < minx ) minx = P[i].x;
00113     else if( P[i].x > maxx ) maxx = P[i].x;
00114     
00115     if( P[i].y < miny ) miny = P[i].y;
00116     else if( P[i].y > maxy ) maxy = P[i].y;
00117 
00118     if( P[i].z < minz ) minz = P[i].z;
00119     else if( P[i].z > maxz ) maxz = P[i].z;
00120   }                      
00121 }                                              
00122 
00123 template< class T >                      
00124 inline void UpdateBoundingBoxE( const int nP, const Ptr< point2<T> > &P,
00125                                 T& minx, T& maxx,
00126                                 T& miny, T& maxy  )
00127 {
00128   for( int i = 0; i < nP; i++ )
00129   {
00130     if( P[i].x < minx ) minx = P[i].x;
00131     else if( P[i].x > maxx ) maxx = P[i].x;
00132     
00133     if( P[i].y < miny ) miny = P[i].y;
00134     else if( P[i].y > maxy ) maxy = P[i].y;
00135   }
00136 }
00137 
00138 template< class T >                      
00139 inline void UpdateBoundingBoxE( const int nP, const Ptr< point1<T> > &P,
00140                                 T& minx, T& maxx )
00141 {
00142   for( int i = 0; i < nP; i++ )
00143   {
00144     if( P[i].x < minx ) minx = P[i].x;
00145     else if( P[i].x > maxx ) maxx = P[i].x;
00146   }
00147 }
00148 
00149 /*-------------------------------------------------------------------------
00150   Calculate the bounding box of a set of points
00151 ------------------------------------------------------------------------*/
00152 
00153 template< class T >
00154 inline void CalcBoundingBoxE( const int nP, const Ptr< point<T> > &P,
00155                               T& minx, T& maxx,
00156                               T& miny, T& maxy,
00157                               T& minz, T& maxz )
00158 {
00159   minx = maxx = P[0].x;
00160   miny = maxy = P[0].y;
00161   minz = maxz = P[0].z;
00162  
00163   UpdateBoundingBoxE( nP, P, minx, maxx, miny, maxy, minz, maxz );
00164 }
00165                                             
00166 template< class T >
00167 inline void CalcBoundingBoxE( const int nP, const Ptr< point2<T> > &P,
00168                               T& minx, T& maxx,
00169                               T& miny, T& maxy  )
00170 {
00171   minx = maxx = P[0].x;
00172   miny = maxy = P[0].y;
00173  
00174   UpdateBoundingBoxE( nP, P, minx, maxx, miny, maxy );
00175 }
00176 
00177 template< class T >
00178 inline void CalcBoundingBoxE( const int nP, const Ptr< point1<T> > &P,
00179                               T& minx, T& maxx )
00180 {
00181   minx = maxx = P[0].x;
00182  
00183   UpdateBoundingBoxE( nP, P, minx, maxx );
00184 }
00185 
00186 /*-------------------------------------------------------------------------
00187   Calculate the bounding box of a set of homogeneous points (after they
00188   are projected into Euclidian space)
00189 ------------------------------------------------------------------------*/
00190 template< class T >
00191 inline void CalcBoundingBoxH( const int nP, const Ptr< hpoint<T> > &Pw,
00192                               T& minx, T& maxx,
00193                               T& miny, T& maxy,
00194                               T& minz, T& maxz )
00195 {
00196   T w = Pw[0].w;
00197 
00198   minx = maxx = Pw[0].x / w;
00199   miny = maxy = Pw[0].y / w;
00200   minz = maxz = Pw[0].z / w;
00201  
00202   UpdateBoundingBoxH( nP, Pw, minx, maxx, miny, maxy, minz, maxz );
00203 }
00204 
00205 template< class T >
00206 inline void CalcBoundingBoxH( const int nP, const Ptr< hpoint2<T> > &Pw,
00207                               T& minx, T& maxx,
00208                               T& miny, T& maxy )
00209 {
00210   T w = Pw[0].w;
00211 
00212   minx = maxx = Pw[0].x / w;
00213   miny = maxy = Pw[0].y / w;
00214  
00215   UpdateBoundingBoxH( nP, Pw, minx, maxx, miny, maxy );
00216 }
00217 
00218 template< class T >
00219 inline void CalcBoundingBoxH( const int nP, const Ptr< hpoint1<T> > &Pw,
00220                               T& minx, T& maxx )
00221 {
00222   T w = Pw[0].w;
00223 
00224   minx = maxx = Pw[0].x / w;
00225  
00226   UpdateBoundingBoxH( nP, Pw, minx, maxx );
00227 }
00228 
00229 
00230 template< class HP, class EP >
00231 inline void CalcBoundingBoxVerts( int nPw, const Ptr< HP > &Pw, 
00232                                   EP &minP, EP &maxP );
00233 
00234 template< class HP, class EP >
00235 inline void UpdateBoundingBoxVerts(int nPw, const Ptr< HP > &Pw,
00236                                    EP &minP, EP &maxP );
00237 
00238 /* -------------- found no other way than this for VC++ :(((( ----------------------- */
00239 
00240 template<>
00241 inline void CalcBoundingBoxVerts( int nP, const Ptr< point1<float> > &P, 
00242                                   point1<float> &minP, point1<float> &maxP )
00243 {                    
00244   CalcBoundingBoxE( nP, P, minP.x, maxP.x );
00245 }
00246 template<>
00247 inline void CalcBoundingBoxVerts( int nP, const Ptr< point1<double> > &P, 
00248                                   point1<double> &minP, point1<double> &maxP )
00249 {                    
00250   CalcBoundingBoxE( nP, P, minP.x, maxP.x );
00251 }
00252 template<>
00253 inline void CalcBoundingBoxVerts( int nPw, const Ptr< hpoint1<float> > &Pw, 
00254                                   point1<float> &minP, point1<float> &maxP )
00255 {                    
00256   CalcBoundingBoxH( nPw, Pw, minP.x, maxP.x );
00257 }
00258 template<>
00259 inline void CalcBoundingBoxVerts( int nPw, const Ptr< hpoint1<double> > &Pw, 
00260                                   point1<double> &minP, point1<double> &maxP )
00261 {                    
00262   CalcBoundingBoxH( nPw, Pw, minP.x, maxP.x );
00263 }
00264 
00265 template<>
00266 inline void CalcBoundingBoxVerts( int nP, const Ptr< point2<float> > &P, 
00267                                   point2<float> &minP, point2<float> &maxP )
00268 {                    
00269   CalcBoundingBoxE( nP, P, minP.x, maxP.x, minP.y, maxP.y );
00270 }
00271 template<>
00272 inline void CalcBoundingBoxVerts( int nP, const Ptr< point2<double> > &P, 
00273                                   point2<double> &minP, point2<double> &maxP )
00274 {                    
00275   CalcBoundingBoxE( nP, P, minP.x, maxP.x, minP.y, maxP.y );
00276 }
00277 template<>
00278 inline void CalcBoundingBoxVerts( int nPw, const Ptr< hpoint2<float> > &Pw, 
00279                                   point2<float> &minP, point2<float> &maxP )
00280 {                    
00281   CalcBoundingBoxH( nPw, Pw, minP.x, maxP.x, minP.y, maxP.y );
00282 }
00283 template<>
00284 inline void CalcBoundingBoxVerts( int nPw, const Ptr< hpoint2<double> > &Pw, 
00285                                   point2<double> &minP, point2<double> &maxP )
00286 {                    
00287   CalcBoundingBoxH( nPw, Pw, minP.x, maxP.x, minP.y, maxP.y );
00288 }
00289 
00290 template<>
00291 inline void CalcBoundingBoxVerts( int nP, const Ptr< point<float> > &P,
00292                                   point<float> &minP, point<float> &maxP )
00293 {                    
00294   CalcBoundingBoxE( nP, P, minP.x, maxP.x, minP.y, maxP.y, minP.z, maxP.z );                 
00295 }  
00296 template<>
00297 inline void CalcBoundingBoxVerts( int nP, const Ptr< point<double> > &P, 
00298                                   point<double> &minP, point<double> &maxP )
00299 {                    
00300   CalcBoundingBoxE( nP, P, minP.x, maxP.x, minP.y, maxP.y, minP.z, maxP.z );                 
00301 } 
00302 template<>
00303 inline void CalcBoundingBoxVerts( int nPw, const Ptr< hpoint<float> > &Pw,
00304                                   point<float> &minP, point<float> &maxP )
00305 {                    
00306   CalcBoundingBoxH( nPw, Pw, minP.x, maxP.x, minP.y, maxP.y, minP.z, maxP.z );                 
00307 }  
00308 template<>
00309 inline void CalcBoundingBoxVerts( int nPw, const Ptr< hpoint<double> > &Pw,
00310                                   point<double> &minP, point<double> &maxP )
00311 {                    
00312   CalcBoundingBoxH( nPw, Pw, minP.x, maxP.x, minP.y, maxP.y, minP.z, maxP.z );                 
00313 } 
00314 
00315 
00316 template<>
00317 inline void UpdateBoundingBoxVerts( int nP, const Ptr< point1<float> > &P, 
00318                                     point1<float> &minP, point1<float> &maxP )
00319 {                    
00320   UpdateBoundingBoxE( nP, P, minP.x, maxP.x );
00321 }
00322 template<>
00323 inline void UpdateBoundingBoxVerts( int nP, const Ptr< point1<double> > &P, 
00324                                   point1<double> &minP, point1<double> &maxP )
00325 {                    
00326   UpdateBoundingBoxE( nP, P, minP.x, maxP.x );
00327 }
00328 template<>
00329 inline void UpdateBoundingBoxVerts( int nPw, const Ptr< hpoint1<float> > &Pw, 
00330                                     point1<float> &minP, point1<float> &maxP )
00331 {                    
00332   UpdateBoundingBoxH( nPw, Pw, minP.x, maxP.x );
00333 }
00334 template<>
00335 inline void UpdateBoundingBoxVerts( int nPw, const Ptr< hpoint1<double> > &Pw, 
00336                                     point1<double> &minP, point1<double> &maxP )
00337 {                    
00338   UpdateBoundingBoxH( nPw, Pw, minP.x, maxP.x );
00339 }
00340 
00341 template<>
00342 inline void UpdateBoundingBoxVerts( int nP, const Ptr< point2<float> > &P, 
00343                                     point2<float> &minP, point2<float> &maxP )
00344 {                    
00345   UpdateBoundingBoxE( nP, P, minP.x, maxP.x, minP.y, maxP.y );
00346 }
00347 template<>
00348 inline void UpdateBoundingBoxVerts( int nP, const Ptr< point2<double> > &P, 
00349                                   point2<double> &minP, point2<double> &maxP )
00350 {                    
00351   UpdateBoundingBoxE( nP, P, minP.x, maxP.x, minP.y, maxP.y );
00352 }
00353 template<>
00354 inline void UpdateBoundingBoxVerts( int nPw, const Ptr< hpoint2<float> > &Pw, 
00355                                     point2<float> &minP, point2<float> &maxP )
00356 {                    
00357   UpdateBoundingBoxH( nPw, Pw, minP.x, maxP.x, minP.y, maxP.y );
00358 }
00359 template<>
00360 inline void UpdateBoundingBoxVerts( int nPw, const Ptr< hpoint2<double> > &Pw, 
00361                                     point2<double> &minP, point2<double> &maxP )
00362 {                    
00363   UpdateBoundingBoxH( nPw, Pw, minP.x, maxP.x, minP.y, maxP.y );
00364 }
00365 
00366 template<>
00367 inline void UpdateBoundingBoxVerts( int nP, const Ptr< point<float> > &P,
00368                                     point<float> &minP, point<float> &maxP )
00369 {                    
00370   UpdateBoundingBoxE( nP, P, minP.x, maxP.x, minP.y, maxP.y, minP.z, maxP.z );                 
00371 }  
00372 template<>
00373 inline void UpdateBoundingBoxVerts( int nP, const Ptr< point<double> > &P,
00374                                     point<double> &minP, point<double> &maxP )
00375 {                    
00376   UpdateBoundingBoxE( nP, P, minP.x, maxP.x, minP.y, maxP.y, minP.z, maxP.z );                 
00377 } 
00378 template<>
00379 inline void UpdateBoundingBoxVerts( int nPw, const Ptr< hpoint<float> > &Pw,
00380                                     point<float> &minP, point<float> &maxP )
00381 {                    
00382   UpdateBoundingBoxH( nPw, Pw, minP.x, maxP.x, minP.y, maxP.y, minP.z, maxP.z );                 
00383 }  
00384 template<>
00385 inline void UpdateBoundingBoxVerts( int nPw, const Ptr< hpoint<double> > &Pw,
00386                                     point<double> &minP, point<double> &maxP )
00387 {                    
00388   UpdateBoundingBoxH( nPw, Pw, minP.x, maxP.x, minP.y, maxP.y, minP.z, maxP.z );                 
00389 } 
00390 
00391 
00392 
00393 /*-----------------------------------------------------------------------
00394   translate and scale a set of points
00395 ------------------------------------------------------------------------*/
00396 template< class T >
00397 inline void NormalizePointsE( const int nP, const Ptr< point<T> > P,
00398                               const T minx, const T scalex,
00399                               const T miny, const T scaley,
00400                               const T minz, const T scalez ) 
00401 {
00402   for( int i = 0; i < nP; i++ )
00403   {
00404     P[i].x = (P[i].x - minx) / (scalex);
00405     P[i].y = (P[i].y - miny) / (scaley);
00406     P[i].z = (P[i].z - minz) / (scalez);
00407   }
00408 }
00409 template< class T >
00410 inline void NormalizePointsE( const int nP, const Ptr< point2<T> > P,
00411                               const T minx, const T scalex,
00412                               const T miny, const T scaley ) 
00413 {
00414   for( int i = 0; i < nP; i++ )
00415   {
00416     P[i].x = (P[i].x - minx) / (scalex);
00417     P[i].y = (P[i].y - miny) / (scaley);
00418   }
00419 }
00420 /*-----------------------------------------------------------------------
00421   translate and scale a set of points, given in their homogeneous
00422   representation
00423 ------------------------------------------------------------------------*/
00424 template< class T >
00425 inline void NormalizePointsH( const int nP, const Ptr< hpoint<T> > Pw,
00426                               const T minx, const T scalex,
00427                               const T miny, const T scaley,
00428                               const T minz, const T scalez ) 
00429 {
00430   int i;
00431   T w;
00432   for( i = 0; i < nP; i++ )
00433   {
00434     w = Pw[i].w;
00435     Pw[i].x = (Pw[i].x - w*minx) / (scalex);
00436     Pw[i].y = (Pw[i].y - w*miny) / (scaley);
00437     Pw[i].z = (Pw[i].z - w*minz) / (scalez);
00438   }
00439 }
00440 
00441 /*-----------------------------------------------------------------------
00442   re-translate and re-scale a set of points
00443 ------------------------------------------------------------------------*/
00444 template< class T >
00445 inline void DeNormalizePointsE( const int nP, const Ptr< point<T> > P,
00446                                 const T minx, const T scalex,
00447                                 const T miny, const T scaley,
00448                                 const T minz, const T scalez ) 
00449 {
00450   for( int i = 0; i < nP; i++ )
00451   {
00452     P[i].x = P[i].x * (scalex) + minx;
00453     P[i].y = P[i].y * (scaley) + miny;
00454     P[i].z = P[i].z * (scalez) + minz;    
00455   }
00456 }
00457 template< class T >
00458 inline void DeNormalizePointsE( const int nP, const Ptr< point2<T> > P,
00459                                 const T minx, const T scalex,
00460                                 const T miny, const T scaley ) 
00461 {
00462   for( int i = 0; i < nP; i++ )
00463   {
00464     P[i].x = P[i].x * (scalex) + minx;
00465     P[i].y = P[i].y * (scaley) + miny;
00466   }
00467 }
00468 /*-----------------------------------------------------------------------
00469   re-translate and re-scale a set of points, given in their homogeneous
00470   representation
00471 ------------------------------------------------------------------------*/
00472 template< class T >
00473 inline void DeNormalizePointsH( const int nP, const Ptr< hpoint<T> > Pw,
00474                                 const T minx, const T scalex,
00475                                 const T miny, const T scaley,
00476                                 const T minz, const T scalez ) 
00477 {
00478   int i;
00479   T w;
00480   for( i = 0; i < nP; i++ )
00481   {
00482     w = Pw[i].w;
00483     Pw[i].x = Pw[i].x * (scalex) + w * minx;
00484     Pw[i].y = Pw[i].y * (scaley) + w * miny;
00485     Pw[i].z = Pw[i].z * (scalez) + w * minz;    
00486   }
00487 }
00488 
00489 template< class T1, class T2 >
00490 void set( gul::bounding_box<T1> &box, 
00491           const gul::point<T2> &minP, const gul::point<T2> &maxP ) 
00492 {
00493   box.x1 = (T1)minP.x;
00494   box.x2 = (T1)maxP.x;
00495   box.y1 = (T1)minP.y;
00496   box.y2 = (T1)maxP.y;
00497   box.z1 = (T1)minP.z;
00498   box.z2 = (T1)maxP.z;
00499 }
00500 template< class T1, class T2 >
00501 void set( gul::bounding_box<T1> &box, 
00502           const gul::hpoint<T2> &minP, const gul::hpoint<T2>& maxP) 
00503 {
00504   box.x1 = (T1)(minP.x/minP.w);
00505   box.x2 = (T1)(maxP.x/maxP.w);
00506   box.y1 = (T1)(minP.y/minP.w);
00507   box.y2 = (T1)(maxP.y/maxP.w);
00508   box.z1 = (T1)(minP.z/minP.w);
00509   box.z2 = (T1)(maxP.z/maxP.w);
00510 }
00511 
00512 template< class T1, class T2 >
00513 void set( gul::bounding_box<T1> &box, 
00514           const gul::point2<T2> &minP, const gul::point2<T2>& maxP) 
00515 {
00516   box.x1 = (T1)minP.x;
00517   box.x2 = (T1)maxP.x;
00518   box.y1 = (T1)minP.y;
00519   box.y2 = (T1)maxP.y;
00520   box.z1 = 0;
00521   box.z2 = 0;  
00522 }
00523 template< class T1, class T2 >
00524 void set( gul::bounding_box<T1> &box, 
00525           const gul::hpoint2<T2> &minP, const gul::hpoint2<T2>& maxP) 
00526 {
00527   box.x1 = (T1)(minP.x/minP.w);
00528   box.x2 = (T1)(maxP.x/maxP.w);
00529   box.y1 = (T1)(minP.y/minP.w);
00530   box.y2 = (T1)(maxP.y/maxP.w);
00531   box.z1 = 0;
00532   box.z2 = 0;  
00533 }
00534 template< class T1, class T2 >
00535 void set( gul::bounding_box<T1> &box, 
00536           const gul::point1<T2> &minP, const gul::point1<T2>& maxP) 
00537 {
00538   box.x1 = (T1)minP.x;
00539   box.x2 = (T1)maxP.x;
00540   box.y1 = 0;
00541   box.y2 = 0;
00542   box.z1 = 0;
00543   box.z2 = 0;  
00544 }
00545 }
00546                        
00547 #endif
00548 
00549 
00550 
00551 
00552 
00553 

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