COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/xy.h @ 1755:bf267b301a5e

Last change on this file since 1755:bf267b301a5e was 1706:163746ec3094, checked in by Balazs Dezso, 15 years ago

Removing NeedCopy?

File size: 12.3 KB
RevLine 
[906]1/* -*- C++ -*-
[1435]2 * lemon/xy.h - Part of LEMON, a generic C++ optimization library
[906]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]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
[921]17#ifndef LEMON_XY_H
18#define LEMON_XY_H
[201]19
20#include <iostream>
[1420]21#include <lemon/utility.h>
[201]22
[491]23///\ingroup misc
[249]24///\file
25///\brief A simple two dimensional vector and a bounding box implementation
26///
[921]27/// The class \ref lemon::xy "xy" implements
[249]28///a two dimensional vector with the usual
29/// operations.
30///
[921]31/// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
[1426]32/// the rectangular bounding box of a set of \ref lemon::xy "xy"'s.
[458]33///
34///\author Attila Bernath
[249]35
36
[921]37namespace lemon {
[431]38
39  /// \addtogroup misc
40  /// @{
41
[1257]42  /// A simple two dimensional vector (plainvector) implementation
[242]43
[1257]44  /// A simple two dimensional vector (plainvector) implementation
[458]45  ///with the usual vector
46  /// operators.
47  ///
48  ///\author Attila Bernath
[207]49  template<typename T>
50    class xy {
[201]51
[207]52    public:
[240]53
[987]54      typedef T Value;
[964]55
[240]56      T x,y;     
[207]57     
[1257]58      ///Default constructor
59      xy() {}
[201]60
[240]61      ///Constructing the instance from coordinates
[514]62      xy(T a, T b) : x(a), y(b) { }
[201]63
64
[1049]65      ///Conversion constructor
66      template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
67
[207]68      ///Gives back the square of the norm of the vector
[1257]69      T normSquare() const {
[1426]70        return x*x+y*y;
[1391]71      }
[201]72 
[207]73      ///Increments the left hand side by u
[1257]74      xy<T>& operator +=(const xy<T>& u) {
[1426]75        x += u.x;
76        y += u.y;
77        return *this;
[1391]78      }
[201]79 
[207]80      ///Decrements the left hand side by u
[1257]81      xy<T>& operator -=(const xy<T>& u) {
[1426]82        x -= u.x;
83        y -= u.y;
84        return *this;
[1391]85      }
[201]86
[207]87      ///Multiplying the left hand side with a scalar
[1257]88      xy<T>& operator *=(const T &u) {
[1426]89        x *= u;
90        y *= u;
91        return *this;
[1391]92      }
[207]93
94      ///Dividing the left hand side by a scalar
[1257]95      xy<T>& operator /=(const T &u) {
[1426]96        x /= u;
97        y /= u;
98        return *this;
[1391]99      }
[201]100 
[207]101      ///Returns the scalar product of two vectors
[1257]102      T operator *(const xy<T>& u) const {
[1426]103        return x*u.x+y*u.y;
[1391]104      }
[201]105 
[207]106      ///Returns the sum of two vectors
107      xy<T> operator+(const xy<T> &u) const {
[1426]108        xy<T> b=*this;
109        return b+=u;
[1391]110      }
[201]111
[1049]112      ///Returns the neg of the vectors
113      xy<T> operator-() const {
[1426]114        xy<T> b=*this;
115        b.x=-b.x; b.y=-b.y;
116        return b;
[1391]117      }
[1049]118
[207]119      ///Returns the difference of two vectors
120      xy<T> operator-(const xy<T> &u) const {
[1426]121        xy<T> b=*this;
122        return b-=u;
[1391]123      }
[201]124
[207]125      ///Returns a vector multiplied by a scalar
126      xy<T> operator*(const T &u) const {
[1426]127        xy<T> b=*this;
128        return b*=u;
[1391]129      }
[201]130
[207]131      ///Returns a vector divided by a scalar
132      xy<T> operator/(const T &u) const {
[1426]133        xy<T> b=*this;
134        return b/=u;
[1391]135      }
[201]136
[207]137      ///Testing equality
[1257]138      bool operator==(const xy<T> &u) const {
[1426]139        return (x==u.x) && (y==u.y);
[1391]140      }
[201]141
[207]142      ///Testing inequality
[1257]143      bool operator!=(xy u) const {
[1426]144        return  (x!=u.x) || (y!=u.y);
[1391]145      }
[201]146
[207]147    };
[201]148
[1071]149  ///Returns a vector multiplied by a scalar
[1083]150
151  ///Returns a vector multiplied by a scalar
152  ///\relates xy
[1071]153  template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
154    return x*u;
[1391]155  }
[1071]156
[814]157  ///Read a plainvector from a stream
158
[967]159  ///Read a plainvector from a stream
[814]160  ///\relates xy
161  ///
[207]162  template<typename T>
[1392]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    }
[207]182    return is;
183  }
[201]184
[814]185  ///Write a plainvector to a stream
186
[967]187  ///Write a plainvector to a stream
[814]188  ///\relates xy
189  ///
[207]190  template<typename T>
[1392]191  inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
[207]192  {
[240]193    os << "(" << z.x << ", " << z.y << ")";
[207]194    return os;
195  }
196
[1202]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 
[244]220
[458]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
[244]226  template<typename T>
227    class BoundingBox {
228      xy<T> bottom_left, top_right;
229      bool _empty;
230    public:
231     
[1426]232      ///Default constructor: creates an empty bounding box
[244]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
[1426]238      ///Were any points added?
[244]239      bool empty() const {
[1426]240        return _empty;
[244]241      }
242
[1391]243      ///Makes the BoundingBox empty
244      void clear() {
[1426]245        _empty=1;
[1391]246      }
247
[244]248      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
249      xy<T> bottomLeft() const {
[1426]250        return bottom_left;
[1391]251      }
[244]252
253      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
254      xy<T> topRight() const {
[1426]255        return top_right;
[1391]256      }
[244]257
[1045]258      ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined)
259      xy<T> bottomRight() const {
[1426]260        return xy<T>(top_right.x,bottom_left.y);
[1391]261      }
[1045]262
263      ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined)
264      xy<T> topLeft() const {
[1426]265        return xy<T>(bottom_left.x,top_right.y);
[1391]266      }
[1045]267
268      ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined)
269      T bottom() const {
[1426]270        return bottom_left.y;
[1391]271      }
[1045]272
273      ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined)
274      T top() const {
[1426]275        return top_right.y;
[1391]276      }
[1045]277
278      ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined)
279      T left() const {
[1426]280        return bottom_left.x;
[1391]281      }
[1045]282
283      ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined)
284      T right() const {
[1426]285        return top_right.x;
[1391]286      }
[1045]287
[1102]288      ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined)
289      T height() const {
[1426]290        return top_right.y-bottom_left.y;
[1391]291      }
[1102]292
293      ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined)
294      T width() const {
[1426]295        return top_right.x-bottom_left.x;
[1391]296      }
[1102]297
[244]298      ///Checks whether a point is inside a bounding box
299      bool inside(const xy<T>& u){
[1426]300        if (_empty)
301          return false;
302        else{
303          return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
304              (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
305        }
[244]306      }
307 
308      ///Increments a bounding box with a point
[1588]309      BoundingBox& add(const xy<T>& u){
[1426]310        if (_empty){
311          bottom_left=top_right=u;
312          _empty = false;
313        }
314        else{
315          if (bottom_left.x > u.x) bottom_left.x = u.x;
316          if (bottom_left.y > u.y) bottom_left.y = u.y;
317          if (top_right.x < u.x) top_right.x = u.x;
318          if (top_right.y < u.y) top_right.y = u.y;
319        }
320        return *this;
[1391]321      }
[244]322 
[1588]323//       ///Sums a bounding box and a point
324//       BoundingBox operator +(const xy<T>& u){
325//         BoundingBox b = *this;
326//         return b += u;
327//       }
[244]328
329      ///Increments a bounding box with an other bounding box
[1588]330      BoundingBox& add(const BoundingBox &u){
[1426]331        if ( !u.empty() ){
[1588]332          this->add(u.bottomLeft());
333          this->add(u.topRight());
[1426]334        }
335        return *this;
[1391]336      }
[244]337 
338      ///Sums two bounding boxes
339      BoundingBox operator +(const BoundingBox& u){
[1426]340        BoundingBox b = *this;
[1588]341        return b.add(u);
342      }
343
344
345      ///Intersection of two bounding boxes
346      BoundingBox operator &(const BoundingBox& u){
347        BoundingBox b;
348        b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
349        b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
350        b.top_right.x=std::min(this->top_right.x,u.top_right.x);
351        b.top_right.y=std::min(this->top_right.y,u.top_right.y);
352        b._empty = this->_empty || u._empty ||
353          b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
354        return b;
[1391]355      }
[244]356
357    };//class Boundingbox
358
359
[1317]360  ///Map of x-coordinates of an xy<>-map
361
362  ///\ingroup maps
363  ///
364  template<class M>
365  class XMap
366  {
[1706]367    M& _map;
[1317]368  public:
[1420]369
[1317]370    typedef typename M::Value::Value Value;
371    typedef typename M::Key Key;
372    ///\e
[1706]373    XMap(M& map) : _map(map) {}
[1317]374    Value operator[](Key k) const {return _map[k].x;}
[1352]375    void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
[1317]376  };
377   
378  ///Returns an \ref XMap class
379
380  ///This function just returns an \ref XMap class.
381  ///
382  ///\ingroup maps
383  ///\relates XMap
384  template<class M>
385  inline XMap<M> xMap(M &m)
386  {
387    return XMap<M>(m);
388  }
389
[1420]390  template<class M>
391  inline XMap<M> xMap(const M &m)
392  {
393    return XMap<M>(m);
394  }
395
[1317]396  ///Constant (read only) version of \ref XMap
397
398  ///\ingroup maps
399  ///
400  template<class M>
401  class ConstXMap
402  {
[1706]403    const M& _map;
[1317]404  public:
[1420]405
[1317]406    typedef typename M::Value::Value Value;
407    typedef typename M::Key Key;
408    ///\e
409    ConstXMap(const M &map) : _map(map) {}
410    Value operator[](Key k) const {return _map[k].x;}
411  };
412   
413  ///Returns a \ref ConstXMap class
414
415  ///This function just returns an \ref ConstXMap class.
416  ///
417  ///\ingroup maps
418  ///\relates ConstXMap
419  template<class M>
420  inline ConstXMap<M> xMap(const M &m)
421  {
422    return ConstXMap<M>(m);
423  }
424
425  ///Map of y-coordinates of an xy<>-map
426   
427  ///\ingroup maps
428  ///
429  template<class M>
430  class YMap
431  {
[1706]432    M& _map;
[1317]433  public:
[1420]434
[1317]435    typedef typename M::Value::Value Value;
436    typedef typename M::Key Key;
437    ///\e
[1706]438    YMap(M& map) : _map(map) {}
[1317]439    Value operator[](Key k) const {return _map[k].y;}
[1352]440    void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
[1317]441  };
442
443  ///Returns an \ref YMap class
444
445  ///This function just returns an \ref YMap class.
446  ///
447  ///\ingroup maps
448  ///\relates YMap
449  template<class M>
450  inline YMap<M> yMap(M &m)
451  {
452    return YMap<M>(m);
453  }
454
[1420]455  template<class M>
456  inline YMap<M> yMap(const M &m)
457  {
458    return YMap<M>(m);
459  }
460
[1317]461  ///Constant (read only) version of \ref YMap
462
463  ///\ingroup maps
464  ///
465  template<class M>
466  class ConstYMap
467  {
[1706]468    const M& _map;
[1317]469  public:
[1420]470
[1317]471    typedef typename M::Value::Value Value;
472    typedef typename M::Key Key;
473    ///\e
474    ConstYMap(const M &map) : _map(map) {}
475    Value operator[](Key k) const {return _map[k].y;}
476  };
477   
478  ///Returns a \ref ConstYMap class
479
480  ///This function just returns an \ref ConstYMap class.
481  ///
482  ///\ingroup maps
483  ///\relates ConstYMap
484  template<class M>
485  inline ConstYMap<M> yMap(const M &m)
486  {
487    return ConstYMap<M>(m);
488  }
489
490
[1352]491  ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
492
493  ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
494  ///\ingroup maps
495  ///
496  template<class M>
497  class NormSquareMap
498  {
[1706]499    const M& _map;
[1352]500  public:
[1420]501
[1352]502    typedef typename M::Value::Value Value;
503    typedef typename M::Key Key;
504    ///\e
505    NormSquareMap(const M &map) : _map(map) {}
506    Value operator[](Key k) const {return _map[k].normSquare();}
507  };
508   
509  ///Returns a \ref NormSquareMap class
510
511  ///This function just returns an \ref NormSquareMap class.
512  ///
513  ///\ingroup maps
514  ///\relates NormSquareMap
515  template<class M>
516  inline NormSquareMap<M> normSquareMap(const M &m)
517  {
518    return NormSquareMap<M>(m);
519  }
520
[431]521  /// @}
[244]522
523
[921]524} //namespace lemon
[201]525
[921]526#endif //LEMON_XY_H
Note: See TracBrowser for help on using the repository browser.