src/lemon/xy.h
author deba
Tue, 19 Apr 2005 13:33:44 +0000
changeset 1374 bcfa3980b432
parent 1352 bdbb9144a49e
child 1391 5b46af577b23
permissions -rw-r--r--
Call the default constructor of the ObserverBase.
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;
athos@207
    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;
athos@207
    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;
athos@207
    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;
athos@207
    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;
athos@207
    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;
athos@207
   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;
athos@207
   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@1049
   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;
athos@207
   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;
athos@207
   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;
athos@207
   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);
athos@207
   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);
athos@207
   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@1071
   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>
athos@207
   162
  inline
athos@207
   163
  std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207
   164
  {
athos@240
   165
athos@240
   166
    is >> z.x >> z.y;
athos@207
   167
    return is;
athos@207
   168
  }
athos@201
   169
alpar@814
   170
  ///Write a plainvector to a stream
alpar@814
   171
alpar@967
   172
  ///Write a plainvector to a stream
alpar@814
   173
  ///\relates xy
alpar@814
   174
  ///
athos@207
   175
  template<typename T>
athos@207
   176
  inline
athos@207
   177
  std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207
   178
  {
athos@240
   179
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   180
    return os;
athos@207
   181
  }
athos@207
   182
alpar@1202
   183
  ///Rotate by 90 degrees
alpar@1202
   184
alpar@1202
   185
  ///Returns its parameter rotated by 90 degrees in positive direction.
alpar@1202
   186
  ///\relates xy
alpar@1202
   187
  ///
alpar@1202
   188
  template<typename T>
alpar@1202
   189
  inline xy<T> rot90(const xy<T> &z)
alpar@1202
   190
  {
alpar@1202
   191
    return xy<T>(-z.y,z.x);
alpar@1202
   192
  }
alpar@1202
   193
alpar@1202
   194
  ///Rotate by 270 degrees
alpar@1202
   195
alpar@1202
   196
  ///Returns its parameter rotated by 90 degrees in negative direction.
alpar@1202
   197
  ///\relates xy
alpar@1202
   198
  ///
alpar@1202
   199
  template<typename T>
alpar@1202
   200
  inline xy<T> rot270(const xy<T> &z)
alpar@1202
   201
  {
alpar@1202
   202
    return xy<T>(z.y,-z.x);
alpar@1202
   203
  }
alpar@1202
   204
alpar@1202
   205
  
athos@244
   206
alpar@458
   207
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   208
alpar@458
   209
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   210
  ///
alpar@458
   211
  ///\author Attila Bernath
athos@244
   212
  template<typename T>
athos@244
   213
    class BoundingBox {
athos@244
   214
      xy<T> bottom_left, top_right;
athos@244
   215
      bool _empty;
athos@244
   216
    public:
athos@244
   217
      
athos@244
   218
      ///Default constructor: an empty bounding box
athos@244
   219
      BoundingBox() { _empty = true; }
athos@244
   220
athos@244
   221
      ///Constructing the instance from one point
athos@244
   222
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   223
athos@244
   224
      ///Is there any point added
athos@244
   225
      bool empty() const {
athos@244
   226
	return _empty;
athos@244
   227
      }
athos@244
   228
athos@244
   229
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   230
      xy<T> bottomLeft() const {
athos@244
   231
	return bottom_left;
athos@244
   232
      };
athos@244
   233
athos@244
   234
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   235
      xy<T> topRight() const {
athos@244
   236
	return top_right;
athos@244
   237
      };
athos@244
   238
alpar@1045
   239
      ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   240
      xy<T> bottomRight() const {
alpar@1045
   241
	return xy<T>(top_right.x,bottom_left.y);
alpar@1045
   242
      };
alpar@1045
   243
alpar@1045
   244
      ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   245
      xy<T> topLeft() const {
alpar@1045
   246
	return xy<T>(bottom_left.x,top_right.y);
alpar@1045
   247
      };
alpar@1045
   248
alpar@1045
   249
      ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   250
      T bottom() const {
alpar@1045
   251
	return bottom_left.y;
alpar@1045
   252
      };
alpar@1045
   253
alpar@1045
   254
      ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   255
      T top() const {
alpar@1045
   256
	return top_right.y;
alpar@1045
   257
      };
alpar@1045
   258
alpar@1045
   259
      ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   260
      T left() const {
alpar@1045
   261
	return bottom_left.x;
alpar@1045
   262
      };
alpar@1045
   263
alpar@1045
   264
      ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   265
      T right() const {
alpar@1045
   266
	return top_right.x;
alpar@1045
   267
      };
alpar@1045
   268
alpar@1102
   269
      ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1102
   270
      T height() const {
alpar@1102
   271
	return top_right.y-bottom_left.y;
alpar@1102
   272
      };
alpar@1102
   273
alpar@1102
   274
      ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1102
   275
      T width() const {
alpar@1102
   276
	return top_right.x-bottom_left.x;
alpar@1102
   277
      };
alpar@1102
   278
athos@244
   279
      ///Checks whether a point is inside a bounding box
athos@244
   280
      bool inside(const xy<T>& u){
athos@244
   281
	if (_empty)
athos@244
   282
	  return false;
athos@244
   283
	else{
athos@244
   284
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   285
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   286
	}
athos@244
   287
      }
athos@244
   288
  
athos@244
   289
      ///Increments a bounding box with a point
athos@244
   290
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   291
	if (_empty){
athos@244
   292
	  bottom_left=top_right=u;
athos@244
   293
	  _empty = false;
athos@244
   294
	}
athos@244
   295
	else{
athos@244
   296
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   297
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   298
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   299
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   300
	}
athos@244
   301
	return *this;
athos@244
   302
      };
