src/lemon/xy.h
author marci
Tue, 11 Jan 2005 17:15:46 +0000
changeset 1074 4a24a46407db
parent 1049 e27446e1deda
child 1083 8043b93e5973
permissions -rw-r--r--
:-}
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, 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@458
    41
  /// A two dimensional vector (plainvector) implementation
alpar@242
    42
alpar@458
    43
  /// A 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
      
athos@207
    57
      ///Default constructor: both coordinates become 0
athos@240
    58
      xy() : x(0), y(0) {}
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
athos@207
    68
      T normSquare(){
athos@240
    69
	return x*x+y*y;
athos@207
    70
      };
athos@201
    71
  
athos@207
    72
      ///Increments the left hand side by u
athos@207
    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
athos@207
    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
athos@207
    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
athos@207
    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
athos@207
   101
      T operator *(const xy<T>& u){
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
athos@207
   137
      bool operator==(const xy<T> &u){
athos@240
   138
	return (x==u.x) && (y==u.y);
athos@207
   139
      };
athos@201
   140
athos@207
   141
      ///Testing inequality
athos@207
   142
      bool operator!=(xy u){
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@1071
   149
  template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
alpar@1071
   150
    return x*u;
alpar@1071
   151
  };
alpar@1071
   152
alpar@814
   153
  ///Read a plainvector from a stream
alpar@814
   154
alpar@967
   155
  ///Read a plainvector from a stream
alpar@814
   156
  ///\relates xy
alpar@814
   157
  ///
athos@207
   158
  template<typename T>
athos@207
   159
  inline
athos@207
   160
  std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207
   161
  {
athos@240
   162
athos@240
   163
    is >> z.x >> z.y;
athos@207
   164
    return is;
athos@207
   165
  }
athos@201
   166
alpar@814
   167
  ///Write a plainvector to a stream
alpar@814
   168
alpar@967
   169
  ///Write a plainvector to a stream
alpar@814
   170
  ///\relates xy
alpar@814
   171
  ///
athos@207
   172
  template<typename T>
athos@207
   173
  inline
athos@207
   174
  std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207
   175
  {
athos@240
   176
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   177
    return os;
athos@207
   178
  }
athos@207
   179
athos@244
   180
alpar@458
   181
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   182
alpar@458
   183
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   184
  ///
alpar@458
   185
  ///\author Attila Bernath
athos@244
   186
  template<typename T>
athos@244
   187
    class BoundingBox {
athos@244
   188
      xy<T> bottom_left, top_right;
athos@244
   189
      bool _empty;
athos@244
   190
    public:
athos@244
   191
      
athos@244
   192
      ///Default constructor: an empty bounding box
athos@244
   193
      BoundingBox() { _empty = true; }
athos@244
   194
athos@244
   195
      ///Constructing the instance from one point
athos@244
   196
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   197
athos@244
   198
      ///Is there any point added
athos@244
   199
      bool empty() const {
athos@244
   200
	return _empty;
athos@244
   201
      }
athos@244
   202
athos@244
   203
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   204
      xy<T> bottomLeft() const {
athos@244
   205
	return bottom_left;
athos@244
   206
      };
athos@244
   207
athos@244
   208
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   209
      xy<T> topRight() const {
athos@244
   210
	return top_right;
athos@244
   211
      };
athos@244
   212
alpar@1045
   213
      ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   214
      xy<T> bottomRight() const {
alpar@1045
   215
	return xy<T>(top_right.x,bottom_left.y);
alpar@1045
   216
      };
alpar@1045
   217
alpar@1045
   218
      ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   219
      xy<T> topLeft() const {
alpar@1045
   220
	return xy<T>(bottom_left.x,top_right.y);
alpar@1045
   221
      };
alpar@1045
   222
alpar@1045
   223
      ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   224
      T bottom() const {
alpar@1045
   225
	return bottom_left.y;
alpar@1045
   226
      };
alpar@1045
   227
alpar@1045
   228
      ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   229
      T top() const {
alpar@1045
   230
	return top_right.y;
alpar@1045
   231
      };
alpar@1045
   232
alpar@1045
   233
      ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   234
      T left() const {
alpar@1045
   235
	return bottom_left.x;
alpar@1045
   236
      };
alpar@1045
   237
alpar@1045
   238
      ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   239
      T right() const {
alpar@1045
   240
	return top_right.x;
alpar@1045
   241
      };
alpar@1045
   242
athos@244
   243
      ///Checks whether a point is inside a bounding box
athos@244
   244
      bool inside(const xy<T>& u){
athos@244
   245
	if (_empty)
athos@244
   246
	  return false;
athos@244
   247
	else{
athos@244
   248
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   249
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   250
	}
athos@244
   251
      }
athos@244
   252
  
athos@244
   253
      ///Increments a bounding box with a point
athos@244
   254
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   255
	if (_empty){
athos@244
   256
	  bottom_left=top_right=u;
athos@244
   257
	  _empty = false;
athos@244
   258
	}
athos@244
   259
	else{
athos@244
   260
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   261
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   262
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   263
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   264
	}
athos@244
   265
	return *this;
athos@244
   266
      };
athos@244
   267
  
athos@244
   268
      ///Sums a bounding box and a point
athos@244
   269
      BoundingBox operator +(const xy<T>& u){
athos@244
   270
	BoundingBox b = *this;
athos@244
   271
	return b += u;
athos@244
   272
      };
athos@244
   273
athos@244
   274
      ///Increments a bounding box with an other bounding box
athos@244
   275
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   276
	if ( !u.empty() ){
athos@244
   277
	  *this += u.bottomLeft();
athos@244
   278
	  *this += u.topRight();
athos@244
   279
	}
athos@244
   280
	return *this;
athos@244
   281
      };
athos@244
   282
  
athos@244
   283
      ///Sums two bounding boxes
athos@244
   284
      BoundingBox operator +(const BoundingBox& u){
athos@244
   285
	BoundingBox b = *this;
athos@244
   286
	return b += u;
athos@244
   287
      };
athos@244
   288
athos@244
   289
    };//class Boundingbox
athos@244
   290
athos@244
   291
alpar@431
   292
  /// @}
athos@244
   293
athos@244
   294
alpar@921
   295
} //namespace lemon
athos@201
   296
alpar@921
   297
#endif //LEMON_XY_H