src/include/xy.h
author alpar
Mon, 26 Apr 2004 18:22:34 +0000
changeset 432 a51ba0e51a3a
parent 431 src/work/athos/xy/xy.h@79a5641f2dbc
child 458 2df1fee6c866
permissions -rw-r--r--
xy.h went to src/include.
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
alpar@431
     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@249
    17
alpar@249
    18
alpar@431
    19
namespace hugo {
alpar@431
    20
alpar@431
    21
  /// \addtogroup misc
alpar@431
    22
  /// @{
alpar@431
    23
alpar@242
    24
/** \brief
alpar@242
    25
2 dimensional vector (plainvector) implementation
alpar@242
    26
alpar@242
    27
*/
athos@207
    28
  template<typename T>
athos@207
    29
    class xy {
athos@201
    30
athos@207
    31
    public:
athos@240
    32
athos@240
    33
      T x,y;     
athos@207
    34
      
athos@207
    35
      ///Default constructor: both coordinates become 0
athos@240
    36
      xy() : x(0), y(0) {}
athos@201
    37
athos@240
    38
      ///Constructing the instance from coordinates
athos@240
    39
      xy(T a, T b) : x(a), y(a) { }
athos@201
    40
athos@201
    41
athos@207
    42
      ///Gives back the square of the norm of the vector
athos@207
    43
      T normSquare(){
athos@240
    44
	return x*x+y*y;
athos@207
    45
      };
athos@201
    46
  
athos@207
    47
      ///Increments the left hand side by u
athos@207
    48
      xy<T>& operator +=(const xy<T>& u){
athos@240
    49
	x += u.x;
athos@240
    50
	y += u.y;
athos@207
    51
	return *this;
athos@207
    52
      };
athos@201
    53
  
athos@207
    54
      ///Decrements the left hand side by u
athos@207
    55
      xy<T>& operator -=(const xy<T>& u){
athos@240
    56
	x -= u.x;
athos@240
    57
	y -= u.y;
athos@207
    58
	return *this;
athos@207
    59
      };
athos@201
    60
athos@207
    61
      ///Multiplying the left hand side with a scalar
athos@207
    62
      xy<T>& operator *=(const T &u){
athos@240
    63
	x *= u;
athos@240
    64
	y *= u;
athos@207
    65
	return *this;
athos@207
    66
      };
athos@207
    67
athos@207
    68
      ///Dividing the left hand side by a scalar
athos@207
    69
      xy<T>& operator /=(const T &u){
athos@240
    70
	x /= u;
athos@240
    71
	y /= u;
athos@207
    72
	return *this;
athos@207
    73
      };
athos@201
    74
  
athos@207
    75
      ///Returns the scalar product of two vectors
athos@207
    76
      T operator *(const xy<T>& u){
athos@240
    77
	return x*u.x+y*u.y;
athos@207
    78
      };
athos@201
    79
  
athos@207
    80
      ///Returns the sum of two vectors
athos@207
    81
      xy<T> operator+(const xy<T> &u) const {
athos@207
    82
	xy<T> b=*this;
athos@207
    83
	return b+=u;
athos@207
    84
      };
athos@201
    85
athos@207
    86
      ///Returns the difference of two vectors
athos@207
    87
      xy<T> operator-(const xy<T> &u) const {
athos@207
    88
	xy<T> b=*this;
athos@207
    89
	return b-=u;
athos@207
    90
      };
athos@201
    91
athos@207
    92
      ///Returns a vector multiplied by a scalar
athos@207
    93
      xy<T> operator*(const T &u) const {
athos@207
    94
	xy<T> b=*this;
athos@207
    95
	return b*=u;
athos@207
    96
      };
athos@201
    97
athos@207
    98
      ///Returns a vector divided by a scalar
athos@207
    99
      xy<T> operator/(const T &u) const {
athos@207
   100
	xy<T> b=*this;
athos@207
   101
	return b/=u;
athos@207
   102
      };
athos@201
   103
athos@207
   104
      ///Testing equality
athos@207
   105
      bool operator==(const xy<T> &u){
athos@240
   106
	return (x==u.x) && (y==u.y);
athos@207
   107
      };
athos@201
   108
athos@207
   109
      ///Testing inequality
athos@207
   110
      bool operator!=(xy u){
athos@240
   111
	return  (x!=u.x) || (y!=u.y);
athos@207
   112
      };
athos@201
   113
athos@207
   114
    };
athos@201
   115
athos@207
   116
  ///Reading a plainvector from a stream
athos@207
   117
  template<typename T>
athos@207
   118
  inline
athos@207
   119
  std::istream& operator>>(std::istream &is, xy<T> &z)
athos@207
   120
  {
athos@240
   121
athos@240
   122
    is >> z.x >> z.y;
athos@207
   123
    return is;
athos@207
   124
  }
athos@201
   125
athos@207
   126
  ///Outputting a plainvector to a stream
athos@207
   127
  template<typename T>
athos@207
   128
  inline
athos@207
   129
  std::ostream& operator<<(std::ostream &os, xy<T> z)
athos@207
   130
  {
athos@240
   131
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   132
    return os;
athos@207
   133
  }
athos@207
   134
athos@244
   135
athos@244
   136
  /** \brief
athos@244
   137
     Implementation of a bounding box of plainvectors.
athos@244
   138
     
athos@244
   139
  */
athos@244
   140
  template<typename T>
athos@244
   141
    class BoundingBox {
athos@244
   142
      xy<T> bottom_left, top_right;
athos@244
   143
      bool _empty;
athos@244
   144
    public:
athos@244
   145
      
athos@244
   146
      ///Default constructor: an empty bounding box
athos@244
   147
      BoundingBox() { _empty = true; }
athos@244
   148
athos@244
   149
      ///Constructing the instance from one point
athos@244
   150
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   151
athos@244
   152
      ///Is there any point added
athos@244
   153
      bool empty() const {
athos@244
   154
	return _empty;
athos@244
   155
      }
athos@244
   156
athos@244
   157
      ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   158
      xy<T> bottomLeft() const {
athos@244
   159
	return bottom_left;
athos@244
   160
      };
athos@244
   161
athos@244
   162
      ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@244
   163
      xy<T> topRight() const {
athos@244
   164
	return top_right;
athos@244
   165
      };
athos@244
   166
athos@244
   167
      ///Checks whether a point is inside a bounding box
athos@244
   168
      bool inside(const xy<T>& u){
athos@244
   169
	if (_empty)
athos@244
   170
	  return false;
athos@244
   171
	else{
athos@244
   172
	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
athos@244
   173
		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
athos@244
   174
	}
athos@244
   175
      }
athos@244
   176
  
athos@244
   177
      ///Increments a bounding box with a point
athos@244
   178
      BoundingBox& operator +=(const xy<T>& u){
athos@244
   179
	if (_empty){
athos@244
   180
	  bottom_left=top_right=u;
athos@244
   181
	  _empty = false;
athos@244
   182
	}
athos@244
   183
	else{
athos@244
   184
	  if (bottom_left.x > u.x) bottom_left.x = u.x;
athos@244
   185
	  if (bottom_left.y > u.y) bottom_left.y = u.y;
athos@244
   186
	  if (top_right.x < u.x) top_right.x = u.x;
athos@244
   187
	  if (top_right.y < u.y) top_right.y = u.y;
athos@244
   188
	}
athos@244
   189
	return *this;
athos@244
   190
      };
athos@244
   191
  
athos@244
   192
      ///Sums a bounding box and a point
athos@244
   193
      BoundingBox operator +(const xy<T>& u){
athos@244
   194
	BoundingBox b = *this;
athos@244
   195
	return b += u;
athos@244
   196
      };
athos@244
   197
athos@244
   198
      ///Increments a bounding box with an other bounding box
athos@244
   199
      BoundingBox& operator +=(const BoundingBox &u){
athos@244
   200
	if ( !u.empty() ){
athos@244
   201
	  *this += u.bottomLeft();
athos@244
   202
	  *this += u.topRight();
athos@244
   203
	}
athos@244
   204
	return *this;
athos@244
   205
      };
athos@244
   206
  
athos@244
   207
      ///Sums two bounding boxes
athos@244
   208
      BoundingBox operator +(const BoundingBox& u){
athos@244
   209
	BoundingBox b = *this;
athos@244
   210
	return b += u;
athos@244
   211
      };
athos@244
   212
athos@244
   213
    };//class Boundingbox
athos@244
   214
athos@244
   215
alpar@431
   216
  /// @}
athos@244
   217
athos@244
   218
athos@207
   219
} //namespace hugo
athos@201
   220
athos@201
   221
#endif //HUGO_XY_H