src/lemon/xy.h
author deba
Wed, 11 May 2005 13:49:17 +0000
changeset 1411 5d161e08bda8
parent 1391 5b46af577b23
child 1420 e37cca875667
permissions -rw-r--r--
Bug fix.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_XY_H
alpar@921
    18
#define LEMON_XY_H
athos@201
    19
athos@201
    20
#include <iostream>
athos@201
    21
klao@491
    22
///\ingroup misc
alpar@249
    23
///\file
alpar@249
    24
///\brief A simple two dimensional vector and a bounding box implementation 
alpar@249
    25
///
alpar@921
    26
/// The class \ref lemon::xy "xy" implements
alpar@249
    27
///a two dimensional vector with the usual
alpar@249
    28
/// operations.
alpar@249
    29
///
alpar@921
    30
/// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
alpar@921
    31
/// the rectangular bounding box a set of \ref lemon::xy "xy"'s.
alpar@458
    32
///
alpar@458
    33
///\author Attila Bernath
alpar@249
    34
alpar@249
    35
alpar@921
    36
namespace lemon {
alpar@431
    37
alpar@431
    38
  /// \addtogroup misc
alpar@431
    39
  /// @{
alpar@431
    40
alpar@1257
    41
  /// A simple two dimensional vector (plainvector) implementation
alpar@242
    42
alpar@1257
    43
  /// A simple two dimensional vector (plainvector) implementation
alpar@458
    44
  ///with the usual vector
alpar@458
    45
  /// operators.
alpar@458
    46
  ///
alpar@458
    47
  ///\author Attila Bernath
athos@207
    48
  template<typename T>
athos@207
    49
    class xy {
athos@201
    50
athos@207
    51
    public:
athos@240
    52
alpar@987
    53
      typedef T Value;
alpar@964
    54
athos@240
    55
      T x,y;     
athos@207
    56
      
alpar@1257
    57
      ///Default constructor
alpar@1257
    58
      xy() {}
athos@201
    59
athos@240
    60
      ///Constructing the instance from coordinates
athos@514
    61
      xy(T a, T b) : x(a), y(b) { }
athos@201
    62
athos@201
    63
alpar@1049
    64
      ///Conversion constructor
alpar@1049
    65
      template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
alpar@1049
    66
athos@207
    67
      ///Gives back the square of the norm of the vector
alpar@1257
    68
      T normSquare() const {
athos@240
    69
	return x*x+y*y;
alpar@1391
    70
      }
athos@201
    71
  
athos@207
    72
      ///Increments the left hand side by u
alpar@1257
    73
      xy<T>& operator +=(const xy<T>& u) {
athos@240
    74
	x += u.x;
athos@240
    75
	y += u.y;
athos@207
    76
	return *this;
alpar@1391
    77
      }
athos@201
    78
  
athos@207
    79
      ///Decrements the left hand side by u
alpar@1257
    80
      xy<T>& operator -=(const xy<T>& u) {
athos@240
    81
	x -= u.x;
athos@240
    82
	y -= u.y;
athos@207
    83
	return *this;
alpar@1391
    84
      }
athos@201
    85
athos@207
    86
      ///Multiplying the left hand side with a scalar
alpar@1257
    87
      xy<T>& operator *=(const T &u) {
athos@240
    88
	x *= u;
athos@240
    89
	y *= u;
athos@207
    90
	return *this;
alpar@1391
    91
      }
athos@207
    92
athos@207
    93
      ///Dividing the left hand side by a scalar
alpar@1257
    94
      xy<T>& operator /=(const T &u) {
athos@240
    95
	x /= u;
athos@240
    96
	y /= u;
athos@207
    97
	return *this;
alpar@1391
    98
      }
athos@201
    99
  
athos@207
   100
      ///Returns the scalar product of two vectors
alpar@1257
   101
      T operator *(const xy<T>& u) const {
athos@240
   102
	return x*u.x+y*u.y;
alpar@1391
   103
      }
athos@201
   104
  
athos@207
   105
      ///Returns the sum of two vectors
athos@207
   106
      xy<T> operator+(const xy<T> &u) const {
athos@207
   107
	xy<T> b=*this;
athos@207
   108
	return b+=u;
alpar@1391
   109
      }
athos@201
   110
alpar@1049
   111
      ///Returns the neg of the vectors
alpar@1049
   112
      xy<T> operator-() const {
alpar@1049
   113
	xy<T> b=*this;
alpar@1049
   114
	b.x=-b.x; b.y=-b.y;
alpar@1049
   115
	return b;
alpar@1391
   116
      }
alpar@1049
   117
athos@207
   118
      ///Returns the difference of two vectors
athos@207
   119
      xy<T> operator-(const xy<T> &u) const {
athos@207
   120
	xy<T> b=*this;
athos@207
   121
	return b-=u;
alpar@1391
   122
      }
athos@201
   123
athos@207
   124
      ///Returns a vector multiplied by a scalar
athos@207
   125
      xy<T> operator*(const T &u) const {
athos@207
   126
	xy<T> b=*this;
athos@207
   127
	return b*=u;
alpar@1391
   128
      }
athos@201
   129
athos@207
   130
      ///Returns a vector divided by a scalar
athos@207
   131
      xy<T> operator/(const T &u) const {
athos@207
   132
	xy<T> b=*this;
athos@207
   133
	return b/=u;
alpar@1391
   134
      }
athos@201
   135
athos@207
   136
      ///Testing equality
alpar@1257
   137
      bool operator==(const xy<T> &u) const {
athos@240
   138
	return (x==u.x) && (y==u.y);
alpar@1391
   139
      }
athos@201
   140
athos@207
   141
      ///Testing inequality
alpar@1257
   142
      bool operator!=(xy u) const {
athos@240
   143
	return  (x!=u.x) || (y!=u.y);
alpar@1391
   144
      }
athos@201
   145
athos@207
   146
    };
athos@201
   147
alpar@1071
   148
  ///Returns a vector multiplied by a scalar
alpar@1083
   149
alpar@1083
   150
  ///Returns a vector multiplied by a scalar
alpar@1083
   151
  ///\relates xy
alpar@1071
   152
  template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
alpar@1071
   153
    return x*u;
alpar@1391
   154
  }
