lemon/xy.h
author alpar
Fri, 03 Feb 2006 15:58:24 +0000
changeset 1953 d4f411003580
parent 1875 98698b69a902
child 1956 a055123339d5
permissions -rw-r--r--
Polish the doc.
     1 /* -*- C++ -*-
     2  * lemon/xy.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_XY_H
    18 #define LEMON_XY_H
    19 
    20 #include <iostream>
    21 #include <lemon/utility.h>
    22 
    23 ///\ingroup misc
    24 ///\file
    25 ///\brief A simple two dimensional vector and a bounding box implementation 
    26 ///
    27 /// The class \ref lemon::xy "xy" implements
    28 ///a two dimensional vector with the usual
    29 /// operations.
    30 ///
    31 /// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
    32 /// the rectangular bounding box of a set of \ref lemon::xy "xy"'s.
    33 ///
    34 ///\author Attila Bernath
    35 
    36 
    37 namespace lemon {
    38 
    39   /// \addtogroup misc
    40   /// @{
    41 
    42   /// A simple two dimensional vector (plainvector) implementation
    43 
    44   /// A simple two dimensional vector (plainvector) implementation
    45   ///with the usual vector
    46   /// operators.
    47   ///
    48   ///\author Attila Bernath
    49   template<typename T>
    50     class xy {
    51 
    52     public:
    53 
    54       typedef T Value;
    55 
    56       T x,y;     
    57       
    58       ///Default constructor
    59       xy() {}
    60 
    61       ///Constructing the instance from coordinates
    62       xy(T a, T b) : x(a), y(b) { }
    63 
    64 
    65       ///Conversion constructor
    66       template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
    67 
    68       ///Gives back the square of the norm of the vector
    69       T normSquare() const {
    70         return x*x+y*y;
    71       }
    72   
    73       ///Increments the left hand side by u
    74       xy<T>& operator +=(const xy<T>& u) {
    75         x += u.x;
    76         y += u.y;
    77         return *this;
    78       }
    79   
    80       ///Decrements the left hand side by u
    81       xy<T>& operator -=(const xy<T>& u) {
    82         x -= u.x;
    83         y -= u.y;
    84         return *this;
    85       }
    86 
    87       ///Multiplying the left hand side with a scalar
    88       xy<T>& operator *=(const T &u) {
    89         x *= u;
    90         y *= u;
    91         return *this;
    92       }
    93 
    94       ///Dividing the left hand side by a scalar
    95       xy<T>& operator /=(const T &u) {
    96         x /= u;
    97         y /= u;
    98         return *this;
    99       }
   100   
   101       ///Returns the scalar product of two vectors
   102       T operator *(const xy<T>& u) const {
   103         return x*u.x+y*u.y;
   104       }
   105   
   106       ///Returns the sum of two vectors
   107       xy<T> operator+(const xy<T> &u) const {
   108         xy<T> b=*this;
   109         return b+=u;
   110       }
   111 
   112       ///Returns the neg of the vectors
   113       xy<T> operator-() const {
   114         xy<T> b=*this;
   115         b.x=-b.x; b.y=-b.y;
   116         return b;
   117       }
   118 
   119       ///Returns the difference of two vectors
   120       xy<T> operator-(const xy<T> &u) const {
   121         xy<T> b=*this;
   122         return b-=u;
   123       }
   124 
   125       ///Returns a vector multiplied by a scalar
   126       xy<T> operator*(const T &u) const {
   127         xy<T> b=*this;
   128         return b*=u;
   129       }
   130 
   131       ///Returns a vector divided by a scalar
   132       xy<T> operator/(const T &u) const {
   133         xy<T> b=*this;
   134         return b/=u;
   135       }
   136 
   137       ///Testing equality
   138       bool operator==(const xy<T> &u) const {
   139         return (x==u.x) && (y==u.y);
   140       }
   141 
   142       ///Testing inequality
   143       bool operator!=(xy u) const {
   144         return  (x!=u.x) || (y!=u.y);
   145       }
   146 
   147     };
   148 
   149   ///Returns a vector multiplied by a scalar
   150 
   151   ///Returns a vector multiplied by a scalar
   152   ///\relates xy
   153   template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
   154     return x*u;
   155   }
   156 
   157   ///Read a plainvector from a stream
   158 
   159   ///Read a plainvector from a stream
   160   ///\relates xy
   161   ///
   162   template<typename T>
   163   inline std::istream& operator>>(std::istream &is, xy<T> &z) {
   164     char c;
   165     if (is >> c) {
   166       if (c != '(') is.putback(c);
   167     } else {
   168       is.clear();
   169     }
   170     if (!(is >> z.x)) return is;
   171     if (is >> c) {
   172       if (c != ',') is.putback(c);
   173     } else {
   174       is.clear();
   175     }
   176     if (!(is >> z.y)) return is;
   177     if (is >> c) {
   178       if (c != ')') is.putback(c);
   179     } else {
   180       is.clear();
   181     }
   182     return is;
   183   }
   184 
   185   ///Write a plainvector to a stream
   186 
   187   ///Write a plainvector to a stream
   188   ///\relates xy
   189   ///
   190   template<typename T>
   191   inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
   192   {
   193     os << "(" << z.x << ", " << z.y << ")";
   194     return os;
   195   }
   196 
   197   ///Rotate by 90 degrees
   198 
   199   ///Returns its parameter rotated by 90 degrees in positive direction.
   200   ///\relates xy
   201   ///
   202   template<typename T>
   203   inline xy<T> rot90(const xy<T> &z)
   204   {
   205     return xy<T>(-z.y,z.x);
   206   }
   207 
   208   ///Rotate by 270 degrees
   209 
   210   ///Returns its parameter rotated by 90 degrees in negative direction.
   211   ///\relates xy
   212   ///
   213   template<typename T>
   214   inline xy<T> rot270(const xy<T> &z)
   215   {
   216     return xy<T>(z.y,-z.x);
   217   }
   218 
   219   
   220 
   221   /// A class to calculate or store the bounding box of plainvectors.
   222 
   223   /// A class to calculate or store the bounding box of plainvectors.
   224   ///
   225   ///\author Attila Bernath
   226   template<typename T>
   227     class BoundingBox {
   228       xy<T> bottom_left, top_right;
   229       bool _empty;
   230     public:
   231       
   232       ///Default constructor: creates an empty bounding box
   233       BoundingBox() { _empty = true; }
   234 
   235       ///Constructing the instance from one point
   236       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
   237 
   238       ///Were any points added?
   239       bool empty() const {
   240         return _empty;
   241       }
   242 
   243       ///Makes the BoundingBox empty
   244       void clear() {
   245         _empty=1;
   246       }
   247 
   248       ///\brief Gives back the bottom left corner
   249       ///(if the bounding box is empty, then the return value is not defined) 
   250       xy<T> bottomLeft() const {
   251         return bottom_left;
   252       }
   253 
   254       ///\brief Sets the bottom left corner
   255       ///(should only bee used for non-empty box) 
   256       void bottomLeft(xy<T> p) {
   257 	bottom_left = p;
   258       }
   259 
   260       ///\brief Gives back the top right corner
   261       ///(if the bounding box is empty, then the return value is not defined) 
   262       xy<T> topRight() const {
   263         return top_right;
   264       }
   265 
   266       ///\brief Sets the top right corner
   267       ///(should only bee used for non-empty box) 
   268       void topRight(xy<T> p) {
   269 	top_right = p;
   270       }
   271 
   272       ///\brief Gives back the bottom right corner
   273       ///(if the bounding box is empty, then the return value is not defined) 
   274       xy<T> bottomRight() const {
   275         return xy<T>(top_right.x,bottom_left.y);
   276       }
   277 
   278       ///\brief Sets the bottom right corner
   279       ///(should only bee used for non-empty box) 
   280       void bottomRight(xy<T> p) {
   281 	top_right.x = p.x;
   282 	bottom_left.y = p.y;
   283       }
   284 
   285       ///\brief Gives back the top left corner
   286       ///(if the bounding box is empty, then the return value is not defined) 
   287       xy<T> topLeft() const {
   288         return xy<T>(bottom_left.x,top_right.y);
   289       }
   290 
   291       ///\brief Sets the top left corner
   292       ///(should only bee used for non-empty box) 
   293       void topLeft(xy<T> p) {
   294 	top_right.y = p.y;
   295 	bottom_left.x = p.x;
   296       }
   297 
   298       ///\brief Gives back the bottom of the box
   299       ///(if the bounding box is empty, then the return value is not defined) 
   300       T bottom() const {
   301         return bottom_left.y;
   302       }
   303 
   304       ///\brief Sets the bottom of the box
   305       ///(should only bee used for non-empty box) 
   306       void bottom(T t) {
   307 	bottom_left.y = t;
   308       }
   309 
   310       ///\brief Gives back the top of the box
   311       ///(if the bounding box is empty, then the return value is not defined) 
   312       T top() const {
   313         return top_right.y;
   314       }
   315 
   316       ///\brief Sets the top of the box
   317       ///(should only bee used for non-empty box) 
   318       void top(T t) {
   319 	top_right.y = t;
   320       }
   321 
   322       ///\brief Gives back the left side of the box
   323       ///(if the bounding box is empty, then the return value is not defined) 
   324       T left() const {
   325         return bottom_left.x;
   326       }
   327 
   328       ///\brief Sets the left side of the box
   329       ///(should only bee used for non-empty box) 
   330       void left(T t) {
   331 	bottom_left.x = t;
   332       }
   333 
   334       ///\brief Gives back the right side of the box
   335       ///(if the bounding box is empty, then the return value is not defined) 
   336       T right() const {
   337         return top_right.x;
   338       }
   339 
   340       ///\brief Sets the right side of the box
   341       ///(should only bee used for non-empty box) 
   342       void right(T t) {
   343 	top_right.x = t;
   344       }
   345 
   346       ///\brief Gives back the height of the box
   347       ///(if the bounding box is empty, then the return value is not defined) 
   348       T height() const {
   349         return top_right.y-bottom_left.y;
   350       }
   351 
   352       ///\brief Gives back the width of the box
   353       ///(if the bounding box is empty, then the return value is not defined) 
   354       T width() const {
   355         return top_right.x-bottom_left.x;
   356       }
   357 
   358       ///Checks whether a point is inside a bounding box
   359       bool inside(const xy<T>& u){
   360         if (_empty)
   361           return false;
   362         else{
   363           return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   364               (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   365         }
   366       }
   367   
   368       ///Increments a bounding box with a point
   369       BoundingBox& add(const xy<T>& u){
   370         if (_empty){
   371           bottom_left=top_right=u;
   372           _empty = false;
   373         }
   374         else{
   375           if (bottom_left.x > u.x) bottom_left.x = u.x;
   376           if (bottom_left.y > u.y) bottom_left.y = u.y;
   377           if (top_right.x < u.x) top_right.x = u.x;
   378           if (top_right.y < u.y) top_right.y = u.y;
   379         }
   380         return *this;
   381       }
   382   
   383 //       ///Sums a bounding box and a point
   384 //       BoundingBox operator +(const xy<T>& u){
   385 //         BoundingBox b = *this;
   386 //         return b += u;
   387 //       }
   388 
   389       ///Increments a bounding box with an other bounding box
   390       BoundingBox& add(const BoundingBox &u){
   391         if ( !u.empty() ){
   392           this->add(u.bottomLeft());
   393 	  this->add(u.topRight());
   394         }
   395         return *this;
   396       }
   397   
   398       ///Sums two bounding boxes
   399       BoundingBox operator +(const BoundingBox& u){
   400         BoundingBox b = *this;
   401         return b.add(u);
   402       }
   403 
   404 
   405       ///Intersection of two bounding boxes
   406       BoundingBox operator &(const BoundingBox& u){
   407         BoundingBox b;
   408 	b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
   409 	b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
   410 	b.top_right.x=std::min(this->top_right.x,u.top_right.x);
   411 	b.top_right.y=std::min(this->top_right.y,u.top_right.y);
   412 	b._empty = this->_empty || u._empty ||
   413 	  b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
   414         return b;
   415       }
   416 
   417     };//class Boundingbox
   418 
   419 
   420   ///Map of x-coordinates of an xy<>-map
   421 
   422   ///\ingroup maps
   423   ///
   424   template<class M>
   425   class XMap 
   426   {
   427     M& _map;
   428   public:
   429 
   430     typedef typename M::Value::Value Value;
   431     typedef typename M::Key Key;
   432     ///\e
   433     XMap(M& map) : _map(map) {}
   434     Value operator[](Key k) const {return _map[k].x;}
   435     void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
   436   };
   437     
   438   ///Returns an \ref XMap class
   439 
   440   ///This function just returns an \ref XMap class.
   441   ///
   442   ///\ingroup maps
   443   ///\relates XMap
   444   template<class M> 
   445   inline XMap<M> xMap(M &m) 
   446   {
   447     return XMap<M>(m);
   448   }
   449 
   450   template<class M> 
   451   inline XMap<M> xMap(const M &m) 
   452   {
   453     return XMap<M>(m);
   454   }
   455 
   456   ///Constant (read only) version of \ref XMap
   457 
   458   ///\ingroup maps
   459   ///
   460   template<class M>
   461   class ConstXMap 
   462   {
   463     const M& _map;
   464   public:
   465 
   466     typedef typename M::Value::Value Value;
   467     typedef typename M::Key Key;
   468     ///\e
   469     ConstXMap(const M &map) : _map(map) {}
   470     Value operator[](Key k) const {return _map[k].x;}
   471   };
   472     
   473   ///Returns a \ref ConstXMap class
   474 
   475   ///This function just returns an \ref ConstXMap class.
   476   ///
   477   ///\ingroup maps
   478   ///\relates ConstXMap
   479   template<class M> 
   480   inline ConstXMap<M> xMap(const M &m) 
   481   {
   482     return ConstXMap<M>(m);
   483   }
   484 
   485   ///Map of y-coordinates of an xy<>-map
   486     
   487   ///\ingroup maps
   488   ///
   489   template<class M>
   490   class YMap 
   491   {
   492     M& _map;
   493   public:
   494 
   495     typedef typename M::Value::Value Value;
   496     typedef typename M::Key Key;
   497     ///\e
   498     YMap(M& map) : _map(map) {}
   499     Value operator[](Key k) const {return _map[k].y;}
   500     void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
   501   };
   502 
   503   ///Returns an \ref YMap class
   504 
   505   ///This function just returns an \ref YMap class.
   506   ///
   507   ///\ingroup maps
   508   ///\relates YMap
   509   template<class M> 
   510   inline YMap<M> yMap(M &m) 
   511   {
   512     return YMap<M>(m);
   513   }
   514 
   515   template<class M> 
   516   inline YMap<M> yMap(const M &m) 
   517   {
   518     return YMap<M>(m);
   519   }
   520 
   521   ///Constant (read only) version of \ref YMap
   522 
   523   ///\ingroup maps
   524   ///
   525   template<class M>
   526   class ConstYMap 
   527   {
   528     const M& _map;
   529   public:
   530 
   531     typedef typename M::Value::Value Value;
   532     typedef typename M::Key Key;
   533     ///\e
   534     ConstYMap(const M &map) : _map(map) {}
   535     Value operator[](Key k) const {return _map[k].y;}
   536   };
   537     
   538   ///Returns a \ref ConstYMap class
   539 
   540   ///This function just returns an \ref ConstYMap class.
   541   ///
   542   ///\ingroup maps
   543   ///\relates ConstYMap
   544   template<class M> 
   545   inline ConstYMap<M> yMap(const M &m) 
   546   {
   547     return ConstYMap<M>(m);
   548   }
   549 
   550 
   551   ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
   552 
   553   ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
   554   ///\ingroup maps
   555   ///
   556   template<class M>
   557   class NormSquareMap 
   558   {
   559     const M& _map;
   560   public:
   561 
   562     typedef typename M::Value::Value Value;
   563     typedef typename M::Key Key;
   564     ///\e
   565     NormSquareMap(const M &map) : _map(map) {}
   566     Value operator[](Key k) const {return _map[k].normSquare();}
   567   };
   568     
   569   ///Returns a \ref NormSquareMap class
   570 
   571   ///This function just returns an \ref NormSquareMap class.
   572   ///
   573   ///\ingroup maps
   574   ///\relates NormSquareMap
   575   template<class M> 
   576   inline NormSquareMap<M> normSquareMap(const M &m) 
   577   {
   578     return NormSquareMap<M>(m);
   579   }
   580 
   581   /// @}
   582 
   583 
   584 } //namespace lemon
   585 
   586 #endif //LEMON_XY_H