CVR-Lib last update 20 Sep 2009

cvrPoint.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2004
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 
00042 /**
00043  * \file   cvrPoint.h
00044  *         Defines the template type cvr::point<T> and all shortcuts like
00045  *         cvr::ipoint.
00046  * \author Pablo Alvarado
00047  * \date   01.11.2002
00048  *
00049  * $Id: cvrPoint.h,v 1.5 2007/11/03 23:50:17 alvarado Exp $
00050  */
00051 
00052 #ifndef _CVR_POINT_H_
00053 #define _CVR_POINT_H_
00054 
00055 #include "cvrAssert.h"
00056 #include "cvrIoHandler.h"
00057 
00058 #include <iostream>
00059 
00060 namespace cvr {
00061 
00062   /**
00063    * Two dimensional point, containing the coordinates x, y.
00064    *
00065    * The template type T will be the one used for each coordinate.  For
00066    * example point<float> uses float for \a x and \a y.
00067    *
00068    * This data structure simplifies the manipulation of 2D points providing
00069    * simple interfaces for adding, substracting, distance (L2), and more.
00070    *
00071    * There are some alias to shorten the notation:
00072    * - cvr::ipoint is equivalent to cvr::point<int>
00073    * - cvr::fpoint is equivalent to cvr::point<sreal>
00074    * - cvr::dpoint is equivalent to cvr::point<dreal>
00075    *
00076    * @ingroup gGeomData
00077    */
00078   template <typename T>
00079   class point {
00080   public:
00081     /**
00082      * Used for the template-based interface for pixels as vectors.
00083      */
00084     typedef T value_type;
00085 
00086     /**
00087      * Return type of the size() member
00088      */
00089     typedef int size_type;
00090 
00091     /*
00092      * Anonymous union to allow the fastes access to the elements as
00093      * semantic tokes (x and y) and as array as well
00094      */
00095     union {
00096       __extension__ struct {
00097         /**
00098          * Coordinate x
00099          */
00100         value_type x;
00101 
00102         /**
00103          * Coordinate y;
00104          */
00105         value_type y;
00106       };
00107 
00108       /**
00109        * Array that shares the same memory with x and y
00110        */
00111       value_type data[2];
00112     };
00113 
00114     /**
00115      * Default constructor
00116      */
00117     explicit point(const T newx=T(),const T newy=T());
00118 
00119     /**
00120      * Copy constructor
00121      */
00122     template <typename U>
00123     point(const point<U>& p);
00124 
00125     /**
00126      * Copy constructor
00127      */
00128     template <typename U>
00129     point<T>& castFrom(const point<U>& p);
00130 
00131     /**
00132      * Set the coordinate values and return a reference to this point
00133      */
00134     inline point<T>& set(const T tx,const T ty);
00135 
00136     /**
00137      * Get the coordinate values
00138      */
00139     inline void get(T& tx,T& ty) const;
00140 
00141     /**
00142      * Calculate %square of distance to the point c.
00143      *
00144      * If you need the Euclidean distance just call sqrt(distanceSqr).
00145      */
00146     inline T distanceSqr(const point<T>& c) const;
00147 
00148     /**
00149      * Return the square of the magnitude of the point
00150      */
00151     inline T absSqr() const;
00152 
00153     /**
00154      * Dot product with another point.
00155      *
00156      * @return this->x*p.x + this->y*p.y.
00157      */
00158     inline T dot(const point<T>& p) const;
00159 
00160     /**
00161      * Multiply point<T> with a given factor
00162      */
00163     template<typename U>
00164     inline point<T>& multiply(const U c);
00165 
00166     /**
00167      * Multiply point<T> with a given factor
00168      */
00169     template<typename U>
00170     inline point<T> operator*(const U c) const;
00171 
00172     /**
00173      * Multiply the other point point<T> with a given factor
00174      */
00175     template<typename U>
00176     inline point<T>& multiply(const point<T>& other,const U c);
00177 
00178     /**
00179      * Multiply point<T> with a given factor
00180      */
00181     template<typename U>
00182     inline point<T>& operator*=(const U c);
00183 
00184     /**
00185      * Multiplies elementwise the components of this and the point c
00186      */
00187     inline point<T> operator*(const point<T>& c) const;
00188 
00189     /**
00190      * Multiplies elementwise the components of this and the point c
00191      */
00192     inline point<T>& operator*=(const point<T>& c);
00193 
00194     /**
00195      * Multiplies elementwise the components of this and the point c, and
00196      * leave the result here.
00197      */
00198     inline point<T>& emultiply(const point<T>& c);
00199 
00200     /**
00201      * Multiplies elementwise the components of \a a and \a b and leave the
00202      * result here.
00203      */
00204     inline point<T>& emultiply(const point<T>& a,const point<T>& b);
00205 
00206     /**
00207      * Divide each component of point<T> with a given factor
00208      */
00209     template<typename U>
00210     inline point<T>& divide(const U c);
00211 
00212     /**
00213      * Divide each component of other other point<T> with a given factor
00214      */
00215     template<typename U>
00216     inline point<T>& divide(const point<T>& other,const U c);
00217 
00218     /**
00219      * Divide each component of point<T> by a given factor
00220      */
00221     template <typename U>
00222     inline point<T> operator/(const U c) const;
00223 
00224     /**
00225      * Divide each component of point<T> by a given factor
00226      */
00227     template <typename U>
00228     inline point<T>& operator/=(const U c);
00229 
00230     /**
00231      * Elementwise division of each component of the points
00232      */
00233     inline point<T> operator/(const point<T>& c) const;
00234 
00235     /**
00236      * Elementwise division of each component of the points
00237      */
00238     inline point<T>& operator/=(const point<T>& c);
00239 
00240     /**
00241      * Elementwise division of each component of the points
00242      */
00243     inline point<T>& edivide(const point<T>& c);
00244 
00245     /**
00246      * Elementwise division of each component of the points
00247      */
00248     inline point<T>& edivide(const point<T>& a,const point<T>& b);
00249 
00250     /**
00251      * Modulo c of the integer part of each component of the point
00252      */
00253     inline point<T> operator%(const int c) const;
00254 
00255     /**
00256      * Add given point to this point and leave the result here.
00257      * @param p the other point to be added to this one
00258      * @return a reference to this point
00259      */
00260     inline point<T>& add(const point<T>& p);
00261 
00262     /**
00263      * Add the two other points and leave the result here
00264      * @param a first point to be added
00265      * @param b second point to be added
00266      * @return a reference to this point, which will contain a+b
00267      */
00268     inline point<T>& add(const point<T>& a,
00269                          const point<T>& b);
00270     /**
00271      * Operator + is simmilar to add, but a new point is returned, i.e.
00272      * this point will not change.
00273      */
00274     inline point<T> operator+(const point<T>& p) const;
00275 
00276     /**
00277      * Operator += is an alias for add()
00278      */
00279     inline point<T>& operator+=(const point<T>& p);
00280 
00281     /**
00282      * Subtract
00283      */
00284     inline point<T>& subtract(const point<T>& p);
00285 
00286 
00287     /**
00288      * Subtract the two other points and leave the result here
00289      * @param a first point
00290      * @param b point to be subtracted from the first one
00291      * @return a reference to this point, which will contain a-b
00292      */
00293     inline point<T>& subtract(const point<T>& a,
00294                               const point<T>& b);
00295 
00296     /**
00297      * Operator -
00298      */
00299     inline point<T> operator-(const point<T>& p) const;
00300 
00301     /**
00302      * Operator -
00303      */
00304     inline point<T> operator-() const;
00305 
00306     /**
00307      * Operator -=
00308      */
00309     inline point<T>& operator-=(const point<T>& p);
00310 
00311     /**
00312      * Copy operator
00313      */
00314     inline point<T>& copy(const point<T>& p);
00315 
00316     /**
00317      * Operator =
00318      */
00319     inline point<T>& operator=(const point<T>& p);
00320 
00321     /**
00322      * Operator ==
00323      */
00324     inline bool operator==(const point<T>& p) const;
00325 
00326     /**
00327      * Operator !=
00328      */
00329     inline bool operator!=(const point<T>& p) const;
00330 
00331     /**
00332      * Operator <
00333      *
00334      * A point is "smaller" than another one if its coordinates produce
00335      * an earlier display of the point in the monitor. i.e. if it
00336      * has a smaller y component or (if the y components are the same) if
00337      * the x component is smaller
00338      */
00339     inline bool operator<(const point<T>& p) const;
00340 
00341     /**
00342      * Operator >
00343      *
00344      * A point is "bigger" than another one if its coordinates produce
00345      * an later display of the point in the monitor. i.e. if it
00346      * has a bigger y component or (if the y components are the same) if
00347      * the x component is bigger
00348      */
00349     inline bool operator>(const point<T>& p) const;
00350 
00351     /**
00352      * @name Access as vector
00353      */
00354     //@{
00355     /**
00356      * Used to simulate vector access.  It allows the use of point in templates
00357      * expecting a vector-like structure.
00358      *
00359      * The correspondence between the elements of the vector and
00360      * the components will be [0] for x and [1] for y
00361      */
00362     inline T& operator[](const int i);
00363 
00364     /**
00365      * Used to simulate read-only vector access.  It allows the use of point
00366      * in templates expecting a vector-like structure.
00367      *
00368      * The correspondence between the elements of the vector and
00369      * the components will be [0] for x and [1] for y
00370      */
00371     inline const T& operator[](const int i) const;
00372 
00373     /**
00374      * Used to simulate the vector size
00375      */
00376     inline size_type size() const;
00377     //@}
00378 
00379   private:
00380     /**
00381      * Square of the given value
00382      *
00383      * Reimplemented to avoid including cvrMath.h just for this extremely
00384      * simple function.
00385      */
00386     inline value_type sqr(const value_type x) const;
00387   };
00388 
00389   /**
00390    * Read the vector from the given ioHandler.  The complete flag indicates
00391    * if the enclosing begin and end should be also be readed
00392    *
00393    * @ingroup gStorable
00394    */
00395   template <typename T>
00396   bool read(ioHandler& handler,point<T>& p,const bool complete=true);
00397 
00398   /**
00399    * Write the vector in the given ioHandler.  The complete flag indicates
00400    * if the enclosing begin and end should be also be written or not
00401    *
00402    * @ingroup gStorable
00403    */
00404   template<typename T>
00405   bool write(ioHandler& handler,const point<T>& p,const bool complete=true);
00406 
00407   /**
00408    * A point with integer coordinates
00409    */
00410   typedef point<int> ipoint;
00411 
00412   /**
00413    * A point with unsigned integer coordinates
00414    */
00415   typedef point<unsigned int> uipoint;
00416 
00417   /**
00418    * A point with double coordinates
00419    */
00420   typedef point<double> dpoint;
00421 
00422   /**
00423    * A point with float coordinates
00424    */
00425   typedef point<float> fpoint;
00426 
00427 }
00428 
00429 namespace std {
00430 
00431   //inline ostream& operator<<(ostream& s,const cvr::point& p);
00432   template <typename T>
00433   inline ostream& operator<<(ostream& s,const cvr::point<T>& p);
00434 
00435   //inline ostream& operator>>(istream& s,const cvr::point& p);
00436   template <typename T>
00437   inline istream& operator>>(istream& s,cvr::point<T>& p);
00438 
00439 }
00440 
00441 #include "cvrPoint_template.h"
00442 
00443 #endif
00444 
00445 
00446 

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8