COIN-OR::LEMON - Graph Library

source: lemon-main/lemon/dim2.h

Last change on this file was 1210:da87dbdf3daf, checked in by Alpar Juttner <alpar@…>, 4 years ago

Resolve deprecation warnings of gcc 9 (#633)

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