LTI-Lib latest version v1.9 - last update 24 Nov 2005
Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ltiPoint.h

00001 /*
00002  * Copyright (C) 1998, 1999, 2000, 2001
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.17 2004/11/17 09:35:23 doerfler 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 double coordinates
00574    */
00575   typedef tpoint<double> dpoint;
00576 
00577   /**
00578    * A point with float coordinates
00579    */
00580   typedef tpoint<float> fpoint;
00581 
00582   /**
00583    * Read the vector from the given ioHandler.  The complete flag indicates
00584    * if the enclosing begin and end should be also be readed
00585    *
00586    * @ingroup gStorable
00587    */
00588   template <class T>
00589   bool read(ioHandler& handler,tpoint<T>& p,const bool complete=true) {
00590     bool b(true);
00591 
00592     if (complete) {
00593       b = b && handler.readBegin();
00594     }
00595 
00596     b = b && handler.read(p.x);
00597     b = b && handler.readDataSeparator();
00598     b = b && handler.read(p.y);
00599 
00600     if (complete) {
00601       b = b && handler.readEnd();
00602     }
00603 
00604     return b;
00605   };
00606 
00607   /**
00608    * Write the vector in the given ioHandler.  The complete flag indicates
00609    * if the enclosing begin and end should be also be written or not
00610    *
00611    * @ingroup gStorable
00612    */
00613   template<class T>
00614   bool write(ioHandler& handler,const tpoint<T>& p,const bool complete=true) {
00615     bool b(true);
00616 
00617     if (complete) {
00618       b = b && handler.writeBegin();
00619     }
00620 
00621     b = b && handler.write(p.x);
00622     b = b && handler.writeDataSeparator();
00623     b = b && handler.write(p.y);
00624 
00625     if (complete) {
00626       b = b && handler.writeEnd();
00627     }
00628 
00629     return b;
00630   };
00631 
00632   /**
00633    * Three dimensional point, containing the coordinates x, y and z.
00634    * The template type T will be the one used for each coordinate.
00635    * 
00636    * This data structure simplifies the manipulation of 3D points providing
00637    * simple interfaces for adding, substracting, distance (L2), and more.
00638    *
00639    * @ingroup gGeomData
00640    */
00641   template <class T>
00642   class tpoint3D {
00643     public:
00644 
00645     /**
00646      * Used for the template-based interface for pixels as vectors.
00647      */
00648     typedef T value_type;
00649 
00650     /**
00651      * Return type of the size() member
00652      */
00653     typedef int size_type;
00654 
00655     /**
00656      * Coordinate x
00657      */
00658     T x;
00659 
00660     /**
00661      * Coordinate y;
00662      */
00663     T y;
00664 
00665     /**
00666      * Coordinate z;
00667      */
00668     T z;
00669 
00670     /**
00671      * Default constructor
00672      */
00673     explicit tpoint3D(const T newx=0,
00674                       const T newy=0,
00675                       const T newz=0) : x(newx),y(newy),z(newz) {};
00676 
00677     /**
00678      * Copy constructor
00679      */
00680     template <class U>
00681     tpoint3D(const tpoint3D<U>& p)
00682       : x(static_cast<T>(p.x)),y(static_cast<T>(p.y)),z(static_cast<T>(p.z)) {
00683     };
00684 
00685     /**
00686      * Copy constructor
00687      */
00688     template <class U>
00689     tpoint3D<T>& castFrom(const tpoint3D<U>& p) {
00690       x = static_cast<T>(p.x);
00691       y = static_cast<T>(p.y);
00692       z = static_cast<T>(p.z);
00693       return (*this);
00694     };
00695 
00696     /**
00697      * Set the coordinate values and return a reference to this point
00698      */
00699     inline tpoint3D<T>& set(const T tx,const T ty,const T tz) {
00700       x = tx;
00701       y = ty;
00702       z = tz;
00703       return *this;
00704     }
00705 
00706     /**
00707      * Calculate distance to the point c
00708      */
00709     inline T distanceTo(const tpoint3D<T>& c) const;
00710 
00711     /**
00712      * Calculate %square of distance to the point c. This
00713      * method is faster than distanceTo (because it does not calculate
00714      * the root of a*a + b*b).
00715      */
00716     inline T distanceSqr(const tpoint3D<T>& c) const;
00717 
00718     /**
00719      * Return the square of the magnitude of this point
00720      */
00721     inline T absSqr() const;
00722 
00723     /**
00724      * Multiply tpoint3D<T> with a given factor
00725      */
00726     template <class U>
00727     inline tpoint3D<T>& multiply(const U c) {
00728       x = static_cast<T>(x*c);
00729       y = static_cast<T>(y*c);
00730       z = static_cast<T>(z*c);
00731       return (*this);
00732     };
00733 
00734     /**
00735      * Multiply tpoint3D<T> with a given factor
00736      */
00737     template <class U>
00738     inline tpoint3D<T>& multiply(const tpoint3D<T>& other,const U c) {
00739       x = static_cast<T>(other.x*c);
00740       y = static_cast<T>(other.y*c);
00741       z = static_cast<T>(other.z*c);
00742       return (*this);
00743     };
00744 
00745     /**
00746      * Multiply tpoint3D<T> with a given factor
00747      */
00748     template <class U>
00749     inline tpoint3D<T> operator*(const U c) const {
00750       return tpoint3D<T>(static_cast<T>(x*c),
00751                          static_cast<T>(y*c),
00752                          static_cast<T>(z*c));
00753     };
00754 
00755     /**
00756      * Multiply tpoint3D<T> with a given factor
00757      */
00758     template <class U>
00759     inline tpoint3D<T>& operator*=(const U c) {
00760       x = static_cast<T>(x*c);
00761       y = static_cast<T>(y*c);
00762       z = static_cast<T>(z*c);
00763       return (*this);
00764     };
00765 
00766     /**
00767      * Multiply element-wise tpoint3D<T> with another point
00768      */
00769     inline tpoint3D<T> operator*(const tpoint3D<T>& c) const;
00770 
00771     /**
00772      * Multiply element-wise tpoint3D<T> with another point c
00773      */
00774     inline tpoint3D<T>& operator*=(const tpoint3D<T>& c);
00775 
00776     /**
00777      * Multiplies elementwise the components of \a a and \a b and leave the
00778      * result here.
00779      */
00780     inline tpoint3D<T>& emultiply(const tpoint3D<T>& a,const tpoint3D<T>& b);
00781 
00782     /**
00783      * Multiplies elementwise the components of this and the point c, and
00784      * leave the result here.
00785      */
00786     inline tpoint3D<T>& emultiply(const tpoint3D<T>& c);
00787 
00788     /**
00789      * Divide each component of tpoint3D<T> with a given factor
00790      */
00791     template<class U>
00792     inline tpoint3D<T>& divide(const U c) {
00793       x = static_cast<T>(x/c);
00794       y = static_cast<T>(y/c);
00795       z = static_cast<T>(z/c);
00796       return (*this);
00797     };
00798 
00799     /**
00800      * Divide each component of tpoint3D<T> with a given factor
00801      */
00802     template<class U>
00803     inline tpoint3D<T>& divide(const tpoint3D<T>& other,const U c) {
00804       x = static_cast<T>(other.x/c);
00805       y = static_cast<T>(other.y/c);
00806       z = static_cast<T>(other.z/c);
00807       return (*this);
00808     };
00809 
00810     /**
00811      * Divide each component of tpoint3D<T> with a given factor
00812      */
00813     template <class U>
00814     inline tpoint3D<T> operator/(const U c) const {
00815       return tpoint3D<T>(static_cast<T>(x/c),
00816                          static_cast<T>(y/c),
00817                          static_cast<T>(z/c));
00818     };
00819 
00820     /**
00821      * Divide each component of tpoint3D<T> with a given factor
00822      */
00823     template <class U>
00824     inline tpoint3D<T>& operator/=(const U c) {
00825       x = static_cast<T>(x/c);
00826       y = static_cast<T>(y/c);
00827       z = static_cast<T>(z/c);
00828       return (*this);
00829     };
00830 
00831 
00832     /**
00833      * Elementwise division of each component of the points
00834      */
00835     inline tpoint3D<T> operator/(const tpoint3D<T>& c) const;
00836 
00837     /**
00838      * Element-wise division
00839      */
00840     inline tpoint3D<T>& operator/=(const tpoint3D<T>& c);
00841 
00842     /**
00843      * Elementwise division of each component of the points
00844      */
00845     inline tpoint3D<T>& edivide(const tpoint3D<T>& c);
00846 
00847     /**
00848      * Elementwise division of each component of the points
00849      */
00850     inline tpoint3D<T>& edivide(const tpoint3D<T>& a,const tpoint3D<T>& b);
00851 
00852     /**
00853      * Modulo c of the integer part of each component of the point3D
00854      */
00855     inline tpoint3D<T> operator%(const int c) const;
00856 
00857     /**
00858      * Add the content of this point with the other point \a p and leave 
00859      * the result in this point.
00860      */
00861     inline tpoint3D<T>& add(const tpoint3D<T>& p);
00862 
00863     /**
00864      * Add the points \a p1 and \a p2 and leave the result in this point.
00865      */
00866     inline tpoint3D<T>& add(const tpoint3D<T>& p1,
00867                             const tpoint3D<T>& p2);
00868 
00869     /**
00870      * Operator +
00871      */
00872     inline tpoint3D<T> operator+(const tpoint3D<T>& p) const;
00873 
00874     /**
00875      * Operator +
00876      */
00877     inline tpoint3D<T>& operator+=(const tpoint3D<T>& p);
00878 
00879     /**
00880      * Subtract
00881      */
00882     inline tpoint3D<T>& subtract(const tpoint3D<T>& p);
00883 
00884     /**
00885      * Subtract
00886      */
00887     inline tpoint3D<T>& subtract(const tpoint3D<T>& p1,
00888                                  const tpoint3D<T>& p2);
00889 
00890     /**
00891      * Operator -
00892      */
00893     inline tpoint3D<T> operator-(const tpoint3D<T>& p) const;
00894 
00895     /**
00896      * Operator -
00897      */
00898     inline tpoint3D<T> operator-=(const tpoint3D<T>& p);
00899 
00900     /**
00901      * Dot product with another point
00902      */
00903     inline T dot(const tpoint3D<T>& p) const;
00904 
00905     /**
00906      * Copy operator
00907      */
00908     inline tpoint3D<T>& copy(const tpoint3D<T>& p);
00909 
00910     /**
00911      * Operator =
00912      */
00913     inline tpoint3D<T>& operator=(const tpoint3D<T>& p) {return copy(p);};
00914 
00915     /**
00916      * Operator ==
00917      */
00918     inline bool operator==(const tpoint3D<T>& p) const;
00919 
00920     /**
00921      * Operator !=
00922      */
00923     inline bool operator!=(const tpoint3D<T>& p) const;
00924 
00925     /**
00926      * Operator<
00927      *
00928      * A point3D is smaller than another one if its z component is
00929      * smaller, or if both z components are equal, if its y component is
00930      * smaller, of if both y are equal, if its x component is smaller
00931      */ 
00932     inline bool operator<(const tpoint3D<T>& p) const;
00933 
00934     /**
00935      * Operator>
00936      *
00937      * A point3D is smaller than another one if its z component is
00938      * smaller, or if both z components are equal, if its y component is
00939      * smaller, of if both y are equal, if its x component is smaller
00940      */ 
00941     inline bool operator>(const tpoint3D<T>& p) const;
00942 
00943     /**
00944      * @name access as vector
00945      */
00946     //@{
00947     /**
00948      * Used to simulate vector access.  It is slower than the normal
00949      * access to the elements x, y and z, but allow the use of point
00950      * in templates expecting a vector-like structure.
00951      *
00952      * The correspondence between the elements of the vector and
00953      * the color components will be [0] for x and [1] for y
00954      */
00955     inline T& operator[](const int i) {
00956       assert(i<3);
00957       switch (i) {
00958         case 0: return x;
00959         case 1: return y;
00960         case 2: return z;
00961         default: return x;
00962       }
00963       return x;
00964     }
00965 
00966     /**
00967      * Used to simulate read-only vector access.  It is slower than
00968      * the normal access to the elements x, y and z, but allow the use
00969      * of point in templates expecting a vector-like structure.
00970      *
00971      * The correspondence between the elements of the vector and
00972      * the color components will be [0] for x and [1] for y
00973      */
00974     inline const T& operator[](const int i) const {
00975       assert(i<3);
00976       switch (i) {
00977         case 0: return x;
00978         case 1: return y;
00979         case 2: return z;
00980         default: return x;
00981       }
00982       return x;
00983     }
00984 
00985     /**
00986      * Used to simulate the vector size
00987      */
00988     inline int size() const {
00989       return 3;
00990     }
00991     //@}
00992 
00993   };
00994 
00995   // implementation: not in Doxygen!!!
00996 
00997   template <class T>
00998   inline tpoint3D<T>& tpoint3D<T>::edivide(const tpoint3D<T>& c) {
00999     x/=c.x;
01000     y/=c.y;
01001     z/=c.z;
01002     return *this;
01003   }
01004 
01005   template <class T>
01006   inline tpoint3D<T>& tpoint3D<T>::edivide(const tpoint3D<T>& a,
01007                                            const tpoint3D<T>& b) {
01008     x=a.x/b.x;
01009     y=a.y/b.y;
01010     z=a.z/b.z;
01011     return *this;
01012   }
01013 
01014   template <class T>
01015   inline tpoint3D<T> tpoint3D<T>::operator/(const tpoint3D<T>& c) const {
01016     return tpoint3D<T>(x/c.x,y/c.y,z/c.z);
01017   }
01018 
01019   /**
01020    * Element-wise division
01021    */
01022   template <class T>
01023   inline tpoint3D<T>& tpoint3D<T>::operator/=(const tpoint3D<T>& c) {
01024     x = static_cast<T>(x/c.x);
01025     y = static_cast<T>(y/c.y);
01026     z = static_cast<T>(z/c.z);
01027     return (*this);
01028   };
01029 
01030   template <class T>
01031   inline tpoint3D<T>& tpoint3D<T>::emultiply(const tpoint3D<T>& c) {
01032     x*=c.x;
01033     y*=c.y;
01034     z*=c.z;
01035     return *this;
01036   }
01037 
01038   template <class T>
01039   inline tpoint3D<T>& tpoint3D<T>::emultiply(const tpoint3D<T>& a,
01040                                          const tpoint3D<T>& b) {
01041     x=a.x*b.x;
01042     y=a.y*b.y;
01043     z=a.z*b.z;
01044     return *this;
01045   }
01046 
01047 
01048   template <class T>
01049   inline tpoint3D<T> tpoint3D<T>::operator%(const int c) const {
01050     return tpoint3D<T>(static_cast<int>(x)%c,
01051                        static_cast<int>(y)%c,
01052                        static_cast<int>(z)%c);
01053   }
01054 
01055   template <class T>
01056   inline tpoint3D<T>& tpoint3D<T>::add(const tpoint3D<T>& p) {
01057     x+=p.x;
01058     y+=p.y;
01059     z+=p.z;
01060     return (*this);
01061   }
01062 
01063   template <class T>
01064   inline tpoint3D<T>& tpoint3D<T>::add(const tpoint3D<T>& p1,
01065                                        const tpoint3D<T>& p2) {
01066     x = p1.x + p2.x;
01067     y = p1.y + p2.y;
01068     z = p1.z + p2.z;
01069 
01070     return (*this);
01071   }
01072 
01073   template <class T>
01074   inline tpoint3D<T> tpoint3D<T>::operator+(const tpoint3D<T>& p) const {
01075     return tpoint3D<T>(x+p.x,y+p.y,z+p.z);
01076   }
01077 
01078   template <class T>
01079   inline tpoint3D<T>& tpoint3D<T>::operator+=(const tpoint3D<T>& p) {
01080     return add(p);
01081   }
01082 
01083   template <class T>
01084   inline tpoint3D<T>& tpoint3D<T>::subtract(const tpoint3D<T>& p) {
01085     x-=p.x;
01086     y-=p.y;
01087     z-=p.z;
01088     return (*this);
01089   }
01090 
01091   template <class T>
01092   inline tpoint3D<T>& tpoint3D<T>::subtract(const tpoint3D<T>& p1,
01093                                             const tpoint3D<T>& p2) {
01094     x = p1.x - p2.x;
01095     y = p1.y - p2.y;
01096     z = p1.z - p2.z;
01097 
01098     return (*this);
01099   }
01100 
01101   template <class T>
01102   inline tpoint3D<T> tpoint3D<T>::operator-(const tpoint3D<T>& p) const {
01103     return tpoint3D<T>(x-p.x,y-p.y,z-p.z);
01104   }
01105 
01106   template <class T>
01107   inline tpoint3D<T> tpoint3D<T>::operator-=(const tpoint3D<T>& p) {
01108     return subtract(p);
01109   }
01110 
01111   template <class T>
01112   inline tpoint3D<T> tpoint3D<T>::operator*(const tpoint3D<T>& c) const {
01113     return tpoint3D<T>(c.x*x,c.y*x,c.z*z);
01114   }
01115 
01116 
01117   template <class T>
01118   inline tpoint3D<T>& tpoint3D<T>::operator*=(const tpoint3D<T>& c) {
01119     x*=c.x;
01120     y*=c.y;
01121     z*=c.z;
01122     return *this;
01123   }
01124 
01125 
01126 
01127   template <class T>
01128   inline T tpoint3D<T>::dot(const tpoint3D<T>& p) const {
01129     return static_cast<T>((x*p.x) + (y*p.y) + (z*p.z));
01130   }
01131 
01132   template <class T>
01133   inline T tpoint3D<T>::absSqr() const {
01134     return static_cast<T>((x*x) + (y*y) + (z*z));
01135   }
01136 
01137   template <class T>
01138   inline T tpoint3D<T>::distanceTo(const tpoint3D<T>& c) const {
01139     tpoint3D<T> t((*this)-c);
01140     return sqrt(t.absSqr());
01141   }
01142 
01143   template <class T>
01144   inline T tpoint3D<T>::distanceSqr(const tpoint3D<T>& c) const {
01145     tpoint3D<T> t((*this)-c);
01146     return t.absSqr();
01147   }
01148 
01149   template <class T>
01150   inline tpoint3D<T>& tpoint3D<T>::copy(const tpoint3D<T>& p) {
01151     x = p.x;
01152     y = p.y;
01153     z = p.z;
01154     return (*this);
01155   }
01156 
01157   template <class T>
01158   inline bool tpoint3D<T>::operator==(const tpoint3D<T>& p) const {
01159     return ((p.y == y) && (p.x == x) && (p.z == z));
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 != p.z));
01165   }
01166 
01167   template <class T>
01168   inline bool tpoint3D<T>::operator<(const tpoint3D<T>& p) const {
01169     return ((z < p.z) ||
01170             ((z==p.z) && ((y < p.y) ||
01171                           ((y == p.y) && (x < p.x)))));
01172   }
01173 
01174   template <class T>
01175   inline bool tpoint3D<T>::operator>(const tpoint3D<T>& p) const {
01176     return ((z > p.z) ||
01177             ((z==p.z) && ((y > p.y) ||
01178                           ((y == p.y) && (x > p.x)))));
01179   }
01180 
01181   /**
01182    * A three dimensional point with integer coordinates
01183    *
01184    * \deprecated Please use ipoint3D instead
01185    */
01186   typedef tpoint3D<int> point3D;
01187 
01188   /**
01189    * A three dimensional point with integer coordinates
01190    */
01191   typedef tpoint3D<int> ipoint3D;
01192 
01193   /**
01194    * A three dimensional point with integer coordinates
01195    */
01196   typedef tpoint3D<float> fpoint3D;
01197   /**
01198    * A three dimensional point with double coordinates
01199    */
01200   typedef tpoint3D<double> dpoint3D;
01201 
01202   /**
01203    * Read the vector from the given ioHandler.  The complete flag indicates
01204    * if the enclosing begin and end should be also be readed
01205    *
01206    * @ingroup gStorable
01207    */
01208   template <class T>
01209     bool read(ioHandler& handler,tpoint3D<T>& p,const bool complete=true) {
01210     bool b(true);
01211 
01212     if (complete) {
01213       b = b && handler.readBegin();
01214     }
01215 
01216     b = b && handler.read(p.x);
01217     b = b && handler.readDataSeparator();
01218     b = b && handler.read(p.y);
01219     b = b && handler.readDataSeparator();
01220     b = b && handler.read(p.z);
01221 
01222     if (complete) {
01223       b = b && handler.readEnd();
01224     }
01225 
01226     return b;
01227   };
01228 
01229   /**
01230    * Write the vector in the given ioHandler.  The complete flag indicates
01231    * if the enclosing begin and end should be also be written or not
01232    *
01233    * @ingroup gStorable
01234    */
01235   template<class T>
01236   bool write(ioHandler& handler,const tpoint3D<T>& p,const bool complete=true) {
01237     bool b(true);
01238 
01239     if (complete) {
01240       b = b && handler.writeBegin();
01241     }
01242     b = b && handler.write(p.x);
01243     b = b && handler.writeDataSeparator();
01244     b = b && handler.write(p.y);
01245     b = b && handler.writeDataSeparator();
01246     b = b && handler.write(p.z);
01247 
01248     if (complete) {
01249       b = b && handler.writeEnd();
01250     }
01251 
01252     return b;
01253   };
01254 }
01255 
01256 namespace std {
01257 
01258   //inline ostream& operator<<(ostream& s,const lti::point& p);
01259   template <class T>
01260     inline ostream& operator<<(ostream& s,const lti::tpoint<T>& p) {
01261     s << "(" << p.x << ","
01262       << p.y << ")";
01263     return s;
01264   };
01265 
01266   //inline ostream& operator>>(istream& s,const lti::point& p);
01267   template <class T>
01268     inline istream& operator>>(istream& s,lti::tpoint<T>& p) {
01269     char c;
01270     T x,y;
01271     s >> c
01272       >> x >> c
01273       >> y >> c;
01274     p.x = x;
01275     p.y = y;
01276 
01277     return s;
01278   };
01279 
01280   //inline ostream& operator<<(ostream& s,const lti::point& p);
01281   template <class T>
01282   inline ostream& operator<<(ostream& s,const lti::tpoint3D<T>& p) {
01283     s << "("
01284       << p.x << ","
01285       << p.y << ","
01286       << p.z << ")";
01287     return s;
01288   };
01289 
01290   //inline ostream& operator>>(istream& s,const lti::point& p);
01291   template <class T>
01292   inline istream& operator>>(istream& s,lti::tpoint3D<T>& p) {
01293     char c;
01294     T x,y,z;
01295     s >> c
01296       >> x >> c
01297       >> y >> c
01298       >> z >> c;
01299     p.x = x;
01300     p.y = y;
01301     p.z = z;
01302 
01303     return s;
01304   };
01305 }
01306 
01307 #endif
01308 
01309 
01310 

Generated on Thu Nov 24 16:27:04 2005 for LTI-Lib by Doxygen 1.4.4