alpar@1071
   155
alpar@814
   156
  ///Read a plainvector from a stream
alpar@814
   157
alpar@967
   158
  ///Read a plainvector from a stream
alpar@814
   159
  ///\relates xy
alpar@814
   160
  ///
athos@207
   161
  template<typename T>
deba@1392
   162
  inline std::istream& operator>>(std::istream &is, xy<T> &z) {
deba@1392
   163
    char c;
deba@1392
   164
    if (is >> c) {
deba@1392
   165
      if (c != '(') is.putback(c);
deba@1392
   166
    } else {
deba@1392
   167
      is.clear();
deba@1392
   168
    }
deba@1392
   169
    if (!(is >> z.x)) return is;
deba@1392
   170
    if (is >> c) {
deba@1392
   171
      if (c != ',') is.putback(c);
deba@1392
   172
    } else {
deba@1392
   173
      is.clear();
deba@1392
   174
    }
deba@1392
   175
    if (!(is >> z.y)) return is;
deba@1392
   176
    if (is >> c) {
deba@1392
   177
      if (c != ')') is.putback(c);
deba@1392
   178
    } else {
deba@1392
   179
      is.clear();
deba@1392
   180
    }
athos@207
   181
    return is;
athos@207
   182
  }
athos@201
   183
alpar@814
   184
  ///Write a plainvector to a stream
alpar@814
   185
alpar@967
   186
  ///Write a plainvector to a stream
alpar@814
   187
  ///\relates xy
alpar@814
   188
  ///
athos@207
   189
  template<typename T>
deba@1392
   190
  inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
athos@207
   191
  {
athos@240
   192
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   193
    return os;
athos@207
   194
  }
athos@207
   195
alpar@1202
   196
  ///Rotate by 90 degrees
alpar@1202
   197
alpar@1202
   198
  ///Returns its parameter rotated by 90 degrees in positive direction.
alpar@1202
   199
  ///\relates xy
alpar@1202
   200
  ///
alpar@1202
   201
  template<typename T>
alpar@1202
   202
  inline xy<T> rot90(const xy<T> &z)
