COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/dim2.h @ 2517:d9cfac072869

Last change on this file since 2517:d9cfac072869 was 2451:d7b7048e045b, checked in by Alpar Juttner, 17 years ago

Two new constructors added to dim2::BoundingBox?

File size: 16.1 KB
RevLine 
[906]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
[2391]5 * Copyright (C) 2003-2007
[1956]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
[2451]275    template<typename T>
[244]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; }
[2451]286     
287      ///Construct an instance from two points
288     
289      ///Construct an instance from two points
290      ///\warning The coordinates of the bottom-left corner must be no more
291      ///than those of the top-right one
292      BoundingBox(Point<T> a,Point<T> b)
293      {
294        bottom_left=a;
295        top_right=b;
296        _empty = false;
297      }
298     
299      ///Construct an instance from four numbers
[244]300
[2451]301      ///Construct an instance from four numbers
302      ///\warning The coordinates of the bottom-left corner must be no more
303      ///than those of the top-right one
304      BoundingBox(T l,T b,T r,T t)
305      {
306        bottom_left=Point<T>(l,b);
307        top_right=Point<T>(r,t);
308        _empty = false;
309      }
310     
[1426]311      ///Were any points added?
[244]312      bool empty() const {
[1426]313        return _empty;
[244]314      }
[2451]315     
[2157]316      ///Make the BoundingBox empty
[1391]317      void clear() {
[1426]318        _empty=1;
[1391]319      }
320
[2157]321      ///Give back the bottom left corner
322
323      ///Give back the bottom left corner.
324      ///If the bounding box is empty, then the return value is not defined.
[2207]325      Point<T> bottomLeft() const {
[1426]326        return bottom_left;
[1391]327      }
[244]328
[2157]329      ///Set the bottom left corner
330
331      ///Set the bottom left corner.
332      ///It should only bee used for non-empty box.
[2207]333      void bottomLeft(Point<T> p) {
[1927]334        bottom_left = p;
335      }
336
[2157]337      ///Give back the top right corner
338
339      ///Give back the top right corner.
340      ///If the bounding box is empty, then the return value is not defined.
[2207]341      Point<T> topRight() const {
[1426]342        return top_right;
[1391]343      }
[244]344
[2157]345      ///Set the top right corner
346
347      ///Set the top right corner.
348      ///It should only bee used for non-empty box.
[2207]349      void topRight(Point<T> p) {
[1927]350        top_right = p;
351      }
352
[2157]353      ///Give back the bottom right corner
354
355      ///Give back the bottom right corner.
356      ///If the bounding box is empty, then the return value is not defined.
[2207]357      Point<T> bottomRight() const {
358        return Point<T>(top_right.x,bottom_left.y);
[1391]359      }
[1045]360
[2157]361      ///Set the bottom right corner
362
363      ///Set the bottom right corner.
364      ///It should only bee used for non-empty box.
[2207]365      void bottomRight(Point<T> p) {
[1927]366        top_right.x = p.x;
367        bottom_left.y = p.y;
368      }
[2157]369 
370      ///Give back the top left corner
[1927]371
[2157]372      ///Give back the top left corner.
373      ///If the bounding box is empty, then the return value is not defined.
[2207]374      Point<T> topLeft() const {
375        return Point<T>(bottom_left.x,top_right.y);
[1391]376      }
[1045]377
[2157]378      ///Set the top left corner
379
380      ///Set the top left corner.
381      ///It should only bee used for non-empty box.
[2207]382      void topLeft(Point<T> p) {
[1927]383        top_right.y = p.y;
384        bottom_left.x = p.x;
385      }
386
[2157]387      ///Give back the bottom of the box
388
389      ///Give back the bottom of the box.
390      ///If the bounding box is empty, then the return value is not defined.
[1045]391      T bottom() const {
[1426]392        return bottom_left.y;
[1391]393      }
[1045]394
[2157]395      ///Set the bottom of the box
396
397      ///Set the bottom of the box.
398      ///It should only bee used for non-empty box.
[1927]399      void bottom(T t) {
400        bottom_left.y = t;
401      }
402
[2157]403      ///Give back the top of the box
404
405      ///Give back the top of the box.
406      ///If the bounding box is empty, then the return value is not defined.
[1045]407      T top() const {
[1426]408        return top_right.y;
[1391]409      }
[1045]410
[2157]411      ///Set the top of the box
412
413      ///Set the top of the box.
414      ///It should only bee used for non-empty box.
[1927]415      void top(T t) {
416        top_right.y = t;
417      }
418
[2157]419      ///Give back the left side of the box
420
421      ///Give back the left side of the box.
422      ///If the bounding box is empty, then the return value is not defined.
[1045]423      T left() const {
[1426]424        return bottom_left.x;
[1391]425      }
[2157]426 
427      ///Set the left side of the box
[1045]428
[2157]429      ///Set the left side of the box.
430      ///It should only bee used for non-empty box
[1927]431      void left(T t) {
432        bottom_left.x = t;
433      }
434
[2157]435      /// Give back the right side of the box
436
437      /// Give back the right side of the box.
438      ///If the bounding box is empty, then the return value is not defined.
[1045]439      T right() const {
[1426]440        return top_right.x;
[1391]441      }
[1045]442
[2157]443      ///Set the right side of the box
444
445      ///Set the right side of the box.
446      ///It should only bee used for non-empty box
[1927]447      void right(T t) {
448        top_right.x = t;
449      }
450
[2157]451      ///Give back the height of the box
452
453      ///Give back the height of the box.
454      ///If the bounding box is empty, then the return value is not defined.
[1102]455      T height() const {
[1426]456        return top_right.y-bottom_left.y;
[1391]457      }
[1102]458
[2157]459      ///Give back the width of the box
460
461      ///Give back the width of the box.
462      ///If the bounding box is empty, then the return value is not defined.
[1102]463      T width() const {
[1426]464        return top_right.x-bottom_left.x;
[1391]465      }
[1102]466
[244]467      ///Checks whether a point is inside a bounding box
[2207]468      bool inside(const Point<T>& u){
[1426]469        if (_empty)
470          return false;
471        else{
472          return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
473              (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
474        }
[244]475      }
476 
477      ///Increments a bounding box with a point
[2207]478      BoundingBox& add(const Point<T>& u){
[1426]479        if (_empty){
480          bottom_left=top_right=u;
481          _empty = false;
482        }
483        else{
484          if (bottom_left.x > u.x) bottom_left.x = u.x;
485          if (bottom_left.y > u.y) bottom_left.y = u.y;
486          if (top_right.x < u.x) top_right.x = u.x;
487          if (top_right.y < u.y) top_right.y = u.y;
488        }
489        return *this;
[1391]490      }
[2214]491   
492      ///Increments a bounding to contain another bounding box
[1588]493      BoundingBox& add(const BoundingBox &u){
[1426]494        if ( !u.empty() ){
[1588]495          this->add(u.bottomLeft());
496          this->add(u.topRight());
[1426]497        }
498        return *this;
[1391]499      }
[244]500 
[1588]501      ///Intersection of two bounding boxes
502      BoundingBox operator &(const BoundingBox& u){
503        BoundingBox b;
504        b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
505        b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
506        b.top_right.x=std::min(this->top_right.x,u.top_right.x);
507        b.top_right.y=std::min(this->top_right.y,u.top_right.y);
508        b._empty = this->_empty || u._empty ||
509          b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
510        return b;
[1391]511      }
[244]512
513    };//class Boundingbox
514
515
[2207]516  ///Map of x-coordinates of a dim2::Point<>-map
[1317]517
518  ///\ingroup maps
[2214]519  ///Map of x-coordinates of a dim2::Point<>-map
[1317]520  ///
521  template<class M>
522  class XMap
523  {
[1706]524    M& _map;
[1317]525  public:
[1420]526
[1317]527    typedef typename M::Value::Value Value;
528    typedef typename M::Key Key;
529    ///\e
[1706]530    XMap(M& map) : _map(map) {}
[1317]531    Value operator[](Key k) const {return _map[k].x;}
[1352]532    void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
[1317]533  };
534   
535  ///Returns an \ref XMap class
536
537  ///This function just returns an \ref XMap class.
538  ///
539  ///\ingroup maps
540  ///\relates XMap
541  template<class M>
542  inline XMap<M> xMap(M &m)
543  {
544    return XMap<M>(m);
545  }
546
[1420]547  template<class M>
548  inline XMap<M> xMap(const M &m)
549  {
550    return XMap<M>(m);
551  }
552
[1317]553  ///Constant (read only) version of \ref XMap
554
555  ///\ingroup maps
[2214]556  ///Constant (read only) version of \ref XMap
[1317]557  ///
558  template<class M>
559  class ConstXMap
560  {
[1706]561    const M& _map;
[1317]562  public:
[1420]563
[1317]564    typedef typename M::Value::Value Value;
565    typedef typename M::Key Key;
566    ///\e
567    ConstXMap(const M &map) : _map(map) {}
568    Value operator[](Key k) const {return _map[k].x;}
569  };
570   
571  ///Returns a \ref ConstXMap class
572
573  ///This function just returns an \ref ConstXMap class.
574  ///
575  ///\ingroup maps
576  ///\relates ConstXMap
577  template<class M>
578  inline ConstXMap<M> xMap(const M &m)
579  {
580    return ConstXMap<M>(m);
581  }
582
[2207]583  ///Map of y-coordinates of a dim2::Point<>-map
[1317]584   
585  ///\ingroup maps
[2214]586  ///Map of y-coordinates of a dim2::Point<>-map
[1317]587  ///
588  template<class M>
589  class YMap
590  {
[1706]591    M& _map;
[1317]592  public:
[1420]593
[1317]594    typedef typename M::Value::Value Value;
595    typedef typename M::Key Key;
596    ///\e
[1706]597    YMap(M& map) : _map(map) {}
[1317]598    Value operator[](Key k) const {return _map[k].y;}
[1352]599    void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
[1317]600  };
601
602  ///Returns an \ref YMap class
603
604  ///This function just returns an \ref YMap class.
605  ///
606  ///\ingroup maps
607  ///\relates YMap
608  template<class M>
609  inline YMap<M> yMap(M &m)
610  {
611    return YMap<M>(m);
612  }
613
[1420]614  template<class M>
615  inline YMap<M> yMap(const M &m)
616  {
617    return YMap<M>(m);
618  }
619
[1317]620  ///Constant (read only) version of \ref YMap
621
622  ///\ingroup maps
[2214]623  ///Constant (read only) version of \ref YMap
[1317]624  ///
625  template<class M>
626  class ConstYMap
627  {
[1706]628    const M& _map;
[1317]629  public:
[1420]630
[1317]631    typedef typename M::Value::Value Value;
632    typedef typename M::Key Key;
633    ///\e
634    ConstYMap(const M &map) : _map(map) {}
635    Value operator[](Key k) const {return _map[k].y;}
636  };
637   
638  ///Returns a \ref ConstYMap class
639
640  ///This function just returns an \ref ConstYMap class.
641  ///
642  ///\ingroup maps
643  ///\relates ConstYMap
644  template<class M>
645  inline ConstYMap<M> yMap(const M &m)
646  {
647    return ConstYMap<M>(m);
648  }
649
650
[2214]651    ///\brief Map of the \ref Point::normSquare() "normSquare()"
652    ///of an \ref Point "Point"-map
653    ///
654    ///Map of the \ref Point::normSquare() "normSquare()"
655    ///of an \ref Point "Point"-map
656    ///\ingroup maps
657    ///
[1352]658  template<class M>
659  class NormSquareMap
660  {
[1706]661    const M& _map;
[1352]662  public:
[1420]663
[1352]664    typedef typename M::Value::Value Value;
665    typedef typename M::Key Key;
666    ///\e
667    NormSquareMap(const M &map) : _map(map) {}
668    Value operator[](Key k) const {return _map[k].normSquare();}
669  };
670   
671  ///Returns a \ref NormSquareMap class
672
673  ///This function just returns an \ref NormSquareMap class.
674  ///
675  ///\ingroup maps
676  ///\relates NormSquareMap
677  template<class M>
678  inline NormSquareMap<M> normSquareMap(const M &m)
679  {
680    return NormSquareMap<M>(m);
681  }
682
[431]683  /// @}
[244]684
[2207]685  } //namespce dim2
686 
[921]687} //namespace lemon
[201]688
[2207]689#endif //LEMON_DIM2_H
Note: See TracBrowser for help on using the repository browser.