src/lemon/xy.h
author klao
Wed, 05 Jan 2005 14:34:00 +0000
changeset 1053 90f8696360b2
parent 1045 1bf336c63f25
child 1071 7c70fc1b2d8b
permissions -rw-r--r--
UndirGraphs: invalid edge bug
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@814
   148
  ///Read a plainvector from a stream
alpar@814
   149
alpar@967
   150
  ///Read a plainvector from a stream
alpar@814
   151
  ///\relates xy
alpar@814
   152
  ///
athos@207
   153
  template<typename T>
athos@207
   154
  inline
athos@207
   155
  std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207
   156
  {
athos@240
   157
athos@240
   158
    is >> z.x >> z.y;
athos@207
   159
    return is;
athos@207
   160
  }
athos@201
   161
alpar@814
   162
  ///Write a plainvector to a stream
alpar@814
   163
alpar@967
   164
  ///Write a plainvector to a stream
alpar@814
   165
  ///\relates xy
alpar@814
   166
  ///
athos@207
   167
  template<typename T>
athos@207
   168
  inline
athos@207
   169
  std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207
   170
  {
athos@240
   171
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   172
    return os;
athos@207
   173
  }
athos@207
   174
athos@244
   175
alpar@458
   176
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   177
alpar@458
   178
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   179
  ///
alpar@458
   180
  ///\author Attila Bernath
athos@244
   181
  template<typename T>
athos@244
   182
    class BoundingBox {
athos@244
   183
      xy<T> bottom_left, top_right;
athos@244
   184
      bool _empty;
athos@244
   185
    public:
athos@244
   186
      
athos@244
   187
      ///Default constructor: an empty bounding box
athos@244
   188
      BoundingBox() { _empty = true; }
athos@244
   189
athos@244
   190
      ///Constructing the instance from one point
athos@244
   191
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   192
athos@244
   193
      ///Is there any point added
athos@244
   194
      bool empty() const {
athos@244
   195
	return _empty;
athos@244
   196
      }
athos@244
   197
athos@244
   198
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   199
      xy<T> bottomLeft() const {
athos@244
   200
	return bottom_left;
athos@244
   201
      };
athos@244
   202
athos@244
   203
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   204
      xy<T> topRight() const {
athos@244
   205
	return top_right;
athos@244
   206
      };
athos@244
   207
alpar@1045
   208
      ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   209
      xy<T> bottomRight() const {
alpar@1045
   210
	return xy<T>(top_right.x,bottom_left.y);
alpar@1045
   211
      };
alpar@1045
   212
alpar@1045
   213
      ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   214
      xy<T> topLeft() const {
alpar@1045
   215
	return xy<T>(bottom_left.x,top_right.y);
alpar@1045
   216
      };
alpar@1045
   217
alpar@1045
   218
      ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   219
      T bottom() const {
alpar@1045
   220
	return bottom_left.y;
alpar@1045
   221
      };
alpar@1045
   222
alpar@1045
   223
      ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   224
      T top() const {
alpar@1045
   225
	return top_right.y;
alpar@1045
   226
      };
alpar@1045
   227
alpar@1045
   228
      ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   229
      T left() const {
alpar@1045
   230
	return bottom_left.x;
alpar@1045
   231
      };
alpar@1045
   232
alpar@1045
   233
      ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
alpar@1045
   234
      T right() const {
alpar@1045
   235
	return top_right.x;
alpar@1045
   236
      };
alpar@1045
   237
athos@244
   238
      ///Checks whether a point is inside a bounding box
athos@244
   239
      bool inside(const xy<T>& u){
athos@244
   240
	if (_empty)
athos@244
   241
	  return false;
athos@244
   242
	else{
athos@244
   243
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   244
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   245
	}
athos@244
   246
      }
athos@244
   247
  
athos@244
   248
      ///Increments a bounding box with a point
athos@244
   249
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   250
	if (_empty){
athos@244
   251
	  bottom_left=top_right=u;
athos@244
   252
	  _empty = false;
athos@244
   253
	}
athos@244
   254
	else{
athos@244
   255
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   256
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   257
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   258
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   259
	}
athos@244
   260
	return *this;
athos@244
   261
      };
athos@244
   262
  
athos@244
   263
      ///Sums a bounding box and a point
athos@244
   264
      BoundingBox operator +(const xy<T>& u){
athos@244
   265
	BoundingBox b = *this;
athos@244
   266
	return b += u;
athos@244
   267
      };
athos@244
   268
athos@244
   269
      ///Increments a bounding box with an other bounding box
athos@244
   270
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   271
	if ( !u.empty() ){
athos@244
   272
	  *this += u.bottomLeft();
athos@244
   273
	  *this += u.topRight();
athos@244
   274
	}
athos@244
   275
	return *this;
athos@244
   276
      };
athos@244
   277
  
athos@244
   278
      ///Sums two bounding boxes
athos@244
   279
      BoundingBox operator +(const BoundingBox& u){
athos@244
   280
	BoundingBox b = *this;
athos@244
   281
	return b += u;
athos@244
   282
      };
athos@244
   283
athos@244
   284
    };//class Boundingbox
athos@244
   285
athos@244
   286
alpar@431
   287
  /// @}
athos@244
   288
athos@244
   289
alpar@921
   290
} //namespace lemon
athos@201
   291
alpar@921
   292
#endif //LEMON_XY_H