src/hugo/xy.h
author deba
Wed, 08 Sep 2004 12:06:45 +0000
changeset 822 88226d9fe821
parent 539 fb261e3a9a0f
child 906 17f31d280385
permissions -rw-r--r--
The MapFactories have been removed from the code because
if we use macros then they increases only the complexity.

The pair iterators of the maps are separeted from the maps.

Some macros and comments has been changed.
athos@207
     1
// -*- c++ -*-
athos@201
     2
#ifndef HUGO_XY_H
athos@201
     3
#define HUGO_XY_H
athos@201
     4
athos@201
     5
#include <iostream>
athos@201
     6
klao@491
     7
///\ingroup misc
alpar@249
     8
///\file
alpar@249
     9
///\brief A simple two dimensional vector and a bounding box implementation 
alpar@249
    10
///
alpar@249
    11
/// The class \ref hugo::xy "xy" implements
alpar@249
    12
///a two dimensional vector with the usual
alpar@249
    13
/// operations.
alpar@249
    14
///
alpar@249
    15
/// The class \ref hugo::BoundingBox "BoundingBox" can be used to determine
alpar@249
    16
/// the rectangular bounding box a set of \ref hugo::xy "xy"'s.
alpar@458
    17
///
alpar@458
    18
///\author Attila Bernath
alpar@249
    19
alpar@249
    20
alpar@431
    21