athos@244
   303
  
athos@244
   304
      ///Sums a bounding box and a point
athos@244
   305
      BoundingBox operator +(const xy<T>& u){
athos@244
   306
	BoundingBox b = *this;
athos@244
   307
	return b += u;
athos@244
   308
      };
athos@244
   309
athos@244
   310
      ///Increments a bounding box with an other bounding box
athos@244
   311
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   312
	if ( !u.empty() ){
athos@244
   313
	  *this += u.bottomLeft();
athos@244
   314
	  *this += u.topRight();
athos@244
   315
	}
athos@244
   316
	return *this;
athos@244
   317
      };
athos@244
   318
  
athos@244
   319
      ///Sums two bounding boxes
athos@244
   320
      BoundingBox operator +(const BoundingBox& u){
athos@244
   321
	BoundingBox b = *this;
athos@244
   322
	return b += u;
athos@244
   323
      };
athos@244
   324
athos@244
   325
    };//class Boundingbox
athos@244
   326
athos@244
   327
alpar@1317
   328
  ///Map of x-coordinates of an xy<>-map
alpar@1317
   329
alpar@1317
   330
  ///\ingroup maps
alpar@1317
   331
  ///
alpar@1317
   332
  template<class M>
alpar@1317
   333
  class XMap 
alpar@1317
   334
  {
alpar@1317
   335
    M &_map;
alpar@1317
   336
  public:
alpar@1317
   337
    typedef typename M::Value::Value Value;
alpar@1317
   338
    typedef typename M::Key Key;
alpar@1317
   339
    ///\e
alpar@1317
   340
    XMap(M &map) : _map(map) {}
alpar@1317
   341
    Value operator[](Key k) const {return _map[k].x;}
alpar@1352
   342
    void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
alpar@1317
   343
  };
alpar@1317
   344
    
alpar@1317
   345
  ///Returns an \ref XMap class
alpar@1317
   346
alpar@1317
   347
  ///This function just returns an \ref XMap class.
alpar@1317
   348
  ///
alpar@1317
   349
  ///\ingroup maps
alpar@1317
   350
  ///\relates XMap
alpar@1317
   351
  template<class M> 
alpar@1317
   352
  inline XMap<M> xMap(M &m) 
alpar@1317
   353
  {
alpar@1317
   354
    return XMap<M>(m);
alpar@1317
   355
  }
alpar@1317
   356
alpar@1317
   357
  ///Constant (read only) version of \ref XMap
alpar@1317
   358
alpar@1317
   359
  ///\ingroup maps
alpar@1317
   360
  ///
alpar@1317
   361
  template<class M>
alpar@1317
   362
  class ConstXMap 
alpar@1317
   363
  {
alpar@1317
   364
    const M &_map;
alpar@1317
   365
  public:
alpar@1317
   366
    typedef typename M::Value::Value Value;
alpar@1317
   367
    typedef typename M::Key Key;
alpar@1317
   368
    ///\e
alpar@1317
   369
    ConstXMap(const M &map) : _map(map) {}
alpar@1317
   370
    Value operator[](Key k) const {return _map[k].x;}
alpar@1317
   371
  };
alpar@1317
   372
    
alpar@1317
   373
  ///Returns a \ref ConstXMap class
alpar@1317
   374
alpar@1317
   375
  ///This function just returns an \ref ConstXMap class.
alpar@1317
   376
  ///
alpar@1317
   377
  ///\ingroup maps
alpar@1317
   378
  ///\relates ConstXMap
alpar@1317
   379
  template<class M> 