alpar@1202
   203
  {
alpar@1202
   204
    return xy<T>(-z.y,z.x);
alpar@1202
   205
  }
alpar@1202
   206
alpar@1202
   207
  ///Rotate by 270 degrees
alpar@1202
   208
alpar@1202
   209
  ///Returns its parameter rotated by 90 degrees in negative direction.
alpar@1202
   210
  ///\relates xy
alpar@1202
   211
  ///
alpar@1202
   212
  template<typename T>
alpar@1202
   213
  inline xy<T> rot270(const xy<T> &z)
alpar@1202
   214
  {
alpar@1202
   215
    return xy<T>(z.y,-z.x);
alpar@1202
   216
  }
alpar@1202
   217
alpar@1202
   218
  
athos@244
   219
alpar@458
   220
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   221
alpar@458
   222
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   223
  ///
alpar@458
   224
  ///\author Attila Bernath
athos@244
   225
  template<typename T>
athos@244
   226
    class BoundingBox {
athos@244
   227
      xy<T> bottom_left, top_right;
athos@244
   228
      bool _empty;
athos@244
   229
    public:
athos@244
   230
      
athos@244
   231
      ///Default constructor: an empty bounding box
athos@244
   232
      BoundingBox() { _empty = true; }
athos@244
   233
athos@244
   234
      ///Constructing the instance from one point
athos@244
   235
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   236
athos@244
   237
      ///Is there any point added
athos@244
   238
      bool empty() const {
athos@244
   239
	return _empty;
athos@244
   240
      }
athos@244
   241
alpar@1391
   242
      ///Makes the BoundingBox empty
alpar@1391
   243
      void clear() {
alpar@1391
   244
	_empty=1;
alpar@1391
   245
      }
alpar@1391
   246
athos@244
   247
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   248
      xy<T> bottomLeft() const {
athos@244
   249
	return bottom_left;
alpar@1391
   250
      }
athos@244
   251
athos@244
   252
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   253
      xy<T> topRight() const {
athos@244
   254
	return top_right;
alpar@1391
   255
      }
athos@244
   256
alpar@1045
   257
      ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   258
      xy<T> bottomRight() const {
alpar@1045
   259
	return xy<T>(top_right.x,bottom_left.y);
alpar@1391
   260
      }
alpar@1045
   261
alpar@1045
   262
      ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   263
      xy<T> topLeft() const {
alpar@1045
   264
	return xy<T>(bottom_left.x,top_right.y);
alpar@1391
   265
      }
alpar@1045
   266
alpar@1045
   267
      ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   268
      T bottom() const {
alpar@1045
   269
	return bottom_left.y;
alpar@1391
   270
      }
alpar@1045
   271
alpar@1045
   272
      ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   273
      T top() const {
alpar@1045
   274
	return top_right.y;
alpar@1391
   275
      }
alpar@1045
   276
alpar@1045
   277
      ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   278
      T left() const {
alpar@1045
   279
	return bottom_left.x;
alpar@1391
   280
      }
alpar@1045
   281
alpar@1045
   282
      ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   283
      T right() const {
alpar@1045
   284
	return top_right.x;
alpar@1391
   285
      }
alpar@1045
   286
alpar@1102
   287
      ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1102
   288
      T height() const {
alpar@1102
   289
	return top_right.y-bottom_left.y;
alpar@1391
   290
      }
alpar@1102
   291
alpar@1102
   292
      ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1102
   293
      T width() const {
alpar@1102
   294
	return top_right.x-bottom_left.x;
alpar@1391
   295
      }
alpar@1102
   296
athos@244
   297
      ///Checks whether a point is inside a bounding box
athos@244
   298
      bool inside(const xy<T>& u){
athos@244
   299
	if (_empty)
athos@244
   300
	  return false;
athos@244
   301
	else{
athos@244
   302
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   303
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   304
	}
athos@244
   305
      }
athos@244
   306
  
athos@244
   307
      ///Increments a bounding box with a point
athos@244
   308
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   309
	if (_empty){
athos@244
   310
	  bottom_left=top_right=u;
athos@244
   311
	  _empty = false;
athos@244
   312
	}
athos@244
   313
	else{
athos@244
   314
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   315
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   316
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   317
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   318
	}
athos@244
   319
	return *this;
alpar@1391
   320
      }
