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

gul_vector.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 GUL_VECTOR_H
00021 #define GUL_VECTOR_H
00022 
00023 namespace gul
00024 {
00025 
00026 /**********************************************************************
00027   Homogeneus 3-D points
00028 **********************************************************************/
00029 
00030                                             /* -- set all components ------ */
00031 template< class T >
00032 inline void set( hpoint<T>& c, T a )
00033 {
00034   c.x = a; c.y = a; c.z = a; c.w = a;
00035 }
00036                                /* ------ add two homogeneous vectors ------ */
00037 template <class T>
00038 inline hpoint<T> operator+ ( const hpoint<T>& a, const hpoint<T>& b )
00039 {
00040   return hpoint<T>(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w);
00041 }
00042                          /* --- invert the sign of a homogeneous vector --- */
00043 template <class T>
00044 inline hpoint<T> operator- ( const hpoint<T>& a )
00045 {
00046   return hpoint(-a.x, -a.y, -a.z, -a.w);
00047 }
00048       /* --- calculate the difference a - b  of homogeneous vectors a,b --- */
00049 
00050 template <class T>
00051 inline hpoint<T> operator- ( const hpoint<T>& a, const hpoint<T>& b )
00052 {
00053   return hpoint<T>(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w);
00054 }
00055                    /* ---- multiply a scalar with a homogeneous vector ---- */
00056 template <class T>
00057 inline hpoint<T> operator* ( const T op, const hpoint<T>& a )
00058 {
00059   return hpoint<T>(op*a.x, op*a.y, op*a.z, op*a.w);
00060 }
00061             /* --- calculate the dot product of two homogeneus vectors ---- */
00062 template <class T>
00063 inline T operator* ( const hpoint<T>& a, const hpoint<T>& b )
00064 {
00065   return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
00066 }
00067             /*----- project a homogeneous point into euclidian space ---------*/
00068 template <class T>
00069 inline point<T> euclid( const hpoint<T>& a )
00070 {
00071   return point<T>(a.x/a.w, a.y/a.w, a.z/a.w);
00072 }
00073 template <class T>
00074 inline T weight( const hpoint<T>& a )
00075 {
00076   return a.w;
00077 }
00078 
00079 /**********************************************************************
00080   Homogeneus 2-D points
00081 **********************************************************************/
00082 
00083                                             /* -- set all components ------ */
00084 template< class T >
00085 inline void set( hpoint2<T>& c, T a )
00086 {
00087   c.x = a; c.y = a; c.w = a;
00088 }
00089                                /* ------ add two homogeneous vectors ------ */
00090 template <class T>
00091 inline hpoint2<T> operator+ ( const hpoint2<T>& a, const hpoint2<T>& b )
00092 {
00093   return hpoint2<T>(a.x+b.x, a.y+b.y, a.w+b.w);
00094 }
00095                          /* --- invert the sign of a homogeneous vector --- */
00096 template <class T>
00097 inline hpoint2<T> operator- ( const hpoint2<T>& a )
00098 {
00099   return hpoint2<T>(-a.x, -a.y, -a.w);
00100 }
00101       /* --- calculate the difference a - b  of homogeneous vectors a,b --- */
00102 template <class T>
00103 inline hpoint2<T> operator- ( const hpoint2<T>& a, const hpoint2<T>& b )
00104 {
00105   return hpoint2<T>(a.x-b.x, a.y-b.y, a.w-b.w);
00106 }
00107                    /* ---- multiply a scalar with a homogeneous vector ---- */
00108 template <class T>
00109 inline hpoint2<T> operator* ( const T op, const hpoint2<T>& a )
00110 {
00111   return hpoint2<T>(op*a.x, op*a.y, op*a.w);
00112 }
00113             /* --- calculate the dot product of two homogeneus vectors ---- */
00114 template <class T>
00115 inline T operator* ( const hpoint2<T> a, const hpoint2<T>& b )
00116 {
00117   return a.x*b.x + a.y*b.y + a.w*b.w;
00118 }
00119             /*----- project a homogeneous point into euclidian space ---------*/
00120 template <class T>
00121 inline point2<T> euclid( const hpoint2<T>& a )
00122 {
00123   return point2<T>(a.x/a.w, a.y/a.w);
00124 }
00125 template <class T>
00126 inline T weight( const hpoint2<T>& a )
00127 {
00128   return a.w;
00129 }
00130 
00131 /**********************************************************************
00132   Homogeneus 1-D points
00133 **********************************************************************/
00134 
00135                                             /* -- set all components ------ */
00136 template< class T >
00137 inline void set( hpoint1<T>& c, T a )
00138 {
00139   c.x = a; c.w = a;
00140 }
00141                                /* ------ add two homogeneous vectors ------ */
00142 template <class T>
00143 inline hpoint1<T> operator+ ( const hpoint1<T>& a, const hpoint1<T>& b )
00144 {
00145   return hpoint1<T>(a.x+b.x, a.w+b.w);
00146 }
00147                          /* --- invert the sign of a homogeneous vector --- */
00148 template <class T>
00149 inline hpoint1<T> operator- ( const hpoint1<T>& a )
00150 {
00151   return hpoint1<T>(-a.x, -a.w);
00152 }
00153       /* --- calculate the difference a - b  of homogeneous vectors a,b --- */
00154 template <class T>
00155 inline hpoint1<T> operator- ( const hpoint1<T>& a, const hpoint1<T>& b )
00156 {
00157   return hpoint1<T>(a.x-b.x, a.w-b.w);
00158 }
00159                    /* ---- multiply a scalar with a homogeneous vector ---- */
00160 template <class T>
00161 inline hpoint1<T> operator* ( const T op, const hpoint1<T>& a )
00162 {
00163   return hpoint1<T>(op*a.x, op*a.w);
00164 }
00165             /* --- calculate the dot product of two homogeneus vectors ---- */
00166 template <class T>
00167 inline T operator* ( const hpoint1<T>& a, const hpoint1<T>& b )
00168 {
00169   return a.x*b.x + a.w*b.w;
00170 }
00171             /*----- project a homogeneous point into euclidian space ---------*/
00172 template <class T>
00173 inline point1<T> euclid( const hpoint1<T>& a )
00174 {
00175   return point1<T>(a.x/a.w);
00176 }
00177 template <class T>
00178 inline T weight( const hpoint1<T>& a )
00179 {
00180   return a.w;
00181 }
00182 
00183 /*************************************************************************
00184   3-D points
00185 *************************************************************************/
00186 
00187                                             /* -- set all components ------ */
00188 template< class T >
00189 inline void set( point<T>& c, T a )
00190 {
00191   c.x = a; c.y = a; c.z = a;
00192 }
00193                          /* --- calculate the sum a + b  of vectors a,b --- */
00194 template <class T>
00195 inline point<T> operator+ ( const point<T>& a, const point<T>& b )
00196 {
00197   return point<T>(a.x+b.x, a.y+b.y, a.z+b.z);
00198 }
00199                                      /* --- invert the sign of a vector --- */
00200 template <class T>
00201 inline point<T> operator- ( const point<T>& a )
00202 {
00203   return point<T>(-a.x, -a.y, -a.z);
00204 }
00205                   /* --- calculate the difference a - b  of vectors a,b --- */
00206 template <class T>
00207 inline point<T> operator- ( const point<T>& a, const point<T>& b )
00208 {
00209   return point<T>(a.x-b.x, a.y-b.y, a.z-b.z);
00210 }
00211                                /* ---- multiply a scalar with a vector ---- */
00212 template <class T>
00213 inline point<T> operator* ( const T op, const point<T>& a  )
00214 {
00215   return point<T>(op*a.x, op*a.y, op*a.z);
00216 }
00217                        /* --- calculate the dot product of two vectors ---- */
00218 template <class T>
00219 inline T operator* ( const point<T>& a, const point<T>& b )
00220 {
00221   return a.x*b.x + a.y*b.y + a.z*b.z;
00222 }
00223             /*----- project a homogeneous point into euclidian space ---------*/
00224 template <class T>
00225 inline point<T> euclid( const point<T>& a )
00226 {
00227   return a;
00228 }
00229 template <class T>
00230 inline T weight( const point<T>& a )
00231 {
00232   return (T)1;
00233 }
00234 
00235 /*************************************************************************
00236   2-D points
00237 *************************************************************************/
00238 
00239                                            /* -- test if unequal ------ */
00240 template< class T >
00241 inline bool operator!= ( const point2<T>& a, const point2<T>& b )
00242 {
00243   if( (a.x != b.x) || (a.y != b.y) ) return true;
00244   return false;
00245 }
00246                                            /* -- test if equal ------ */
00247 /* template< class T >
00248 inline bool operator== ( const point2<T> a, const point2<T> b )
00249 {
00250   if( (a.x == b.x) && (a.y == b.y) ) return true;
00251   return false;
00252 } */
00253 
00254                                         /* -- set all components ------ */
00255 template< class T >
00256 inline void set( point2<T>& c, T a )
00257 {
00258   c.x = a; c.y = a;
00259 }
00260                          /* --- calculate the sum a + b  of vectors a,b --- */
00261 template <class T>
00262 inline point2<T> operator+ ( const point2<T>& a, const point2<T>& b )
00263 {
00264   return point2<T>(a.x+b.x, a.y+b.y);
00265 }
00266                                      /* --- invert the sign of a vector --- */
00267 template <class T>
00268 inline point2<T> operator- ( const point2<T>& a )
00269 {
00270   return point2<T>(-a.x, -a.y);
00271 }
00272                   /* --- calculate the difference a - b  of vectors a,b --- */
00273 template <class T>
00274 inline point2<T> operator- ( const point2<T>& a, const point2<T>& b )
00275 {
00276   return point2<T>(a.x-b.x, a.y-b.y);
00277 }
00278                                /* ---- multiply a scalar with a vector ---- */
00279 template <class T>
00280 inline point2<T> operator* ( const T op, const point2<T>& a  )
00281 {
00282   return point2<T>(op*a.x, op*a.y);
00283 }
00284                        /* --- calculate the dot product of two vectors ---- */
00285 template <class T>
00286 inline T operator* ( const point2<T>& a, const point2<T>& b )
00287 {
00288   return a.x*b.x + a.y*b.y;
00289 }
00290             /*----- project a homogeneous point into euclidian space ---------*/
00291 template <class T>
00292 inline point2<T> euclid( const point2<T>& a )
00293 {
00294   return a;
00295 }
00296 template <class T>
00297 inline T weight( const point2<T>& a )
00298 {
00299   return (T)1;
00300 }
00301 
00302 
00303 /*************************************************************************
00304   1-D points
00305 *************************************************************************/
00306                                             /* -- set all components ------ */
00307 template< class T >
00308 inline void set( point1<T>& c, T a )
00309 {
00310   c.x = a;
00311 }
00312                          /* --- calculate the sum a + b  of vectors a,b --- */
00313 template <class T>
00314 inline point1<T> operator+ ( const point1<T>& a, const point1<T>& b )
00315 {
00316   return point1<T>(a.x+b.x);
00317 }
00318                                      /* --- invert the sign of a vector --- */
00319 template <class T>
00320 inline point1<T> operator- ( const point1<T>& a )
00321 {
00322   return point1<T>(-a.x);
00323 }
00324                   /* --- calculate the difference a - b  of vectors a,b --- */
00325 template <class T>
00326 inline point1<T> operator- ( const point1<T>& a, const point1<T>& b )
00327 {
00328   return point1<T>(a.x-b.x);
00329 }
00330                                /* ---- multiply a scalar with a vector ---- */
00331 template <class T>
00332 inline point1<T> operator* ( const T op, const point1<T>& a  )
00333 {
00334   return point1<T>(op*a.x);
00335 }
00336                        /* --- calculate the dot product of two vectors ---- */
00337 template <class T>
00338 inline T operator* ( const point1<T> a, const point1<T>& b )
00339 {
00340   return a.x*b.x;
00341 }
00342             /*----- project a homogeneous point into euclidian space ---------*/
00343 template <class T>
00344 inline point1<T> euclid( const point1<T>& a )
00345 {
00346   return a;
00347 }
00348 template <class T>
00349 inline T weight( const point1<T>& a )
00350 {
00351   return (T)1;
00352 }
00353 
00354 /***************************************************************************
00355  special functions
00356 ***************************************************************************/
00357 
00358 template< class T >
00359 inline point<T> ortho( const hpoint<T>& a )
00360 {
00361   return point<T>(a.x, a.y, a.z);
00362 } 
00363 template< class T >
00364 inline point2<T> ortho( const hpoint2<T>& a )
00365 {
00366   return point2<T>(a.x, a.y);
00367 } 
00368 template< class T >
00369 inline point1<T> ortho( const hpoint1<T>& a )
00370 {
00371   return point1<T>(a.x);
00372 } 
00373 
00374 template< class T >
00375 inline point<T> ortho( const point<T>& a )
00376 {
00377   return point<T>(a.x, a.y, a.z);
00378 } 
00379 template< class T >
00380 inline point2<T> ortho( const point2<T>& a )
00381 {
00382   return point2<T>(a.x, a.y);
00383 } 
00384 template< class T >
00385 inline point1<T> ortho( const point1<T>& a )
00386 {
00387   return point1<T>(a.x);
00388 } 
00389 
00390                      /* --- calculate the cross product of two vectors ---- */
00391 template<class T> 
00392 inline point<T> cross_product( const point<T>& a, const point<T>& b )
00393 {
00394   return point<T>(a.y*b.z-b.y*a.z,
00395                   b.x*a.z-a.x*b.z,
00396                   a.x*b.y-b.x*a.y);
00397 }
00398 template<class T> 
00399 inline point<T> cross_product( const point2<T>& a, const point2<T>& b )
00400 {
00401   return point<T>((T)0,
00402                   (T)0,
00403                   a.x*b.y-b.x*a.y);
00404 }
00405                 /*--- calculate the length (euclidian norm) of a vector ---  */ 
00406 template< class T >
00407 inline T length( const point<T>& a )
00408 {
00409   return rtr<T>::sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
00410 }
00411 
00412                                 /* normalize a vector (so that length = 1) */ 
00413 template< class T >
00414 inline T normalize( point<T> *c, const point<T>& a ) 
00415 {
00416   T d;
00417   
00418   d = rtr<T>::sqrt( a.x*a.x + a.y*a.y + a.z*a.z );
00419   if( d != (T)0 )    
00420   {
00421     c->x = a.x / d;
00422     c->y = a.y / d;
00423     c->z = a.z / d;
00424   }
00425   return d;
00426 }    
00427 template< class T >
00428 inline T normalize( point2<T> *c, const point2<T>& a ) 
00429 {
00430   T d;
00431   
00432   d = rtr<T>::sqrt( a.x*a.x + a.y*a.y );
00433   if( d != (T)0 )    
00434   {
00435     c->x = a.x / d;
00436     c->y = a.y / d;
00437   }
00438   return d;
00439 }    
00440 
00441                                    /* calculate the distance of two points */   
00442 template< class T >
00443 inline T distance( const point<T>& a, const point<T>& b )
00444 {
00445   T x = a.x-b.x, y = a.y-b.y, z = a.z-b.z, d;
00446   
00447   d = rtr<T>::sqrt( x*x + y*y + z*z );
00448   return d;
00449 }
00450 template< class T >
00451 inline T distance( const point2<T>& a, const point2<T>& b )
00452 {
00453   T x = a.x-b.x, y = a.y-b.y, d;
00454   
00455   d = rtr<T>::sqrt( x*x + y*y );
00456   return d;
00457 }
00458 template< class T >
00459 inline T distance( const point1<T>& a, const point1<T>& b )
00460 {
00461   T x = a.x-b.x, d;
00462   
00463   d = rtr<T>::fabs( x );
00464   return d;
00465 }
00466 
00467 template< class T >
00468 inline T euclidian_distance( const hpoint<T>& a, const hpoint<T>& b )
00469 {
00470   point<T> ea = euclid(a);
00471   point<T> eb = euclid(b);
00472 
00473   return distance(ea,eb);
00474 }
00475 
00476 template< class T >
00477 inline T euclidian_distance( const hpoint2<T>& a, const hpoint2<T>& b )
00478 {
00479   point2<T> ea = euclid(a);
00480   point2<T> eb = euclid(b);
00481 
00482   return distance(ea,eb);
00483 }
00484 
00485 template< class T >
00486 inline T euclidian_distance( const hpoint1<T>& a, const hpoint1<T>& b )
00487 {
00488   point1<T> ea = euclid(a);
00489   point1<T> eb = euclid(b);
00490 
00491   return distance(ea,eb);
00492 }
00493 
00494 // this is a bit ridiculous, but i need the 'euclidian_distance' for 
00495 // homogeneous & normal points
00496 
00497 template< class T >
00498 inline T euclidian_distance( const point<T>& a, const point<T>& b )
00499 {
00500   return distance(a,b);
00501 }
00502 
00503 template< class T >
00504 inline T euclidian_distance( const point2<T>& a, const point2<T>& b )
00505 {
00506   return distance(a,b);
00507 }
00508 
00509 template< class T >
00510 inline T euclidian_distance( const point1<T>& a, const point1<T>& b )
00511 {
00512   return distance(a,b);
00513 }
00514 
00515 /*----------------------------------------------------------------------*//**
00516   Project a point onto a line (in 3-D).
00517   S = a point on the line
00518   T = direction vector of the line ( |T| = 1 !!!! )                        */
00519 /*-------------------------------------------------------------------------*/
00520 template< class HP >
00521 inline HP ProjectToLine( const HP& S, const HP& T, const HP& P )
00522 {
00523   return S + ((P-S)*T)*T;
00524 }                           
00525 
00526 }    /* namespace "gul" */
00527 
00528 #endif

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