src/lemon/xy.h
author alpar
Sat, 20 Nov 2004 10:19:06 +0000
changeset 1011 5bd6c7671c9e
parent 967 6563019430ba
child 1045 1bf336c63f25
permissions -rw-r--r--
- snapshot-rollback functionarity added to ListGraph
- The iterface of the snapshot-rollback functionarity in SmartGraph has
changed to be compatible with ListGraph::SnapShot.
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
athos@207
    64
      ///Gives back the square of the norm of the vector
athos@207
    65
      T normSquare(){
athos@240
    66
	return x*x+y*y;
athos@207
    67
      };
athos@201
    68
  
athos@207
    69
      ///Increments the left hand side by u
athos@207
    70
      xy<T>& operator +=(const xy<T>& u){
athos@240
    71
	x += u.x;
athos@240
    72
	y += u.y;
athos@207
    73
	return *this;
athos@207
    74
      };
athos@201
    75
  
athos@207
    76
      ///Decrements the left hand side by u
athos@207
    77
      xy<T>& operator -=(const xy<T>& u){
athos@240
    78
	x -= u.x;
athos@240
    79
	y -= u.y;
athos@207
    80
	return *this;
athos@207
    81
      };
athos@201
    82
athos@207
    83
      ///Multiplying the left hand side with a scalar
athos@207
    84
      xy<T>& operator *=(const T &u){
athos@240
    85
	x *= u;
athos@240
    86
	y *= u;
athos@207
    87
	return *this;
athos@207
    88
      };
athos@207
    89
athos@207
    90
      ///Dividing the left hand side by a scalar
athos@207
    91
      xy<T>& operator /=(const T &u){
athos@240
    92
	x /= u;
athos@240
    93
	y /= u;
athos@207
    94
	return *this;
athos@207
    95
      };
athos@201
    96
  
athos@207
    97
      ///Returns the scalar product of two vectors
athos@207
    98
      T operator *(const xy<T>& u){
athos@240
    99
	return x*u.x+y*u.y;
athos@207
   100
      };
athos@201
   101
  
athos@207
   102
      ///Returns the sum of two vectors
athos@207
   103
      xy<T> operator+(const xy<T> &u) const {
athos@207
   104
	xy<T> b=*this;
athos@207
   105
	return b+=u;
athos@207
   106
      };
athos@201
   107
athos@207
   108
      ///Returns the difference of two vectors
athos@207
   109
      xy<T> operator-(const xy<T> &u) const {
athos@207
   110
	xy<T> b=*this;
athos@207
   111
	return b-=u;
athos@207
   112
      };
athos@201
   113
athos@207
   114
      ///Returns a vector multiplied by a scalar
athos@207
   115
      xy<T> operator*(const T &u) const {
athos@207
   116
	xy<T> b=*this;
athos@207
   117
	return b*=u;
athos@207
   118
      };
athos@201
   119
athos@207
   120
      ///Returns a vector divided by a scalar
athos@207
   121
      xy<T> operator/(const T &u) const {
athos@207
   122
	xy<T> b=*this;
athos@207
   123
	return b/=u;
athos@207
   124
      };
athos@201
   125
athos@207
   126
      ///Testing equality
athos@207
   127
      bool operator==(const xy<T> &u){
athos@240
   128
	return (x==u.x) && (y==u.y);
athos@207
   129
      };
athos@201
   130
athos@207
   131
      ///Testing inequality
athos@207
   132
      bool operator!=(xy u){
athos@240
   133
	return  (x!=u.x) || (y!=u.y);
athos@207
   134
      };
athos@201
   135
athos@207
   136
    };
athos@201
   137
alpar@814
   138
  ///Read a plainvector from a stream
alpar@814
   139
alpar@967
   140
  ///Read a plainvector from a stream
alpar@814
   141
  ///\relates xy
alpar@814
   142
  ///
athos@207
   143
  template<typename T>
athos@207
   144
  inline
athos@207
   145
  std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207
   146
  {
athos@240
   147
athos@240
   148
    is >> z.x >> z.y;
athos@207
   149
    return is;
athos@207
   150
  }
athos@201
   151
alpar@814
   152
  ///Write a plainvector to a stream
alpar@814
   153
alpar@967
   154
  ///Write a plainvector to a stream
alpar@814
   155
  ///\relates xy
alpar@814
   156
  ///
athos@207
   157
  template<typename T>
athos@207
   158
  inline
athos@207
   159
  std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207
   160
  {
athos@240
   161
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   162
    return os;
athos@207
   163
  }
athos@207
   164
athos@244
   165
alpar@458
   166
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   167
alpar@458
   168
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   169
  ///
alpar@458
   170
  ///\author Attila Bernath
athos@244
   171
  template<typename T>
athos@244
   172
    class BoundingBox {
athos@244
   173
      xy<T> bottom_left, top_right;
athos@244
   174
      bool _empty;
athos@244
   175
    public:
athos@244
   176
      
athos@244
   177
      ///Default constructor: an empty bounding box
athos@244
   178
      BoundingBox() { _empty = true; }
athos@244
   179
athos@244
   180
      ///Constructing the instance from one point
athos@244
   181
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   182
athos@244
   183
      ///Is there any point added
athos@244
   184
      bool empty() const {
athos@244
   185
	return _empty;
athos@244
   186
      }
athos@244
   187
athos@244
   188
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   189
      xy<T> bottomLeft() const {
athos@244
   190
	return bottom_left;
athos@244
   191
      };
athos@244
   192
athos@244
   193
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   194
      xy<T> topRight() const {
athos@244
   195
	return top_right;
athos@244
   196
      };
athos@244
   197
athos@244
   198
      ///Checks whether a point is inside a bounding box
athos@244
   199
      bool inside(const xy<T>& u){
athos@244
   200
	if (_empty)
athos@244
   201
	  return false;
athos@244
   202
	else{
athos@244
   203
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   204
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   205
	}
athos@244
   206
      }
athos@244
   207
  
athos@244
   208
      ///Increments a bounding box with a point
athos@244
   209
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   210
	if (_empty){
athos@244
   211
	  bottom_left=top_right=u;
athos@244
   212
	  _empty = false;
athos@244
   213
	}
athos@244
   214
	else{
athos@244
   215
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   216
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   217
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   218
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   219
	}
athos@244
   220
	return *this;
athos@244
   221
      };
athos@244
   222
  
athos@244
   223
      ///Sums a bounding box and a point
athos@244
   224
      BoundingBox operator +(const xy<T>& u){
athos@244
   225
	BoundingBox b = *this;
athos@244
   226
	return b += u;
athos@244
   227
      };
athos@244
   228
athos@244
   229
      ///Increments a bounding box with an other bounding box
athos@244
   230
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   231
	if ( !u.empty() ){
athos@244
   232
	  *this += u.bottomLeft();
athos@244
   233
	  *this += u.topRight();
athos@244
   234
	}
athos@244
   235
	return *this;
athos@244
   236
      };
athos@244
   237
  
athos@244
   238
      ///Sums two bounding boxes
athos@244
   239
      BoundingBox operator +(const BoundingBox& u){
athos@244
   240
	BoundingBox b = *this;
athos@244
   241
	return b += u;
athos@244
   242
      };
athos@244
   243
athos@244
   244
    };//class Boundingbox
athos@244
   245
athos@244
   246
alpar@431
   247
  /// @}
athos@244
   248
athos@244
   249
alpar@921
   250
} //namespace lemon
athos@201
   251
alpar@921
   252
#endif //LEMON_XY_H