COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/xy.h @ 2190:dd887831e9c1

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