athos@244
   321
  
athos@244
   322
      ///Sums a bounding box and a point
athos@244
   323
      BoundingBox operator +(const xy<T>& u){
athos@244
   324
	BoundingBox b = *this;
athos@244
   325
	return b += u;
alpar@1391
   326
      }
athos@244
   327
athos@244
   328
      ///Increments a bounding box with an other bounding box
athos@244
   329
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   330
	if ( !u.empty() ){
athos@244
   331
	  *this += u.bottomLeft();
athos@244
   332
	  *this += u.topRight();
athos@244
   333
	}
athos@244
   334
	return *this;
alpar@1391
   335
      }
athos@244
   336
  
athos@244
   337
      ///Sums two bounding boxes
athos@244
   338
      BoundingBox operator +(const BoundingBox& u){
athos@244
   339
	BoundingBox b = *this;
athos@244
   340
	return b += u;
alpar@1391
   341
      }
athos@244
   342
athos@244
   343
    };//class Boundingbox
athos@244
   344
athos@244
   345
alpar@1317
   346
  ///Map of x-coordinates of an xy<>-map
alpar@1317
   347
alpar@1317
   348
  ///\ingroup maps
alpar@1317
   349
  ///
alpar@1317
   350
  template<class M>
alpar@1317
   351
  class XMap 
alpar@1317
   352
  {
alpar@1317
   353
    M &_map;
alpar@1317
   354
  public:
alpar@1317
   355
    typedef typename M::Value::Value Value;
alpar@1317
   356
    typedef typename M::Key Key;
alpar@1317
   357
    ///\e
alpar@1317
   358
    XMap(M &map) : _map(map) {}
alpar@1317
   359
    Value operator[](Key k) const {return _map[k].x;}
alpar@1352
   360
    void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
alpar@1317
   361
  };
alpar@1317
   362
    
alpar@1317
   363
  ///Returns an \ref XMap class
alpar@1317
   364
alpar@1317
   365
  ///This function just returns an \ref XMap class.
alpar@1317
   366
  ///
alpar@1317
   367
  ///\ingroup maps
alpar@1317
   368
  ///\relates XMap
alpar@1317
   369
  template<class M> 
alpar@1317
   370
  inline XMap<M> xMap(M &m) 
alpar@1317
   371
  {
alpar@1317
   372
    return XMap<M>(m);
alpar@1317
   373
  }
alpar@1317
   374
alpar@1317
   375
  ///Constant (read only) version of \ref XMap
alpar@1317
   376
alpar@1317
   377
  ///\ingroup maps
alpar@1317
   378
  ///
alpar@1317
   379
  template<class M>
alpar@1317
   380
  class ConstXMap 
alpar@1317
   381
  {
alpar@1317
   382
    const M &_map;
alpar@1317
   383
  public:
alpar@1317
   384
    typedef typename M::Value::Value Value;
alpar@1317
   385
    typedef typename M::Key Key;
alpar@1317
   386
    ///\e
alpar@1317
   387
    ConstXMap(const M &map) : _map(map) {}
alpar@1317
   388
    Value operator[](Key k) const {return _map[k].x;}
alpar@1317
   389
  };
alpar@1317
   390
    
alpar@1317
   391
  ///Returns a \ref ConstXMap class
alpar@1317
   392
alpar@1317
   393
  ///This function just returns an \ref ConstXMap class.
alpar@1317
   394
  ///
alpar@1317
   395
  ///\ingroup maps
alpar@1317
   396
  ///\relates ConstXMap
alpar@1317
   397
  template<class M> 
alpar@1317
   398
  inline ConstXMap<M> xMap(const M &m) 
alpar@1317
   399
  {
alpar@1317
   400
    return ConstXMap<M>(m);
alpar@1317
   401
  }
alpar@1317
   402
alpar@1317
   403
  ///Map of y-coordinates of an xy<>-map
alpar@1317
   404
    
alpar@1317
   405
  ///\ingroup maps
alpar@1317
   406
  ///
alpar@1317
   407
  template<class M>
