LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiPoint.h

00001 /*
00002  * Copyright (C) 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiPoint.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 01.11.2002
00030  * revisions ..: $Id: ltiPoint.h,v 1.13 2006/12/06 22:23:10 alvarado Exp $
00031  */
00032 
00033 #ifndef LTI_POINT_H
00034 #define LTI_POINT_H
00035 
00036 #include <iostream>
00037 #include "ltiIoHandler.h"
00038 #include "ltiAssert.h"
00039 #include "ltiConfig.h"
00040 #include "ltiMath.h" // for sqrt
00041 
00042 namespace lti {
00043 
00044   /**
00045    * Two dimensional point, containing the coordinates x, y.
00046    *
00047    * The template type T will be the one used for each coordinate.  For
00048    * example tpoint<float> uses float for \a x and \a y.
00049    * 
00050    * This data structure simplifies the manipulation of 2D points providing
00051    * simple interfaces for adding, substracting, distance (L2), and more.
00052    *
00053    * @ingroup gGeomData
00054    */
00055   template <class T>
00056   class tpoint {
00057     public:
00058     /**
00059      * Used for the template-based interface for pixels as vectors.
00060      */
00061     typedef T value_type;
00062 
00063     /**
00064      * Return type of the size() member
00065      */
00066     typedef int size_type;
00067 
00068     /**
00069      * Coordinate x
00070      */
00071     T x;
00072 
00073     /**
00074      * Coordinate y;
00075      */
00076     T y;
00077 
00078     /**
00079      * Default constructor
00080      */
00081     explicit tpoint(const T newx=0,const T newy=0) : x(newx),y(newy) {};
00082 
00083     /**
00084      * Copy constructor
00085      */
00086     template <class U>
00087     tpoint(const tpoint<U>& p)
00088       : x(static_cast<T>(p.x)),y(static_cast<T>(p.y)) {};
00089 
00090     /**
00091      * Copy constructor
00092      */
00093     template <class U>
00094     tpoint<T>& castFrom(const tpoint<U>& p) {
00095       x = static_cast<T>(p.x);
00096       y = static_cast<T>(p.y);
00097       return (*this);
00098     };
00099 
00100     /**
00101      * Set the coordinate values and return a reference to this point
00102      */
00103     inline tpoint<T>& set(const T tx,const T ty) {
00104       x = tx;
00105       y = ty;
00106       return *this;
00107     }
00108 
00109     /**
00110      * Calculate distance to the point c
00111      */
00112     inline T distanceTo(const tpoint<T>& c) const;
00113 
00114     /**
00115      * Calculate %square of distance to the point c. This
00116      * method is faster than distanceTo (because it does not compute
00117      * the square root of a*a + b*b).
00118      */
00119     inline T distanceSqr(const tpoint<T>& c) const;
00120 
00121     /**
00122      * Return the square of the magnitude of the point
00123      */
00124     inline T absSqr() const;
00125 
00126     /**
00127      * Multiply tpoint<T> with a given factor
00128      */
00129     template<class U>
00130     inline tpoint<T>& multiply(const U c) {
00131       x = static_cast<T>(x*c);
00132       y = static_cast<T>(y*c);
00133       return *this;
00134     };
00135 
00136     /**
00137      * Multiply the other point tpoint<T> with a given factor
00138      */
00139     template<class U>
00140     inline tpoint<T>& multiply(const tpoint<T>& other,const U c) {
00141       x = static_cast<T>(other.x*c);
00142       y = static_cast<T>(other.y*c);
00143       return *this;
00144     };
00145 
00146 
00147     /**
00148      * Multiply tpoint<T> with a given factor
00149      */
00150     template<class U>
00151     inline tpoint<T> operator*(const U c) const {
00152       return tpoint<T>(static_cast<T>(x*c),static_cast<T>(y*c));
00153     };
00154 
00155     /**
00156      * Multiply tpoint<T> with a given factor
00157      */
00158     template<class U>
00159     inline tpoint<T>& operator*=(const U c) {
00160       x = static_cast<T>(x*c);
00161       y = static_cast<T>(y*c);
00162       return *this;
00163     };
00164 
00165     /**
00166      * Multiplies elementwise the components of this and the point c
00167      */
00168     inline tpoint<T> operator*(const tpoint<T>& c) const;
00169 
00170     /**
00171      * Multiplies elementwise the components of this and the point c
00172      */
00173     inline tpoint<T>& operator*=(const tpoint<T>& c);
00174 
00175     /**
00176      * Multiplies elementwise the components of \a a and \a b and leave the
00177      * result here.
00178      */
00179     inline tpoint<T>& emultiply(const tpoint<T>& a,const tpoint<T>& b);
00180 
00181     /**
00182      * Multiplies elementwise the components of this and the point c, and
00183      * leave the result here.
00184      */
00185     inline tpoint<T>& emultiply(const tpoint<T>& c);
00186 
00187     /**
00188      * Divide each component of tpoint<T> with a given factor
00189      */
00190     template<class U>
00191     inline tpoint<T>& divide(const U c) {
00192       x = static_cast<T>(x/c);
00193       y = static_cast<T>(y/c);
00194       return *this;
00195     };
00196 
00197     /**
00198      * Divide each component of other other tpoint<T> with a given factor
00199      */
00200     template<class U>
00201     inline tpoint<T>& divide(const tpoint<T>& other,const U c) {
00202       x = static_cast<T>(other.x/c);
00203       y = static_cast<T>(other.y/c);
00204       return *this;
00205     };
00206 
00207     /**
00208      * Divide each component of tpoint<T> by a given factor
00209      */
00210     template <class U>
00211     inline tpoint<T> operator/(const U c) const {
00212       return tpoint<T>(static_cast<T>(x/c),static_cast<T>(y/c));
00213     };
00214 
00215     /**
00216      * Divide each component of tpoint<T> by a given factor
00217      */
00218     template <class U>
00219     inline tpoint<T>& operator/=(const U c) {
00220       x = static_cast<T>(x/c);
00221       y = static_cast<T>(y/c);
00222       return *this;
00223     };
00224 
00225     /**
00226      * Elementwise division of each component of the points
00227      */
00228     inline tpoint<T> operator/(const tpoint<T>& c) const;
00229 
00230     /**
00231      * Elementwise division of each component of the points
00232      */
00233     inline tpoint<T>& operator/=(const tpoint<T>& c);
00234 
00235     /**
00236      * Elementwise division of each component of the points
00237      */
00238     inline tpoint<T>& edivide(const tpoint<T>& c);
00239 
00240     /**
00241      * Elementwise division of each component of the points
00242      */
00243     inline tpoint<T>& edivide(const tpoint<T>& a,const tpoint<T>& b);
00244 
00245     /**
00246      * Modulo c of the integer part of each component of the point
00247      */
00248     inline tpoint<T> operator%(const int c) const;
00249 
00250     /**
00251      * Add given point to this point and leave the result here.
00252      * @param p the other point to be added to this one
00253      * @return a reference to this point
00254      */
00255     inline tpoint<T>& add(const tpoint<T>& p);
00256 
00257     /**
00258      * Add the two other points and leave the result here
00259      * @param a first point to be added
00260      * @param b second point to be added
00261      * @return a reference to this point, which will contain a+b
00262      */
00263     inline tpoint<T>& add(const tpoint<T>& a,
00264                           const tpoint<T>& b);
00265     /**
00266      * Operator + is simmilar to add, but a new point is returned, i.e.
00267      * this point will not change.
00268      */
00269     inline tpoint<T> operator+(const tpoint<T>& p) const;
00270 
00271     /**
00272      * Operator += is an alias for add()
00273      */
00274     inline tpoint<T>& operator+=(const tpoint<T>& p);
00275 
00276     /**
00277      * Subtract
00278      */
00279     inline tpoint<T>& subtract(const tpoint<T>& p);
00280 
00281 
00282     /**
00283      * Subtract the two other points and leave the result here
00284      * @param a first point
00285      * @param b point to be subtracted from the first one
00286      * @return a reference to this point, which will contain a-b
00287      */
00288     inline tpoint<T>& subtract(const tpoint<T>& a,
00289                                const tpoint<T>& b);
00290 
00291     /**
00292      * Operator -
00293      */
00294     inline tpoint<T> operator-(const tpoint<T>& p) const;
00295 
00296     /**
00297      * Operator -=
00298      */
00299     inline tpoint<T>& operator-=(const tpoint<T>& p);
00300 
00301     /**
00302      * Dot product with another point.
00303      *
00304      * @return this->x*p.x + this->y*p.y.
00305      */
00306     inline T dot(const tpoint<T>& p) const;
00307 
00308     /**
00309      * Copy operator
00310      */
00311     inline tpoint<T>& copy(const tpoint<T>& p);
00312 
00313     /**
00314      * Operator =
00315      */
00316     inline tpoint<T>& operator=(const tpoint<T>& p) {return copy(p);};
00317 
00318     /**
00319      * Operator ==
00320      */
00321     inline bool operator==(const tpoint<T>& p) const;
00322 
00323     /**
00324      * Operator !=
00325      */
00326     inline bool operator!=(const tpoint<T>& p) const;
00327 
00328     /**
00329      * Operator <
00330      * a point is "smaller" than another one if its coordinates produce
00331      * an earlier display of the point in the monitor. i.e. if it
00332      * has a smaller y component or (if the y components are the same) if
00333      * the x component is smaller
00334      */
00335     inline bool operator<(const tpoint<T>& p) const;
00336 
00337     /**
00338      * Operator >
00339      * a point is "bigger" than another one if its coordinates produce
00340      * an later display of the point in the monitor. i.e. if it
00341      * has a bigger y component or (if the y components are the same) if
00342      * the x component is bigger
00343      */
00344     inline bool operator>(const tpoint<T>& p) const;
00345 
00346     /**
00347      * @name Access as vector
00348      */
00349     //@{
00350     /**
00351      * Used to simulate vector access.  It is slower than the normal
00352      * access to the elements x and y, but allow the use of point in templates
00353      * expecting a vector-like structure.
00354      *
00355      * The correspondence between the elements of the vector and
00356      * the color components will be [0] for x and [1] for y
00357      */
00358     inline T& operator[](const int i) {
00359       assert(i<2);
00360       return (i>0) ? y : x;
00361     }
00362 
00363     /**
00364      * Used to simulate read-only vector access.  It is slower than the normal
00365      * access to the elements x and y, but allow the use of point in templates
00366      * expecting a vector-like structure.
00367      *
00368      * The correspondence between the elements of the vector and
00369      * the color components will be [0] for x and [1] for y
00370      */
00371     inline const T& operator[](const int i) const {
00372       assert(i<2);
00373       return (i>0) ? y : x;
00374     }
00375 
00376     /**
00377      * Used to simulate the vector size
00378      */
00379     inline int size() const {
00380       return 2;
00381     }
00382     //@}
00383   };
00384 
00385   // implementation: not in Doc++!!!
00386   template <class T>
00387   inline T tpoint<T>::distanceTo(const tpoint<T>& c) const {
00388     tpoint<T> t((*this)-c);
00389     return sqrt(t.absSqr());
00390   }
00391 
00392   template <class T>
00393   inline T tpoint<T>::distanceSqr(const tpoint<T>& c) const {
00394     tpoint<T> t((*this)-c);
00395     return t.absSqr();
00396   }
00397 
00398   template <class T>
00399   inline tpoint<T> tpoint<T>::operator*(const tpoint<T>& c) const {
00400     return tpoint<T>(c.x*x,c.y*y);
00401   }
00402 
00403 
00404   template <class T>
00405   inline tpoint<T>& tpoint<T>::operator*=(const tpoint<T>& c) {
00406     x*=c.x;
00407     y*=c.y;
00408     return *this;
00409   }
00410 
00411   template <class T>
00412   inline tpoint<T>& tpoint<T>::emultiply(const tpoint<T>& c) {
00413     x*=c.x;
00414     y*=c.y;
00415     return *this;
00416   }
00417 
00418   template <class T>
00419   inline tpoint<T>& tpoint<T>::emultiply(const tpoint<T>& a,
00420                                          const tpoint<T>& b) {
00421     x=a.x*b.x;
00422     y=a.y*b.y;
00423     return *this;
00424   }
00425 
00426 
00427   template <class T>
00428   inline tpoint<T> tpoint<T>::operator/(const tpoint<T>& c) const {
00429     return tpoint<T>(x/c.x,y/c.y);
00430   }
00431 
00432   template <class T>
00433   inline tpoint<T>& tpoint<T>::operator/=(const tpoint<T>& c) {
00434     x/=c.x;
00435     y/=c.y;
00436     return *this;
00437   }
00438 
00439   template <class T>
00440   inline tpoint<T>& tpoint<T>::edivide(const tpoint<T>& c) {
00441     x/=c.x;
00442     y/=c.y;
00443     return *this;
00444   }
00445 
00446   template <class T>
00447   inline tpoint<T>& tpoint<T>::edivide(const tpoint<T>& a,
00448                                        const tpoint<T>& b) {
00449     x=a.x/b.x;
00450     y=a.y/b.y;
00451     return *this;
00452   }
00453 
00454 
00455   template <class T>
00456   inline tpoint<T> tpoint<T>::operator%(const int c) const {
00457     return tpoint<T>(static_cast<int>(x)%c,static_cast<int>(y)%c);
00458   }
00459 
00460   template <class T>
00461   inline tpoint<T>& tpoint<T>::add(const tpoint<T>& p) {
00462     x+=p.x;
00463     y+=p.y;
00464     return (*this);
00465   }
00466 
00467   template <class T>
00468   inline tpoint<T>& tpoint<T>::add(const tpoint<T>& a,
00469                                    const tpoint<T>& b) {
00470     x=a.x+b.x;
00471     y=a.y+b.y;
00472 
00473     return (*this);
00474   }
00475 
00476 
00477   template <class T>
00478   inline tpoint<T> tpoint<T>::operator+(const tpoint<T>& p) const {
00479     return tpoint<T>(x+p.x,y+p.y);
00480   }
00481 
00482   template <class T>
00483   inline tpoint<T>& tpoint<T>::operator+=(const tpoint<T>& p) {
00484     return add(p);
00485   }
00486 
00487   template <class T>
00488   inline tpoint<T>& tpoint<T>::subtract(const tpoint<T>& p) {
00489     x-=p.x;
00490     y-=p.y;
00491     return (*this);
00492   }
00493 
00494   template <class T>
00495   inline tpoint<T>& tpoint<T>::subtract(const tpoint<T>& a,
00496                                         const tpoint<T>& b) {
00497     x=a.x-b.x;
00498     y=a.y-b.y;
00499 
00500     return (*this);
00501   }
00502 
00503   template <class T>
00504   inline tpoint<T> tpoint<T>::operator-(const tpoint<T>& p) const {
00505     return tpoint<T>(x-p.x,y-p.y);
00506   }
00507 
00508   template <class T>
00509   inline tpoint<T>& tpoint<T>::operator-=(const tpoint<T>& p) {
00510     return subtract(p);
00511   }
00512 
00513   template <class T>
00514   inline T tpoint<T>::dot(const tpoint<T>& p) const {
00515     return static_cast<T>((x*p.x) + (y*p.y));
00516   }
00517 
00518   template <class T>
00519   inline T tpoint<T>::absSqr() const {
00520     return static_cast<T>((x*x) + (y*y));
00521   }
00522 
00523   template <class T>
00524   inline tpoint<T>& tpoint<T>::copy(const tpoint<T>& p) {
00525     x = p.x;
00526     y = p.y;
00527     return (*this);
00528   }
00529 
00530   template <class T>
00531   inline bool tpoint<T>::operator==(const tpoint<T>& p) const {
00532     return ((p.y == y) && (p.x == x));
00533   }
00534 
00535   template <class T>
00536   inline bool tpoint<T>::operator!=(const tpoint<T>& p) const {
00537     return ((p.y != y) || (p.x != x));
00538   }
00539 
00540   template <class T>
00541   inline bool tpoint<T>::operator<(const tpoint<T>& p) const {
00542     if (y < p.y) {
00543       return true;
00544     } else if (y == p.y) {
00545       return (x < p.x);
00546     }
00547     return false;
00548   }
00549 
00550   template <class T>
00551   inline bool tpoint<T>::operator>(const tpoint<T>& p) const {
00552     if (y > p.y) {
00553       return true;
00554     } else if (y == p.y) {
00555       return (x > p.x);
00556     }
00557     return false;
00558   }
00559 
00560   /**
00561    * A point with integer coordinates.
00562    *
00563    * \deprecated Please use ipoint instead.
00564    */
00565   typedef tpoint<int> point;
00566 
00567   /**
00568    * A point with integer coordinates
00569    */
00570   typedef tpoint<int> ipoint;
00571 
00572   /**
00573    * A point with unsigned integer coordinates
00574    */
00575   typedef tpoint<unsigned int> uipoint;
00576 
00577   /**
00578    * A point with double coordinates
00579    */
00580   typedef tpoint<double> dpoint;
00581 
00582   /**
00583    * A point with float coordinates
00584    */
00585   typedef tpoint<float> fpoint;
00586 
00587   /**
00588    * Read the vector from the given ioHandler.  The complete flag indicates
00589    * if the enclosing begin and end should be also be readed
00590    *
00591    * @ingroup gStorable
00592    */
00593   template <class T>
00594   bool read(ioHandler& handler,tpoint<T>& p,const bool complete=true) {
00595     bool b(true);
00596 
00597     if (complete) {
00598       b = b && handler.readBegin();
00599     }
00600 
00601     b = b && handler.read(p.x);
00602     b = b && handler.readDataSeparator();
00603     b = b && handler.read(p.y);
00604 
00605     if (complete) {
00606       b = b && handler.readEnd();
00607     }
00608 
00609     return b;
00610   };
00611 
00612   /**
00613    * Write the vector in the given ioHandler.  The complete flag indicates
00614    * if the enclosing begin and end should be also be written or not
00615    *
00616    * @ingroup gStorable
00617    */
00618   template<class T>
00619   bool write(ioHandler& handler,const tpoint<T>& p,const bool complete=true) {
00620     bool b(true);
00621 
00622     if (complete) {
00623       b = b && handler.writeBegin();
00624     }
00625 
00626     b = b && handler.write(p.x);
00627     b = b && handler.writeDataSeparator();
00628     b = b && handler.write(p.y);
00629 
00630     if (complete) {
00631       b = b && handler.writeEnd();
00632     }
00633 
00634     return b;
00635   };
00636 
00637   /**
00638    * Three dimensional point, containing the coordinates x, y and z.
00639    * The template type T will be the one used for each coordinate.
00640    * 
00641    * This data structure simplifies the manipulation of 3D points providing
00642    * simple interfaces for adding, substracting, distance (L2), and more.
00643    *
00644    * @ingroup gGeomData
00645    */
00646   template <class T>
00647   class tpoint3D {
00648     public:
00649 
00650     /**
00651      * Used for the template-based interface for pixels as vectors.
00652      */
00653     typedef T value_type;
00654 
00655     /**
00656      * Return type of the size() member
00657      */
00658     typedef int size_type;
00659 
00660     /**
00661      * Coordinate x
00662      */
00663     T x;
00664 
00665     /**
00666      * Coordinate y;
00667      */
00668     T y;
00669 
00670     /**
00671      * Coordinate z;
00672      */
00673     T z;
00674 
00675     /**
00676      * Default constructor
00677      */
00678     explicit tpoint3D(const T newx=0,
00679                       const T newy=0,
00680                       const T newz=0) : x(newx),y(newy),z(newz) {};
00681 
00682     /**
00683      * Copy constructor
00684      */
00685     template <class U>
00686     tpoint3D(const tpoint3D<U>& p)
00687       : x(static_cast<T>(p.x)),y(static_cast<T>(p.y)),z(static_cast<T>(p.z)) {
00688     };
00689 
00690     /**
00691      * Copy constructor
00692      */
00693     template <class U>
00694     tpoint3D<T>& castFrom(const tpoint3D<U>& p) {
00695       x = static_cast<T>(p.x);
00696       y = static_cast<T>(p.y);
00697       z = static_cast<T>(p.z);
00698       return (*this);
00699     };
00700 
00701     /**
00702      * Set the coordinate values and return a reference to this point
00703      */
00704     inline tpoint3D<T>& set(const T tx,const T ty,const T tz) {
00705       x = tx;
00706       y = ty;
00707       z = tz;
00708       return *this;
00709     }
00710 
00711     /**
00712      * Calculate distance to the point c
00713      */
00714     inline T distanceTo(const tpoint3D<T>& c) const;
00715 
00716     /**
00717      * Calculate %square of distance to the point c. This
00718      * method is faster than distanceTo (because it does not calculate
00719      * the root of a*a + b*b).
00720      */
00721     inline T distanceSqr(const tpoint3D<T>& c) const;
00722 
00723     /**
00724      * Return the square of the magnitude of this point
00725      */
00726     inline T absSqr() const;
00727 
00728     /**
00729      * Multiply tpoint3D<T> with a given factor
00730      */
00731     template <class U>
00732     inline tpoint3D<T>& multiply(const U c) {
00733       x = static_cast<T>(x*c);
00734       y = static_cast<T>(y*c);
00735       z = static_cast<T>(z*c);
00736       return (*this);
00737     };
00738 
00739     /**
00740      * Multiply tpoint3D<T> with a given factor
00741      */
00742     template <class U>
00743     inline tpoint3D<T>& multiply(const tpoint3D<T>& other,const U c) {
00744       x = static_cast<T>(other.x*c);
00745       y = static_cast<T>(other.y*c);
00746       z = static_cast<T>(other.z*c);
00747       return (*this);
00748     };
00749 
00750     /**
00751      * Multiply tpoint3D<T> with a given factor
00752      */
00753     template <class U>
00754     inline tpoint3D<T> operator*(const U c) const {
00755       return tpoint3D<T>(static_cast<T>(x*c),
00756                          static_cast<T>(y*c),
00757                          static_cast<T>(z*c));
00758     };
00759 
00760     /**
00761      * Multiply tpoint3D<T> with a given factor
00762      */
00763     template <class U>
00764     inline tpoint3D<T>& operator*=(const U c) {
00765       x = static_cast<T>(x*c);
00766       y = static_cast<T>(y*c);
00767       z = static_cast<T>(z*c);
00768       return (*this);
00769     };
00770 
00771     /**
00772      * Multiply element-wise tpoint3D<T> with another point
00773      */
00774     inline tpoint3D<T> operator*(const tpoint3D<T>& c) const;
00775 
00776     /**
00777      * Multiply element-wise tpoint3D<T> with another point c
00778      */
00779     inline tpoint3D<T>& operator*=(const tpoint3D<T>& c);
00780 
00781     /**
00782      * Multiplies elementwise the components of \a a and \a b and leave the
00783      * result here.
00784      */
00785     inline tpoint3D<T>& emultiply(const tpoint3D<T>& a,const tpoint3D<T>& b);
00786 
00787     /**
00788      * Multiplies elementwise the components of this and the point c, and
00789      * leave the result here.
00790      */
00791     inline tpoint3D<T>& emultiply(const tpoint3D<T>& c);
00792 
00793     /**
00794      * Divide each component of tpoint3D<T> with a given factor
00795      */
00796     template<class U>
00797     inline tpoint3D<T>& divide(const U c) {
00798       x = static_cast<T>(x/c);
00799       y = static_cast<T>(y/c);
00800       z = static_cast<T>(z/c);
00801       return (*this);
00802     };
00803 
00804     /**
00805      * Divide each component of tpoint3D<T> with a given factor
00806      */
00807     template<class U>
00808     inline tpoint3D<T>& divide(const tpoint3D<T>& other,const U c) {
00809       x = static_cast<T>(other.x/c);
00810       y = static_cast<T>(other.y/c);
00811       z = static_cast<T>(other.z/c);
00812       return (*this);
00813     };
00814 
00815     /**
00816      * Divide each component of tpoint3D<T> with a given factor
00817      */
00818     template <class U>
00819     inline tpoint3D<T> operator/(const U c) const {
00820       return tpoint3D<T>(static_cast<T>(x/c),
00821                          static_cast<T>(y/c),
00822                          static_cast<T>(z/c));
00823     };
00824 
00825     /**
00826      * Divide each component of tpoint3D<T> with a given factor
00827      */
00828     template <class U>
00829     inline tpoint3D<T>& operator/=(const U c) {
00830       x = static_cast<T>(x/c);
00831       y = static_cast<T>(y/c);
00832       z = static_cast<T>(z/c);
00833       return (*this);
00834     };
00835 
00836 
00837     /**
00838      * Elementwise division of each component of the points
00839      */
00840     inline tpoint3D<T> operator/(const tpoint3D<T>& c) const;
00841 
00842     /**
00843      * Element-wise division
00844      */
00845     inline tpoint3D<T>& operator/=(const tpoint3D<T>& c);
00846 
00847     /**
00848      * Elementwise division of each component of the points
00849      */
00850     inline tpoint3D<T>& edivide(const tpoint3D<T>& c);
00851 
00852     /**
00853      * Elementwise division of each component of the points
00854      */
00855     inline tpoint3D<T>& edivide(const tpoint3D<T>& a,const tpoint3D<T>& b);
00856 
00857     /**
00858      * Modulo c of the integer part of each component of the point3D
00859      */
00860     inline tpoint3D<T> operator%(const int c) const;
00861 
00862     /**
00863      * Add the content of this point with the other point \a p and leave 
00864      * the result in this point.
00865      */
00866     inline tpoint3D<T>& add(const tpoint3D<T>& p);
00867 
00868     /**
00869      * Add the points \a p1 and \a p2 and leave the result in this point.
00870      */
00871     inline tpoint3D<T>& add(const tpoint3D<T>& p1,
00872                             const tpoint3D<T>& p2);
00873 
00874     /**
00875      * Operator +
00876      */
00877     inline tpoint3D<T> operator+(const tpoint3D<T>& p) const;
00878 
00879     /**
00880      * Operator +
00881      */
00882     inline tpoint3D<T>& operator+=(const tpoint3D<T>& p);
00883 
00884     /**
00885      * Subtract
00886      */
00887     inline tpoint3D<T>& subtract(const tpoint3D<T>& p);
00888 
00889     /**
00890      * Subtract
00891      */
00892     inline tpoint3D<T>& subtract(const tpoint3D<T>& p1,
00893                                  const tpoint3D<T>& p2);
00894 
00895     /**
00896      * Operator -
00897      */
00898     inline tpoint3D<T> operator-(const tpoint3D<T>& p) const;
00899 
00900     /**
00901      * Operator -
00902      */
00903     inline tpoint3D<T> operator-=(const tpoint3D<T>& p);
00904 
00905     /**
00906      * Dot product with another point
00907      */
00908     inline T dot(const tpoint3D<T>& p) const;
00909 
00910     /**
00911      * Copy operator
00912      */
00913     inline tpoint3D<T>& copy(const tpoint3D<T>& p);
00914 
00915     /**
00916      * Operator =
00917      */
00918     inline tpoint3D<T>& operator=(const tpoint3D<T>& p) {return copy(p);};
00919 
00920     /**
00921      * Operator ==
00922      */
00923     inline bool operator==(const tpoint3D<T>& p) const;
00924 
00925     /**
00926      * Operator !=
00927      */
00928     inline bool operator!=(const tpoint3D<T>& p) const;
00929 
00930     /**
00931      * Operator<
00932      *
00933      * A point3D is smaller than another one if its z component is
00934      * smaller, or if both z components are equal, if its y component is
00935      * smaller, of if both y are equal, if its x component is smaller
00936      */ 
00937     inline bool operator<(const tpoint3D<T>& p) const;
00938 
00939     /**
00940      * Operator>
00941      *
00942      * A point3D is smaller than another one if its z component is
00943      * smaller, or if both z components are equal, if its y component is
00944      * smaller, of if both y are equal, if its x component is smaller
00945      */ 
00946     inline bool operator>(const tpoint3D<T>& p) const;
00947 
00948     /**
00949      * @name access as vector
00950      */
00951     //@{
00952     /**
00953      * Used to simulate vector access.  It is slower than the normal
00954      * access to the elements x, y and z, but allow the use of point
00955      * in templates expecting a vector-like structure.
00956      *
00957      * The correspondence between the elements of the vector and
00958      * the color components will be [0] for x and [1] for y
00959      */
00960     inline T& operator[](const int i) {
00961       assert(i<3);
00962       switch (i) {
00963         case 0: return x;
00964         case 1: return y;
00965         case 2: return z;
00966         default: return x;
00967       }
00968       return x;
00969     }
00970 
00971     /**
00972      * Used to simulate read-only vector access.  It is slower than
00973      * the normal access to the elements x, y and z, but allow the use
00974      * of point in templates expecting a vector-like structure.
00975      *
00976      * The correspondence between the elements of the vector and
00977      * the color components will be [0] for x and [1] for y
00978      */
00979     inline const T& operator[](const int i) const {
00980       assert(i<3);
00981       switch (i) {
00982         case 0: return x;
00983         case 1: return y;
00984         case 2: return z;
00985         default: return x;
00986       }
00987       return x;
00988     }
00989 
00990     /**
00991      * Used to simulate the vector size
00992      */
00993     inline int size() const {
00994       return 3;
00995     }
00996     //@}
00997 
00998   };
00999 
01000   // implementation: not in Doxygen!!!
01001 
01002   template <class T>
01003   inline tpoint3D<T>& tpoint3D<T>::edivide(const tpoint3D<T>& c) {
01004     x/=c.x;
01005     y/=c.y;
01006     z/=c.z;
01007     return *this;
01008   }
01009 
01010   template <class T>
01011   inline tpoint3D<T>& tpoint3D<T>::edivide(const tpoint3D<T>& a,
01012                                            const tpoint3D<T>& b) {
01013     x=a.x/b.x;
01014     y=a.y/b.y;
01015     z=a.z/b.z;
01016     return *this;
01017   }
01018 
01019   template <class T>
01020   inline tpoint3D<T> tpoint3D<T>::operator/(const tpoint3D<T>& c) const {
01021     return tpoint3D<T>(x/c.x,y/c.y,z/c.z);
01022   }
01023 
01024   /**
01025    * Element-wise division
01026    */
01027   template <class T>
01028   inline tpoint3D<T>& tpoint3D<T>::operator/=(const tpoint3D<T>& c) {
01029     x = static_cast<T>(x/c.x);
01030     y = static_cast<T>(y/c.y);
01031     z = static_cast<T>(z/c.z);
01032     return (*this);
01033   };
01034 
01035   template <class T>
01036   inline tpoint3D<T>& tpoint3D<T>::emultiply(const tpoint3D<T>& c) {
01037     x*=c.x;
01038     y*=c.y;
01039     z*=c.z;
01040     return *this;
01041   }
01042 
01043   template <class T>
01044   inline tpoint3D<T>& tpoint3D<T>::emultiply(const tpoint3D<T>& a,
01045                                          const tpoint3D<T>& b) {
01046     x=a.x*b.x;
01047     y=a.y*b.y;
01048     z=a.z*b.z;
01049     return *this;
01050   }
01051 
01052 
01053   template <class T>
01054   inline tpoint3D<T> tpoint3D<T>::operator%(const int c) const {
01055     return tpoint3D<T>(static_cast<int>(x)%c,
01056                        static_cast<int>(y)%c,
01057                        static_cast<int>(z)%c);
01058   }
01059 
01060   template <class T>
01061   inline tpoint3D<T>& tpoint3D<T>::add(const tpoint3D<T>& p) {
01062     x+=p.x;
01063     y+=p.y;
01064     z+=p.z;
01065     return (*this);
01066   }
01067 
01068   template <class T>
01069   inline tpoint3D<T>& tpoint3D<T>::add(const tpoint3D<T>& p1,
01070                                        const tpoint3D<T>& p2) {
01071     x = p1.x + p2.x;
01072     y = p1.y + p2.y;
01073     z = p1.z + p2.z;
01074 
01075     return (*this);
01076   }
01077 
01078   template <class T>
01079   inline tpoint3D<T> tpoint3D<T>::operator+(const tpoint3D<T>& p) const {
01080     return tpoint3D<T>(x+p.x,y+p.y,z+p.z);
01081   }
01082 
01083   template <class T>
01084   inline tpoint3D<T>& tpoint3D<T>::operator+=(const tpoint3D<T>& p) {
01085     return add(p);
01086   }
01087 
01088   template <class T>
01089   inline tpoint3D<T>& tpoint3D<T>::subtract(const tpoint3D<T>& p) {
01090     x-=p.x;
01091     y-=p.y;
01092     z-=p.z;
01093     return (*this);
01094   }
01095 
01096   template <class T>
01097   inline tpoint3D<T>& tpoint3D<T>::subtract(const tpoint3D<T>& p1,
01098                                             const tpoint3D<T>& p2) {
01099     x = p1.x - p2.x;
01100     y = p1.y - p2.y;
01101     z = p1.z - p2.z;
01102 
01103     return (*this);
01104   }
01105 
01106   template <class T>
01107   inline tpoint3D<T> tpoint3D<T>::operator-(const tpoint3D<T>& p) const {
01108     return tpoint3D<T>(x-p.x,y-p.y,z-p.z);
01109   }
01110 
01111   template <class T>
01112   inline tpoint3D<T> tpoint3D<T>::operator-=(const tpoint3D<T>& p) {
01113     return subtract(p);
01114   }
01115 
01116   template <class T>
01117   inline tpoint3D<T> tpoint3D<T>::operator*(const tpoint3D<T>& c) const {
01118     return tpoint3D<T>(c.x*x,c.y*x,c.z*z);
01119   }
01120 
01121 
01122   template <class T>
01123   inline tpoint3D<T>& tpoint3D<T>::operator*=(const tpoint3D<T>& c) {
01124     x*=c.x;
01125     y*=c.y;
01126     z*=c.z;
01127     return *this;
01128   }
01129 
01130 
01131 
01132   template <class T>
01133   inline T tpoint3D<T>::dot(const tpoint3D<T>& p) const {
01134     return static_cast<T>((x*p.x) + (y*p.y) + (z*p.z));
01135   }
01136 
01137   template <class T>
01138   inline T tpoint3D<T>::absSqr() const {
01139     return static_cast<T>((x*x) + (y*y) + (z*z));
01140   }
01141 
01142   template <class T>
01143   inline T tpoint3D<T>::distanceTo(const tpoint3D<T>& c) const {
01144     tpoint3D<T> t((*this)-c);
01145     return sqrt(t.absSqr());
01146   }
01147 
01148   template <class T>
01149   inline T tpoint3D<T>::distanceSqr(const tpoint3D<T>& c) const {
01150     tpoint3D<T> t((*this)-c);
01151     return t.absSqr();
01152   }
01153 
01154   template <class T>
01155   inline tpoint3D<T>& tpoint3D<T>::copy(const tpoint3D<T>& p) {
01156     x = p.x;
01157     y = p.y;
01158     z = p.z;
01159     return (*this);
01160   }
01161 
01162   template <class T>
01163   inline bool tpoint3D<T>::operator==(const tpoint3D<T>& p) const {
01164     return ((p.y == y) && (p.x == x) && (p.z == z));
01165   }
01166 
01167   template <class T>
01168   inline bool tpoint3D<T>::operator!=(const tpoint3D<T>& p) const {
01169     return ((p.y != y) || (p.x != x) || (p.z != p.z));
01170   }
01171 
01172   template <class T>
01173   inline bool tpoint3D<T>::operator<(const tpoint3D<T>& p) const {
01174     return ((z < p.z) ||
01175             ((z==p.z) && ((y < p.y) ||
01176                           ((y == p.y) && (x < p.x)))));
01177   }
01178 
01179   template <class T>
01180   inline bool tpoint3D<T>::operator>(const tpoint3D<T>& p) const {
01181     return ((z > p.z) ||
01182             ((z==p.z) && ((y > p.y) ||
01183                           ((y == p.y) && (x > p.x)))));
01184   }
01185 
01186   /**
01187    * A three dimensional point with integer coordinates
01188    *
01189    * \deprecated Please use ipoint3D instead
01190    */
01191   typedef tpoint3D<int> point3D;
01192 
01193   /**
01194    * A three dimensional point with integer coordinates
01195    */
01196   typedef tpoint3D<int> ipoint3D;
01197 
01198   /**
01199    * A three dimensional point with integer coordinates
01200    */
01201   typedef tpoint3D<float> fpoint3D;
01202   /**
01203    * A three dimensional point with double coordinates
01204    */
01205   typedef tpoint3D<double> dpoint3D;
01206 
01207   /**
01208    * Read the vector from the given ioHandler.  The complete flag indicates
01209    * if the enclosing begin and end should be also be readed
01210    *
01211    * @ingroup gStorable
01212    */
01213   template <class T>
01214     bool read(ioHandler& handler,tpoint3D<T>& p,const bool complete=true) {
01215     bool b(true);
01216 
01217     if (complete) {
01218       b = b && handler.readBegin();
01219     }
01220 
01221     b = b && handler.read(p.x);
01222     b = b && handler.readDataSeparator();
01223     b = b && handler.read(p.y);
01224     b = b && handler.readDataSeparator();
01225     b = b && handler.read(p.z);
01226 
01227     if (complete) {
01228       b = b && handler.readEnd();
01229     }
01230 
01231     return b;
01232   };
01233 
01234   /**
01235    * Write the vector in the given ioHandler.  The complete flag indicates
01236    * if the enclosing begin and end should be also be written or not
01237    *
01238    * @ingroup gStorable
01239    */
01240   template<class T>
01241   bool write(ioHandler& handler,const tpoint3D<T>& p,const bool complete=true) {
01242     bool b(true);
01243 
01244     if (complete) {
01245       b = b && handler.writeBegin();
01246     }
01247     b = b && handler.write(p.x);
01248     b = b && handler.writeDataSeparator();
01249     b = b && handler.write(p.y);
01250     b = b && handler.writeDataSeparator();
01251     b = b && handler.write(p.z);
01252 
01253     if (complete) {
01254       b = b && handler.writeEnd();
01255     }
01256 
01257     return b;
01258   };
01259 }
01260 
01261 namespace std {
01262 
01263   //inline ostream& operator<<(ostream& s,const lti::point& p);
01264   template <class T>
01265     inline ostream& operator<<(ostream& s,const lti::tpoint<T>& p) {
01266     s << "(" << p.x << ","
01267       << p.y << ")";
01268     return s;
01269   };
01270 
01271   //inline ostream& operator>>(istream& s,const lti::point& p);
01272   template <class T>
01273     inline istream& operator>>(istream& s,lti::tpoint<T>& p) {
01274     char c;
01275     s >> c
01276       >> p.x >> c
01277       >> p.y >> c;
01278 
01279     return s;
01280   };
01281 
01282   //inline ostream& operator<<(ostream& s,const lti::point& p);
01283   template <class T>
01284   inline ostream& operator<<(ostream& s,const lti::tpoint3D<T>& p) {
01285     s << "("
01286       << p.x << ","
01287       << p.y << ","
01288       << p.z << ")";
01289     return s;
01290   };
01291 
01292   //inline ostream& operator>>(istream& s,const lti::point& p);
01293   template <class T>
01294   inline istream& operator>>(istream& s,lti::tpoint3D<T>& p) {
01295     char c;
01296     s >> c
01297       >> p.x >> c
01298       >> p.y >> c
01299       >> p.z >> c;
01300 
01301     return s;
01302   };
01303 }
01304 
01305 #endif

Generated on Sat Apr 10 15:25:58 2010 for LTI-Lib by Doxygen 1.6.1