namespace hugo {
alpar@431
    22
alpar@431
    23
  /// \addtogroup misc
alpar@431
    24
  /// @{
alpar@431
    25
alpar@458
    26
  /// A two dimensional vector (plainvector) implementation
alpar@242
    27
alpar@458
    28
  /// A two dimensional vector (plainvector) implementation
alpar@458
    29
  ///with the usual vector
alpar@458
    30
  /// operators.
alpar@458
    31
  ///
alpar@458
    32
  ///\author Attila Bernath
athos@207
    33
  template<typename T>
athos@207
    34
    class xy {
athos@201
    35
athos@207
    36
    public:
athos@240
    37
athos@240
    38
      T x,y;     
athos@207
    39
      
athos@207
    40
      ///Default constructor: both coordinates become 0
athos@240
    41
      xy() : x(0), y(0) {}
athos@201
    42
athos@240
    43
      ///Constructing the instance from coordinates
athos@514
    44
      xy(T a, T b) : x(a), y(b) { }
athos@201
    45
athos@201
    46
athos@207
    47
      ///Gives back the square of the norm of the vector
athos@207
    48
      T normSquare(){
athos@240
    49
	return x*x+y*y;
athos@207
    50
      };
athos@201
    51
  
athos@207
    52
      ///Increments the left hand side by u
athos@207
    53
      xy<T>& operator +=(const xy<T>& u){
athos@240
    54
	x += u.x;
athos@240
    55
	y += u.y;
athos@207
    56
	return *this;
athos@207
    57
      };
athos@201
    58
  
athos@207
    59
      ///Decrements the left hand side by u
athos@207
    60
      xy<T>& operator -=(const xy<T>& u){
athos@240
    61
	x -= u.x;
athos@240
    62
	y -= u.y;
athos@207
    63
	return *this;
athos@207
    64
      };
athos@201
    65
athos@207
    66
      ///Multiplying the left hand side with a scalar
athos@207
    67
      xy<T>& operator *=(const T &u){
athos@240
    68
	x *= u;
athos@240
    69
	y *= u;
athos@207
    70
	return *this;
athos@207
    71
      };
athos@207
    72
athos@207
    73
      ///Dividing the left hand side by a scalar
athos@207
    74
      xy<T>& operator /=(const T &u){
athos@240
    75
	x /= u;
athos@240
    76
	y /= u;
athos@207
    77
	return *this;
athos@207
    78
      };
athos@201
    79
  
athos@207
    80
      ///Returns the scalar product of two vectors
athos@207
    81
      T operator *(const xy<T>& u){
athos@240
    82
	return x*u.x+y*u.y;
athos@207
    83
      };
athos@201
    84
  
athos@207
    85
      ///Returns the sum of two vectors
athos@207
    86
      xy<T> operator+(const xy<T> &u) const {
athos@207
    87
	xy<T> b=*this;
athos@207
    88
	return b+=u;
athos@207
    89
      };
athos@201
    90
athos@207
    91
      ///Returns the difference of two vectors
athos@207
    92
      xy<T> operator-(const xy<T> &u) const {
athos@207
    93
	xy<T> b=*this;
athos@207
    94
	return b-=u;
athos@207
    95
      };
athos@201
    96
athos@207
    97
      ///Returns a vector multiplied by a scalar
athos@207
    98
      xy<T> operator*(const T &u) const {
athos@207
    99
	xy<T> b=*this;
athos@207
   100
	return b*=u;
athos@207
   101
      };
athos@201
   102
athos@207
   103
      ///Returns a vector divided by a scalar
athos@207
   104
      xy<T> operator/(const T &u) const {
athos@207
   105
	xy<T> b=*this;
athos@207
   106
	return b/=u;
athos@207
   107
      };
athos@201
   108
athos@207
   109
      ///Testing equality
athos@207
   110
      bool operator==(const xy<T> &u){
athos@240
   111
	return (x==u.x) && (y==u.y);
athos@207
   112
      };
athos@201
   113
athos@207
   114
      ///Testing inequality
athos@207
   115
      bool operator!=(xy u){
athos@240
   116
	return  (x!=u.x) || (y!=u.y);
athos@207
   117
      };
athos@201
   118
athos@207
   119
    };
athos@201
   120
alpar@814
   121
  ///Read a plainvector from a stream
alpar@814
   122
alpar@814
   123
  ///\relates xy
alpar@814
   124
  ///
athos@207
   125
  template<typename T>
athos@207
   126
  inline
athos@207
   127
  std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207
   128
  {
athos@240
   129
athos@240
   130
    is >> z.x >> z.y;
athos@207
   131
    return is;
athos@207
   132
  }
athos@201
   133
alpar@814
   134
  ///Write a plainvector to a stream
alpar@814
   135
alpar@814
   136
  ///\relates xy
alpar@814
   137
  ///
athos@207
   138
  template<typename T>
athos@207
   139
  inline
athos@207
   140
  std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207
   141
  {
athos@240
   142
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   143
    return os;
athos@207
   144
  }
athos@207
   145
athos@244
   146
alpar@458
   147
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   148
alpar@458
   149
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   150
  ///
alpar@458
   151
  ///\author Attila Bernath
athos@244
   152
  template<typename T>
athos@244
   153
    class BoundingBox {
athos@244
   154
      xy<T> bottom_left, top_right;
athos@244
   155
      bool _empty;
athos@244
   156
    public:
athos@244
   157
      
athos@244
   158
      ///Default constructor: an empty bounding box
athos@244
   159
      BoundingBox() { _empty = true; }
athos@244
   160
athos@244
   161
      ///Constructing the instance from one point
athos@244
   162
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   163
athos@244
   164
      ///Is there any point added
athos@244
   165
      bool empty() const {
athos@244
   166
	return _empty;
athos@244
   167
      }
athos@244
   168
athos@244
   169
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   170
      xy<T> bottomLeft() const {
athos@244
   171
	return bottom_left;
athos@244
   172
      };
athos@244
   173
athos@244
   174
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   175
      xy<T> topRight() const {
athos@244
   176
	return top_right;
athos@244
   177
      };
athos@244
   178
athos@244
   179
      ///Checks whether a point is inside a bounding box
athos@244
   180
      bool inside(const xy<T>& u){
athos@244
   181
	if (_empty)
athos@244
   182
	  return false;
athos@244
   183
	else{
athos@244
   184
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   185
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   186
	}
athos@244
   187
      }
athos@244
   188
  
athos@244
   189
      ///Increments a bounding box with a point
athos@244
   190
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   191
	if (_empty){
athos@244
   192
	  bottom_left=top_right=u;
athos@244
   193
	  _empty = false;
athos@244
   194
	}
athos@244
   195
	else{
athos@244
   196
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   197
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   198
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   199
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   200
	}
athos@244
   201
	return *this;
athos@244
   202
      };
athos@244
   203
  
athos@244
   204
      ///Sums a bounding box and a point
athos@244
   205
      BoundingBox operator +(const xy<T>& u){
athos@244
   206
	BoundingBox b = *this;
athos@244
   207
	return b += u;
athos@244
   208
      };
athos@244
   209
athos@244
   210
      ///Increments a bounding box with an other bounding box
athos@244
   211
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   212
	if ( !u.empty() ){
athos@244
   213
	  *this += u.bottomLeft();
athos@244
   214
	  *this += u.topRight();
athos@244
   215
	}
athos@244
   216
	return *this;
athos@244
   217
      };
athos@244
   218
  
athos@244
   219
      ///Sums two bounding boxes
athos@244
   220
      BoundingBox operator +(const BoundingBox& u){
athos@244
   221
	BoundingBox b = *this;
athos@244
   222
	return b += u;
athos@244
   223
      };
athos@244
   224
athos@244
   225
    };//class Boundingbox
athos@244
   226
athos@244
   227
alpar@431
   228
  /// @}
athos@244
   229
athos@244
   230
athos@207
   231
} //namespace hugo
athos@201
   232
athos@201
   233
#endif //HUGO_XY_H