alpar@1317
   408
  class YMap 
alpar@1317
   409
  {
alpar@1317
   410
    M &_map;
alpar@1317
   411
  public:
alpar@1317
   412
    typedef typename M::Value::Value Value;
alpar@1317
   413
    typedef typename M::Key Key;
alpar@1317
   414
    ///\e
alpar@1317
   415
    YMap(M &map) : _map(map) {}
alpar@1317
   416
    Value operator[](Key k) const {return _map[k].y;}
alpar@1352
   417
    void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
alpar@1317
   418
  };
alpar@1317
   419
alpar@1317
   420
  ///Returns an \ref YMap class
alpar@1317
   421
alpar@1317
   422
  ///This function just returns an \ref YMap class.
alpar@1317
   423
  ///
alpar@1317
   424
  ///\ingroup maps
alpar@1317
   425
  ///\relates YMap
alpar@1317
   426
  template<class M> 
alpar@1317
   427
  inline YMap<M> yMap(M &m) 
alpar@1317
   428
  {
alpar@1317
   429
    return YMap<M>(m);
alpar@1317
   430
  }
alpar@1317
   431
alpar@1317
   432
  ///Constant (read only) version of \ref YMap
alpar@1317
   433
alpar@1317
   434
  ///\ingroup maps
alpar@1317
   435
  ///
alpar@1317
   436
  template<class M>
alpar@1317
   437
  class ConstYMap 
alpar@1317
   438
  {
alpar@1317
   439
    const M &_map;
alpar@1317
   440
  public:
alpar@1317
   441
    typedef typename M::Value::Value Value;
alpar@1317
   442
    typedef typename M::Key Key;
alpar@1317
   443
    ///\e
alpar@1317
   444
    ConstYMap(const M &map) : _map(map) {}
alpar@1317
   445
    Value operator[](Key k) const {return _map[k].y;}
alpar@1317
   446
  };
alpar@1317
   447
    
alpar@1317
   448
  ///Returns a \ref ConstYMap class
alpar@1317
   449
alpar@1317
   450
  ///This function just returns an \ref ConstYMap class.
alpar@1317
   451
  ///
alpar@1317
   452
  ///\ingroup maps
alpar@1317
   453
  ///\relates ConstYMap
alpar@1317
   454
  template<class M> 
alpar@1317
   455
  inline ConstYMap<M> yMap(const M &m) 
alpar@1317
   456
  {
alpar@1317
   457
    return ConstYMap<M>(m);
alpar@1317
   458
  }
alpar@1317
   459
alpar@1317
   460
alpar@1352
   461
  ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
alpar@1352
   462
alpar@1352
   463
  ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
alpar@1352
   464
  ///\ingroup maps
alpar@1352
   465
  ///
alpar@1352
   466
  template<class M>
alpar@1352
   467
  class NormSquareMap 
alpar@1352
   468
  {
alpar@1352
   469
    const M &_map;
alpar@1352
   470
  public:
alpar@1352
   471
    typedef typename M::Value::Value Value;
alpar@1352
   472
    typedef typename M::Key Key;
alpar@1352
   473
    ///\e
alpar@1352
   474
    NormSquareMap(const M &map) : _map(map) {}
alpar@1352
   475
    Value operator[](Key k) const {return _map[k].normSquare();}
alpar@1352
   476
  };
alpar@1352
   477
    
alpar@1352
   478
  ///Returns a \ref NormSquareMap class
alpar@1352
   479
alpar@1352
   480
  ///This function just returns an \ref NormSquareMap class.
alpar@1352
   481
  ///
alpar@1352
   482
  ///\ingroup maps
alpar@1352
   483
  ///\relates NormSquareMap
alpar@1352
   484
  template<class M> 
alpar@1352
   485
  inline NormSquareMap<M> normSquareMap(const M &m) 
alpar@1352
   486
  {
alpar@1352
   487
    return NormSquareMap<M>(m);
alpar@1352
   488
  }
alpar@1352
   489
alpar@431
   490
  /// @}
athos@244
   491
athos@244
   492
alpar@921
   493
} //namespace lemon
athos@201
   494
alpar@921
   495
#endif //LEMON_XY_H