alpar@1317
   380
  inline ConstXMap<M> xMap(const M &m) 
alpar@1317
   381
  {
alpar@1317
   382
    return ConstXMap<M>(m);
alpar@1317
   383
  }
alpar@1317
   384
alpar@1317
   385
  ///Map of y-coordinates of an xy<>-map
alpar@1317
   386
    
alpar@1317
   387
  ///\ingroup maps
alpar@1317
   388
  ///
alpar@1317
   389
  template<class M>
alpar@1317
   390
  class YMap 
alpar@1317
   391
  {
alpar@1317
   392
    M &_map;
alpar@1317
   393
  public:
alpar@1317
   394
    typedef typename M::Value::Value Value;
alpar@1317
   395
    typedef typename M::Key Key;
alpar@1317
   396
    ///\e
alpar@1317
   397
    YMap(M &map) : _map(map) {}
alpar@1317
   398
    Value operator[](Key k) const {return _map[k].y;}
alpar@1352
   399
    void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
alpar@1317
   400
  };
alpar@1317
   401
alpar@1317
   402
  ///Returns an \ref YMap class
alpar@1317
   403
alpar@1317
   404
  ///This function just returns an \ref YMap class.
alpar@1317
   405
  ///
alpar@1317
   406
  ///\ingroup maps
alpar@1317
   407
  ///\relates YMap
alpar@1317
   408
  template<class M> 
alpar@1317
   409
  inline YMap<M> yMap(M &m) 
alpar@1317
   410
  {
alpar@1317
   411
    return YMap<M>(m);
alpar@1317
   412
  }
alpar@1317
   413
alpar@1317
   414
  ///Constant (read only) version of \ref YMap
alpar@1317
   415
alpar@1317
   416
  ///\ingroup maps
alpar@1317
   417
  ///
alpar@1317
   418
  template<class M>
alpar@1317
   419
  class ConstYMap 
alpar@1317
   420
  {
alpar@1317
   421
    const M &_map;
alpar@1317
   422
  public:
alpar@1317
   423
    typedef typename M::Value::Value Value;
alpar@1317
   424
    typedef typename M::Key Key;
alpar@1317
   425
    ///\e
alpar@1317
   426
    ConstYMap(const M &map) : _map(map) {}
alpar@1317
   427
    Value operator[](Key k) const {return _map[k].y;}
alpar@1317
   428
  };
alpar@1317
   429
    
alpar@1317
   430
  ///Returns a \ref ConstYMap class
alpar@1317
   431
alpar@1317
   432
  ///This function just returns an \ref ConstYMap class.
alpar@1317
   433
  ///
alpar@1317
   434
  ///\ingroup maps
alpar@1317
   435
  ///\relates ConstYMap
alpar@1317
   436
  template<class M> 
alpar@1317
   437
  inline ConstYMap<M> yMap(const M &m) 
alpar@1317
   438
  {
alpar@1317
   439
    return ConstYMap<M>(m);
alpar@1317
   440
  }
alpar@1317
   441
alpar@1317
   442
alpar@1352
   443
  ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
alpar@1352
   444
alpar@1352
   445
  ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
alpar@1352
   446
  ///\ingroup maps
alpar@1352
   447
  ///
alpar@1352
   448
  template<class M>
alpar@1352
   449
  class NormSquareMap 
alpar@1352
   450
  {
alpar@1352
   451
    const M &_map;
alpar@1352
   452
  public:
alpar@1352
   453
    typedef typename M::Value::Value Value;
alpar@1352
   454
    typedef typename M::Key Key;
alpar@1352
   455
    ///\e
alpar@1352
   456
    NormSquareMap(const M &map) : _map(map) {}
alpar@1352
   457
    Value operator[](Key k) const {return _map[k].normSquare();}
alpar@1352
   458
  };
alpar@1352
   459
    
alpar@1352
   460
  ///Returns a \ref NormSquareMap class
alpar@1352
   461
alpar@1352
   462
  ///This function just returns an \ref NormSquareMap class.
alpar@1352
   463
  ///
alpar@1352
   464
  ///\ingroup maps
alpar@1352
   465
  ///\relates NormSquareMap
alpar@1352
   466
  template<class M> 
alpar@1352
   467
  inline NormSquareMap<M> normSquareMap(const M &m) 
alpar@1352
   468
  {
alpar@1352
   469
    return NormSquareMap<M>(m);
alpar@1352
   470
  }
alpar@1352
   471
alpar@431
   472
  /// @}
athos@244
   473
athos@244
   474
alpar@921
   475
} //namespace lemon
athos@201
   476
alpar@921
   477
#endif //LEMON_XY_H