COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/dim2.h @ 2247:269a0dcee70b

Last change on this file since 2247:269a0dcee70b was 2217:4a10a45d55f6, checked in by Balazs Dezso, 18 years ago

Doc fix

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